001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf;
027
028 import javax.swing.JComponent;
029 import javax.swing.SwingUtilities;
030 import javax.accessibility.Accessible;
031
032 import java.awt.Component;
033 import java.awt.Container;
034 import java.awt.Dimension;
035 import java.awt.Graphics;
036 import java.awt.Insets;
037
038 /**
039 * The base class for all UI delegate objects in the Swing pluggable
040 * look and feel architecture. The UI delegate object for a Swing
041 * component is responsible for implementing the aspects of the
042 * component that depend on the look and feel.
043 * The <code>JComponent</code> class
044 * invokes methods from this class in order to delegate operations
045 * (painting, layout calculations, etc.) that may vary depending on the
046 * look and feel installed. <b>Client programs should not invoke methods
047 * on this class directly.</b>
048 *
049 * @see javax.swing.JComponent
050 * @see javax.swing.UIManager
051 *
052 */
053 public abstract class ComponentUI {
054 /**
055 * Sole constructor. (For invocation by subclass constructors,
056 * typically implicit.)
057 */
058 public ComponentUI() {
059 }
060
061 /**
062 * Configures the specified component appropriate for the look and feel.
063 * This method is invoked when the <code>ComponentUI</code> instance is being installed
064 * as the UI delegate on the specified component. This method should
065 * completely configure the component for the look and feel,
066 * including the following:
067 * <ol>
068 * <li>Install any default property values for color, fonts, borders,
069 * icons, opacity, etc. on the component. Whenever possible,
070 * property values initialized by the client program should <i>not</i>
071 * be overridden.
072 * <li>Install a <code>LayoutManager</code> on the component if necessary.
073 * <li>Create/add any required sub-components to the component.
074 * <li>Create/install event listeners on the component.
075 * <li>Create/install a <code>PropertyChangeListener</code> on the component in order
076 * to detect and respond to component property changes appropriately.
077 * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
078 * <li>Initialize any appropriate instance data.
079 * </ol>
080 * @param c the component where this UI delegate is being installed
081 *
082 * @see #uninstallUI
083 * @see javax.swing.JComponent#setUI
084 * @see javax.swing.JComponent#updateUI
085 */
086 public void installUI(JComponent c) {
087 }
088
089 /**
090 * Reverses configuration which was done on the specified component during
091 * <code>installUI</code>. This method is invoked when this
092 * <code>UIComponent</code> instance is being removed as the UI delegate
093 * for the specified component. This method should undo the
094 * configuration performed in <code>installUI</code>, being careful to
095 * leave the <code>JComponent</code> instance in a clean state (no
096 * extraneous listeners, look-and-feel-specific property objects, etc.).
097 * This should include the following:
098 * <ol>
099 * <li>Remove any UI-set borders from the component.
100 * <li>Remove any UI-set layout managers on the component.
101 * <li>Remove any UI-added sub-components from the component.
102 * <li>Remove any UI-added event/property listeners from the component.
103 * <li>Remove any UI-installed keyboard UI from the component.
104 * <li>Nullify any allocated instance data objects to allow for GC.
105 * </ol>
106 * @param c the component from which this UI delegate is being removed;
107 * this argument is often ignored,
108 * but might be used if the UI object is stateless
109 * and shared by multiple components
110 *
111 * @see #installUI
112 * @see javax.swing.JComponent#updateUI
113 */
114 public void uninstallUI(JComponent c) {
115 }
116
117 /**
118 * Paints the specified component appropriate for the look and feel.
119 * This method is invoked from the <code>ComponentUI.update</code> method when
120 * the specified component is being painted. Subclasses should override
121 * this method and use the specified <code>Graphics</code> object to
122 * render the content of the component.
123 *
124 * @param g the <code>Graphics</code> context in which to paint
125 * @param c the component being painted;
126 * this argument is often ignored,
127 * but might be used if the UI object is stateless
128 * and shared by multiple components
129 *
130 * @see #update
131 */
132 public void paint(Graphics g, JComponent c) {
133 }
134
135 /**
136 * Notifies this UI delegate that it's time to paint the specified
137 * component. This method is invoked by <code>JComponent</code>
138 * when the specified component is being painted.
139 * By default this method will fill the specified component with
140 * its background color (if its <code>opaque</code> property is
141 * <code>true</code>) and then immediately call <code>paint</code>.
142 * In general this method need not be overridden by subclasses;
143 * all look-and-feel rendering code should reside in the <code>paint</code>
144 * method.
145 *
146 * @param g the <code>Graphics</code> context in which to paint
147 * @param c the component being painted;
148 * this argument is often ignored,
149 * but might be used if the UI object is stateless
150 * and shared by multiple components
151 *
152 * @see #paint
153 * @see javax.swing.JComponent#paintComponent
154 */
155 public void update(Graphics g, JComponent c) {
156 if (c.isOpaque()) {
157 g.setColor(c.getBackground());
158 g.fillRect(0, 0, c.getWidth(), c.getHeight());
159 }
160 paint(g, c);
161 }
162
163 /**
164 * Returns the specified component's preferred size appropriate for
165 * the look and feel. If <code>null</code> is returned, the preferred
166 * size will be calculated by the component's layout manager instead
167 * (this is the preferred approach for any component with a specific
168 * layout manager installed). The default implementation of this
169 * method returns <code>null</code>.
170 *
171 * @param c the component whose preferred size is being queried;
172 * this argument is often ignored,
173 * but might be used if the UI object is stateless
174 * and shared by multiple components
175 *
176 * @see javax.swing.JComponent#getPreferredSize
177 * @see java.awt.LayoutManager#preferredLayoutSize
178 */
179 public Dimension getPreferredSize(JComponent c) {
180 return null;
181 }
182
183 /**
184 * Returns the specified component's minimum size appropriate for
185 * the look and feel. If <code>null</code> is returned, the minimum
186 * size will be calculated by the component's layout manager instead
187 * (this is the preferred approach for any component with a specific
188 * layout manager installed). The default implementation of this
189 * method invokes <code>getPreferredSize</code> and returns that value.
190 *
191 * @param c the component whose minimum size is being queried;
192 * this argument is often ignored,
193 * but might be used if the UI object is stateless
194 * and shared by multiple components
195 *
196 * @return a <code>Dimension</code> object or <code>null</code>
197 *
198 * @see javax.swing.JComponent#getMinimumSize
199 * @see java.awt.LayoutManager#minimumLayoutSize
200 * @see #getPreferredSize
201 */
202 public Dimension getMinimumSize(JComponent c) {
203 return getPreferredSize(c);
204 }
205
206 /**
207 * Returns the specified component's maximum size appropriate for
208 * the look and feel. If <code>null</code> is returned, the maximum
209 * size will be calculated by the component's layout manager instead
210 * (this is the preferred approach for any component with a specific
211 * layout manager installed). The default implementation of this
212 * method invokes <code>getPreferredSize</code> and returns that value.
213 *
214 * @param c the component whose maximum size is being queried;
215 * this argument is often ignored,
216 * but might be used if the UI object is stateless
217 * and shared by multiple components
218 * @return a <code>Dimension</code> object or <code>null</code>
219 *
220 * @see javax.swing.JComponent#getMaximumSize
221 * @see java.awt.LayoutManager2#maximumLayoutSize
222 */
223 public Dimension getMaximumSize(JComponent c) {
224 return getPreferredSize(c);
225 }
226
227 /**
228 * Returns <code>true</code> if the specified <i>x,y</i> location is
229 * contained within the look and feel's defined shape of the specified
230 * component. <code>x</code> and <code>y</code> are defined to be relative
231 * to the coordinate system of the specified component. Although
232 * a component's <code>bounds</code> is constrained to a rectangle,
233 * this method provides the means for defining a non-rectangular
234 * shape within those bounds for the purpose of hit detection.
235 *
236 * @param c the component where the <i>x,y</i> location is being queried;
237 * this argument is often ignored,
238 * but might be used if the UI object is stateless
239 * and shared by multiple components
240 * @param x the <i>x</i> coordinate of the point
241 * @param y the <i>y</i> coordinate of the point
242 *
243 * @see javax.swing.JComponent#contains
244 * @see java.awt.Component#contains
245 */
246 public boolean contains(JComponent c, int x, int y) {
247 return c.inside(x, y);
248 }
249
250 /**
251 * Returns an instance of the UI delegate for the specified component.
252 * Each subclass must provide its own static <code>createUI</code>
253 * method that returns an instance of that UI delegate subclass.
254 * If the UI delegate subclass is stateless, it may return an instance
255 * that is shared by multiple components. If the UI delegate is
256 * stateful, then it should return a new instance per component.
257 * The default implementation of this method throws an error, as it
258 * should never be invoked.
259 */
260 public static ComponentUI createUI(JComponent c) {
261 throw new Error("ComponentUI.createUI not implemented.");
262 }
263
264 /**
265 * Returns the baseline. The baseline is measured from the top of
266 * the component. This method is primarily meant for
267 * <code>LayoutManager</code>s to align components along their
268 * baseline. A return value less than 0 indicates this component
269 * does not have a reasonable baseline and that
270 * <code>LayoutManager</code>s should not align this component on
271 * its baseline.
272 * <p>
273 * This method returns -1. Subclasses that have a meaningful baseline
274 * should override appropriately.
275 *
276 * @param c <code>JComponent</code> baseline is being requested for
277 * @param width the width to get the baseline for
278 * @param height the height to get the baseline for
279 * @throws NullPointerException if <code>c</code> is <code>null</code>
280 * @throws IllegalArgumentException if width or height is < 0
281 * @return baseline or a value < 0 indicating there is no reasonable
282 * baseline
283 * @see javax.swing.JComponent#getBaseline(int,int)
284 * @since 1.6
285 */
286 public int getBaseline(JComponent c, int width, int height) {
287 if (c == null) {
288 throw new NullPointerException("Component must be non-null");
289 }
290 if (width < 0 || height < 0) {
291 throw new IllegalArgumentException(
292 "Width and height must be >= 0");
293 }
294 return -1;
295 }
296
297 /**
298 * Returns an enum indicating how the baseline of he component
299 * changes as the size changes. This method is primarily meant for
300 * layout managers and GUI builders.
301 * <p>
302 * This method returns <code>BaselineResizeBehavior.OTHER</code>.
303 * Subclasses that support a baseline should override appropriately.
304 *
305 * @param c <code>JComponent</code> to return baseline resize behavior for
306 * @return an enum indicating how the baseline changes as the component
307 * size changes
308 * @throws NullPointerException if <code>c</code> is <code>null</code>
309 * @see javax.swing.JComponent#getBaseline(int, int)
310 * @since 1.6
311 */
312 public Component.BaselineResizeBehavior getBaselineResizeBehavior(
313 JComponent c) {
314 if (c == null) {
315 throw new NullPointerException("Component must be non-null");
316 }
317 return Component.BaselineResizeBehavior.OTHER;
318 }
319
320 /**
321 * Returns the number of accessible children in the object. If all
322 * of the children of this object implement <code>Accessible</code>,
323 * this
324 * method should return the number of children of this object.
325 * UIs might wish to override this if they present areas on the
326 * screen that can be viewed as components, but actual components
327 * are not used for presenting those areas.
328 *
329 * Note: As of v1.3, it is recommended that developers call
330 * <code>Component.AccessibleAWTComponent.getAccessibleChildrenCount()</code> instead
331 * of this method.
332 *
333 * @see #getAccessibleChild
334 * @return the number of accessible children in the object
335 */
336 public int getAccessibleChildrenCount(JComponent c) {
337 return SwingUtilities.getAccessibleChildrenCount(c);
338 }
339
340 /**
341 * Returns the <code>i</code>th <code>Accessible</code> child of the object.
342 * UIs might need to override this if they present areas on the
343 * screen that can be viewed as components, but actual components
344 * are not used for presenting those areas.
345 *
346 * <p>
347 *
348 * Note: As of v1.3, it is recommended that developers call
349 * <code>Component.AccessibleAWTComponent.getAccessibleChild()</code> instead of
350 * this method.
351 *
352 * @see #getAccessibleChildrenCount
353 * @param i zero-based index of child
354 * @return the <code>i</code>th <code>Accessible</code> child of the object
355 */
356 public Accessible getAccessibleChild(JComponent c, int i) {
357 return SwingUtilities.getAccessibleChild(c, i);
358 }
359 }
|