1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59: public class BasicButtonUI extends ButtonUI
60: {
61:
65: protected int defaultTextIconGap = 4;
66:
67:
71: protected int defaultTextShiftOffset = 0;
72:
73: private int textShiftOffset;
74:
75: private Color focusColor;
76:
77:
85: public static ComponentUI createUI(final JComponent c)
86: {
87: return new BasicButtonUI();
88: }
89:
90: public int getDefaultTextIconGap(AbstractButton b)
91: {
92: return defaultTextIconGap;
93: }
94:
95: protected void clearTextShiftOffset()
96: {
97: textShiftOffset = 0;
98: }
99:
100: protected int getTextShiftOffset()
101: {
102: return textShiftOffset;
103: }
104:
105: protected void setTextShiftOffset()
106: {
107: textShiftOffset = defaultTextShiftOffset;
108: }
109:
110:
116: protected String getPropertyPrefix()
117: {
118: return "Button.";
119: }
120:
121: protected void installDefaults(AbstractButton b)
122: {
123: UIDefaults defaults = UIManager.getLookAndFeelDefaults();
124: String prefix = getPropertyPrefix();
125: focusColor = defaults.getColor(prefix + "focus");
126: b.setForeground(defaults.getColor(prefix + "foreground"));
127: b.setBackground(defaults.getColor(prefix + "background"));
128: b.setMargin(defaults.getInsets(prefix + "margin"));
129: b.setBorder(defaults.getBorder(prefix + "border"));
130: b.setIconTextGap(defaults.getInt(prefix + "textIconGap"));
131: b.setInputMap(JComponent.WHEN_FOCUSED,
132: (InputMap) defaults.get(prefix + "focusInputMap"));
133: b.setOpaque(true);
134: }
135:
136: protected void uninstallDefaults(AbstractButton b)
137: {
138: b.setForeground(null);
139: b.setBackground(null);
140: b.setBorder(null);
141: b.setIconTextGap(defaultTextIconGap);
142: b.setMargin(null);
143: }
144:
145: protected BasicButtonListener listener;
146:
147: protected BasicButtonListener createButtonListener(AbstractButton b)
148: {
149: return new BasicButtonListener(b);
150: }
151:
152: protected void installListeners(AbstractButton b)
153: {
154: listener = createButtonListener(b);
155: b.addChangeListener(listener);
156: b.addPropertyChangeListener(listener);
157: b.addFocusListener(listener);
158: b.addMouseListener(listener);
159: b.addMouseMotionListener(listener);
160: }
161:
162: protected void uninstallListeners(AbstractButton b)
163: {
164: b.removeChangeListener(listener);
165: b.removePropertyChangeListener(listener);
166: b.removeFocusListener(listener);
167: b.removeMouseListener(listener);
168: b.removeMouseMotionListener(listener);
169: }
170:
171: protected void installKeyboardActions(AbstractButton b)
172: {
173: listener.installKeyboardActions(b);
174: }
175:
176: protected void uninstallKeyboardActions(AbstractButton b)
177: {
178: listener.uninstallKeyboardActions(b);
179: }
180:
181:
189: public void installUI(final JComponent c)
190: {
191: super.installUI(c);
192: if (c instanceof AbstractButton)
193: {
194: AbstractButton b = (AbstractButton) c;
195: installDefaults(b);
196: installListeners(b);
197: installKeyboardActions(b);
198: }
199: }
200:
201:
209: public Dimension getPreferredSize(JComponent c)
210: {
211: AbstractButton b = (AbstractButton)c;
212: Dimension d =
213: BasicGraphicsUtils.getPreferredButtonSize
214: (b, defaultTextIconGap + defaultTextShiftOffset);
215: return d;
216: }
217:
218: private static Icon currentIcon(AbstractButton b)
219: {
220: Icon i = b.getIcon();
221: ButtonModel model = b.getModel();
222:
223: if (model.isPressed() && b.getPressedIcon() != null)
224: i = b.getPressedIcon();
225:
226: else if (model.isRollover())
227: {
228: if (b.isSelected() && b.getRolloverSelectedIcon() != null)
229: i = b.getRolloverSelectedIcon();
230: else if (b.getRolloverIcon() != null)
231: i = b.getRolloverIcon();
232: }
233:
234: else if (b.isSelected())
235: {
236: if (b.isEnabled() && b.getSelectedIcon() != null)
237: i = b.getSelectedIcon();
238: else if (b.getDisabledSelectedIcon() != null)
239: i = b.getDisabledSelectedIcon();
240: }
241:
242: else if (! b.isEnabled() && b.getDisabledIcon() != null)
243: i = b.getDisabledIcon();
244:
245: return i;
246: }
247:
248:
255: public void paint(Graphics g, JComponent c)
256: {
257: AbstractButton b = (AbstractButton) c;
258:
259: Rectangle tr = new Rectangle();
260: Rectangle ir = new Rectangle();
261: Rectangle vr = new Rectangle();
262:
263: Font f = c.getFont();
264:
265: g.setFont(f);
266:
267: SwingUtilities.calculateInnerArea(b, vr);
268: String text = SwingUtilities.layoutCompoundLabel(c, g.getFontMetrics(f),
269: b.getText(),
270: currentIcon(b),
271: b.getVerticalAlignment(),
272: b.getHorizontalAlignment(),
273: b.getVerticalTextPosition(),
274: b.getHorizontalTextPosition(),
275: vr, ir, tr,
276: b.getIconTextGap()
277: + defaultTextShiftOffset);
278:
279: if ((b.getModel().isArmed() && b.getModel().isPressed())
280: || b.isSelected())
281: paintButtonPressed(g, b);
282: else
283: paintButtonNormal(g, vr, c);
284:
285: paintIcon(g, c, ir);
286: if (text != null)
287: paintText(g, b, tr, text);
288: if (b.isFocusOwner())
289: paintFocus(g, b, vr, tr, ir);
290: }
291:
292:
307: protected void paintFocus(Graphics g, AbstractButton b, Rectangle vr,
308: Rectangle tr, Rectangle ir)
309: {
310:
311:
312: }
313:
314:
323: protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect)
324: {
325: AbstractButton b = (AbstractButton) c;
326: Icon i = currentIcon(b);
327:
328: if (i != null)
329: i.paintIcon(c, g, iconRect.x, iconRect.y);
330: }
331:
332:
340: protected void paintButtonPressed(Graphics g, AbstractButton b)
341: {
342: if (b.isContentAreaFilled())
343: {
344: Rectangle area = new Rectangle();
345: SwingUtilities.calculateInnerArea(b, area);
346: g.setColor(b.getBackground().darker());
347: g.fillRect(area.x, area.y, area.width, area.height);
348: }
349: }
350:
351:
360: private void paintButtonNormal(Graphics g, Rectangle area, JComponent b)
361: {
362: if (((AbstractButton)b).isContentAreaFilled() && b.isOpaque())
363: {
364: g.setColor(b.getBackground());
365: g.fillRect(area.x, area.y, area.width, area.height);
366: }
367: }
368:
369:
378: protected void paintText(Graphics g, JComponent c, Rectangle textRect,
379: String text)
380: {
381: paintText(g, (AbstractButton) c, textRect, text);
382: }
383:
384:
395: protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
396: String text)
397: {
398: Font f = b.getFont();
399: g.setFont(f);
400: FontMetrics fm = g.getFontMetrics(f);
401:
402: if (b.isEnabled())
403: {
404: g.setColor(b.getForeground());
405: g.drawString(text, textRect.x, textRect.y + fm.getAscent());
406: }
407: else
408: {
409: g.setColor(b.getBackground().brighter());
410: g.drawString(text, textRect.x, textRect.y + fm.getAscent());
411: g.setColor(b.getBackground().darker());
412: g.drawString(text, textRect.x + 1, textRect.y + fm.getAscent() + 1);
413: }
414: }
415: }