KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > border > CompoundBorder


1 /*
2  * @(#)CompoundBorder.java 1.20 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 package javax.swing.border;
8
9 import java.awt.Graphics JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.Component JavaDoc;
12
13 /**
14  * A composite Border class used to compose two Border objects
15  * into a single border by nesting an inside Border object within
16  * the insets of an outside Border object.
17  *
18  * For example, this class may be used to add blank margin space
19  * to a component with an existing decorative border:
20  * <p>
21  * <code><pre>
22  * Border border = comp.getBorder();
23  * Border margin = new EmptyBorder(10,10,10,10);
24  * comp.setBorder(new CompoundBorder(border, margin));
25  * </pre></code>
26  * <p>
27  * <strong>Warning:</strong>
28  * Serialized objects of this class will not be compatible with
29  * future Swing releases. The current serialization support is
30  * appropriate for short term storage or RMI between applications running
31  * the same version of Swing. As of 1.4, support for long term storage
32  * of all JavaBeans<sup><font size="-2">TM</font></sup>
33  * has been added to the <code>java.beans</code> package.
34  * Please see {@link java.beans.XMLEncoder}.
35  *
36  * @version 1.20 12/19/03
37  * @author David Kloba
38  */

39 public class CompoundBorder extends AbstractBorder JavaDoc {
40     protected Border JavaDoc outsideBorder;
41     protected Border JavaDoc insideBorder;
42
43     /**
44      * Creates a compound border with null outside and inside borders.
45      */

46     public CompoundBorder() {
47     this.outsideBorder = null;
48     this.insideBorder = null;
49     }
50
51     /**
52      * Creates a compound border with the specified outside and
53      * inside borders. Either border may be null.
54      * @param outsideBorder the outside border
55      * @param insideBorder the inside border to be nested
56      */

57     public CompoundBorder(Border JavaDoc outsideBorder, Border JavaDoc insideBorder) {
58     this.outsideBorder = outsideBorder;
59     this.insideBorder = insideBorder;
60     }
61
62     /**
63      * Returns whether or not this compound border is opaque.
64      * Returns true if both the inside and outside borders are
65      * non-null and opaque; returns false otherwise.
66      */

67     public boolean isBorderOpaque() {
68     return (outsideBorder == null || outsideBorder.isBorderOpaque()) &&
69                (insideBorder == null || insideBorder.isBorderOpaque());
70     }
71
72     /**
73      * Paints the compound border by painting the outside border
74      * with the specified position and size and then painting the
75      * inside border at the specified position and size offset by
76      * the insets of the outside border.
77      * @param c the component for which this border is being painted
78      * @param g the paint graphics
79      * @param x the x position of the painted border
80      * @param y the y position of the painted border
81      * @param width the width of the painted border
82      * @param height the height of the painted border
83      */

84     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
85     Insets JavaDoc nextInsets;
86     int px, py, pw, ph;
87
88     px = x;
89     py = y;
90     pw = width;
91     ph = height;
92
93     if(outsideBorder != null) {
94         outsideBorder.paintBorder(c, g, px, py, pw, ph);
95
96         nextInsets = outsideBorder.getBorderInsets(c);
97         px += nextInsets.left;
98         py += nextInsets.top;
99         pw = pw - nextInsets.right - nextInsets.left;
100         ph = ph - nextInsets.bottom - nextInsets.top;
101     }
102     if(insideBorder != null)
103         insideBorder.paintBorder(c, g, px, py, pw, ph);
104
105     }
106      
107     /**
108      * Reinitialize the insets parameter with this Border's current Insets.
109      * @param c the component for which this border insets value applies
110      * @param insets the object to be reinitialized
111      */

112     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
113     Insets JavaDoc nextInsets;
114
115     insets.top = insets.left = insets.right = insets.bottom = 0;
116     if(outsideBorder != null) {
117         nextInsets = outsideBorder.getBorderInsets(c);
118         insets.top += nextInsets.top;
119         insets.left += nextInsets.left;
120         insets.right += nextInsets.right;
121         insets.bottom += nextInsets.bottom;
122     }
123     if(insideBorder != null) {
124         nextInsets = insideBorder.getBorderInsets(c);
125         insets.top += nextInsets.top;
126         insets.left += nextInsets.left;
127         insets.right += nextInsets.right;
128         insets.bottom += nextInsets.bottom;
129     }
130         return insets;
131     }
132
133     /**
134      * Returns the insets of the composite border by adding
135      * the insets of the outside border to the insets of the
136      * inside border.
137      * @param c the component for which this border insets value applies
138      */

139     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
140     return getBorderInsets(c, new Insets JavaDoc(0,0,0,0));
141     }
142
143     /**
144      * Returns the outside border object.
145      */

146     public Border JavaDoc getOutsideBorder() {
147         return outsideBorder;
148     }
149
150     /**
151      * Returns the inside border object.
152      */

153     public Border JavaDoc getInsideBorder() {
154         return insideBorder;
155     }
156 }
157
158
Popular Tags