KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > plaf > WindowsSlidingButtonUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol.plaf;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.FontMetrics JavaDoc;
27 import java.awt.Graphics JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.Insets JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31
32 import javax.swing.plaf.basic.BasicToggleButtonUI JavaDoc;
33 import java.awt.geom.AffineTransform JavaDoc;
34 import javax.swing.AbstractButton JavaDoc;
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.ButtonModel JavaDoc;
37 import javax.swing.Icon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.UIDefaults JavaDoc;
41 import javax.swing.UIManager JavaDoc;
42 import javax.swing.border.Border JavaDoc;
43 import javax.swing.border.CompoundBorder JavaDoc;
44 import javax.swing.plaf.ComponentUI JavaDoc;
45 import javax.swing.plaf.basic.BasicGraphicsUtils JavaDoc;
46 import org.netbeans.swing.tabcontrol.SlidingButton;
47 import org.netbeans.swing.tabcontrol.SlidingButtonUI;
48
49 /**
50  *
51  * @see SlidingButtonUI
52  *
53  * @author Milos Kleint
54  */

55 public class WindowsSlidingButtonUI extends SlidingButtonUI {
56
57     //XXX
58
private static final SlidingButtonUI INSTANCE = new WindowsSlidingButtonUI();
59     
60     // Has the shared instance defaults been initialized?
61
private boolean defaults_initialized = false;
62     protected Color JavaDoc focusColor;
63     protected static int dashedRectGapX;
64     protected static int dashedRectGapY;
65     protected static int dashedRectGapWidth;
66     protected static int dashedRectGapHeight;
67     
68     
69     /** Private, no need for outer classes to instantiate */
70     protected WindowsSlidingButtonUI() {
71     }
72     
73     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
74         return INSTANCE;
75     }
76
77     /** Install a border on the button */
78     protected void installBorder (AbstractButton JavaDoc b) {
79         // XXX
80
b.setBorder (//BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(),
81
BorderFactory.createEmptyBorder(2, 2, 2, 2));
82     }
83     
84     public void installDefaults (AbstractButton JavaDoc b) {
85         super.installDefaults(b);
86     if(!defaults_initialized) {
87             try {
88                 //Null checks so this can be tested on other platforms
89
Integer JavaDoc in = ((Integer JavaDoc)UIManager.get("Button.dashedRectGapX"));
90                 dashedRectGapX = in == null ? 3 : in.intValue();
91                 in = ((Integer JavaDoc)UIManager.get("Button.dashedRectGapY"));
92                 dashedRectGapY = in == null ? 3 : in.intValue();
93                 in = ((Integer JavaDoc)UIManager.get("Button.dashedRectGapWidth"));
94                 dashedRectGapWidth = in == null ? 3 : in.intValue();
95                 in = ((Integer JavaDoc)UIManager.get("Button.dashedRectGapHeight"));
96                 dashedRectGapHeight = in == null ? 3 : in.intValue();
97                 focusColor = UIManager.getColor(getPropertyPrefix() + "focus");
98                 defaults_initialized = true;
99             } catch (NullPointerException JavaDoc npe) {
100                 //We're testing on a non windows platform, the defaults don't
101
//exist
102
dashedRectGapX = 2;
103                 dashedRectGapY = 2;
104                 dashedRectGapWidth = 2;
105                 dashedRectGapHeight = 2;
106             }
107     }
108     }
109     
110     protected void uninstallDefaults(AbstractButton JavaDoc b) {
111     super.uninstallDefaults(b);
112     defaults_initialized = false;
113     }
114     
115     protected void paintBackground(Graphics2D JavaDoc g, AbstractButton JavaDoc b) {
116         if (((SlidingButton) b).isBlinkState()) {
117             g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR);
118             g.fillRect (0, 0, b.getWidth(), b.getHeight());
119         } else {
120             super.paintBackground(g, b);
121         }
122     }
123     
124     protected void paintButtonPressed(Graphics JavaDoc g, AbstractButton JavaDoc b) {
125         // This is a special case in which the toggle button in the
126
// Rollover JToolBar will render the button in a pressed state
127
Color JavaDoc oldColor = g.getColor();
128         
129         if (((SlidingButton) b).isBlinkState()) {
130             g.setColor(WinClassicEditorTabCellRenderer.ATTENTION_COLOR);
131             g.fillRect (0, 0, b.getWidth(), b.getHeight());
132         }
133         
134         int w = b.getWidth();
135         int h = b.getHeight();
136         UIDefaults JavaDoc table = UIManager.getLookAndFeelDefaults();
137         if (b.getModel().isRollover() && (! b.getModel().isPressed() && ! b.getModel().isSelected())) {
138             g.setColor(table.getColor("ToggleButton.highlight"));
139             g.drawRect(0, 0, w-1, h-1);
140             g.drawRect(0, 0, 0, h-1);
141             
142             Color JavaDoc shade = table.getColor("ToggleButton.shadow");
143             Component JavaDoc p = b.getParent();
144             if (p != null && p.getBackground().equals(shade)) {
145                 shade = table.getColor("ToggleButton.darkShadow");
146             }
147             g.setColor(shade);
148             g.drawLine(w-1, 0, w-1, h-1);
149             g.drawLine(0, h-1, w-1, h-1);
150         } else {
151             Color JavaDoc shade = table.getColor("ToggleButton.shadow");
152             Component JavaDoc p = b.getParent();
153             if (p != null && p.getBackground().equals(shade)) {
154                 shade = table.getColor("ToggleButton.darkShadow");
155             }
156             g.setColor(shade);
157             g.drawRect(0, 0, w-1, h-1);
158             g.setColor(table.getColor("ToggleButton.highlight"));
159             g.drawLine(w-1, 0, w-1, h-1);
160             g.drawLine(0, h-1, w-1, h-1);
161         }
162         g.setColor(oldColor);
163     }
164     
165     protected void paintFocus(Graphics JavaDoc g, AbstractButton JavaDoc b, Rectangle JavaDoc viewRect, Rectangle JavaDoc textRect, Rectangle JavaDoc iconRect){
166
167     int width = b.getWidth();
168     int height = b.getHeight();
169     g.setColor(getFocusColor());
170     BasicGraphicsUtils.drawDashedRect(g, dashedRectGapX, dashedRectGapY,
171                       width - dashedRectGapWidth, height - dashedRectGapHeight);
172     }
173     
174
175     protected Color JavaDoc getFocusColor() {
176     return focusColor;
177     }
178     
179     
180     
181     
182     // ********************************
183
// Layout Methods
184
// ********************************
185
public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
186     Dimension JavaDoc d = super.getPreferredSize(c);
187
188     /* Ensure that the width and height of the button is odd,
189      * to allow for the focus line if focus is painted
190      */

191         AbstractButton JavaDoc b = (AbstractButton JavaDoc)c;
192     if (b.isFocusPainted()) {
193         if(d.width % 2 == 0) { d.width += 1; }
194         if(d.height % 2 == 0) { d.height += 1; }
195     }
196     return d;
197     }
198     
199     
200 }
201
Popular Tags