001: /*
002: * @(#)DemoButton.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis;
027:
028: import java.awt.*;
029: import java.awt.event.*;
030: import java.util.ArrayList;
031: import java.util.EventObject;
032:
033: public class DemoButton extends Component {
034: public static final Font DEFAULT_FONT = new Font("sanserif",
035: Font.PLAIN, 12);
036: public static final Color DEFAULT_COLOR = Builder.SUN_BLUE;
037: private static boolean pending;
038: private String label;
039: private boolean mousePressed;
040: private boolean keyPressed;
041: private boolean inside;
042: private ArrayList listeners = new ArrayList();
043: private Dimension preferredSize;
044:
045: public DemoButton(String label) {
046: this .label = label;
047: setForeground(DEFAULT_COLOR);
048: enableEvents(MouseEvent.MOUSE_CLICKED
049: | MouseEvent.MOUSE_ENTERED | MouseEvent.MOUSE_EXITED
050: | MouseEvent.MOUSE_PRESSED | MouseEvent.MOUSE_RELEASED
051: | KeyEvent.KEY_PRESSED
052: // Makes no difference - not needed!
053: // FocusEvent.FOCUS_GAINED |
054: // FocusEvent.FOCUS_LOST
055: );
056: }
057:
058: public String getLabel() {
059: return label;
060: }
061:
062: public Dimension getPreferredSize() {
063: if (preferredSize == null) {
064: Graphics g = getGraphics();
065: FontMetrics fm = g.getFontMetrics(DEFAULT_FONT);
066: int fw = fm.stringWidth(label);
067: int fh = fm.getHeight();
068: preferredSize = new Dimension(fw + 4, fh + 4);
069: }
070: return preferredSize;
071: }
072:
073: public Dimension getMinimumSize() {
074: return getPreferredSize();
075: }
076:
077: public Dimension getMaximumSize() {
078: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
079: }
080:
081: public void paint(Graphics g) {
082: Dimension d = getSize();
083: Color color = getForeground();
084: if (inside) {
085: if (mousePressed) {
086: color = color.darker();
087: pending = true;
088: }
089: if (!pending) {
090: color = color.brighter();
091: }
092: }
093: if (keyPressed) {
094: color = color.darker();
095: }
096: g.setColor(color);
097: g.fillRect(0, 0, d.width, d.height);
098: int w = d.width - 1;
099: int h = d.height - 1;
100: if ((mousePressed && inside) || keyPressed) {
101: g.setColor(Color.white);
102: } else {
103: g.setColor(Color.black);
104: }
105: g.drawLine(0, h, w, h);
106: g.drawLine(w, 0, w, h);
107: if ((mousePressed && inside) || keyPressed) {
108: g.setColor(Color.black);
109: } else {
110: g.setColor(Color.white);
111: }
112: g.drawLine(0, 0, w, 0);
113: g.drawLine(0, 0, 0, h);
114: int max = 160;
115: if (color.getRed() > max || color.getGreen() > max
116: || color.getBlue() > max) {
117: g.setColor(Color.black);
118: } else {
119: g.setColor(Color.white);
120: }
121: g.setFont(DEFAULT_FONT);
122: FontMetrics fm = g.getFontMetrics(DEFAULT_FONT);
123: int fw = fm.stringWidth(label);
124: int fa = fm.getAscent();
125: g
126: .drawString(label, (d.width - fw) / 2,
127: (d.height + fa) / 2 - 1);
128: if (hasFocus()) {
129: int margin = 2;
130: g.drawRect(margin, margin, w - 2 * margin, h - 2 * margin);
131: }
132: }
133:
134: protected void processMouseEvent(MouseEvent e) {
135: switch (e.getID()) {
136: case MouseEvent.MOUSE_CLICKED:
137: break;
138: case MouseEvent.MOUSE_ENTERED:
139: inside = true;
140: repaint();
141: break;
142: case MouseEvent.MOUSE_EXITED:
143: inside = false;
144: repaint();
145: break;
146: case MouseEvent.MOUSE_PRESSED:
147: mousePressed = true;
148: // NOTE: Personal doesn't need to request focus,
149: // as it is done in Component's dispatchEventImpl(),
150: // but Basis and even J2SE do need it...
151: requestFocus();
152: repaint();
153: break;
154: case MouseEvent.MOUSE_RELEASED:
155: mousePressed = false;
156: repaint();
157: if (!inside) {
158: return;
159: }
160: pending = false;
161: for (int i = 0; i < listeners.size(); i++) {
162: DemoButtonListener listener = (DemoButtonListener) listeners
163: .get(i);
164: listener.buttonPressed(new EventObject(this ));
165: }
166: break;
167: }
168: super .processMouseEvent(e);
169: }
170:
171: protected void processKeyEvent(KeyEvent e) {
172: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
173: switch (e.getID()) {
174: case KeyEvent.KEY_PRESSED:
175: keyPressed = true;
176: repaint();
177: break;
178: case KeyEvent.KEY_RELEASED:
179: keyPressed = false;
180: repaint();
181: pending = false;
182: for (int i = 0; i < listeners.size(); i++) {
183: DemoButtonListener listener = (DemoButtonListener) listeners
184: .get(i);
185: listener.buttonPressed(new EventObject(this ));
186: }
187: break;
188: }
189: return;
190: }
191: super .processKeyEvent(e);
192: }
193:
194: protected void processFocusEvent(FocusEvent e) {
195: switch (e.getID()) {
196: case FocusEvent.FOCUS_GAINED:
197: repaint();
198: break;
199: case FocusEvent.FOCUS_LOST:
200: keyPressed = false;
201: repaint();
202: break;
203: }
204: super .processFocusEvent(e);
205: }
206:
207: public synchronized void addDemoButtonListener(
208: DemoButtonListener listener) {
209: listeners.add(listener);
210: }
211:
212: public synchronized void removeDemoButtonListener(
213: DemoButtonListener listener) {
214: listeners.remove(listener);
215: }
216:
217: public boolean isFocusTraversable() {
218: return true;
219: }
220:
221: public String toString() {
222: return "DemoButton[" + label + "]";
223: }
224: }
|