001: // ImageButton.java
002: // $Id: ImageButton.java,v 1.13 2000/08/16 21:37:56 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.widgets;
007:
008: import java.awt.AWTEventMulticaster;
009: import java.awt.Canvas;
010: import java.awt.Color;
011: import java.awt.Container;
012: import java.awt.Dimension;
013: import java.awt.Graphics;
014: import java.awt.Image;
015: import java.awt.Shape;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.awt.event.MouseAdapter;
019: import java.awt.event.MouseEvent;
020:
021: public class ImageButton extends Canvas {
022:
023: /**
024: * This MouseListener is used to do all the paint operations
025: * and to generate ActionEvents when a click occured
026: */
027:
028: private class ImageButtonListener extends MouseAdapter {
029:
030: public void mousePressed(MouseEvent me) {
031: // paint down
032: paintShadow(false);
033: }
034:
035: public void mouseReleased(MouseEvent me) {
036: // paint up
037: paintShadow(true);
038: }
039:
040: public void mouseClicked(MouseEvent me) {
041: // fire a new ActionEvent
042: fireActionEvent();
043: }
044: }
045:
046: protected boolean switchable = false;
047: protected Image img1 = null;
048: protected Image img2 = null;
049: protected Image currentImg = null;
050: private int width = -1;
051: private int height = -1;
052: private String command;
053:
054: transient ActionListener actionListener;
055:
056: int max(int a, int b) {
057: return ((a < b) ? b : a);
058: }
059:
060: /**
061: * Gets the size of the Image to calculate the minimum size of the
062: * Button
063: */
064:
065: protected void initSize() {
066: if (switchable) {
067: width = max(img1.getWidth(this ), img2.getWidth(this ));
068: height = max(img1.getHeight(this ), img2.getHeight(this ));
069: } else {
070: width = currentImg.getWidth(this );
071: height = currentImg.getHeight(this );
072: }
073: }
074:
075: public void switchImage() {
076: if (switchable) {
077: if (currentImg == img1)
078: currentImg = img2;
079: else
080: currentImg = img1;
081: }
082: paintShadow(true);
083: }
084:
085: /**
086: * paint the ImageButton in its initial shape
087: * @param g A Graphics
088: */
089:
090: public void paint(Graphics g) {
091: paintShadow(true);
092: }
093:
094: /**
095: * paints the ImageButton using double buffering
096: * @param raised A boolean which shows the state of the button
097: */
098:
099: protected void paintShadow(boolean raised) {
100: Graphics g = getGraphics();
101: Shape s = g.getClip();
102: Image dbi;
103: Graphics dbg;
104: Color bg = getBackground();
105: Dimension d = getSize();
106: int dx;
107: int dy;
108:
109: dbi = ImageCache.getImage(this , d.width, d.height);
110: dbg = dbi.getGraphics();
111: dbg.setClip(s);
112: dbg.setColor(bg);
113: dx = d.width - width;
114: dy = d.height - height;
115: dbg.clearRect(0, 0, d.width, d.height);
116: dbg.fill3DRect(1, 1, d.width - 2, d.height - 2, raised);
117: dbg.drawImage(currentImg, dx / 2, dy / 2, this );
118: g.drawImage(dbi, 0, 0, this );
119: }
120:
121: /**
122: * called when more informations about the image are available.
123: * When the size is available, the ImageButton notifies its container
124: * that the size may have changed.
125: * @see java.awt.image.ImageObserver
126: */
127:
128: public boolean imageUpdate(Image img, int flaginfo, int x, int y,
129: int width, int height) {
130: // if ((flaginfo & (HEIGHT|WIDTH)) != 0) {
131: // this.width = width;
132: // this.height = height;
133: // Container parent = getParent();
134: // if(parent != null)
135: // parent.doLayout();
136: // }
137: initSize();
138: Container parent = getParent();
139: if (parent != null)
140: parent.doLayout();
141: return super .imageUpdate(img, flaginfo, x, y, width, height);
142: }
143:
144: /**
145: * Returns the minimum size of the ImageButton
146: */
147:
148: public Dimension getMinimumSize() {
149: return new Dimension(width + 8, height + 8);
150: }
151:
152: /**
153: * Returns the preferred size of the ImageButton
154: */
155:
156: public Dimension getPreferredSize() {
157: return new Dimension(width + 8, height + 8);
158: }
159:
160: /**
161: * Sets the action command String used when an ActionEvent is fired
162: * @param command The command String
163: */
164:
165: public void setActionCommand(String command) {
166: this .command = command;
167: }
168:
169: /**
170: * Returns the action command String
171: */
172:
173: public String getActionCommand() {
174: return command;
175: }
176:
177: /**
178: * Adds an action listener to this ImageButton
179: * @param al The ActionListener
180: */
181:
182: public synchronized void addActionListener(ActionListener al) {
183: actionListener = AWTEventMulticaster.add(actionListener, al);
184: }
185:
186: /**
187: * Removes an action listener to this ImageButton
188: * @param al The ActionListener
189: */
190:
191: public synchronized void removeActionListener(ActionListener al) {
192: actionListener = AWTEventMulticaster.remove(actionListener, al);
193: }
194:
195: /**
196: * fire a new ActionEvent and process it, if some listeners are listening
197: */
198:
199: protected void fireActionEvent() {
200: if (actionListener != null) {
201: ActionEvent ae = new ActionEvent(this ,
202: ActionEvent.ACTION_PERFORMED, command);
203: actionListener.actionPerformed(ae);
204: }
205: }
206:
207: /**
208: * Construct an ImageButton with the specified action command
209: * @param img1 The image of this ImageButton
210: * @param img2 The image of this ImageButton
211: * @param command The action command String
212: */
213:
214: public ImageButton(Image img1, Image img2, String command) {
215: this .switchable = true;
216: this .img1 = img1;
217: this .img2 = img2;
218: this .currentImg = img1;
219: this .command = command;
220: addMouseListener(new ImageButtonListener());
221: prepareImage(img1, this );
222: prepareImage(img2, this );
223: initSize();
224: }
225:
226: /**
227: * Construct an ImageButton with the specified action command
228: * @param img1 The image of this ImageButton
229: * @param realesed The image of this ImageButton
230: */
231:
232: public ImageButton(Image img1, Image img2) {
233: this (img1, img2, "");
234: }
235:
236: /**
237: * Construct an ImageButton with the specified action command
238: * @param img The image of this ImageButton
239: * @param command The action command String
240: */
241:
242: public ImageButton(Image img, String command) {
243: this .switchable = false;
244: this .currentImg = img;
245: this .command = command;
246: addMouseListener(new ImageButtonListener());
247: prepareImage(currentImg, this );
248: initSize();
249: }
250:
251: /**
252: * Construct an ImageButton with no action command
253: * @param img The image of this ImageButton
254: */
255:
256: public ImageButton(Image img) {
257: this (img, "");
258: }
259: }
|