blob: 9917640cace247f714d9f4f2b5346549aac1cf68 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.plaf.metal;
27
28import sun.swing.SwingUtilities2;
29import javax.swing.*;
30import javax.swing.border.*;
31import javax.swing.plaf.basic.*;
32import java.awt.*;
33import java.awt.event.*;
34import java.beans.*;
35import javax.swing.plaf.*;
36
37/**
38 * MetalButtonUI implementation
39 * <p>
40 * <strong>Warning:</strong>
41 * Serialized objects of this class will not be compatible with
42 * future Swing releases. The current serialization support is
43 * appropriate for short term storage or RMI between applications running
44 * the same version of Swing. As of 1.4, support for long term storage
45 * of all JavaBeans<sup><font size="-2">TM</font></sup>
46 * has been added to the <code>java.beans</code> package.
47 * Please see {@link java.beans.XMLEncoder}.
48 *
49 * @author Tom Santos
50 */
51public class MetalButtonUI extends BasicButtonUI {
52
53 private final static MetalButtonUI metalButtonUI = new MetalButtonUI();
54
55 // NOTE: These are not really needed, but at this point we can't pull
56 // them. Their values are updated purely for historical reasons.
57 protected Color focusColor;
58 protected Color selectColor;
59 protected Color disabledTextColor;
60
61 // ********************************
62 // Create PLAF
63 // ********************************
64 public static ComponentUI createUI(JComponent c) {
65 return metalButtonUI;
66 }
67
68 // ********************************
69 // Install
70 // ********************************
71 public void installDefaults(AbstractButton b) {
72 super.installDefaults(b);
73 }
74
75 public void uninstallDefaults(AbstractButton b) {
76 super.uninstallDefaults(b);
77 }
78
79 // ********************************
80 // Create Listeners
81 // ********************************
82 protected BasicButtonListener createButtonListener(AbstractButton b) {
83 return super.createButtonListener(b);
84 }
85
86
87 // ********************************
88 // Default Accessors
89 // ********************************
90 protected Color getSelectColor() {
91 selectColor = UIManager.getColor(getPropertyPrefix() + "select");
92 return selectColor;
93 }
94
95 protected Color getDisabledTextColor() {
96 disabledTextColor = UIManager.getColor(getPropertyPrefix() +
97 "disabledText");
98 return disabledTextColor;
99 }
100
101 protected Color getFocusColor() {
102 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
103 return focusColor;
104 }
105
106 // ********************************
107 // Paint
108 // ********************************
109 /**
110 * If necessary paints the background of the component, then
111 * invokes <code>paint</code>.
112 *
113 * @param g Graphics to paint to
114 * @param c JComponent painting on
115 * @throws NullPointerException if <code>g</code> or <code>c</code> is
116 * null
117 * @see javax.swing.plaf.ComponentUI#update
118 * @see javax.swing.plaf.ComponentUI#paint
119 * @since 1.5
120 */
121 public void update(Graphics g, JComponent c) {
122 AbstractButton button = (AbstractButton)c;
123 if ((c.getBackground() instanceof UIResource) &&
124 button.isContentAreaFilled() && c.isEnabled()) {
125 ButtonModel model = button.getModel();
126 if (!MetalUtils.isToolBarButton(c)) {
127 if (!model.isArmed() && !model.isPressed() &&
128 MetalUtils.drawGradient(
129 c, g, "Button.gradient", 0, 0, c.getWidth(),
130 c.getHeight(), true)) {
131 paint(g, c);
132 return;
133 }
134 }
135 else if (model.isRollover() && MetalUtils.drawGradient(
136 c, g, "Button.gradient", 0, 0, c.getWidth(),
137 c.getHeight(), true)) {
138 paint(g, c);
139 return;
140 }
141 }
142 super.update(g, c);
143 }
144
145 protected void paintButtonPressed(Graphics g, AbstractButton b) {
146 if ( b.isContentAreaFilled() ) {
147 Dimension size = b.getSize();
148 g.setColor(getSelectColor());
149 g.fillRect(0, 0, size.width, size.height);
150 }
151 }
152
153 protected void paintFocus(Graphics g, AbstractButton b,
154 Rectangle viewRect, Rectangle textRect, Rectangle iconRect){
155
156 Rectangle focusRect = new Rectangle();
157 String text = b.getText();
158 boolean isIcon = b.getIcon() != null;
159
160 // If there is text
161 if ( text != null && !text.equals( "" ) ) {
162 if ( !isIcon ) {
163 focusRect.setBounds( textRect );
164 }
165 else {
166 focusRect.setBounds( iconRect.union( textRect ) );
167 }
168 }
169 // If there is an icon and no text
170 else if ( isIcon ) {
171 focusRect.setBounds( iconRect );
172 }
173
174 g.setColor(getFocusColor());
175 g.drawRect((focusRect.x-1), (focusRect.y-1),
176 focusRect.width+1, focusRect.height+1);
177
178 }
179
180
181 protected void paintText(Graphics g, JComponent c, Rectangle textRect, String text) {
182 AbstractButton b = (AbstractButton) c;
183 ButtonModel model = b.getModel();
184 FontMetrics fm = SwingUtilities2.getFontMetrics(c, g);
185 int mnemIndex = b.getDisplayedMnemonicIndex();
186
187 /* Draw the Text */
188 if(model.isEnabled()) {
189 /*** paint the text normally */
190 g.setColor(b.getForeground());
191 }
192 else {
193 /*** paint the text disabled ***/
194 g.setColor(getDisabledTextColor());
195 }
196 SwingUtilities2.drawStringUnderlineCharAt(c, g,text,mnemIndex,
197 textRect.x, textRect.y + fm.getAscent());
198 }
199}