Annotation of java/classes/org/w3c/tools/widgets/ImageButton.java, revision 1.6
1.1 ylafon 1: // ImageButton.java
1.6 ! ylafon 2: // $Id: ImageButton.java,v 1.5 1997/06/30 14:52:16 ylafon Exp $
1.1 ylafon 3: // (c) COPYRIGHT MIT and INRIA, 1996.
4: // Please first read the full copyright statement in file COPYRIGHT.html
5:
6: package w3c.tools.widgets ;
7:
8: import java.awt.*;
9: import java.awt.event.*;
10:
11: public class ImageButton extends Canvas {
12:
1.4 ylafon 13: /**
14: * This MouseListener is used to do all the paint operations
15: * and to generate ActionEvents when a click occured
16: */
17:
1.1 ylafon 18: private class ImageButtonListener extends MouseAdapter {
19:
20: public void mousePressed(MouseEvent me) {
1.4 ylafon 21: // paint down
1.6 ! ylafon 22: paintShadow(false);
1.1 ylafon 23: }
24:
25: public void mouseReleased(MouseEvent me) {
1.4 ylafon 26: // paint up
1.6 ! ylafon 27: paintShadow(true);
1.1 ylafon 28: }
1.4 ylafon 29:
30: public void mouseClicked(MouseEvent me) {
31: // fire a new ActionEvent
1.6 ! ylafon 32: fireActionEvent();
1.1 ylafon 33: }
34: }
35:
36: protected Image img;
1.3 ylafon 37: private int width = -1;
38: private int height = -1;
1.4 ylafon 39: private String command;
40:
41: transient ActionListener actionListener;
42:
43: /**
1.5 ylafon 44: * Gets the size of the Image to calculate the minimum size of the
45: * Button
46: */
47:
48: protected void initSize() {
49: width = img.getWidth(this);
50: height = img.getHeight(this);
51: }
52:
53: /**
1.4 ylafon 54: * paint the ImageButton in its initial shape
55: * @param g A Graphics
56: */
57:
1.1 ylafon 58: public void paint(Graphics g) {
59: paintShadow(true);
60: }
61:
1.4 ylafon 62: /**
63: * paints the ImageButton using double buffering
64: * @param raised A boolean which shows the state of the button
65: */
66:
1.1 ylafon 67: protected void paintShadow(boolean raised) {
68: Graphics g = getGraphics();
69: Shape s = g.getClip();
70: Image dbi;
71: Graphics dbg;
72: Color bg = getBackground();
73: Dimension d = getSize();
74: int dx;
75: int dy;
1.5 ylafon 76:
1.1 ylafon 77: dbi = ImageCache.getImage(this, d.width, d.height);
78: dbg = dbi.getGraphics();
79: dbg.setClip(s);
80: dbg.setColor(bg);
1.3 ylafon 81: dx = d.width - width;
82: dy = d.height - height;
1.2 ylafon 83: dbg.clearRect(0, 0, d.width, d.height);
1.1 ylafon 84: dbg.fill3DRect(1, 1, d.width-2, d.height-2, raised);
85: dbg.drawImage(img, dx/2, dy/2, this);
86: g.drawImage(dbi, 0, 0, this);
87: }
88:
1.4 ylafon 89: /**
90: * called when more informations about the image are available.
91: * When the size is available, the ImageButton notifies its container
92: * that the size may have changed.
93: * @see ImageObserver
94: */
95:
1.3 ylafon 96: public boolean imageUpdate(Image img, int flaginfo,
97: int x, int y, int width, int height) {
98: if ((flaginfo & (HEIGHT|WIDTH)) != 0) {
99: this.width = width;
100: this.height = height;
101: Container parent = getParent();
102: if(parent != null)
103: parent.doLayout();
104: }
105: return super.imageUpdate(img, flaginfo, x, y, width, height);
106: }
1.4 ylafon 107:
108: /**
109: * Returns the minimum size of the ImageButton
110: */
111:
1.1 ylafon 112: public Dimension getMinimumSize() {
1.3 ylafon 113: return new Dimension(width+8, height+8);
1.1 ylafon 114: }
115:
1.4 ylafon 116: /**
117: * Sets the action command String used when an ActionEvent is fired
118: * @param command The command String
119: */
120:
121: public void setActionCommand(String command) {
122: this.command = command;
123: }
124:
125: /**
126: * Returns the action command String
127: */
128:
129: public String getActionCommand() {
130: return command;
131: }
132:
133: /**
134: * Adds an action listener to this ImageButton
135: * @param al The ActionListener
136: */
137:
138: public synchronized void addActionListener(ActionListener al) {
139: actionListener = AWTEventMulticaster.add(actionListener, al);
140: }
141:
142: /**
143: * Removes an action listener to this ImageButton
144: * @param al The ActionListener
145: */
146:
147: public synchronized void removeActionListener(ActionListener al) {
148: actionListener = AWTEventMulticaster.remove(actionListener, al);
149: }
150:
151: /**
152: * fire a new ActionEvent and process it, if some listeners are listening
153: */
154:
155: protected void fireActionEvent() {
156: if(actionListener != null) {
157: ActionEvent ae = new ActionEvent(this,
158: ActionEvent.ACTION_PERFORMED,
159: command);
160: actionListener.actionPerformed(ae);
161: }
162: }
163:
164: /**
165: * Construct an ImageButton with the specified action command
166: * @param img The image of this ImageButton
167: * @param command The action command String
168: */
169:
170: public ImageButton(Image img, String command) {
1.1 ylafon 171: this.img = img;
1.4 ylafon 172: this.command = command;
1.6 ! ylafon 173: addMouseListener(new ImageButtonListener());
1.5 ylafon 174: prepareImage(img, this);
175: initSize();
1.4 ylafon 176: }
177:
178: /**
179: * Construct an ImageButton with no action command
180: * @param img The image of this ImageButton
181: */
182:
183: public ImageButton(Image img) {
184: this(img, "");
1.1 ylafon 185: }
186: }
Webmaster