blob: 3b49ad5ee4e078f9c6f17112ec2efdc71fbc733b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 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 com.sun.java.swing.plaf.windows;
27
28import javax.swing.plaf.basic.*;
29import javax.swing.border.*;
30import javax.swing.plaf.*;
31import javax.swing.*;
32
33import java.awt.*;
34import java.beans.PropertyChangeEvent;
35
36
37
38/**
39 * A Windows toggle button.
40 * <p>
41 * <strong>Warning:</strong>
42 * Serialized objects of this class will not be compatible with
43 * future Swing releases. The current serialization support is appropriate
44 * for short term storage or RMI between applications running the same
45 * version of Swing. A future release of Swing will provide support for
46 * long term persistence.
47 *
48 * @author Jeff Dinkins
49 */
50public class WindowsToggleButtonUI extends BasicToggleButtonUI
51{
52 protected static int dashedRectGapX;
53 protected static int dashedRectGapY;
54 protected static int dashedRectGapWidth;
55 protected static int dashedRectGapHeight;
56
57 protected Color focusColor;
58
59 private final static WindowsToggleButtonUI windowsToggleButtonUI = new WindowsToggleButtonUI();
60
61 private boolean defaults_initialized = false;
62
63 public static ComponentUI createUI(JComponent b) {
64 return windowsToggleButtonUI;
65 }
66
67
68 // ********************************
69 // Defaults
70 // ********************************
71 protected void installDefaults(AbstractButton b) {
72 super.installDefaults(b);
73 if(!defaults_initialized) {
74 String pp = getPropertyPrefix();
75 dashedRectGapX = ((Integer)UIManager.get("Button.dashedRectGapX")).intValue();
76 dashedRectGapY = ((Integer)UIManager.get("Button.dashedRectGapY")).intValue();
77 dashedRectGapWidth = ((Integer)UIManager.get("Button.dashedRectGapWidth")).intValue();
78 dashedRectGapHeight = ((Integer)UIManager.get("Button.dashedRectGapHeight")).intValue();
79 focusColor = UIManager.getColor(pp + "focus");
80 defaults_initialized = true;
81 }
82
83 XPStyle xp = XPStyle.getXP();
84 if (xp != null) {
85 b.setBorder(xp.getBorder(b, WindowsButtonUI.getXPButtonType(b)));
86 LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
87 LookAndFeel.installProperty(b, "rolloverEnabled", Boolean.TRUE);
88 }
89 }
90
91 protected void uninstallDefaults(AbstractButton b) {
92 super.uninstallDefaults(b);
93 defaults_initialized = false;
94 }
95
96
97 protected Color getFocusColor() {
98 return focusColor;
99 }
100
101
102 // ********************************
103 // Paint Methods
104 // ********************************
105
106 private transient Color cachedSelectedColor = null;
107 private transient Color cachedBackgroundColor = null;
108 private transient Color cachedHighlightColor = null;
109
110 protected void paintButtonPressed(Graphics g, AbstractButton b) {
111 if (XPStyle.getXP() == null && b.isContentAreaFilled()) {
112 Color oldColor = g.getColor();
113 Color c1 = b.getBackground();
114 Color c2 = UIManager.getColor("ToggleButton.highlight");
115 if (c1 != cachedBackgroundColor || c2 != cachedHighlightColor) {
116 int r1 = c1.getRed(), r2 = c2.getRed();
117 int g1 = c1.getGreen(), g2 = c2.getGreen();
118 int b1 = c1.getBlue(), b2 = c2.getBlue();
119 cachedSelectedColor = new Color(
120 Math.min(r1, r2) + Math.abs(r1 - r2) / 2,
121 Math.min(g1, g2) + Math.abs(g1 - g2) / 2,
122 Math.min(b1, b2) + Math.abs(b1 - b2) / 2
123 );
124 cachedBackgroundColor = c1;
125 cachedHighlightColor = c2;
126 }
127 g.setColor(cachedSelectedColor);
128 g.fillRect(0, 0, b.getWidth(), b.getHeight());
129 g.setColor(oldColor);
130 }
131 }
132
133 public void paint(Graphics g, JComponent c) {
134 if (XPStyle.getXP() != null) {
135 WindowsButtonUI.paintXPButtonBackground(g, c);
136 }
137 super.paint(g, c);
138 }
139
140
141 /**
142 * Overridden method to render the text without the mnemonic
143 */
144 protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
145 WindowsGraphicsUtils.paintText(g, b, textRect, text, getTextShiftOffset());
146 }
147
148 protected void paintFocus(Graphics g, AbstractButton b,
149 Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
150 g.setColor(getFocusColor());
151 BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
152 b.getWidth() - dashedRectGapWidth,
153 b.getHeight() - dashedRectGapHeight);
154 }
155
156 // ********************************
157 // Layout Methods
158 // ********************************
159 public Dimension getPreferredSize(JComponent c) {
160 Dimension d = super.getPreferredSize(c);
161
162 /* Ensure that the width and height of the button is odd,
163 * to allow for the focus line if focus is painted
164 */
165 AbstractButton b = (AbstractButton)c;
166 if (d != null && b.isFocusPainted()) {
167 if(d.width % 2 == 0) { d.width += 1; }
168 if(d.height % 2 == 0) { d.height += 1; }
169 }
170 return d;
171 }
172}