API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.plaf.basic. BasicBorders View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

/*
 * @(#)BasicBorders.java	1.36 06/04/07
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.swing.plaf.basic;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.text.JTextComponent;

import java.awt.Component;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;


/**
 * Factory object that can vend Borders appropriate for the basic L & F.
 * @version 1.36 04/07/06
 * @author Georges Saab
 * @author Amy Fowler
 */

public class BasicBorders {

    public static Border getButtonBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border buttonBorder = new BorderUIResource.CompoundBorderUIResource(
			   new BasicBorders.ButtonBorder(
					   table.getColor("Button.shadow"),
                                           table.getColor("Button.darkShadow"),
                                           table.getColor("Button.light"),
                                           table.getColor("Button.highlight")),
	       			     new MarginBorder());
	return buttonBorder;
    }

    public static Border getRadioButtonBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border radioButtonBorder = new BorderUIResource.CompoundBorderUIResource(
			   new BasicBorders.RadioButtonBorder(
					   table.getColor("RadioButton.shadow"),
                                           table.getColor("RadioButton.darkShadow"),
                                           table.getColor("RadioButton.light"),
                                           table.getColor("RadioButton.highlight")),
	       			     new MarginBorder());
	return radioButtonBorder;
    }

    public static Border getToggleButtonBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border toggleButtonBorder = new BorderUIResource.CompoundBorderUIResource(
			             new BasicBorders.ToggleButtonBorder(
 					   table.getColor("ToggleButton.shadow"),
                                           table.getColor("ToggleButton.darkShadow"),
                                           table.getColor("ToggleButton.light"),
                                           table.getColor("ToggleButton.highlight")),
				     new MarginBorder());
	return toggleButtonBorder;
    }

    public static Border getMenuBarBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border menuBarBorder = new BasicBorders.MenuBarBorder(
				        table.getColor("MenuBar.shadow"),
                                        table.getColor("MenuBar.highlight")
                                   );
	return menuBarBorder;
    }

    public static Border getSplitPaneBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border splitPaneBorder = new BasicBorders.SplitPaneBorder(
				     table.getColor("SplitPane.highlight"),
				     table.getColor("SplitPane.darkShadow"));
	return splitPaneBorder;
    }

    /**
     * Returns a border instance for a JSplitPane divider
     * @since 1.3
     */
    public static Border getSplitPaneDividerBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border splitPaneBorder = new BasicBorders.SplitPaneDividerBorder(
				     table.getColor("SplitPane.highlight"),
				     table.getColor("SplitPane.darkShadow"));
	return splitPaneBorder;
    }

    public static Border getTextFieldBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border textFieldBorder = new BasicBorders.FieldBorder(
                                           table.getColor("TextField.shadow"),
                                           table.getColor("TextField.darkShadow"),
                                           table.getColor("TextField.light"),
                                           table.getColor("TextField.highlight"));
	return textFieldBorder;
    }

    public static Border getProgressBarBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border progressBarBorder = new BorderUIResource.LineBorderUIResource(Color.green, 2);
	return progressBarBorder;
    }

    public static Border getInternalFrameBorder() {
	UIDefaults table = UIManager.getLookAndFeelDefaults();
	Border internalFrameBorder = new BorderUIResource.CompoundBorderUIResource(
				new BevelBorder(BevelBorder.RAISED,
					table.getColor("InternalFrame.borderLight"),
                                        table.getColor("InternalFrame.borderHighlight"),
                                        table.getColor("InternalFrame.borderDarkShadow"),
                                        table.getColor("InternalFrame.borderShadow")),
				BorderFactory.createLineBorder(
					table.getColor("InternalFrame.borderColor"), 1));

	return internalFrameBorder;
    }
 
    /**
     * Special thin border for rollover toolbar buttons.
     * @since 1.4
     */
    public static class RolloverButtonBorder extends ButtonBorder {

        public RolloverButtonBorder(Color shadow, Color darkShadow, 
                                  Color highlight, Color lightHighlight) {
            super(shadow, darkShadow, highlight, lightHighlight);
        }

        public void paintBorder( Component c, Graphics g, int x, int y, int w, int h ) {
            AbstractButton b = (AbstractButton) c;
            ButtonModel model = b.getModel();

	    Color shade = shadow;
	    Component p = b.getParent();
	    if (p != null && p.getBackground().equals(shadow)) {
		shade = darkShadow;
	    }

            if ((model.isRollover() && !(model.isPressed() && !model.isArmed())) ||
                model.isSelected()) {

		Color oldColor = g.getColor();
		g.translate(x, y);

		if (model.isPressed() && model.isArmed() || model.isSelected()) {
		    // Draw the pressd button
		    g.setColor(shade);
		    g.drawRect(0, 0, w-1, h-1);
		    g.setColor(lightHighlight);
		    g.drawLine(w-1, 0, w-1, h-1);
		    g.drawLine(0, h-1, w-1, h-1);
		} else {
		    // Draw a rollover button
		    g.setColor(lightHighlight);
		    g.drawRect(0, 0, w-1, h-1);
		    g.setColor(shade);
		    g.drawLine(w-1, 0, w-1, h-1);
		    g.drawLine(0, h-1, w-1, h-1);
		}
		g.translate(-x, -y);
		g.setColor(oldColor);
            }
        }
    }


    /**
     * A border which is like a Margin border but it will only honor the margin
     * if the margin has been explicitly set by the developer.
     * 
     * Note: This is identical to the package private class
     * MetalBorders.RolloverMarginBorder and should probably be consolidated.
     */
    static class RolloverMarginBorder extends EmptyBorder {
	
	public RolloverMarginBorder() {
	    super(3,3,3,3); // hardcoded margin for JLF requirements.
	}

	public Insets getBorderInsets(Component c) {
	    return getBorderInsets(c, new Insets(0,0,0,0));
	}

	public Insets getBorderInsets(Component c, Insets insets) {
	    Insets margin = null;

	    if (c instanceof AbstractButton) {
		margin = ((AbstractButton)c).getMargin();
	    }
	    if (margin == null || margin instanceof UIResource) {
		// default margin so replace
		insets.left = left;
		insets.top = top;
		insets.right = right;
		insets.bottom = bottom;
	    } else {
		// Margin which has been explicitly set by the user.
		insets.left = margin.left;
		insets.top = margin.top;
		insets.right = margin.right;
		insets.bottom = margin.bottom;
	    }
	    return insets;
	}
    }

   public static class ButtonBorder extends AbstractBorder implements UIResource {
        protected Color shadow;
        protected Color darkShadow;
        protected Color highlight;
        protected Color lightHighlight;

        public ButtonBorder(Color shadow, Color darkShadow, 
                            Color highlight, Color lightHighlight) {
            this.shadow = shadow;
            this.darkShadow = darkShadow;
            this.highlight = highlight; 
            this.lightHighlight = lightHighlight;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, 
                            int width, int height) {
            boolean isPressed = false;
            boolean isDefault = false;
      
            if (c instanceof AbstractButton) {
	        AbstractButton b = (AbstractButton)c;
	        ButtonModel model = b.getModel();
	
   	        isPressed = model.isPressed() && model.isArmed();

                if (c instanceof JButton) {
                    isDefault = ((JButton)c).isDefaultButton();
                }
            }	
            BasicGraphicsUtils.drawBezel(g, x, y, width, height, 
				   isPressed, isDefault, shadow,
                                   darkShadow, highlight, lightHighlight);
        }

        public Insets getBorderInsets(Component c)       {
            return getBorderInsets(c, new Insets(0,0,0,0));
        }

        public Insets getBorderInsets(Component c, Insets insets)       {
            // leave room for default visual
            insets.top = 2;
            insets.left = insets.bottom = insets.right = 3;
	    return insets;
        }

    }

    public static class ToggleButtonBorder extends ButtonBorder {

        public ToggleButtonBorder(Color shadow, Color darkShadow, 
                                  Color highlight, Color lightHighlight) {
            super(shadow, darkShadow, highlight, lightHighlight);
        }

        public void paintBorder(Component c, Graphics g, int x, int y, 
                                int width, int height) {
                BasicGraphicsUtils.drawBezel(g, x, y, width, height, 
                                             false, false,
                                             shadow, darkShadow, 
                                             highlight, lightHighlight);
	}

        public Insets getBorderInsets(Component c)       {
            return new Insets(2, 2, 2, 2);
        }

        public Insets getBorderInsets(Component c, Insets insets)       {
            insets.top = insets.left = insets.bottom = insets.right = 2;
	    return insets;
        }
    }

    public static class RadioButtonBorder extends ButtonBorder {

        public RadioButtonBorder(Color shadow, Color darkShadow, 
                                 Color highlight, Color lightHighlight) {
            super(shadow, darkShadow, highlight, lightHighlight);
        }

      
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
	  
	    if (c instanceof AbstractButton) {
	        AbstractButton b = (AbstractButton)c;
	        ButtonModel model = b.getModel();
	      
	        if (model.isArmed() && model.isPressed() || model.isSelected()) {
		    BasicGraphicsUtils.drawLoweredBezel(g, x, y, width, height,
                                                        shadow, darkShadow, 
                                                        highlight, lightHighlight);
	        } else {
		    BasicGraphicsUtils.drawBezel(g, x, y, width, height, 
					       false, b.isFocusPainted() && b.hasFocus(),
                                                 shadow, darkShadow, 
                                                 highlight, lightHighlight);
	        }
	    } else {	
	        BasicGraphicsUtils.drawBezel(g, x, y, width, height, false, false,
                                             shadow, darkShadow, highlight, lightHighlight);
	    }
        }
      
        public Insets getBorderInsets(Component c)       {
	    return getBorderInsets(c, new Insets(0,0,0,0));
        }

        public Insets getBorderInsets(Component c, Insets insets)       {
            insets.top = insets.left = insets.bottom = insets.right = 2;
	    return insets;
        }
    }

    public static class MenuBarBorder extends AbstractBorder implements UIResource {
        private Color shadow;
        private Color highlight;

        public MenuBarBorder(Color shadow, Color highlight) {
            this.shadow = shadow;
            this.highlight = highlight;
        }

	public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
	    Color oldColor = g.getColor();
	    g.translate(x, y);
	    g.setColor(shadow);
	    g.drawLine(0, height-2, width, height-2);
	    g.setColor(highlight);	    
	    g.drawLine(0, height-1, width, height-1);
	    g.translate(-x,-y);
	    g.setColor(oldColor);
	}
	
	public Insets getBorderInsets(Component c)       {
	    return getBorderInsets(c, new Insets(0,0,0,0));
	}

        public Insets getBorderInsets(Component c, Insets insets)       {
            insets.top = 0;
	    insets.left = 0;
	    insets.bottom = 2;
	    insets.right = 0;
	    return insets;
        }
    }

    public static class MarginBorder extends AbstractBorder implements UIResource {

        public Insets getBorderInsets(Component c)       {
	    return getBorderInsets(c, new Insets(0,0,0,0));
        }

        public Insets getBorderInsets(Component c, Insets insets)       {
            Insets margin = null;
            //
            // Ideally we'd have an interface defined for classes which
            // support margins (to avoid this hackery), but we've
            // decided against it for simplicity
            //
           if (c instanceof AbstractButton) {
               AbstractButton b = (AbstractButton)c;
               margin = b.getMargin();
           } else if (c instanceof JToolBar) {
               JToolBar t = (JToolBar)c;
               margin = t.getMargin();
           } else if (c instanceof JTextComponent) {
               JTextComponent t = (JTextComponent)c;
               margin = t.getMargin();
           }
	   insets.top = margin != null? margin.top : 0;
	   insets.left = margin != null? margin.left : 0;
	   insets.bottom = margin != null? margin.bottom : 0;
	   insets.right = margin != null? margin.right : 0;
	       
	   return insets;
        }
    }

    public static class FieldBorder extends AbstractBorder implements UIResource {
        protected Color shadow;
        protected Color darkShadow;
        protected Color highlight;
        protected Color lightHighlight;

        public FieldBorder(Color shadow, Color darkShadow, 
                           Color highlight, Color lightHighlight) {
            this.shadow = shadow;
            this.highlight = highlight;
            this.darkShadow = darkShadow;
            this.lightHighlight = lightHighlight;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, 
                            int width, int height) {
            BasicGraphicsUtils.drawEtchedRect(g, x, y, width, height,
                                              shadow, darkShadow, 
                                              highlight, lightHighlight);
        }

        public Insets getBorderInsets(Component c) {
	    return getBorderInsets(c, new Insets(0,0,0,0));
	}

	public Insets getBorderInsets(Component c, Insets insets) {
            Insets margin = null;
            if (c instanceof JTextComponent) {
                margin = ((JTextComponent)c).getMargin();
            }
	    insets.top = margin != null? 2+margin.top : 2;
	    insets.left = margin != null? 2+margin.left : 2;
	    insets.bottom = margin != null? 2+margin.bottom : 2;
	    insets.right = margin != null? 2+margin.right : 2;
	       
	    return insets;
        }
    }


    /**
     * Draws the border around the divider in a splitpane
     * (when BasicSplitPaneUI is used). To get the appropriate effect, this
     * needs to be used with a SplitPaneBorder.
     */
    static class SplitPaneDividerBorder implements Border, UIResource {
        Color highlight;
        Color shadow;

        SplitPaneDividerBorder(Color highlight, Color shadow) {
	    this.highlight = highlight;
	    this.shadow = shadow;
	}

	public void paintBorder(Component c, Graphics g, int x, int y,
				int width, int height) {
	    Component          child;
	    Rectangle          cBounds;
	    JSplitPane         splitPane = ((BasicSplitPaneDivider)c).
		                         getBasicSplitPaneUI().getSplitPane();
	    Dimension          size = c.getSize();
	    
	    child = splitPane.getLeftComponent();
	    // This is needed for the space between the divider and end of
	    // splitpane.
	    g.setColor(c.getBackground());
	    g.drawRect(x, y, width - 1, height - 1);
	    if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
		if(child != null) {
		    g.setColor(highlight);
		    g.drawLine(0, 0, 0, size.height);
		}
		child = splitPane.getRightComponent();
		if(child != null) {
		    g.setColor(shadow);
		    g.drawLine(size.width - 1, 0, size.width - 1, size.height);
		}
	    } else {
		if(child != null) {
		    g.setColor(highlight);
		    g.drawLine(0, 0, size.width, 0);
		}
		child = splitPane.getRightComponent();
		if(child != null) {
		    g.setColor(shadow);
		    g.drawLine(0, size.height - 1, size.width,
			       size.height - 1);
		}
	    }
	}
	public Insets getBorderInsets(Component c) {
	    Insets insets = new Insets(0,0,0,0);
	    if (c instanceof BasicSplitPaneDivider) {
		BasicSplitPaneUI bspui = ((BasicSplitPaneDivider)c).
		                         getBasicSplitPaneUI();

		if (bspui != null) {
		    JSplitPane splitPane = bspui.getSplitPane();

		    if (splitPane != null) {
			if (splitPane.getOrientation() ==
			    JSplitPane.HORIZONTAL_SPLIT) {
			    insets.top = insets.bottom = 0;
			    insets.left = insets.right = 1;
			    return insets;
			}
			// VERTICAL_SPLIT
			insets.top = insets.bottom = 1;
			insets.left = insets.right = 0;
			return insets;
		    }
		}
	    }
	    insets.top = insets.bottom = insets.left = insets.right = 1;
	    return insets;
	}
	public boolean isBorderOpaque() { return true; }
    }


    /**
     * Draws the border around the splitpane. To work correctly you shoudl
     * also install a border on the divider (property SplitPaneDivider.border).
     */
    public static class SplitPaneBorder implements Border, UIResource {
        protected Color highlight;
        protected Color shadow;

        public SplitPaneBorder(Color highlight, Color shadow) {
	    this.highlight = highlight;
	    this.shadow = shadow;
	}

	public void paintBorder(Component c, Graphics g, int x, int y,
				int width, int height) {
	    // The only tricky part with this border is that the divider is
	    // not positioned at the top (for horizontal) or left (for vert),
	    // so this border draws to where the divider is:
	    // -----------------
	    // |xxxxxxx xxxxxxx|
	    // |x     ---     x|
	    // |x     |	|     x|
	    // |x     |D|     x|
	    // |x     | |     x|
	    // |x     ---     x|
	    // |xxxxxxx xxxxxxx|
	    // -----------------
	    // The above shows (rather excessively) what this looks like for
	    // a horizontal orientation. This border then draws the x's, with
	    // the SplitPaneDividerBorder drawing its own border.

	    Component          child;
	    Rectangle          cBounds;

	    JSplitPane splitPane = (JSplitPane)c;
	    
	    child = splitPane.getLeftComponent();
	    // This is needed for the space between the divider and end of
	    // splitpane.
	    g.setColor(c.getBackground());
	    g.drawRect(x, y, width - 1, height - 1);
	    if(splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) {
		if(child != null) {
		    cBounds = child.getBounds();
		    g.setColor(shadow);
		    g.drawLine(0, 0, cBounds.width + 1, 0);
		    g.drawLine(0, 1, 0, cBounds.height + 1);

		    g.setColor(highlight);
		    g.drawLine(0, cBounds.height + 1, cBounds.width + 1,
			       cBounds.height + 1);
		}
		child = splitPane.getRightComponent();
		if(child != null) {
		    cBounds = child.getBounds();

		    int             maxX = cBounds.x + cBounds.width;
		    int             maxY = cBounds.y + cBounds.height;
		    
		    g.setColor(shadow);
		    g.drawLine(cBounds.x - 1, 0, maxX, 0);
		    g.setColor(highlight);
		    g.drawLine(cBounds.x - 1, maxY, maxX, maxY);
		    g.drawLine(maxX, 0, maxX, maxY + 1);
		}
	    } else {
		if(child != null) {
		    cBounds = child.getBounds();
		    g.setColor(shadow);
		    g.drawLine(0, 0, cBounds.width + 1, 0);
		    g.drawLine(0, 1, 0, cBounds.height);
		    g.setColor(highlight);
		    g.drawLine(1 + cBounds.width, 0, 1 + cBounds.width,
			       cBounds.height + 1);
		    g.drawLine(0, cBounds.height + 1, 0, cBounds.height + 1);
		}
		child = splitPane.getRightComponent();
		if(child != null) {
		    cBounds = child.getBounds();

		    int             maxX = cBounds.x + cBounds.width;
		    int             maxY = cBounds.y + cBounds.height;
		    
		    g.setColor(shadow);
		    g.drawLine(0, cBounds.y - 1, 0, maxY);
		    g.drawLine(maxX, cBounds.y - 1, maxX, cBounds.y - 1);
		    g.setColor(highlight);
		    g.drawLine(0, maxY, cBounds.width + 1, maxY);
		    g.drawLine(maxX, cBounds.y, maxX, maxY);
		}
	    }
	}
	public Insets getBorderInsets(Component c) {
	    return new Insets(1, 1, 1, 1);
	}
	public boolean isBorderOpaque() { return true; }
    }
    
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar