1 /*
2 * Copyright 1997-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 package javax.swing;
26
27 import java.awt.Color;
28 import java.awt.Font;
29 import javax.swing.JComponent;
30 import javax.swing.border;
31
32 /**
33 * Factory class for vending standard <code>Border</code> objects. Wherever
34 * possible, this factory will hand out references to shared
35 * <code>Border</code> instances.
36 * For further information and examples see
37 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html">How
38 to Use Borders</a>,
39 * a section in <em>The Java Tutorial</em>.
40 *
41 * @author David Kloba
42 */
43 public class BorderFactory
44 {
45
46 /** Don't let anyone instantiate this class */
47 private BorderFactory() {
48 }
49
50
51 //// LineBorder ///////////////////////////////////////////////////////////////
52 /**
53 * Creates a line border withe the specified color.
54 *
55 * @param color a <code>Color</code> to use for the line
56 * @return the <code>Border</code> object
57 */
58 public static Border createLineBorder(Color color) {
59 return new LineBorder(color, 1);
60 }
61
62 /**
63 * Creates a line border with the specified color
64 * and width. The width applies to all four sides of the
65 * border. To specify widths individually for the top,
66 * bottom, left, and right, use
67 * {@link #createMatteBorder(int,int,int,int,Color)}.
68 *
69 * @param color a <code>Color</code> to use for the line
70 * @param thickness an integer specifying the width in pixels
71 * @return the <code>Border</code> object
72 */
73 public static Border createLineBorder(Color color, int thickness) {
74 return new LineBorder(color, thickness);
75 }
76
77 // public static Border createLineBorder(Color color, int thickness,
78 // boolean drawRounded) {
79 // return new JLineBorder(color, thickness, drawRounded);
80 // }
81
82 //// BevelBorder /////////////////////////////////////////////////////////////
83 ///////////////////////////////////////////////////////////////////////////////
84 static final Border sharedRaisedBevel = new BevelBorder(BevelBorder.RAISED);
85 static final Border sharedLoweredBevel = new BevelBorder(BevelBorder.LOWERED);
86
87 /**
88 * Creates a border with a raised beveled edge, using
89 * brighter shades of the component's current background color
90 * for highlighting, and darker shading for shadows.
91 * (In a raised border, highlights are on top and shadows
92 * are underneath.)
93 *
94 * @return the <code>Border</code> object
95 */
96 public static Border createRaisedBevelBorder() {
97 return createSharedBevel(BevelBorder.RAISED);
98 }
99
100 /**
101 * Creates a border with a lowered beveled edge, using
102 * brighter shades of the component's current background color
103 * for highlighting, and darker shading for shadows.
104 * (In a lowered border, shadows are on top and highlights
105 * are underneath.)
106 *
107 * @return the <code>Border</code> object
108 */
109 public static Border createLoweredBevelBorder() {
110 return createSharedBevel(BevelBorder.LOWERED);
111 }
112
113 /**
114 * Creates a beveled border of the specified type, using
115 * brighter shades of the component's current background color
116 * for highlighting, and darker shading for shadows.
117 * (In a lowered border, shadows are on top and highlights
118 * are underneath.)
119 *
120 * @param type an integer specifying either
121 * <code>BevelBorder.LOWERED</code> or
122 * <code>BevelBorder.RAISED</code>
123 * @return the <code>Border</code> object
124 */
125 public static Border createBevelBorder(int type) {
126 return createSharedBevel(type);
127 }
128
129 /**
130 * Creates a beveled border of the specified type, using
131 * the specified highlighting and shadowing. The outer
132 * edge of the highlighted area uses a brighter shade of
133 * the highlight color. The inner edge of the shadow area
134 * uses a brighter shade of the shadow color.
135 *
136 * @param type an integer specifying either
137 * <code>BevelBorder.LOWERED</code> or
138 * <code>BevelBorder.RAISED</code>
139 * @param highlight a <code>Color</code> object for highlights
140 * @param shadow a <code>Color</code> object for shadows
141 * @return the <code>Border</code> object
142 */
143 public static Border createBevelBorder(int type, Color highlight, Color shadow) {
144 return new BevelBorder(type, highlight, shadow);
145 }
146
147 /**
148 * Creates a beveled border of the specified type, using
149 * the specified colors for the inner and outer highlight
150 * and shadow areas.
151 * <p>
152 * Note: The shadow inner and outer colors are
153 * switched for a lowered bevel border.
154 *
155 * @param type an integer specifying either
156 * <code>BevelBorder.LOWERED</code> or
157 * <code>BevelBorder.RAISED</code>
158 * @param highlightOuter a <code>Color</code> object for the
159 * outer edge of the highlight area
160 * @param highlightInner a <code>Color</code> object for the
161 * inner edge of the highlight area
162 * @param shadowOuter a <code>Color</code> object for the
163 * outer edge of the shadow area
164 * @param shadowInner a <code>Color</code> object for the
165 * inner edge of the shadow area
166 * @return the <code>Border</code> object
167 */
168 public static Border createBevelBorder(int type,
169 Color highlightOuter, Color highlightInner,
170 Color shadowOuter, Color shadowInner) {
171 return new BevelBorder(type, highlightOuter, highlightInner,
172 shadowOuter, shadowInner);
173 }
174
175 static Border createSharedBevel(int type) {
176 if(type == BevelBorder.RAISED) {
177 return sharedRaisedBevel;
178 } else if(type == BevelBorder.LOWERED) {
179 return sharedLoweredBevel;
180 }
181 return null;
182 }
183 //// EtchedBorder ///////////////////////////////////////////////////////////
184 static final Border sharedEtchedBorder = new EtchedBorder();
185 private static Border sharedRaisedEtchedBorder;
186
187 /**
188 * Creates a border with an "etched" look using
189 * the component's current background color for
190 * highlighting and shading.
191 *
192 * @return the <code>Border</code> object
193 */
194 public static Border createEtchedBorder() {
195 return sharedEtchedBorder;
196 }
197
198 /**
199 * Creates a border with an "etched" look using
200 * the specified highlighting and shading colors.
201 *
202 * @param highlight a <code>Color</code> object for the border highlights
203 * @param shadow a <code>Color</code> object for the border shadows
204 * @return the <code>Border</code> object
205 */
206 public static Border createEtchedBorder(Color highlight, Color shadow) {
207 return new EtchedBorder(highlight, shadow);
208 }
209
210 /**
211 * Creates a border with an "etched" look using
212 * the component's current background color for
213 * highlighting and shading.
214 *
215 * @param type one of <code>EtchedBorder.RAISED</code>, or
216 * <code>EtchedBorder.LOWERED</code>
217 * @return the <code>Border</code> object
218 * @exception IllegalArgumentException if type is not either
219 * <code>EtchedBorder.RAISED</code> or
220 * <code>EtchedBorder.LOWERED</code>
221 * @since 1.3
222 */
223 public static Border createEtchedBorder(int type) {
224 switch (type) {
225 case EtchedBorder.RAISED:
226 if (sharedRaisedEtchedBorder == null) {
227 sharedRaisedEtchedBorder = new EtchedBorder
228 (EtchedBorder.RAISED);
229 }
230 return sharedRaisedEtchedBorder;
231 case EtchedBorder.LOWERED:
232 return sharedEtchedBorder;
233 default:
234 throw new IllegalArgumentException("type must be one of EtchedBorder.RAISED or EtchedBorder.LOWERED");
235 }
236 }
237
238 /**
239 * Creates a border with an "etched" look using
240 * the specified highlighting and shading colors.
241 *
242 * @param type one of <code>EtchedBorder.RAISED</code>, or
243 * <code>EtchedBorder.LOWERED</code>
244 * @param highlight a <code>Color</code> object for the border highlights
245 * @param shadow a <code>Color</code> object for the border shadows
246 * @return the <code>Border</code> object
247 * @since 1.3
248 */
249 public static Border createEtchedBorder(int type, Color highlight,
250 Color shadow) {
251 return new EtchedBorder(type, highlight, shadow);
252 }
253
254 //// TitledBorder ////////////////////////////////////////////////////////////
255 /**
256 * Creates a new titled border with the specified title,
257 * the default border type (determined by the current look and feel),
258 * the default text position (sitting on the top line),
259 * the default justification (leading), and the default
260 * font and text color (determined by the current look and feel).
261 *
262 * @param title a <code>String</code> containing the text of the title
263 * @return the <code>TitledBorder</code> object
264 */
265 public static TitledBorder createTitledBorder(String title) {
266 return new TitledBorder(title);
267 }
268
269 /**
270 * Creates a new titled border with an empty title,
271 * the specified border object,
272 * the default text position (sitting on the top line),
273 * the default justification (leading), and the default
274 * font and text color (determined by the current look and feel).
275 *
276 * @param border the <code>Border</code> object to add the title to; if
277 * <code>null</code> the <code>Border</code> is determined
278 * by the current look and feel.
279 * @return the <code>TitledBorder</code> object
280 */
281 public static TitledBorder createTitledBorder(Border border) {
282 return new TitledBorder(border);
283 }
284
285 /**
286 * Adds a title to an existing border,
287 * with default positioning (sitting on the top line),
288 * default justification (leading) and the default
289 * font and text color (determined by the current look and feel).
290 *
291 * @param border the <code>Border</code> object to add the title to
292 * @param title a <code>String</code> containing the text of the title
293 * @return the <code>TitledBorder</code> object
294 */
295 public static TitledBorder createTitledBorder(Border border,
296 String title) {
297 return new TitledBorder(border, title);
298 }
299
300 /**
301 * Adds a title to an existing border, with the specified
302 * positioning and using the default
303 * font and text color (determined by the current look and feel).
304 *
305 * @param border the <code>Border</code> object to add the title to
306 * @param title a <code>String</code> containing the text of the title
307 * @param titleJustification an integer specifying the justification
308 * of the title -- one of the following:
309 *<ul>
310 *<li><code>TitledBorder.LEFT</code>
311 *<li><code>TitledBorder.CENTER</code>
312 *<li><code>TitledBorder.RIGHT</code>
313 *<li><code>TitledBorder.LEADING</code>
314 *<li><code>TitledBorder.TRAILING</code>
315 *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
316 *</ul>
317 * @param titlePosition an integer specifying the vertical position of
318 * the text in relation to the border -- one of the following:
319 *<ul>
320 *<li><code> TitledBorder.ABOVE_TOP</code>
321 *<li><code>TitledBorder.TOP</code> (sitting on the top line)
322 *<li><code>TitledBorder.BELOW_TOP</code>
323 *<li><code>TitledBorder.ABOVE_BOTTOM</code>
324 *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
325 *<li><code>TitledBorder.BELOW_BOTTOM</code>
326 *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
327 *</ul>
328 * @return the <code>TitledBorder</code> object
329 */
330 public static TitledBorder createTitledBorder(Border border,
331 String title,
332 int titleJustification,
333 int titlePosition) {
334 return new TitledBorder(border, title, titleJustification,
335 titlePosition);
336 }
337
338 /**
339 * Adds a title to an existing border, with the specified
340 * positioning and font, and using the default text color
341 * (determined by the current look and feel).
342 *
343 * @param border the <code>Border</code> object to add the title to
344 * @param title a <code>String</code> containing the text of the title
345 * @param titleJustification an integer specifying the justification
346 * of the title -- one of the following:
347 *<ul>
348 *<li><code>TitledBorder.LEFT</code>
349 *<li><code>TitledBorder.CENTER</code>
350 *<li><code>TitledBorder.RIGHT</code>
351 *<li><code>TitledBorder.LEADING</code>
352 *<li><code>TitledBorder.TRAILING</code>
353 *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
354 *</ul>
355 * @param titlePosition an integer specifying the vertical position of
356 * the text in relation to the border -- one of the following:
357 *<ul>
358 *<li><code> TitledBorder.ABOVE_TOP</code>
359 *<li><code>TitledBorder.TOP</code> (sitting on the top line)
360 *<li><code>TitledBorder.BELOW_TOP</code>
361 *<li><code>TitledBorder.ABOVE_BOTTOM</code>
362 *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
363 *<li><code>TitledBorder.BELOW_BOTTOM</code>
364 *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
365 *</ul>
366 * @param titleFont a Font object specifying the title font
367 * @return the TitledBorder object
368 */
369 public static TitledBorder createTitledBorder(Border border,
370 String title,
371 int titleJustification,
372 int titlePosition,
373 Font titleFont) {
374 return new TitledBorder(border, title, titleJustification,
375 titlePosition, titleFont);
376 }
377
378 /**
379 * Adds a title to an existing border, with the specified
380 * positioning, font and color.
381 *
382 * @param border the <code>Border</code> object to add the title to
383 * @param title a <code>String</code> containing the text of the title
384 * @param titleJustification an integer specifying the justification
385 * of the title -- one of the following:
386 *<ul>
387 *<li><code>TitledBorder.LEFT</code>
388 *<li><code>TitledBorder.CENTER</code>
389 *<li><code>TitledBorder.RIGHT</code>
390 *<li><code>TitledBorder.LEADING</code>
391 *<li><code>TitledBorder.TRAILING</code>
392 *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
393 *</ul>
394 * @param titlePosition an integer specifying the vertical position of
395 * the text in relation to the border -- one of the following:
396 *<ul>
397 *<li><code> TitledBorder.ABOVE_TOP</code>
398 *<li><code>TitledBorder.TOP</code> (sitting on the top line)
399 *<li><code>TitledBorder.BELOW_TOP</code>
400 *<li><code>TitledBorder.ABOVE_BOTTOM</code>
401 *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
402 *<li><code>TitledBorder.BELOW_BOTTOM</code>
403 *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
404 *</ul>
405 * @param titleFont a <code>Font</code> object specifying the title font
406 * @param titleColor a <code>Color</code> object specifying the title color
407 * @return the <code>TitledBorder</code> object
408 */
409 public static TitledBorder createTitledBorder(Border border,
410 String title,
411 int titleJustification,
412 int titlePosition,
413 Font titleFont,
414 Color titleColor) {
415 return new TitledBorder(border, title, titleJustification,
416 titlePosition, titleFont, titleColor);
417 }
418 //// EmptyBorder ///////////////////////////////////////////////////////////
419 final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
420
421 /**
422 * Creates an empty border that takes up no space. (The width
423 * of the top, bottom, left, and right sides are all zero.)
424 *
425 * @return the <code>Border</code> object
426 */
427 public static Border createEmptyBorder() {
428 return emptyBorder;
429 }
430
431 /**
432 * Creates an empty border that takes up space but which does
433 * no drawing, specifying the width of the top, left, bottom, and
434 * right sides.
435 *
436 * @param top an integer specifying the width of the top,
437 * in pixels
438 * @param left an integer specifying the width of the left side,
439 * in pixels
440 * @param bottom an integer specifying the width of the bottom,
441 * in pixels
442 * @param right an integer specifying the width of the right side,
443 * in pixels
444 * @return the <code>Border</code> object
445 */
446 public static Border createEmptyBorder(int top, int left,
447 int bottom, int right) {
448 return new EmptyBorder(top, left, bottom, right);
449 }
450
451 //// CompoundBorder ////////////////////////////////////////////////////////
452 /**
453 * Creates a compound border with a <code>null</code> inside edge and a
454 * <code>null</code> outside edge.
455 *
456 * @return the <code>CompoundBorder</code> object
457 */
458 public static CompoundBorder createCompoundBorder() {
459 return new CompoundBorder();
460 }
461
462 /**
463 * Creates a compound border specifying the border objects to use
464 * for the outside and inside edges.
465 *
466 * @param outsideBorder a <code>Border</code> object for the outer
467 * edge of the compound border
468 * @param insideBorder a <code>Border</code> object for the inner
469 * edge of the compound border
470 * @return the <code>CompoundBorder</code> object
471 */
472 public static CompoundBorder createCompoundBorder(Border outsideBorder,
473 Border insideBorder) {
474 return new CompoundBorder(outsideBorder, insideBorder);
475 }
476
477 //// MatteBorder ////////////////////////////////////////////////////////
478 /**
479 * Creates a matte-look border using a solid color. (The difference between
480 * this border and a line border is that you can specify the individual
481 * border dimensions.)
482 *
483 * @param top an integer specifying the width of the top,
484 * in pixels
485 * @param left an integer specifying the width of the left side,
486 * in pixels
487 * @param bottom an integer specifying the width of the right side,
488 * in pixels
489 * @param right an integer specifying the width of the bottom,
490 * in pixels
491 * @param color a <code>Color</code> to use for the border
492 * @return the <code>MatteBorder</code> object
493 */
494 public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
495 Color color) {
496 return new MatteBorder(top, left, bottom, right, color);
497 }
498
499 /**
500 * Creates a matte-look border that consists of multiple tiles of a
501 * specified icon. Multiple copies of the icon are placed side-by-side
502 * to fill up the border area.
503 * <p>
504 * Note:<br>
505 * If the icon doesn't load, the border area is painted gray.
506 *
507 * @param top an integer specifying the width of the top,
508 * in pixels
509 * @param left an integer specifying the width of the left side,
510 * in pixels
511 * @param bottom an integer specifying the width of the right side,
512 * in pixels
513 * @param right an integer specifying the width of the bottom,
514 * in pixels
515 * @param tileIcon the <code>Icon</code> object used for the border tiles
516 * @return the <code>MatteBorder</code> object
517 */
518 public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
519 Icon tileIcon) {
520 return new MatteBorder(top, left, bottom, right, tileIcon);
521 }
522 }