001 /* AbstractBorder.java --
002 Copyright (C) 2003, 2006, Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing.border;
040
041 import java.awt.Component;
042 import java.awt.Graphics;
043 import java.awt.Insets;
044 import java.awt.Rectangle;
045 import java.io.Serializable;
046
047
048 /**
049 * An invisible zero-width border, serving as a base class for
050 * implementing more interesting borders.
051 *
052 * @author Sascha Brawer (brawer@dandelis.ch)
053 * @author Ronald Veldema (rveldema@cs.vu.nl)
054 */
055 public abstract class AbstractBorder implements Border, Serializable
056 {
057 static final long serialVersionUID = -545885975315191844L;
058
059 /**
060 * Constructs a new AbstractBorder.
061 */
062 public AbstractBorder()
063 {
064 // Nothing to do here.
065 }
066
067 /**
068 * Performs nothing, because the default implementation provided by
069 * this class is an invisible, zero-width border. Subclasses will
070 * likely want to override this method, but they are not required
071 * to do so.
072 *
073 * @param c the component whose border is to be painted.
074 * @param g the graphics for painting.
075 * @param x the horizontal position for painting the border.
076 * @param y the vertical position for painting the border.
077 * @param width the width of the available area for painting the border.
078 * @param height the height of the available area for painting the border.
079 */
080 public void paintBorder(Component c, Graphics g, int x, int y, int width,
081 int height)
082 {
083 // A previous version of Classpath had emitted a warning when
084 // this method was called. The warning was removed because it is
085 // perfectly legal for a subclass to not override the paintBorder
086 // method. An example would be EmptyBorder.
087 }
088
089 /**
090 * Returns the insets required for drawing this border around the specified
091 * component.
092 *
093 * @param c the component that the border applies to (ignored here,
094 * subclasses may use it).
095 *
096 * @return an Insets object whose <code>left</code>, <code>right</code>,
097 * <code>top</code> and <code>bottom</code> fields indicate the
098 * width of the border at the respective edge, which is zero
099 * for the default implementation provided by AbstractButton.
100 *
101 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
102 */
103 public Insets getBorderInsets(Component c)
104 {
105 return new Insets(0, 0, 0, 0);
106 }
107
108 /**
109 * Returns the insets required for drawing this border around the specified
110 * component. The default implementation provided here sets the
111 * <code>left</code>, <code>right</code>, <code>top</code> and
112 * <code>bottom</code> fields of the passed <code>insets</code> parameter to
113 * zero.
114 *
115 * @param c the component that the border applies to (ignored here,
116 * subclasses may use it).
117 * @param insets an instance that will be overwritten and returned as the
118 * result (<code>null</code> not permitted).
119 *
120 * @return The border insets (the same object that was passed as the
121 * <code>insets</code> argument).
122 *
123 * @see #getBorderInsets(Component)
124 *
125 * @throws NullPointerException if <code>insets</code> is <code>null</code>.
126 */
127 public Insets getBorderInsets(Component c, Insets insets)
128 {
129 insets.left = insets.right = insets.top = insets.bottom = 0;
130 return insets;
131 }
132
133 /**
134 * Determines whether or not this border is opaque. An opaque border
135 * fills every pixel in its area when painting. Partially
136 * translucent borders must return <code>false</code>, or ugly
137 * artifacts can appear on screen. The default implementation
138 * provided here always returns <code>false</code>.
139 *
140 * @return <code>false</code>.
141 */
142 public boolean isBorderOpaque()
143 {
144 return false;
145 }
146
147 /**
148 * Returns a rectangle that covers the specified area minus the insets
149 * required to draw this border. Components that wish to determine an area
150 * into which they can safely draw without intersecting with a border might
151 * want to use this helper method.
152 *
153 * @param c the component in the center of this border.
154 * @param x the horizontal position of the border.
155 * @param y the vertical position of the border.
156 * @param width the width of the available area for the border.
157 * @param height the height of the available area for the border.
158 *
159 * @return The interior rectangle.
160 */
161 public Rectangle getInteriorRectangle(Component c, int x, int y, int width,
162 int height)
163 {
164 return getInteriorRectangle(c, this, x, y, width, height);
165 }
166
167 /**
168 * Returns a rectangle that covers the specified area minus the insets
169 * required to draw the specified border (if the border is <code>null</code>,
170 * zero insets are assumed). Components that wish to determine an area into
171 * which they can safely draw without intersecting with a border might want
172 * to use this helper method.
173 *
174 * @param c the component in the center of this border.
175 * @param b the border (<code>null</code> permitted).
176 * @param x the horizontal position of the border.
177 * @param y the vertical position of the border.
178 * @param width the width of the available area for the border.
179 * @param height the height of the available area for the border.
180 *
181 * @return The interior rectangle.
182 */
183 public static Rectangle getInteriorRectangle(Component c, Border b, int x,
184 int y, int width, int height)
185 {
186 Insets borderInsets;
187
188 if (b != null)
189 {
190 borderInsets = b.getBorderInsets(c);
191 x += borderInsets.left;
192 y += borderInsets.top;
193 width -= borderInsets.left + borderInsets.right;
194 height -= borderInsets.top + borderInsets.bottom;
195 }
196
197 return new Rectangle(x, y, width, height);
198 }
199 }