KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicBorders


1 /*
2  * @(#)BasicBorders.java 1.33 03/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 javax.swing.plaf.basic;
9
10 import javax.swing.*;
11 import javax.swing.border.*;
12 import javax.swing.plaf.*;
13 import javax.swing.text.JTextComponent JavaDoc;
14
15 import java.awt.Component JavaDoc;
16 import java.awt.Insets JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Rectangle JavaDoc;
19 import java.awt.Color JavaDoc;
20 import java.awt.Graphics JavaDoc;
21 import java.io.Serializable JavaDoc;
22
23
24 /**
25  * Factory object that can vend Borders appropriate for the basic L & F.
26  * @version 1.33 12/19/03
27  * @author Georges Saab
28  * @author Amy Fowler
29  */

30
31 public class BasicBorders {
32
33     public static Border getButtonBorder() {
34     UIDefaults table = UIManager.getLookAndFeelDefaults();
35     Border buttonBorder = new BorderUIResource.CompoundBorderUIResource(
36                new BasicBorders.ButtonBorder JavaDoc(
37                        table.getColor("Button.shadow"),
38                                            table.getColor("Button.darkShadow"),
39                                            table.getColor("Button.light"),
40                                            table.getColor("Button.highlight")),
41                          new MarginBorder());
42     return buttonBorder;
43     }
44
45     public static Border getRadioButtonBorder() {
46     UIDefaults table = UIManager.getLookAndFeelDefaults();
47     Border radioButtonBorder = new BorderUIResource.CompoundBorderUIResource(
48                new BasicBorders.RadioButtonBorder JavaDoc(
49                        table.getColor("RadioButton.shadow"),
50                                            table.getColor("RadioButton.darkShadow"),
51                                            table.getColor("RadioButton.light"),
52                                            table.getColor("RadioButton.highlight")),
53                          new MarginBorder());
54     return radioButtonBorder;
55     }
56
57     public static Border getToggleButtonBorder() {
58     UIDefaults table = UIManager.getLookAndFeelDefaults();
59     Border toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource(
60                          new BasicBorders.ToggleButtonBorder JavaDoc(
61                        table.getColor("ToggleButton.shadow"),
62                                            table.getColor("ToggleButton.darkShadow"),
63                                            table.getColor("ToggleButton.light"),
64                                            table.getColor("ToggleButton.highlight")),
65                      new MarginBorder());
66     return toggleButtonBorder;
67     }
68
69     public static Border getMenuBarBorder() {
70     UIDefaults table = UIManager.getLookAndFeelDefaults();
71     Border menuBarBorder = new BasicBorders.MenuBarBorder JavaDoc(
72                         table.getColor("MenuBar.shadow"),
73                                         table.getColor("MenuBar.highlight")
74                                    );
75     return menuBarBorder;
76     }
77
78     public static Border getSplitPaneBorder() {
79     UIDefaults table = UIManager.getLookAndFeelDefaults();
80     Border splitPaneBorder = new BasicBorders.SplitPaneBorder JavaDoc(
81                      table.getColor("SplitPane.highlight"),
82                      table.getColor("SplitPane.darkShadow"));
83     return splitPaneBorder;
84     }
85
86     /**
87      * Returns a border instance for a JSplitPane divider
88      * @since 1.3
89      */

90     public static Border getSplitPaneDividerBorder() {
91     UIDefaults table = UIManager.getLookAndFeelDefaults();
92     Border splitPaneBorder = new BasicBorders.SplitPaneDividerBorder JavaDoc(
93                      table.getColor("SplitPane.highlight"),
94                      table.getColor("SplitPane.darkShadow"));
95     return splitPaneBorder;
96     }
97
98     public static Border getTextFieldBorder() {
99     UIDefaults table = UIManager.getLookAndFeelDefaults();
100     Border textFieldBorder = new BasicBorders.FieldBorder JavaDoc(
101                                            table.getColor("TextField.shadow"),
102                                            table.getColor("TextField.darkShadow"),
103                                            table.getColor("TextField.light"),
104                                            table.getColor("TextField.highlight"));
105     return textFieldBorder;
106     }
107
108     public static Border getProgressBarBorder() {
109     UIDefaults table = UIManager.getLookAndFeelDefaults();
110     Border progressBarBorder = new BorderUIResource.LineBorderUIResource(Color.green, 2);
111     return progressBarBorder;
112     }
113
114     public static Border getInternalFrameBorder() {
115     UIDefaults table = UIManager.getLookAndFeelDefaults();
116     Border internalFrameBorder = new BorderUIResource.CompoundBorderUIResource(
117                 new BevelBorder(BevelBorder.RAISED,
118                     table.getColor("InternalFrame.borderLight"),
119                                         table.getColor("InternalFrame.borderHighlight"),
120                                         table.getColor("InternalFrame.borderDarkShadow"),
121                                         table.getColor("InternalFrame.borderShadow")),
122                 BorderFactory.createLineBorder(
123                     table.getColor("InternalFrame.borderColor"), 1));
124
125     return internalFrameBorder;
126     }
127  
128     /**
129      * Special thin border for rollover toolbar buttons.
130      */

131     public static class RolloverButtonBorder extends ButtonBorder {
132
133         public RolloverButtonBorder(Color JavaDoc shadow, Color JavaDoc darkShadow,
134                                   Color JavaDoc highlight, Color JavaDoc lightHighlight) {
135             super(shadow, darkShadow, highlight, lightHighlight);
136         }
137
138         public void paintBorder( Component JavaDoc c, Graphics JavaDoc g, int x, int y, int w, int h ) {
139             AbstractButton b = (AbstractButton) c;
140             ButtonModel model = b.getModel();
141
142         Color JavaDoc shade = shadow;
143         Component JavaDoc p = b.getParent();
144         if (p != null && p.getBackground().equals(shadow)) {
145         shade = darkShadow;
146         }
147
148             if ((model.isRollover() && !(model.isPressed() && !model.isArmed())) ||
149                 model.isSelected()) {
150
151         Color JavaDoc oldColor = g.getColor();
152         g.translate(x, y);
153
154         if (model.isPressed() && model.isArmed() || model.isSelected()) {
155             // Draw the pressd button
156
g.setColor(shade);
157             g.drawRect(0, 0, w-1, h-1);
158             g.setColor(lightHighlight);
159             g.drawLine(w-1, 0, w-1, h-1);
160             g.drawLine(0, h-1, w-1, h-1);
161         } else {
162             // Draw a rollover button
163
g.setColor(lightHighlight);
164             g.drawRect(0, 0, w-1, h-1);
165             g.setColor(shade);
166             g.drawLine(w-1, 0, w-1, h-1);
167             g.drawLine(0, h-1, w-1, h-1);
168         }
169         g.translate(-x, -y);
170         g.setColor(oldColor);
171             }
172         }
173     }
174
175
176     /**
177      * A border which is like a Margin border but it will only honor the margin
178      * if the margin has been explicitly set by the developer.
179      *
180      * Note: This is identical to the package private class
181      * MetalBorders.RolloverMarginBorder and should probably be consolidated.
182      */

183     static class RolloverMarginBorder extends EmptyBorder {
184     
185     public RolloverMarginBorder() {
186         super(3,3,3,3); // hardcoded margin for JLF requirements.
187
}
188
189     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
190         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
191     }
192
193     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
194         Insets JavaDoc margin = null;
195
196         if (c instanceof AbstractButton) {
197         margin = ((AbstractButton)c).getMargin();
198         }
199         if (margin == null || margin instanceof UIResource) {
200         // default margin so replace
201
insets.left = left;
202         insets.top = top;
203         insets.right = right;
204         insets.bottom = bottom;
205         } else {
206         // Margin which has been explicitly set by the user.
207
insets.left = margin.left;
208         insets.top = margin.top;
209         insets.right = margin.right;
210         insets.bottom = margin.bottom;
211         }
212         return insets;
213     }
214     }
215
216    public static class ButtonBorder extends AbstractBorder implements UIResource {
217         protected Color JavaDoc shadow;
218         protected Color JavaDoc darkShadow;
219         protected Color JavaDoc highlight;
220         protected Color JavaDoc lightHighlight;
221
222         public ButtonBorder(Color JavaDoc shadow, Color JavaDoc darkShadow,
223                             Color JavaDoc highlight, Color JavaDoc lightHighlight) {
224             this.shadow = shadow;
225             this.darkShadow = darkShadow;
226             this.highlight = highlight;
227             this.lightHighlight = lightHighlight;
228         }
229
230         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
231                             int width, int height) {
232             boolean isPressed = false;
233             boolean isDefault = false;
234       
235             if (c instanceof AbstractButton) {
236             AbstractButton b = (AbstractButton)c;
237             ButtonModel model = b.getModel();
238     
239             isPressed = model.isPressed() && model.isArmed();
240
241                 if (c instanceof JButton) {
242                     isDefault = ((JButton)c).isDefaultButton();
243                 }
244             }
245             BasicGraphicsUtils.drawBezel(g, x, y, width, height,
246                    isPressed, isDefault, shadow,
247                                    darkShadow, highlight, lightHighlight);
248         }
249
250         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
251             return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
252         }
253
254         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
255             // leave room for default visual
256
insets.top = 2;
257             insets.left = insets.bottom = insets.right = 3;
258         return insets;
259         }
260
261     }
262
263     public static class ToggleButtonBorder extends ButtonBorder {
264
265         public ToggleButtonBorder(Color JavaDoc shadow, Color JavaDoc darkShadow,
266                                   Color JavaDoc highlight, Color JavaDoc lightHighlight) {
267             super(shadow, darkShadow, highlight, lightHighlight);
268         }
269
270         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
271                                 int width, int height) {
272                 BasicGraphicsUtils.drawBezel(g, x, y, width, height,
273                                              false, false,
274                                              shadow, darkShadow,
275                                              highlight, lightHighlight);
276     }
277
278         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
279             return new Insets JavaDoc(2, 2, 2, 2);
280         }
281
282         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
283             insets.top = insets.left = insets.bottom = insets.right = 2;
284         return insets;
285         }
286     }
287
288     public static class RadioButtonBorder extends ButtonBorder {
289
290         public RadioButtonBorder(Color JavaDoc shadow, Color JavaDoc darkShadow,
291                                  Color JavaDoc highlight, Color JavaDoc lightHighlight) {
292             super(shadow, darkShadow, highlight, lightHighlight);
293         }
294
295       
296         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
297       
298         if (c instanceof AbstractButton) {
299             AbstractButton b = (AbstractButton)c;
300             ButtonModel model = b.getModel();
301           
302             if (model.isArmed() && model.isPressed() || model.isSelected()) {
303             BasicGraphicsUtils.drawLoweredBezel(g, x, y, width, height,
304                                                         shadow, darkShadow,
305                                                         highlight, lightHighlight);
306             } else {
307             BasicGraphicsUtils.drawBezel(g, x, y, width, height,
308                            false, b.isFocusPainted() && b.hasFocus(),
309                                                  shadow, darkShadow,
310                                                  highlight, lightHighlight);
311             }
312         } else {
313             BasicGraphicsUtils.drawBezel(g, x, y, width, height, false, false,
314                                              shadow, darkShadow, highlight, lightHighlight);
315         }
316         }
317       
318         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
319         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
320         }
321
322         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
323             insets.top = insets.left = insets.bottom = insets.right = 2;
324         return insets;
325         }
326     }
327
328     public static class MenuBarBorder extends AbstractBorder implements UIResource {
329         private Color JavaDoc shadow;
330         private Color JavaDoc highlight;
331
332         public MenuBarBorder(Color JavaDoc shadow, Color JavaDoc highlight) {
333             this.shadow = shadow;
334             this.highlight = highlight;
335         }
336
337     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
338         Color JavaDoc oldColor = g.getColor();
339         g.translate(x, y);
340         g.setColor(shadow);
341         g.drawLine(0, height-2, width, height-2);
342         g.setColor(highlight);
343         g.drawLine(0, height-1, width, height-1);
344         g.translate(-x,-y);
345         g.setColor(oldColor);
346     }
347     
348     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
349         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
350     }
351
352         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
353             insets.top = 0;
354         insets.left = 0;
355         insets.bottom = 2;
356         insets.right = 0;
357         return insets;
358         }
359     }
360
361     public static class MarginBorder extends AbstractBorder implements UIResource {
362
363         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
364         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
365         }
366
367         public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
368             Insets JavaDoc margin = null;
369             //
370
// Ideally we'd have an interface defined for classes which
371
// support margins (to avoid this hackery), but we've
372
// decided against it for simplicity
373
//
374
if (c instanceof AbstractButton) {
375                AbstractButton b = (AbstractButton)c;
376                margin = b.getMargin();
377            } else if (c instanceof JToolBar) {
378                JToolBar t = (JToolBar)c;
379                margin = t.getMargin();
380            } else if (c instanceof JTextComponent JavaDoc) {
381                JTextComponent JavaDoc t = (JTextComponent JavaDoc)c;
382                margin = t.getMargin();
383            }
384        insets.top = margin != null? margin.top : 0;
385        insets.left = margin != null? margin.left : 0;
386        insets.bottom = margin != null? margin.bottom : 0;
387        insets.right = margin != null? margin.right : 0;
388            
389        return insets;
390         }
391     }
392
393     public static class FieldBorder extends AbstractBorder implements UIResource {
394         protected Color JavaDoc shadow;
395         protected Color JavaDoc darkShadow;
396         protected Color JavaDoc highlight;
397         protected Color JavaDoc lightHighlight;
398
399         public FieldBorder(Color JavaDoc shadow, Color JavaDoc darkShadow,
400                            Color JavaDoc highlight, Color JavaDoc lightHighlight) {
401             this.shadow = shadow;
402             this.highlight = highlight;
403             this.darkShadow = darkShadow;
404             this.lightHighlight = lightHighlight;
405         }
406
407         public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
408                             int width, int height) {
409             BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height,
410                                               shadow, darkShadow,
411                                               highlight, lightHighlight);
412         }
413
414         public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
415         return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
416     }
417
418     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
419             Insets JavaDoc margin = null;
420             if (c instanceof JTextComponent JavaDoc) {
421                 margin = ((JTextComponent JavaDoc)c).getMargin();
422             }
423         insets.top = margin != null? 2+margin.top : 2;
424         insets.left = margin != null? 2+margin.left : 2;
425         insets.bottom = margin != null? 2+margin.bottom : 2;
426         insets.right = margin != null? 2+margin.right : 2;
427            
428         return insets;
429         }
430     }
431
432
433     /**
434      * Draws the border around the divider in a splitpane
435      * (when BasicSplitPaneUI is used). To get the appropriate effect, this
436      * needs to be used with a SplitPaneBorder.
437      */

438     static class SplitPaneDividerBorder implements Border, UIResource {
439         Color JavaDoc highlight;
440         Color JavaDoc shadow;
441
442         SplitPaneDividerBorder(Color JavaDoc highlight, Color JavaDoc shadow) {
443         this.highlight = highlight;
444         this.shadow = shadow;
445     }
446
447     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
448                 int width, int height) {
449         Component JavaDoc child;
450         Rectangle JavaDoc cBounds;
451         JSplitPane splitPane = ((BasicSplitPaneDivider JavaDoc)c).
452                                  getBasicSplitPaneUI().getSplitPane();
453         Dimension JavaDoc size = c.getSize();
454         
455         child = splitPane.getLeftComponent();
456         // This is needed for the space between the divider and end of
457
// splitpane.
458
g.setColor(c.getBackground());
459         g.drawRect(x, y, width - 1, height - 1);
460         if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
461         if(child != null) {
462             g.setColor(highlight);
463             g.drawLine(0, 0, 0, size.height);
464         }
465         child = splitPane.getRightComponent();
466         if(child != null) {
467             g.setColor(shadow);
468             g.drawLine(size.width - 1, 0, size.width - 1, size.height);
469         }
470         } else {
471         if(child != null) {
472             g.setColor(highlight);
473             g.drawLine(0, 0, size.width, 0);
474         }
475         child = splitPane.getRightComponent();
476         if(child != null) {
477             g.setColor(shadow);
478             g.drawLine(0, size.height - 1, size.width,
479                    size.height - 1);
480         }
481         }
482     }
483     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
484         Insets JavaDoc insets = new Insets JavaDoc(0,0,0,0);
485         if (c instanceof BasicSplitPaneDivider JavaDoc) {
486         BasicSplitPaneUI JavaDoc bspui = ((BasicSplitPaneDivider JavaDoc)c).
487                                  getBasicSplitPaneUI();
488
489         if (bspui != null) {
490             JSplitPane splitPane = bspui.getSplitPane();
491
492             if (splitPane != null) {
493             if (splitPane.getOrientation() ==
494                 JSplitPane.HORIZONTAL_SPLIT) {
495                 insets.top = insets.bottom = 0;
496                 insets.left = insets.right = 1;
497                 return insets;
498             }
499             // VERTICAL_SPLIT
500
insets.top = insets.bottom = 1;
501             insets.left = insets.right = 0;
502             return insets;
503             }
504         }
505         }
506         insets.top = insets.bottom = insets.left = insets.right = 1;
507         return insets;
508     }
509     public boolean isBorderOpaque() { return true; }
510     }
511
512
513     /**
514      * Draws the border around the splitpane. To work correctly you shoudl
515      * also install a border on the divider (property SplitPaneDivider.border).
516      */

517     public static class SplitPaneBorder implements Border, UIResource {
518         protected Color JavaDoc highlight;
519         protected Color JavaDoc shadow;
520
521         public SplitPaneBorder(Color JavaDoc highlight, Color JavaDoc shadow) {
522         this.highlight = highlight;
523         this.shadow = shadow;
524     }
525
526     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
527                 int width, int height) {
528         // The only tricky part with this border is that the divider is
529
// not positioned at the top (for horizontal) or left (for vert),
530
// so this border draws to where the divider is:
531
// -----------------
532
// |xxxxxxx xxxxxxx|
533
// |x --- x|
534
// |x | | x|
535
// |x |D| x|
536
// |x | | x|
537
// |x --- x|
538
// |xxxxxxx xxxxxxx|
539
// -----------------
540
// The above shows (rather excessively) what this looks like for
541
// a horizontal orientation. This border then draws the x's, with
542
// the SplitPaneDividerBorder drawing its own border.
543

544         Component JavaDoc child;
545         Rectangle JavaDoc cBounds;
546
547         JSplitPane splitPane = (JSplitPane)c;
548         
549         child = splitPane.getLeftComponent();
550         // This is needed for the space between the divider and end of
551
// splitpane.
552
g.setColor(c.getBackground());
553         g.drawRect(x, y, width - 1, height - 1);
554         if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
555         if(child != null) {
556             cBounds = child.getBounds();
557             g.setColor(shadow);
558             g.drawLine(0, 0, cBounds.width + 1, 0);
559             g.drawLine(0, 1, 0, cBounds.height + 2);
560
561             g.setColor(highlight);
562             g.drawLine(1, cBounds.height + 1, cBounds.width + 1,
563                    cBounds.height + 1);
564         }
565         child = splitPane.getRightComponent();
566         if(child != null) {
567             cBounds = child.getBounds();
568
569             int maxX = cBounds.x + cBounds.width;
570             int maxY = cBounds.y + cBounds.height;
571             
572             g.setColor(shadow);
573             g.drawLine(cBounds.x - 1, 0, maxX, 0);
574             g.drawLine(cBounds.x - 1, maxY, cBounds.x, maxY);
575             g.setColor(highlight);
576             g.drawLine(cBounds.x, maxY, maxX, maxY);
577             g.drawLine(maxX, 0, maxX, maxY + 1);
578         }
579         } else {
580         if(child != null) {
581             cBounds = child.getBounds();
582             g.setColor(shadow);
583             g.drawLine(0, 0, cBounds.width + 1, 0);
584             g.drawLine(0, 1, 0, cBounds.height);
585             g.setColor(highlight);
586             g.drawLine(1 + cBounds.width, 0, 1 + cBounds.width,
587                    cBounds.height + 1);
588             g.drawLine(0, cBounds.height + 1, 0, cBounds.height + 1);
589         }
590         child = splitPane.getRightComponent();
591         if(child != null) {
592             cBounds = child.getBounds();
593
594             int maxX = cBounds.x + cBounds.width;
595             int maxY = cBounds.y + cBounds.height;
596             
597             g.setColor(shadow);
598             g.drawLine(0, cBounds.y - 1, 0, maxY);
599             g.drawLine(maxX, cBounds.y - 1, maxX, cBounds.y - 1);
600             g.setColor(highlight);
601             g.drawLine(0, maxY, cBounds.width + 1, maxY);
602             g.drawLine(maxX, cBounds.y, maxX, maxY);
603         }
604         }
605     }
606     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
607         return new Insets JavaDoc(1, 1, 1, 1);
608     }
609     public boolean isBorderOpaque() { return true; }
610     }
611     
612 }
613
Popular Tags