KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsBorders


1 /*
2  * @(#)WindowsBorders.java 1.32 06/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import javax.swing.*;
11 import javax.swing.border.*;
12 import javax.swing.plaf.*;
13 import javax.swing.plaf.basic.*;
14
15 import java.awt.Component JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Image JavaDoc;
19 import java.awt.Rectangle JavaDoc;
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics JavaDoc;
22 import java.io.Serializable JavaDoc;
23
24 import com.sun.java.swing.plaf.windows.TMSchema.*;
25 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
26
27 /**
28  * Factory object that can vend Borders appropriate for the Windows 95 L & F.
29  * @version 1.32 12/19/06
30  * @author Rich Schiavi
31  */

32
33 public class WindowsBorders {
34
35     /**
36      * Returns a border instance for a Windows Progress Bar
37      * @since 1.4
38      */

39     public static Border getProgressBarBorder() {
40     UIDefaults table = UIManager.getLookAndFeelDefaults();
41     Border progressBarBorder = new BorderUIResource.CompoundBorderUIResource(
42                      new WindowsBorders.ProgressBarBorder(
43                           table.getColor("ProgressBar.shadow"),
44                               table.getColor("ProgressBar.highlight")),
45                           new EmptyBorder(1,1,1,1)
46                     );
47     return progressBarBorder;
48     }
49
50     /**
51      * Returns a border instance for a Windows ToolBar
52      *
53      * @return a border used for the toolbar
54      * @since 1.4
55      */

56     public static Border getToolBarBorder() {
57     UIDefaults table = UIManager.getLookAndFeelDefaults();
58     Border toolBarBorder = new WindowsBorders.ToolBarBorder(
59                     table.getColor("ToolBar.shadow"),
60                     table.getColor("ToolBar.highlight"));
61     return toolBarBorder;
62     }
63     
64     /**
65      * Returns an new instance of a border used to indicate which cell item
66      * has focus.
67      *
68      * @return a border to indicate which cell item has focus
69      * @since 1.4
70      */

71     public static Border getFocusCellHighlightBorder() {
72         return new ComplementDashedBorder();
73     }
74
75     public static Border getTableHeaderBorder() {
76     UIDefaults table = UIManager.getLookAndFeelDefaults();
77         Border tableHeaderBorder = new BorderUIResource.CompoundBorderUIResource(
78                new BasicBorders.ButtonBorder(
79                        table.getColor("Table.shadow"),
80                                            table.getColor("Table.darkShadow"),
81                                            table.getColor("Table.light"),
82                                            table.getColor("Table.highlight")),
83                          new BasicBorders.MarginBorder());
84     return tableHeaderBorder;
85     }
86
87     public static Border getInternalFrameBorder() {
88         UIDefaults table = UIManager.getLookAndFeelDefaults();
89         Border internalFrameBorder = new
90             BorderUIResource.CompoundBorderUIResource(
91                 BorderFactory.createBevelBorder(BevelBorder.RAISED,
92                     table.getColor("InternalFrame.borderColor"),
93                     table.getColor("InternalFrame.borderHighlight"),
94                     table.getColor("InternalFrame.borderDarkShadow"),
95                     table.getColor("InternalFrame.borderShadow")),
96                 new WindowsBorders.InternalFrameLineBorder(
97                     table.getColor("InternalFrame.activeBorderColor"),
98                     table.getColor("InternalFrame.inactiveBorderColor"),
99                     table.getInt("InternalFrame.borderWidth")));
100
101         return internalFrameBorder;
102     }
103
104     public static class ProgressBarBorder extends AbstractBorder implements UIResource {
105         protected Color JavaDoc shadow;
106         protected Color JavaDoc highlight;
107
108         public ProgressBarBorder(Color JavaDoc shadow, Color JavaDoc highlight) {
109             this.highlight = highlight;
110             this.shadow = shadow;
111         }
112
113         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
114                                 int width, int height) {
115         g.setColor(shadow);
116         g.drawLine(x,y, width-1,y); // draw top
117
g.drawLine(x,y, x,height-1); // draw left
118
g.setColor(highlight);
119         g.drawLine(x,height-1, width-1,height-1); // draw bottom
120
g.drawLine(width-1,y, width-1,height-1); // draw right
121
}
122
123         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
124             return new Insets JavaDoc(1,1,1,1);
125         }
126
127         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
128         insets.top = insets.left = insets.bottom = insets.right = 1;
129         return insets;
130     }
131     }
132
133     /**
134      * A border for the ToolBar. If the ToolBar is floatable then the handle grip is drawn
135      * <p>
136      * @since 1.4
137      */

138     public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants {
139         protected Color JavaDoc shadow;
140         protected Color JavaDoc highlight;
141
142         public ToolBarBorder(Color JavaDoc shadow, Color JavaDoc highlight) {
143             this.highlight = highlight;
144             this.shadow = shadow;
145         }
146
147         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
148                                 int width, int height) {
149         g.translate(x, y);
150
151             XPStyle xp = XPStyle.getXP();
152         if (xp != null) {
153                 Border xpBorder = xp.getBorder(c, Part.TP_TOOLBAR);
154         if (xpBorder != null) {
155             xpBorder.paintBorder(c, g, 0, 0, width, height);
156         }
157         }
158         if (((JToolBar)c).isFloatable()) {
159         boolean vertical = ((JToolBar)c).getOrientation() == VERTICAL;
160
161         if (xp != null) {
162                     Part part = vertical ? Part.RP_GRIPPERVERT : Part.RP_GRIPPER;
163                     Skin skin = xp.getSkin(c, part);
164             int dx, dy, dw, dh;
165             if (vertical) {
166             dx = 0;
167             dy = 2;
168             dw = width - 1;
169             dh = skin.getHeight();
170             } else {
171             dw = skin.getWidth();
172             dh = height - 1;
173             dx = c.getComponentOrientation().isLeftToRight() ? 2 : (width-dw-2);
174             dy = 0;
175             }
176                     skin.paintSkin(g, dx, dy, dw, dh, State.NORMAL);
177
178         } else {
179
180         if (!vertical) {
181             if (c.getComponentOrientation().isLeftToRight()) {
182             g.setColor(shadow);
183             g.drawLine(4, 3, 4, height - 4);
184             g.drawLine(4, height - 4, 2, height - 4);
185
186             g.setColor(highlight);
187             g.drawLine(2, 3, 3, 3);
188             g.drawLine(2, 3, 2, height - 5);
189             } else {
190             g.setColor(shadow);
191             g.drawLine(width - 3, 3, width - 3, height - 4);
192             g.drawLine(width - 4, height - 4, width - 4, height - 4);
193
194             g.setColor(highlight);
195             g.drawLine(width - 5, 3, width - 4, 3);
196             g.drawLine(width - 5, 3, width - 5, height - 5);
197             }
198         } else { // Vertical
199
g.setColor(shadow);
200             g.drawLine(3, 4, width - 4, 4);
201             g.drawLine(width - 4, 2, width - 4, 4);
202
203             g.setColor(highlight);
204             g.drawLine(3, 2, width - 4, 2);
205             g.drawLine(3, 2, 3, 3);
206         }
207         }
208         }
209
210         g.translate(-x, -y);
211         }
212
213         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
214             return getBorderInsets(c, new Insets JavaDoc(1,1,1,1));
215         }
216
217         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
218         insets.top = insets.left = insets.bottom = insets.right = 1;
219         if (((JToolBar)c).isFloatable()) {
220         int gripInset = (XPStyle.getXP() != null) ? 12 : 9;
221         if (((JToolBar)c).getOrientation() == HORIZONTAL) {
222             if (c.getComponentOrientation().isLeftToRight()) {
223             insets.left = gripInset;
224             } else {
225             insets.right = gripInset;
226             }
227         } else {
228             insets.top = gripInset;
229         }
230         }
231         return insets;
232     }
233     }
234
235     /**
236      * This class is an implementation of a dashed border.
237      * @since 1.4
238      */

239     public static class DashedBorder extends LineBorder implements UIResource {
240         public DashedBorder(Color JavaDoc color) {
241             super(color);
242         }
243
244         public DashedBorder(Color JavaDoc color, int thickness) {
245             super(color, thickness);
246         }
247
248     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
249         Color JavaDoc oldColor = g.getColor();
250         int i;
251
252         g.setColor(lineColor);
253         for(i = 0; i < thickness; i++) {
254         BasicGraphicsUtils.drawDashedRect(g, x+i, y+i, width-i-i, height-i-i);
255         }
256         g.setColor(oldColor);
257     }
258     }
259
260     /**
261      * A dashed border that paints itself in the complementary color
262      * of the component's background color.
263      */

264     static class ComplementDashedBorder extends LineBorder implements UIResource {
265         private Color JavaDoc origColor;
266         private Color JavaDoc paintColor;
267
268         public ComplementDashedBorder() {
269             super(null);
270         }
271
272         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
273             Color JavaDoc color = c.getBackground();
274
275             if (origColor != color) {
276                 origColor = color;
277                 paintColor = new Color JavaDoc(~origColor.getRGB());
278             }
279
280             g.setColor(paintColor);
281             BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
282         }
283     }
284
285     /**
286      * This class is an implementation of the InternalFrameLine border.
287      * @since 1.4
288      */

289     public static class InternalFrameLineBorder extends LineBorder implements
290             UIResource {
291         protected Color JavaDoc activeColor;
292         protected Color JavaDoc inactiveColor;
293
294         public InternalFrameLineBorder(Color JavaDoc activeBorderColor,
295                                        Color JavaDoc inactiveBorderColor,
296                                        int thickness) {
297             super(activeBorderColor, thickness);
298             activeColor = activeBorderColor;
299             inactiveColor = inactiveBorderColor;
300         }
301
302         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
303                 int width, int height) {
304
305             JInternalFrame jif = null;
306             if (c instanceof JInternalFrame) {
307                 jif = (JInternalFrame)c;
308             } else if (c instanceof JInternalFrame.JDesktopIcon) {
309                 jif = ((JInternalFrame.JDesktopIcon)c).getInternalFrame();
310             } else {
311                 return;
312             }
313
314             if (jif.isSelected()) {
315                 // Set the line color so the line border gets the correct
316
// color.
317
lineColor = activeColor;
318                 super.paintBorder(c, g, x, y, width, height);
319             } else {
320                 lineColor = inactiveColor;
321                 super.paintBorder(c, g, x, y, width, height);
322             }
323         }
324     }
325 }
326
Popular Tags