001: /*
002: * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.X11;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029: import java.awt.event.MouseEvent;
030: import java.awt.event.FocusEvent;
031: import java.awt.event.KeyEvent;
032: import java.awt.event.ActionEvent;
033: import javax.swing.plaf.basic.*;
034: import javax.swing.SwingUtilities;
035: import javax.swing.SwingConstants;
036:
037: public class XButtonPeer extends XComponentPeer implements ButtonPeer {
038:
039: boolean pressed;
040: boolean armed;
041:
042: private Insets focusInsets;
043: private Insets borderInsets;
044: private Insets contentAreaInsets;
045:
046: private final static String propertyPrefix = "Button" + ".";
047: protected Color focusColor = SystemColor.windowText;
048:
049: private boolean disposed = false;
050:
051: String label;
052:
053: protected String getPropertyPrefix() {
054: return propertyPrefix;
055: }
056:
057: void preInit(XCreateWindowParams params) {
058: super .preInit(params);
059: borderInsets = new Insets(2, 2, 2, 2);
060: focusInsets = new Insets(0, 0, 0, 0);
061: contentAreaInsets = new Insets(3, 3, 3, 3);
062: }
063:
064: public XButtonPeer(Button target) {
065: super (target);
066: pressed = false;
067: armed = false;
068: label = target.getLabel();
069: updateMotifColors(getPeerBackground());
070: }
071:
072: public void dispose() {
073: synchronized (target) {
074: disposed = true;
075: }
076: super .dispose();
077: }
078:
079: public boolean isFocusable() {
080: return true;
081: }
082:
083: public void setLabel(java.lang.String label) {
084: this .label = label;
085: repaint();
086: }
087:
088: public void paint(Graphics g) {
089: paint(g, target);
090: }
091:
092: public void setBackground(Color c) {
093: updateMotifColors(c);
094: super .setBackground(c);
095: }
096:
097: void handleJavaMouseEvent(MouseEvent e) {
098: super .handleJavaMouseEvent(e);
099: int id = e.getID();
100: switch (id) {
101: case MouseEvent.MOUSE_PRESSED:
102: if (XToolkit.isLeftMouseButton(e)) {
103: Button b = (Button) e.getSource();
104:
105: if (b.contains(e.getX(), e.getY())) {
106: if (!isEnabled()) {
107: // Disabled buttons ignore all input...
108: return;
109: }
110: pressed = true;
111: armed = true;
112: repaint();
113: }
114: }
115:
116: break;
117:
118: case MouseEvent.MOUSE_RELEASED:
119: if (XToolkit.isLeftMouseButton(e)) {
120: if (armed) {
121: action(e.getWhen(), e.getModifiers());
122: }
123: pressed = false;
124: armed = false;
125: repaint();
126: }
127:
128: break;
129:
130: case MouseEvent.MOUSE_ENTERED:
131: if (pressed)
132: armed = true;
133: // repaint();
134:
135: break;
136:
137: case MouseEvent.MOUSE_EXITED:
138: armed = false;
139: // repaint();
140:
141: break;
142:
143: }
144: }
145:
146: // NOTE: This method is called by privileged threads.
147: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
148: public void action(final long when, final int modifiers) {
149: postEvent(new ActionEvent(target, ActionEvent.ACTION_PERFORMED,
150: ((Button) target).getActionCommand(), when, modifiers));
151: }
152:
153: public void focusGained(FocusEvent e) {
154: super .focusGained(e);
155: repaint();
156: }
157:
158: public void focusLost(FocusEvent e) {
159: super .focusLost(e);
160: repaint();
161: }
162:
163: void handleJavaKeyEvent(KeyEvent e) {
164: int id = e.getID();
165: switch (id) {
166: case KeyEvent.KEY_PRESSED:
167: if (e.getKeyCode() == KeyEvent.VK_SPACE) {
168: pressed = true;
169: armed = true;
170: repaint();
171: action(e.getWhen(), e.getModifiers());
172: }
173:
174: break;
175:
176: case KeyEvent.KEY_RELEASED:
177: if (e.getKeyCode() == KeyEvent.VK_SPACE) {
178: pressed = false;
179: armed = false;
180: repaint();
181: }
182:
183: break;
184:
185: }
186: }
187:
188: public Dimension getMinimumSize() {
189: FontMetrics fm = getFontMetrics(getPeerFont());
190: if (label == null) {
191: label = "";
192: }
193: return new Dimension(fm.stringWidth(label) + 14,
194: fm.getHeight() + 8);
195: }
196:
197: /**
198: * DEPRECATED
199: */
200: public Dimension minimumSize() {
201: return getMinimumSize();
202: }
203:
204: /*
205: This method is called from Toolkit Thread and so it should not call any client code
206:
207: */
208: public void paint(Graphics g, Component c) {
209: if (!disposed && (g != null)) {
210: Dimension size = getPeerSize();
211:
212: g.setColor(getPeerBackground()); /* erase the existing button remains */
213: g.fillRect(0, 0, size.width, size.height);
214: paintBorder(g, borderInsets.left, borderInsets.top,
215: size.width
216: - (borderInsets.left + borderInsets.right),
217: size.height
218: - (borderInsets.top + borderInsets.bottom));
219:
220: FontMetrics fm = g.getFontMetrics();
221:
222: Rectangle textRect, iconRect, viewRect;
223:
224: textRect = new Rectangle();
225: viewRect = new Rectangle();
226: iconRect = new Rectangle();
227:
228: viewRect.width = size.width
229: - (contentAreaInsets.left + contentAreaInsets.right);
230: viewRect.height = size.height
231: - (contentAreaInsets.top + contentAreaInsets.bottom);
232:
233: viewRect.x = contentAreaInsets.left;
234: viewRect.y = contentAreaInsets.right;
235: String llabel = (label != null) ? label : "";
236:
237: // layout the text and icon
238: String text = SwingUtilities.layoutCompoundLabel(fm,
239: llabel, null, SwingConstants.CENTER,
240: SwingConstants.CENTER, SwingConstants.CENTER,
241: SwingConstants.CENTER, viewRect, iconRect,
242: textRect, 0);
243:
244: Font f = getPeerFont();
245:
246: g.setFont(f);
247:
248: // perform UI specific press action, e.g. Windows L&F shifts text
249: if (pressed && armed) {
250: paintButtonPressed(g, target);
251: }
252:
253: paintText(g, target, textRect, text);
254:
255: if (hasFocus()) {
256: // paint UI specific focus
257: paintFocus(
258: g,
259: focusInsets.left,
260: focusInsets.top,
261: size.width
262: - (focusInsets.left + focusInsets.right)
263: - 1,
264: size.height
265: - (focusInsets.top + focusInsets.bottom)
266: - 1);
267: }
268: }
269: flush();
270: }
271:
272: public void paintBorder(Graphics g, int x, int y, int w, int h) {
273: drawMotif3DRect(g, x, y, w - 1, h - 1, pressed);
274: }
275:
276: public void setFont(Font f) {
277: super .setFont(f);
278: target.repaint();
279: }
280:
281: protected void paintFocus(Graphics g, int x, int y, int w, int h) {
282: g.setColor(focusColor);
283: g.drawRect(x, y, w, h);
284: }
285:
286: protected void paintButtonPressed(Graphics g, Component b) {
287: Dimension size = getPeerSize();
288: g.setColor(selectColor);
289: g
290: .fillRect(
291: contentAreaInsets.left,
292: contentAreaInsets.top,
293: size.width
294: - (contentAreaInsets.left + contentAreaInsets.right),
295: size.height
296: - (contentAreaInsets.top + contentAreaInsets.bottom));
297:
298: }
299:
300: protected void paintText(Graphics g, Component c,
301: Rectangle textRect, String text) {
302: FontMetrics fm = g.getFontMetrics();
303:
304: int mnemonicIndex = -1;
305:
306: /* Draw the Text */
307: if (isEnabled()) {
308: /*** paint the text normally */
309: g.setColor(getPeerForeground());
310: BasicGraphicsUtils.drawStringUnderlineCharAt(g, text,
311: mnemonicIndex, textRect.x, textRect.y
312: + fm.getAscent());
313: } else {
314: /*** paint the text disabled ***/
315: g.setColor(getPeerBackground().brighter());
316:
317: BasicGraphicsUtils.drawStringUnderlineCharAt(g, text,
318: mnemonicIndex, textRect.x, textRect.y
319: + fm.getAscent());
320: g.setColor(c.getBackground().darker());
321: BasicGraphicsUtils.drawStringUnderlineCharAt(g, text,
322: mnemonicIndex, textRect.x - 1, textRect.y
323: + fm.getAscent() - 1);
324: }
325: }
326: }
|