001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ButtonFactory.java,v 1.23 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.gui;
024:
025: import net.infonode.gui.border.HighlightBorder;
026: import net.infonode.util.ColorUtil;
027:
028: import javax.swing.*;
029: import javax.swing.border.Border;
030: import javax.swing.border.CompoundBorder;
031: import javax.swing.border.EmptyBorder;
032: import javax.swing.border.LineBorder;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035: import javax.swing.plaf.ButtonUI;
036: import java.awt.*;
037: import java.awt.event.*;
038: import java.net.URL;
039:
040: public class ButtonFactory {
041: private ButtonFactory() {
042: }
043:
044: private static class ButtonHighlighter implements
045: ComponentListener, HierarchyListener {
046: /* private static final Color HIGHLIGHTED_COLOR = new Color(140, 160, 255);
047: private static final Color PRESSED_COLOR = new Color(60, 80, 200);
048: */
049: private JButton button;
050: private Border pressedBorder;
051: private Border highlightedBorder;
052: private Border normalBorder;
053: private boolean rollover;
054: private long rolloverStart; // Ugly hack to avoid false rollover callbacks which occur when the button is moved
055:
056: ButtonHighlighter(JButton button, int padding) {
057: this .button = button;
058:
059: normalBorder = new EmptyBorder(padding + 2, padding + 2,
060: padding + 2, padding + 2);
061: pressedBorder = new EmptyBorder(padding + 2, padding + 2,
062: padding, padding);
063: highlightedBorder = new EmptyBorder(padding + 1,
064: padding + 1, padding + 1, padding + 1);
065:
066: button.setContentAreaFilled(false);
067: setNormalState();
068:
069: button.addChangeListener(new ChangeListener() {
070: public void stateChanged(ChangeEvent e) {
071: rollover = (System.currentTimeMillis() - rolloverStart) > 20
072: && ButtonHighlighter.this .button.getModel()
073: .isRollover();
074: update();
075:
076: if (ButtonHighlighter.this .button.getModel()
077: .isRollover())
078: rolloverStart = 0;
079: }
080: });
081:
082: button.addHierarchyListener(this );
083: button.addComponentListener(this );
084: }
085:
086: private void setNormalState() {
087: button.setBackground(null);
088: button.setOpaque(false);
089: button.setBorder(normalBorder);
090: rollover = false;
091: }
092:
093: public void componentHidden(ComponentEvent e) {
094: setNormalState();
095: rolloverStart = System.currentTimeMillis();
096: }
097:
098: public void componentMoved(ComponentEvent e) {
099: setNormalState();
100: rolloverStart = System.currentTimeMillis();
101: }
102:
103: public void componentResized(ComponentEvent e) {
104: setNormalState();
105: rolloverStart = System.currentTimeMillis();
106: }
107:
108: public void componentShown(ComponentEvent e) {
109: setNormalState();
110: rolloverStart = System.currentTimeMillis();
111: }
112:
113: public void hierarchyChanged(HierarchyEvent e) {
114: setNormalState();
115: rolloverStart = System.currentTimeMillis();
116: }
117:
118: private void update() {
119: boolean pressed = button.getModel().isArmed();
120:
121: if (button.isEnabled() && (rollover || pressed)) {
122: button.setOpaque(true);
123: Color backgroundColor = ComponentUtil
124: .getBackgroundColor(button.getParent());
125: backgroundColor = backgroundColor == null ? UIManagerUtil
126: .getColor("control", Color.LIGHT_GRAY)
127: : backgroundColor;
128: button.setBackground(ColorUtil.mult(backgroundColor,
129: pressed ? 0.8 : 1.15));
130:
131: button.setBorder(pressed ? new CompoundBorder(
132: new LineBorder(ColorUtil.mult(backgroundColor,
133: 0.3)), pressedBorder)
134: : new CompoundBorder(new LineBorder(ColorUtil
135: .mult(backgroundColor, 0.5)),
136: highlightedBorder));
137: } else {
138: setNormalState();
139: }
140: }
141:
142: }
143:
144: private static final Border normalBorder = new CompoundBorder(
145: new LineBorder(new Color(70, 70, 70)), new CompoundBorder(
146: new HighlightBorder(), new EmptyBorder(1, 6, 1, 6)));
147: private static final Border pressedBorder = new CompoundBorder(
148: new LineBorder(new Color(70, 70, 70)), new CompoundBorder(
149: new HighlightBorder(true), new EmptyBorder(2, 7, 0,
150: 5)));
151:
152: private static JButton initButton(final JButton button) {
153: button.setMargin(null);
154: button.setBorder(normalBorder);
155: button.addMouseListener(new MouseAdapter() {
156: public void mousePressed(MouseEvent e) {
157: button.setBorder(pressedBorder);
158: }
159:
160: public void mouseReleased(MouseEvent e) {
161: button.setBorder(normalBorder);
162: }
163: });
164:
165: return button;
166: }
167:
168: private static JButton newButton(String text) {
169: return initButton(new JButton(text));
170: }
171:
172: private static JButton newButton(Icon icon) {
173: return initButton(new JButton(icon));
174: }
175:
176: private static JButton newButton(Icon icon, String text) {
177: return initButton(new JButton(text, icon));
178: }
179:
180: public static final JButton createDialogButton(String text,
181: ActionListener action) {
182: JButton b = new JButton(text);
183: b.setFont(b.getFont().deriveFont(Font.BOLD));
184: b.addActionListener(action);
185: return b;
186: }
187:
188: public static final JButton createButton(String text,
189: ActionListener action) {
190: return createButton(text, true, action);
191: }
192:
193: public static final JButton createButton(String text,
194: boolean opaque, ActionListener action) {
195: JButton b = newButton(text);
196: b.setOpaque(opaque);
197: b.addActionListener(action);
198: return b;
199: }
200:
201: public static final JButton createButton(String iconResource,
202: String text, ActionListener action) {
203: URL iconURL = ButtonFactory.class.getClassLoader().getResource(
204: iconResource);
205: return createButton(iconURL == null ? null : new ImageIcon(
206: iconURL), text, action);
207: }
208:
209: public static final JButton createButton(Icon icon, String text,
210: ActionListener action) {
211: JButton b;
212:
213: if (icon != null) {
214: b = newButton(icon);
215: b.setToolTipText(text);
216: } else {
217: b = newButton(text);
218: }
219:
220: b.addActionListener(action);
221: return b;
222: }
223:
224: public static final JButton createButton(Icon icon,
225: String tooltipText, boolean opaque, ActionListener action) {
226: JButton b = newButton(icon);
227: b.setToolTipText(tooltipText);
228: b.addActionListener(action);
229: b.setOpaque(opaque);
230: return b;
231: }
232:
233: public static final JButton createFlatHighlightButton(Icon icon,
234: String tooltipText, int padding, ActionListener action) {
235: final JButton b = new JButton(icon) {
236: public void setUI(ButtonUI ui) {
237: super .setUI(new FlatIconButtonUI());
238: }
239: };
240: b.setVerticalAlignment(SwingConstants.CENTER);
241: b.setToolTipText(tooltipText);
242: b.setMargin(new Insets(0, 0, 0, 0));
243: new ButtonHighlighter(b, padding);
244:
245: b.setRolloverEnabled(true);
246:
247: if (action != null)
248: b.addActionListener(action);
249:
250: return b;
251: }
252:
253: public static final void applyButtonHighlighter(JButton b,
254: int padding) {
255: b.setVerticalAlignment(SwingConstants.CENTER);
256: b.setMargin(new Insets(0, 0, 0, 0));
257: new ButtonHighlighter(b, padding);
258:
259: b.setRolloverEnabled(true);
260: }
261:
262: public static final JButton createFlatHighlightButton(Icon icon,
263: String tooltipText, int padding, boolean focusable,
264: ActionListener action) {
265: final JButton b = createFlatHighlightButton(icon, tooltipText,
266: padding, action);
267: b.setFocusable(focusable);
268: return b;
269: }
270:
271: public static final JButton createHighlightButton(String text,
272: ActionListener action) {
273: JButton b = newButton(text);
274: b.addActionListener(action);
275: return b;
276: }
277:
278: public static final JButton createHighlightButton(Icon icon,
279: ActionListener action) {
280: JButton b = newButton(icon);
281: b.addActionListener(action);
282: return b;
283: }
284:
285: public static final JButton createHighlightButton(Icon icon,
286: String text, ActionListener action) {
287: JButton b = newButton(icon, text);
288: b.addActionListener(action);
289: return b;
290: }
291:
292: public static final JButton createFlatIconHoverButton(Icon icon,
293: Icon hovered, Icon pressed) {
294: final JButton b = new JButton(icon) {
295: public void setUI(ButtonUI ui) {
296: super .setUI(new FlatIconButtonUI());
297: }
298: };
299: b.setPressedIcon(pressed);
300: b.setRolloverEnabled(true);
301: b.setRolloverIcon(hovered);
302: b.setVerticalAlignment(SwingConstants.CENTER);
303: return b;
304: }
305: }
|