KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > ui > VariableBorder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.options.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Graphics2D JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.Shape JavaDoc;
28 import java.awt.geom.Line2D JavaDoc;
29 import javax.swing.JToggleButton JavaDoc;
30 import javax.swing.JToolBar JavaDoc;
31 import javax.swing.border.Border JavaDoc;
32
33 /**
34  * VariableBorder is a simple Border that only draw a line if a color is given.
35  *
36  * @author Christopher Atlan
37  */

38 public class VariableBorder implements Border JavaDoc {
39     private Color JavaDoc topColor;
40     private Color JavaDoc leftColor;
41     private Color JavaDoc bottomColor;
42     private Color JavaDoc rightColor;
43     
44     /** Creates a new instance of VariableBorder */
45     public VariableBorder(final Color JavaDoc topColor,
46             final Color JavaDoc leftColor,
47             final Color JavaDoc bottomColor,
48             final Color JavaDoc rightColor) {
49         this.topColor = topColor;
50         this.leftColor = leftColor;
51         this.bottomColor = bottomColor;
52         this.rightColor = rightColor;
53     }
54     
55     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
56         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
57         Shape JavaDoc s;
58         
59         if(topColor != null) {
60             s = new Line2D.Double JavaDoc(x,y,x+width, y);
61             g2d.setColor(topColor);
62             g2d.fill(s);
63         }
64         
65         if(leftColor != null) {
66             s = new Line2D.Double JavaDoc(x,y,x, y+height);
67             g2d.setColor(leftColor);
68             g2d.fill(s);
69         }
70         
71         if(bottomColor != null) {
72             s = new Line2D.Double JavaDoc(x,y+height-1,x+width, y+height-1);
73             g2d.setColor(bottomColor);
74             g2d.fill(s);
75         }
76         
77         if(rightColor != null) {
78             s = new Line2D.Double JavaDoc(x+width-1,y,x+width-1, y+height);
79             g2d.setColor(rightColor);
80             g2d.fill(s);
81         }
82         
83
84     }
85     
86     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
87         Insets JavaDoc i = new Insets JavaDoc(0, 0, 0, 0);
88         
89         if(topColor != null)
90             i.top = 1;
91         
92         if(leftColor != null)
93             i.left = 1;
94         
95         if(bottomColor != null)
96             i.bottom = 1;
97         
98         if(rightColor != null)
99             i.right = 1;
100         
101         if (c instanceof JToolBar JavaDoc) {
102             Insets JavaDoc toolBarInsets = ((JToolBar JavaDoc)c).getMargin();
103             i.top += toolBarInsets.top;
104             i.left += toolBarInsets.left;
105             i.right += toolBarInsets.right;
106             i.bottom += toolBarInsets.bottom;
107         }
108         
109         if (c instanceof JToggleButton JavaDoc) {
110             Insets JavaDoc buttonInsets = ((JToggleButton JavaDoc)c).getMargin();
111             i.top += buttonInsets.top;
112             i.left += buttonInsets.left;
113             i.right += buttonInsets.right;
114             i.bottom += buttonInsets.bottom;
115         }
116         
117         return i;
118     }
119     
120     public boolean isBorderOpaque() {
121         return false;
122     }
123 }
124
Popular Tags