blob: 828342defc2d455c966a7d1a14ba27b687c6142d [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 com.sun.java.swing.plaf.windows;
27
28import javax.swing.*;
29import javax.swing.border.*;
30import javax.swing.plaf.*;
31import javax.swing.plaf.basic.*;
32
33import java.awt.Component;
34import java.awt.Insets;
35import java.awt.Dimension;
36import java.awt.Image;
37import java.awt.Rectangle;
38import java.awt.Color;
39import java.awt.Graphics;
40import java.io.Serializable;
41
42import static com.sun.java.swing.plaf.windows.TMSchema.*;
43import static com.sun.java.swing.plaf.windows.XPStyle.Skin;
44
45/**
46 * Factory object that can vend Borders appropriate for the Windows 95 L & F.
47 * @author Rich Schiavi
48 */
49
50public class WindowsBorders {
51
52 /**
53 * Returns a border instance for a Windows Progress Bar
54 * @since 1.4
55 */
56 public static Border getProgressBarBorder() {
57 UIDefaults table = UIManager.getLookAndFeelDefaults();
58 Border progressBarBorder = new BorderUIResource.CompoundBorderUIResource(
59 new WindowsBorders.ProgressBarBorder(
60 table.getColor("ProgressBar.shadow"),
61 table.getColor("ProgressBar.highlight")),
62 new EmptyBorder(1,1,1,1)
63 );
64 return progressBarBorder;
65 }
66
67 /**
68 * Returns a border instance for a Windows ToolBar
69 *
70 * @return a border used for the toolbar
71 * @since 1.4
72 */
73 public static Border getToolBarBorder() {
74 UIDefaults table = UIManager.getLookAndFeelDefaults();
75 Border toolBarBorder = new WindowsBorders.ToolBarBorder(
76 table.getColor("ToolBar.shadow"),
77 table.getColor("ToolBar.highlight"));
78 return toolBarBorder;
79 }
80
81 /**
82 * Returns an new instance of a border used to indicate which cell item
83 * has focus.
84 *
85 * @return a border to indicate which cell item has focus
86 * @since 1.4
87 */
88 public static Border getFocusCellHighlightBorder() {
89 return new ComplementDashedBorder();
90 }
91
92 public static Border getTableHeaderBorder() {
93 UIDefaults table = UIManager.getLookAndFeelDefaults();
94 Border tableHeaderBorder = new BorderUIResource.CompoundBorderUIResource(
95 new BasicBorders.ButtonBorder(
96 table.getColor("Table.shadow"),
97 table.getColor("Table.darkShadow"),
98 table.getColor("Table.light"),
99 table.getColor("Table.highlight")),
100 new BasicBorders.MarginBorder());
101 return tableHeaderBorder;
102 }
103
104 public static Border getInternalFrameBorder() {
105 UIDefaults table = UIManager.getLookAndFeelDefaults();
106 Border internalFrameBorder = new
107 BorderUIResource.CompoundBorderUIResource(
108 BorderFactory.createBevelBorder(BevelBorder.RAISED,
109 table.getColor("InternalFrame.borderColor"),
110 table.getColor("InternalFrame.borderHighlight"),
111 table.getColor("InternalFrame.borderDarkShadow"),
112 table.getColor("InternalFrame.borderShadow")),
113 new WindowsBorders.InternalFrameLineBorder(
114 table.getColor("InternalFrame.activeBorderColor"),
115 table.getColor("InternalFrame.inactiveBorderColor"),
116 table.getInt("InternalFrame.borderWidth")));
117
118 return internalFrameBorder;
119 }
120
121 public static class ProgressBarBorder extends AbstractBorder implements UIResource {
122 protected Color shadow;
123 protected Color highlight;
124
125 public ProgressBarBorder(Color shadow, Color highlight) {
126 this.highlight = highlight;
127 this.shadow = shadow;
128 }
129
130 public void paintBorder(Component c, Graphics g, int x, int y,
131 int width, int height) {
132 g.setColor(shadow);
133 g.drawLine(x,y, width-1,y); // draw top
134 g.drawLine(x,y, x,height-1); // draw left
135 g.setColor(highlight);
136 g.drawLine(x,height-1, width-1,height-1); // draw bottom
137 g.drawLine(width-1,y, width-1,height-1); // draw right
138 }
139
140 public Insets getBorderInsets(Component c, Insets insets) {
141 insets.set(1,1,1,1);
142 return insets;
143 }
144 }
145
146 /**
147 * A border for the ToolBar. If the ToolBar is floatable then the handle grip is drawn
148 * <p>
149 * @since 1.4
150 */
151 public static class ToolBarBorder extends AbstractBorder implements UIResource, SwingConstants {
152 protected Color shadow;
153 protected Color highlight;
154
155 public ToolBarBorder(Color shadow, Color highlight) {
156 this.highlight = highlight;
157 this.shadow = shadow;
158 }
159
160 public void paintBorder(Component c, Graphics g, int x, int y,
161 int width, int height) {
162 g.translate(x, y);
163
164 XPStyle xp = XPStyle.getXP();
165 if (xp != null) {
166 Border xpBorder = xp.getBorder(c, Part.TP_TOOLBAR);
167 if (xpBorder != null) {
168 xpBorder.paintBorder(c, g, 0, 0, width, height);
169 }
170 }
171 if (((JToolBar)c).isFloatable()) {
172 boolean vertical = ((JToolBar)c).getOrientation() == VERTICAL;
173
174 if (xp != null) {
175 Part part = vertical ? Part.RP_GRIPPERVERT : Part.RP_GRIPPER;
176 Skin skin = xp.getSkin(c, part);
177 int dx, dy, dw, dh;
178 if (vertical) {
179 dx = 0;
180 dy = 2;
181 dw = width - 1;
182 dh = skin.getHeight();
183 } else {
184 dw = skin.getWidth();
185 dh = height - 1;
186 dx = c.getComponentOrientation().isLeftToRight() ? 2 : (width-dw-2);
187 dy = 0;
188 }
189 skin.paintSkin(g, dx, dy, dw, dh, State.NORMAL);
190
191 } else {
192
193 if (!vertical) {
194 if (c.getComponentOrientation().isLeftToRight()) {
195 g.setColor(shadow);
196 g.drawLine(4, 3, 4, height - 4);
197 g.drawLine(4, height - 4, 2, height - 4);
198
199 g.setColor(highlight);
200 g.drawLine(2, 3, 3, 3);
201 g.drawLine(2, 3, 2, height - 5);
202 } else {
203 g.setColor(shadow);
204 g.drawLine(width - 3, 3, width - 3, height - 4);
205 g.drawLine(width - 4, height - 4, width - 4, height - 4);
206
207 g.setColor(highlight);
208 g.drawLine(width - 5, 3, width - 4, 3);
209 g.drawLine(width - 5, 3, width - 5, height - 5);
210 }
211 } else { // Vertical
212 g.setColor(shadow);
213 g.drawLine(3, 4, width - 4, 4);
214 g.drawLine(width - 4, 2, width - 4, 4);
215
216 g.setColor(highlight);
217 g.drawLine(3, 2, width - 4, 2);
218 g.drawLine(3, 2, 3, 3);
219 }
220 }
221 }
222
223 g.translate(-x, -y);
224 }
225
226 public Insets getBorderInsets(Component c, Insets insets) {
227 insets.set(1,1,1,1);
228 if (((JToolBar)c).isFloatable()) {
229 int gripInset = (XPStyle.getXP() != null) ? 12 : 9;
230 if (((JToolBar)c).getOrientation() == HORIZONTAL) {
231 if (c.getComponentOrientation().isLeftToRight()) {
232 insets.left = gripInset;
233 } else {
234 insets.right = gripInset;
235 }
236 } else {
237 insets.top = gripInset;
238 }
239 }
240 return insets;
241 }
242 }
243
244 /**
245 * This class is an implementation of a dashed border.
246 * @since 1.4
247 */
248 public static class DashedBorder extends LineBorder implements UIResource {
249 public DashedBorder(Color color) {
250 super(color);
251 }
252
253 public DashedBorder(Color color, int thickness) {
254 super(color, thickness);
255 }
256
257 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
258 Color oldColor = g.getColor();
259 int i;
260
261 g.setColor(lineColor);
262 for(i = 0; i < thickness; i++) {
263 BasicGraphicsUtils.drawDashedRect(g, x+i, y+i, width-i-i, height-i-i);
264 }
265 g.setColor(oldColor);
266 }
267 }
268
269 /**
270 * A dashed border that paints itself in the complementary color
271 * of the component's background color.
272 */
273 static class ComplementDashedBorder extends LineBorder implements UIResource {
274 private Color origColor;
275 private Color paintColor;
276
277 public ComplementDashedBorder() {
278 super(null);
279 }
280
281 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
282 Color color = c.getBackground();
283
284 if (origColor != color) {
285 origColor = color;
286 paintColor = new Color(~origColor.getRGB());
287 }
288
289 g.setColor(paintColor);
290 BasicGraphicsUtils.drawDashedRect(g, x, y, width, height);
291 }
292 }
293
294 /**
295 * This class is an implementation of the InternalFrameLine border.
296 * @since 1.4
297 */
298 public static class InternalFrameLineBorder extends LineBorder implements
299 UIResource {
300 protected Color activeColor;
301 protected Color inactiveColor;
302
303 public InternalFrameLineBorder(Color activeBorderColor,
304 Color inactiveBorderColor,
305 int thickness) {
306 super(activeBorderColor, thickness);
307 activeColor = activeBorderColor;
308 inactiveColor = inactiveBorderColor;
309 }
310
311 public void paintBorder(Component c, Graphics g, int x, int y,
312 int width, int height) {
313
314 JInternalFrame jif = null;
315 if (c instanceof JInternalFrame) {
316 jif = (JInternalFrame)c;
317 } else if (c instanceof JInternalFrame.JDesktopIcon) {
318 jif = ((JInternalFrame.JDesktopIcon)c).getInternalFrame();
319 } else {
320 return;
321 }
322
323 if (jif.isSelected()) {
324 // Set the line color so the line border gets the correct
325 // color.
326 lineColor = activeColor;
327 super.paintBorder(c, g, x, y, width, height);
328 } else {
329 lineColor = inactiveColor;
330 super.paintBorder(c, g, x, y, width, height);
331 }
332 }
333 }
334}