1 /*
2 * Copyright 1997-2007 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.border;
26
27 import java.awt.Graphics;
28 import java.awt.Insets;
29 import java.awt.Component;
30 import java.beans.ConstructorProperties;
31
32 /**
33 * A composite Border class used to compose two Border objects
34 * into a single border by nesting an inside Border object within
35 * the insets of an outside Border object.
36 *
37 * For example, this class may be used to add blank margin space
38 * to a component with an existing decorative border:
39 * <p>
40 * <code><pre>
41 * Border border = comp.getBorder();
42 * Border margin = new EmptyBorder(10,10,10,10);
43 * comp.setBorder(new CompoundBorder(border, margin));
44 * </pre></code>
45 * <p>
46 * <strong>Warning:</strong>
47 * Serialized objects of this class will not be compatible with
48 * future Swing releases. The current serialization support is
49 * appropriate for short term storage or RMI between applications running
50 * the same version of Swing. As of 1.4, support for long term storage
51 * of all JavaBeans<sup><font size="-2">TM</font></sup>
52 * has been added to the <code>java.beans</code> package.
53 * Please see {@link java.beans.XMLEncoder}.
54 *
55 * @author David Kloba
56 */
57 public class CompoundBorder extends AbstractBorder {
58 protected Border outsideBorder;
59 protected Border insideBorder;
60
61 /**
62 * Creates a compound border with null outside and inside borders.
63 */
64 public CompoundBorder() {
65 this.outsideBorder = null;
66 this.insideBorder = null;
67 }
68
69 /**
70 * Creates a compound border with the specified outside and
71 * inside borders. Either border may be null.
72 * @param outsideBorder the outside border
73 * @param insideBorder the inside border to be nested
74 */
75 @ConstructorProperties({"outsideBorder", "insideBorder"})
76 public CompoundBorder(Border outsideBorder, Border insideBorder) {
77 this.outsideBorder = outsideBorder;
78 this.insideBorder = insideBorder;
79 }
80
81 /**
82 * Returns whether or not this compound border is opaque.
83 * Returns true if both the inside and outside borders are
84 * non-null and opaque; returns false otherwise.
85 */
86 public boolean isBorderOpaque() {
87 return (outsideBorder == null || outsideBorder.isBorderOpaque()) &&
88 (insideBorder == null || insideBorder.isBorderOpaque());
89 }
90
91 /**
92 * Paints the compound border by painting the outside border
93 * with the specified position and size and then painting the
94 * inside border at the specified position and size offset by
95 * the insets of the outside border.
96 * @param c the component for which this border is being painted
97 * @param g the paint graphics
98 * @param x the x position of the painted border
99 * @param y the y position of the painted border
100 * @param width the width of the painted border
101 * @param height the height of the painted border
102 */
103 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
104 Insets nextInsets;
105 int px, py, pw, ph;
106
107 px = x;
108 py = y;
109 pw = width;
110 ph = height;
111
112 if(outsideBorder != null) {
113 outsideBorder.paintBorder(c, g, px, py, pw, ph);
114
115 nextInsets = outsideBorder.getBorderInsets(c);
116 px += nextInsets.left;
117 py += nextInsets.top;
118 pw = pw - nextInsets.right - nextInsets.left;
119 ph = ph - nextInsets.bottom - nextInsets.top;
120 }
121 if(insideBorder != null)
122 insideBorder.paintBorder(c, g, px, py, pw, ph);
123
124 }
125
126 /**
127 * Reinitialize the insets parameter with this Border's current Insets.
128 * @param c the component for which this border insets value applies
129 * @param insets the object to be reinitialized
130 */
131 public Insets getBorderInsets(Component c, Insets insets) {
132 Insets nextInsets;
133
134 insets.top = insets.left = insets.right = insets.bottom = 0;
135 if(outsideBorder != null) {
136 nextInsets = outsideBorder.getBorderInsets(c);
137 insets.top += nextInsets.top;
138 insets.left += nextInsets.left;
139 insets.right += nextInsets.right;
140 insets.bottom += nextInsets.bottom;
141 }
142 if(insideBorder != null) {
143 nextInsets = insideBorder.getBorderInsets(c);
144 insets.top += nextInsets.top;
145 insets.left += nextInsets.left;
146 insets.right += nextInsets.right;
147 insets.bottom += nextInsets.bottom;
148 }
149 return insets;
150 }
151
152 /**
153 * Returns the outside border object.
154 */
155 public Border getOutsideBorder() {
156 return outsideBorder;
157 }
158
159 /**
160 * Returns the inside border object.
161 */
162 public Border getInsideBorder() {
163 return insideBorder;
164 }
165 }