001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.tabcontrol.plaf;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.Dimension;
047: import java.awt.Font;
048: import java.awt.FontMetrics;
049: import java.awt.Graphics;
050: import java.awt.Graphics2D;
051: import java.awt.Insets;
052: import java.awt.Rectangle;
053:
054: import javax.swing.plaf.basic.BasicToggleButtonUI;
055: import java.awt.geom.AffineTransform;
056: import javax.swing.AbstractButton;
057: import javax.swing.BorderFactory;
058: import javax.swing.ButtonModel;
059: import javax.swing.Icon;
060: import javax.swing.JButton;
061: import javax.swing.JComponent;
062: import javax.swing.UIDefaults;
063: import javax.swing.UIManager;
064: import javax.swing.border.Border;
065: import javax.swing.border.CompoundBorder;
066: import javax.swing.plaf.ComponentUI;
067: import javax.swing.plaf.basic.BasicGraphicsUtils;
068: import org.netbeans.swing.tabcontrol.SlidingButton;
069: import org.netbeans.swing.tabcontrol.SlidingButtonUI;
070:
071: /**
072: *
073: * @see SlidingButtonUI
074: *
075: * @author Milos Kleint
076: */
077: public class WindowsSlidingButtonUI extends SlidingButtonUI {
078:
079: //XXX
080: private static final SlidingButtonUI INSTANCE = new WindowsSlidingButtonUI();
081:
082: // Has the shared instance defaults been initialized?
083: private boolean defaults_initialized = false;
084: protected Color focusColor;
085: protected static int dashedRectGapX;
086: protected static int dashedRectGapY;
087: protected static int dashedRectGapWidth;
088: protected static int dashedRectGapHeight;
089:
090: /** Private, no need for outer classes to instantiate */
091: protected WindowsSlidingButtonUI() {
092: }
093:
094: public static ComponentUI createUI(JComponent c) {
095: return INSTANCE;
096: }
097:
098: /** Install a border on the button */
099: protected void installBorder(AbstractButton b) {
100: // XXX
101: b.setBorder(//BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(),
102: BorderFactory.createEmptyBorder(2, 2, 2, 2));
103: }
104:
105: public void installDefaults(AbstractButton b) {
106: super .installDefaults(b);
107: if (!defaults_initialized) {
108: try {
109: //Null checks so this can be tested on other platforms
110: Integer in = ((Integer) UIManager
111: .get("Button.dashedRectGapX"));
112: dashedRectGapX = in == null ? 3 : in.intValue();
113: in = ((Integer) UIManager.get("Button.dashedRectGapY"));
114: dashedRectGapY = in == null ? 3 : in.intValue();
115: in = ((Integer) UIManager
116: .get("Button.dashedRectGapWidth"));
117: dashedRectGapWidth = in == null ? 3 : in.intValue();
118: in = ((Integer) UIManager
119: .get("Button.dashedRectGapHeight"));
120: dashedRectGapHeight = in == null ? 3 : in.intValue();
121: focusColor = UIManager.getColor(getPropertyPrefix()
122: + "focus");
123: defaults_initialized = true;
124: } catch (NullPointerException npe) {
125: //We're testing on a non windows platform, the defaults don't
126: //exist
127: dashedRectGapX = 2;
128: dashedRectGapY = 2;
129: dashedRectGapWidth = 2;
130: dashedRectGapHeight = 2;
131: }
132: }
133: }
134:
135: protected void uninstallDefaults(AbstractButton b) {
136: super .uninstallDefaults(b);
137: defaults_initialized = false;
138: }
139:
140: protected void paintBackground(Graphics2D g, AbstractButton b) {
141: if (((SlidingButton) b).isBlinkState()) {
142: g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR);
143: g.fillRect(0, 0, b.getWidth(), b.getHeight());
144: } else {
145: super .paintBackground(g, b);
146: }
147: }
148:
149: protected void paintButtonPressed(Graphics g, AbstractButton b) {
150: // This is a special case in which the toggle button in the
151: // Rollover JToolBar will render the button in a pressed state
152: Color oldColor = g.getColor();
153:
154: if (((SlidingButton) b).isBlinkState()) {
155: g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR);
156: g.fillRect(0, 0, b.getWidth(), b.getHeight());
157: }
158:
159: int w = b.getWidth();
160: int h = b.getHeight();
161: UIDefaults table = UIManager.getLookAndFeelDefaults();
162: if (b.getModel().isRollover()
163: && (!b.getModel().isPressed() && !b.getModel()
164: .isSelected())) {
165: g.setColor(table.getColor("ToggleButton.highlight"));
166: g.drawRect(0, 0, w - 1, h - 1);
167: g.drawRect(0, 0, 0, h - 1);
168:
169: Color shade = table.getColor("ToggleButton.shadow");
170: Component p = b.getParent();
171: if (p != null && p.getBackground().equals(shade)) {
172: shade = table.getColor("ToggleButton.darkShadow");
173: }
174: g.setColor(shade);
175: g.drawLine(w - 1, 0, w - 1, h - 1);
176: g.drawLine(0, h - 1, w - 1, h - 1);
177: } else {
178: Color shade = table.getColor("ToggleButton.shadow");
179: Component p = b.getParent();
180: if (p != null && p.getBackground().equals(shade)) {
181: shade = table.getColor("ToggleButton.darkShadow");
182: }
183: g.setColor(shade);
184: g.drawRect(0, 0, w - 1, h - 1);
185: g.setColor(table.getColor("ToggleButton.highlight"));
186: g.drawLine(w - 1, 0, w - 1, h - 1);
187: g.drawLine(0, h - 1, w - 1, h - 1);
188: }
189: g.setColor(oldColor);
190: }
191:
192: protected void paintFocus(Graphics g, AbstractButton b,
193: Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
194:
195: int width = b.getWidth();
196: int height = b.getHeight();
197: g.setColor(getFocusColor());
198: BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX,
199: dashedRectGapY, width - dashedRectGapWidth, height
200: - dashedRectGapHeight);
201: }
202:
203: protected Color getFocusColor() {
204: return focusColor;
205: }
206:
207: // ********************************
208: // Layout Methods
209: // ********************************
210: public Dimension getPreferredSize(JComponent c) {
211: Dimension d = super .getPreferredSize(c);
212:
213: /* Ensure that the width and height of the button is odd,
214: * to allow for the focus line if focus is painted
215: */
216: AbstractButton b = (AbstractButton) c;
217: if (b.isFocusPainted()) {
218: if (d.width % 2 == 0) {
219: d.width += 1;
220: }
221: if (d.height % 2 == 0) {
222: d.height += 1;
223: }
224: }
225: return d;
226: }
227:
228: }
|