1 /*
2 * Copyright 1997-2006 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.Rectangle;
30 import java.awt.Component;
31 import java.io.Serializable;
32
33 /**
34 * A class that implements an empty border with no size.
35 * This provides a convenient base class from which other border
36 * classes can be easily derived.
37 * <p>
38 * <strong>Warning:</strong>
39 * Serialized objects of this class will not be compatible with
40 * future Swing releases. The current serialization support is
41 * appropriate for short term storage or RMI between applications running
42 * the same version of Swing. As of 1.4, support for long term storage
43 * of all JavaBeans<sup><font size="-2">TM</font></sup>
44 * has been added to the <code>java.beans</code> package.
45 * Please see {@link java.beans.XMLEncoder}.
46 *
47 * @author David Kloba
48 */
49 public abstract class AbstractBorder implements Border, Serializable
50 {
51 /**
52 * This default implementation does no painting.
53 * @param c the component for which this border is being painted
54 * @param g the paint graphics
55 * @param x the x position of the painted border
56 * @param y the y position of the painted border
57 * @param width the width of the painted border
58 * @param height the height of the painted border
59 */
60 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
61 }
62
63 /**
64 * This default implementation returns a new {@link Insets} object
65 * that is initialized by the {@link #getBorderInsets(Component,Insets)}
66 * method.
67 * By default the {@code top}, {@code left}, {@code bottom},
68 * and {@code right} fields are set to {@code 0}.
69 *
70 * @param c the component for which this border insets value applies
71 * @return a new {@link Insets} object
72 */
73 public Insets getBorderInsets(Component c) {
74 return getBorderInsets(c, new Insets(0, 0, 0, 0));
75 }
76
77 /**
78 * Reinitializes the insets parameter with this Border's current Insets.
79 * @param c the component for which this border insets value applies
80 * @param insets the object to be reinitialized
81 * @return the <code>insets</code> object
82 */
83 public Insets getBorderInsets(Component c, Insets insets) {
84 insets.left = insets.top = insets.right = insets.bottom = 0;
85 return insets;
86 }
87
88 /**
89 * This default implementation returns false.
90 * @return false
91 */
92 public boolean isBorderOpaque() { return false; }
93
94 /**
95 * This convenience method calls the static method.
96 * @param c the component for which this border is being computed
97 * @param x the x position of the border
98 * @param y the y position of the border
99 * @param width the width of the border
100 * @param height the height of the border
101 * @return a <code>Rectangle</code> containing the interior coordinates
102 */
103 public Rectangle getInteriorRectangle(Component c, int x, int y, int width, int height) {
104 return getInteriorRectangle(c, this, x, y, width, height);
105 }
106
107 /**
108 * Returns a rectangle using the arguments minus the
109 * insets of the border. This is useful for determining the area
110 * that components should draw in that will not intersect the border.
111 * @param c the component for which this border is being computed
112 * @param b the <code>Border</code> object
113 * @param x the x position of the border
114 * @param y the y position of the border
115 * @param width the width of the border
116 * @param height the height of the border
117 * @return a <code>Rectangle</code> containing the interior coordinates
118 */
119 public static Rectangle getInteriorRectangle(Component c, Border b, int x, int y, int width, int height) {
120 Insets insets;
121 if(b != null)
122 insets = b.getBorderInsets(c);
123 else
124 insets = new Insets(0, 0, 0, 0);
125 return new Rectangle(x + insets.left,
126 y + insets.top,
127 width - insets.right - insets.left,
128 height - insets.top - insets.bottom);
129 }
130
131 /**
132 * Returns the baseline. A return value less than 0 indicates the border
133 * does not have a reasonable baseline.
134 * <p>
135 * The default implementation returns -1. Subclasses that support
136 * baseline should override appropriately. If a value >= 0 is
137 * returned, then the component has a valid baseline for any
138 * size >= the minimum size and <code>getBaselineResizeBehavior</code>
139 * can be used to determine how the baseline changes with size.
140 *
141 * @param c <code>Component</code> baseline is being requested for
142 * @param width the width to get the baseline for
143 * @param height the height to get the baseline for
144 * @return the baseline or < 0 indicating there is no reasonable
145 * baseline
146 * @throws IllegalArgumentException if width or height is < 0
147 * @see java.awt.Component#getBaseline(int,int)
148 * @see java.awt.Component#getBaselineResizeBehavior()
149 * @since 1.6
150 */
151 public int getBaseline(Component c, int width, int height) {
152 if (width < 0 || height < 0) {
153 throw new IllegalArgumentException(
154 "Width and height must be >= 0");
155 }
156 return -1;
157 }
158
159 /**
160 * Returns an enum indicating how the baseline of a component
161 * changes as the size changes. This method is primarily meant for
162 * layout managers and GUI builders.
163 * <p>
164 * The default implementation returns
165 * <code>BaselineResizeBehavior.OTHER</code>, subclasses that support
166 * baseline should override appropriately. Subclasses should
167 * never return <code>null</code>; if the baseline can not be
168 * calculated return <code>BaselineResizeBehavior.OTHER</code>. Callers
169 * should first ask for the baseline using
170 * <code>getBaseline</code> and if a value >= 0 is returned use
171 * this method. It is acceptable for this method to return a
172 * value other than <code>BaselineResizeBehavior.OTHER</code> even if
173 * <code>getBaseline</code> returns a value less than 0.
174 *
175 * @param c <code>Component</code> to return baseline resize behavior for
176 * @return an enum indicating how the baseline changes as the border is
177 * resized
178 * @see java.awt.Component#getBaseline(int,int)
179 * @see java.awt.Component#getBaselineResizeBehavior()
180 * @since 1.6
181 */
182 public Component.BaselineResizeBehavior getBaselineResizeBehavior(
183 Component c) {
184 if (c == null) {
185 throw new NullPointerException("Component must be non-null");
186 }
187 return Component.BaselineResizeBehavior.OTHER;
188 }
189
190 /*
191 * Convenience function for determining ComponentOrientation.
192 * Helps us avoid having Munge directives throughout the code.
193 */
194 static boolean isLeftToRight( Component c ) {
195 return c.getComponentOrientation().isLeftToRight();
196 }
197
198 }