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: paintFocus(g, b, vr, tr, ir);
289: }
290:
291:
306: protected void paintFocus(Graphics g, AbstractButton b, Rectangle vr,
307: Rectangle tr, Rectangle ir)
308: {
309: if (b.hasFocus() && b.isFocusPainted())
310: {
311: Color saved_color = g.getColor();
312: g.setColor(focusColor);
313: Rectangle focusRect = ir.union(tr);
314: g.drawRect(focusRect.x, focusRect.y,
315: focusRect.width, focusRect.height);
316: g.setColor(saved_color);
317: }
318: }
319:
320:
329: protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect)
330: {
331: AbstractButton b = (AbstractButton) c;
332: Icon i = currentIcon(b);
333:
334: if (i != null)
335: i.paintIcon(c, g, iconRect.x, iconRect.y);
336: }
337:
338:
346: protected void paintButtonPressed(Graphics g, AbstractButton b)
347: {
348: if (b.isContentAreaFilled())
349: {
350: Rectangle area = new Rectangle();
351: SwingUtilities.calculateInnerArea(b, area);
352: g.setColor(b.getBackground().darker());
353: g.fillRect(area.x, area.y, area.width, area.height);
354: }
355: }
356:
357:
366: private void paintButtonNormal(Graphics g, Rectangle area, JComponent b)
367: {
368: if (((AbstractButton)b).isContentAreaFilled() && b.isOpaque())
369: {
370: g.setColor(b.getBackground());
371: g.fillRect(area.x, area.y, area.width, area.height);
372: }
373: }
374:
375:
384: protected void paintText(Graphics g, JComponent c, Rectangle textRect,
385: String text)
386: {
387: paintText(g, (AbstractButton) c, textRect, text);
388: }
389:
390:
401: protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
402: String text)
403: {
404: Font f = b.getFont();
405: g.setFont(f);
406: FontMetrics fm = g.getFontMetrics(f);
407:
408: if (b.isEnabled())
409: {
410: g.setColor(b.getForeground());
411: g.drawString(text, textRect.x, textRect.y + fm.getAscent());
412: }
413: else
414: {
415: g.setColor(b.getBackground().brighter());
416: g.drawString(text, textRect.x, textRect.y + fm.getAscent());
417: g.setColor(b.getBackground().darker());
418: g.drawString(text, textRect.x + 1, textRect.y + fm.getAscent() + 1);
419: }
420: }
421: }