Annotation of java/classes/org/w3c/tools/widgets/ImageButton.java, revision 1.7
1.1 ylafon 1: // ImageButton.java
1.7 ! ylafon 2: // $Id: ImageButton.java,v 1.6 1997/07/02 08:50:55 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.7 ! ylafon 113: return new Dimension(width+8, height+8);
! 114: }
! 115:
! 116: /**
! 117: * Returns the preferred size of the ImageButton
! 118: */
! 119:
! 120: public Dimension getPreferredSize() {
1.3 ylafon 121: return new Dimension(width+8, height+8);
1.1 ylafon 122: }
123:
1.4 ylafon 124: /**
125: * Sets the action command String used when an ActionEvent is fired
126: * @param command The command String
127: */
128:
129: public void setActionCommand(String command) {
130: this.command = command;
131: }
132:
133: /**
134: * Returns the action command String
135: */
136:
137: public String getActionCommand() {
138: return command;
139: }
140:
141: /**
142: * Adds an action listener to this ImageButton
143: * @param al The ActionListener
144: */
145:
146: public synchronized void addActionListener(ActionListener al) {
147: actionListener = AWTEventMulticaster.add(actionListener, al);
148: }
149:
150: /**
151: * Removes an action listener to this ImageButton
152: * @param al The ActionListener
153: */
154:
155: public synchronized void removeActionListener(ActionListener al) {
156: actionListener = AWTEventMulticaster.remove(actionListener, al);
157: }
158:
159: /**
160: * fire a new ActionEvent and process it, if some listeners are listening
161: */
162:
163: protected void fireActionEvent() {
164: if(actionListener != null) {
165: ActionEvent ae = new ActionEvent(this,
166: ActionEvent.ACTION_PERFORMED,
167: command);
168: actionListener.actionPerformed(ae);
169: }
170: }
171:
172: /**
173: * Construct an ImageButton with the specified action command
174: * @param img The image of this ImageButton
175: * @param command The action command String
176: */
177:
178: public ImageButton(Image img, String command) {
1.1 ylafon 179: this.img = img;
1.4 ylafon 180: this.command = command;
1.6 ylafon 181: addMouseListener(new ImageButtonListener());
1.5 ylafon 182: prepareImage(img, this);
183: initSize();
1.4 ylafon 184: }
185:
186: /**
187: * Construct an ImageButton with no action command
188: * @param img The image of this ImageButton
189: */
190:
191: public ImageButton(Image img) {
192: this(img, "");
1.1 ylafon 193: }
194: }
Webmaster