1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.swing.plaf;
27 
28 import javax.swing.JComponent;
29 import javax.swing.SwingUtilities;
30 import javax.accessibility.Accessible;
31 
32 import java.awt.Component;
33 import java.awt.Container;
34 import java.awt.Dimension;
35 import java.awt.Graphics;
36 import java.awt.Insets;
37 
38 
39 /**
40  * The base class for all UI delegate objects in the Swing pluggable
41  * look and feel architecture.  The UI delegate object for a Swing
42  * component is responsible for implementing the aspects of the
43  * component that depend on the look and feel.
44  * The <code>JComponent</code> class
45  * invokes methods from this class in order to delegate operations
46  * (painting, layout calculations, etc.) that may vary depending on the
47  * look and feel installed.  <b>Client programs should not invoke methods
48  * on this class directly.</b>
49  *
50  * @see javax.swing.JComponent
51  * @see javax.swing.UIManager
52  *
53  */
54 public abstract class ComponentUI {
55     /**
56      * Sole constructor. (For invocation by subclass constructors,
57      * typically implicit.)
58      */
ComponentUI()59     public ComponentUI() {
60     }
61 
62     /**
63      * Configures the specified component appropriately for the look and feel.
64      * This method is invoked when the <code>ComponentUI</code> instance is being installed
65      * as the UI delegate on the specified component.  This method should
66      * completely configure the component for the look and feel,
67      * including the following:
68      * <ol>
69      * <li>Install default property values for color, fonts, borders,
70      *     icons, opacity, etc. on the component.  Whenever possible,
71      *     property values initialized by the client program should <i>not</i>
72      *     be overridden.
73      * <li>Install a <code>LayoutManager</code> on the component if necessary.
74      * <li>Create/add any required sub-components to the component.
75      * <li>Create/install event listeners on the component.
76      * <li>Create/install a <code>PropertyChangeListener</code> on the component in order
77      *     to detect and respond to component property changes appropriately.
78      * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
79      * <li>Initialize any appropriate instance data.
80      * </ol>
81      * @param c the component where this UI delegate is being installed
82      *
83      * @see #uninstallUI
84      * @see javax.swing.JComponent#setUI
85      * @see javax.swing.JComponent#updateUI
86      */
installUI(JComponent c)87     public void installUI(JComponent c) {
88     }
89 
90     /**
91      * Reverses configuration which was done on the specified component during
92      * <code>installUI</code>.  This method is invoked when this
93      * <code>UIComponent</code> instance is being removed as the UI delegate
94      * for the specified component.  This method should undo the
95      * configuration performed in <code>installUI</code>, being careful to
96      * leave the <code>JComponent</code> instance in a clean state (no
97      * extraneous listeners, look-and-feel-specific property objects, etc.).
98      * This should include the following:
99      * <ol>
100      * <li>Remove any UI-set borders from the component.
101      * <li>Remove any UI-set layout managers on the component.
102      * <li>Remove any UI-added sub-components from the component.
103      * <li>Remove any UI-added event/property listeners from the component.
104      * <li>Remove any UI-installed keyboard UI from the component.
105      * <li>Nullify any allocated instance data objects to allow for GC.
106      * </ol>
107      * @param c the component from which this UI delegate is being removed;
108      *          this argument is often ignored,
109      *          but might be used if the UI object is stateless
110      *          and shared by multiple components
111      *
112      * @see #installUI
113      * @see javax.swing.JComponent#updateUI
114      */
uninstallUI(JComponent c)115     public void uninstallUI(JComponent c) {
116     }
117 
118     /**
119      * Paints the specified component appropriately for the look and feel.
120      * This method is invoked from the <code>ComponentUI.update</code> method when
121      * the specified component is being painted.  Subclasses should override
122      * this method and use the specified <code>Graphics</code> object to
123      * render the content of the component.
124      *
125      * @param g the <code>Graphics</code> context in which to paint
126      * @param c the component being painted;
127      *          this argument is often ignored,
128      *          but might be used if the UI object is stateless
129      *          and shared by multiple components
130      *
131      * @see #update
132      */
paint(Graphics g, JComponent c)133     public void paint(Graphics g, JComponent c) {
134     }
135 
136     /**
137      * Notifies this UI delegate that it is time to paint the specified
138      * component.  This method is invoked by <code>JComponent</code>
139      * when the specified component is being painted.
140      *
141      * <p>By default this method fills the specified component with
142      * its background color if its {@code opaque} property is {@code true},
143      * and then immediately calls {@code paint}. In general this method need
144      * not be overridden by subclasses; all look-and-feel rendering code should
145      * reside in the {@code paint} method.
146      *
147      * @param g the <code>Graphics</code> context in which to paint
148      * @param c the component being painted;
149      *          this argument is often ignored,
150      *          but might be used if the UI object is stateless
151      *          and shared by multiple components
152      *
153      * @see #paint
154      * @see javax.swing.JComponent#paintComponent
155      */
update(Graphics g, JComponent c)156     public void update(Graphics g, JComponent c) {
157         if (c.isOpaque()) {
158             g.setColor(c.getBackground());
159             g.fillRect(0, 0, c.getWidth(),c.getHeight());
160         }
161         paint(g, c);
162     }
163 
164     /**
165      * Returns the specified component's preferred size appropriate for
166      * the look and feel.  If <code>null</code> is returned, the preferred
167      * size will be calculated by the component's layout manager instead
168      * (this is the preferred approach for any component with a specific
169      * layout manager installed).  The default implementation of this
170      * method returns <code>null</code>.
171      *
172      * @param c the component whose preferred size is being queried;
173      *          this argument is often ignored,
174      *          but might be used if the UI object is stateless
175      *          and shared by multiple components
176      *
177      * @see javax.swing.JComponent#getPreferredSize
178      * @see java.awt.LayoutManager#preferredLayoutSize
179      */
getPreferredSize(JComponent c)180     public Dimension getPreferredSize(JComponent c) {
181         return null;
182     }
183 
184     /**
185      * Returns the specified component's minimum size appropriate for
186      * the look and feel.  If <code>null</code> is returned, the minimum
187      * size will be calculated by the component's layout manager instead
188      * (this is the preferred approach for any component with a specific
189      * layout manager installed).  The default implementation of this
190      * method invokes <code>getPreferredSize</code> and returns that value.
191      *
192      * @param c the component whose minimum size is being queried;
193      *          this argument is often ignored,
194      *          but might be used if the UI object is stateless
195      *          and shared by multiple components
196      *
197      * @return a <code>Dimension</code> object or <code>null</code>
198      *
199      * @see javax.swing.JComponent#getMinimumSize
200      * @see java.awt.LayoutManager#minimumLayoutSize
201      * @see #getPreferredSize
202      */
getMinimumSize(JComponent c)203     public Dimension getMinimumSize(JComponent c) {
204         return getPreferredSize(c);
205     }
206 
207     /**
208      * Returns the specified component's maximum size appropriate for
209      * the look and feel.  If <code>null</code> is returned, the maximum
210      * size will be calculated by the component's layout manager instead
211      * (this is the preferred approach for any component with a specific
212      * layout manager installed).  The default implementation of this
213      * method invokes <code>getPreferredSize</code> and returns that value.
214      *
215      * @param c the component whose maximum size is being queried;
216      *          this argument is often ignored,
217      *          but might be used if the UI object is stateless
218      *          and shared by multiple components
219      * @return a <code>Dimension</code> object or <code>null</code>
220      *
221      * @see javax.swing.JComponent#getMaximumSize
222      * @see java.awt.LayoutManager2#maximumLayoutSize
223      */
getMaximumSize(JComponent c)224     public Dimension getMaximumSize(JComponent c) {
225         return getPreferredSize(c);
226     }
227 
228     /**
229      * Returns <code>true</code> if the specified <i>x,y</i> location is
230      * contained within the look and feel's defined shape of the specified
231      * component. <code>x</code> and <code>y</code> are defined to be relative
232      * to the coordinate system of the specified component.  Although
233      * a component's <code>bounds</code> is constrained to a rectangle,
234      * this method provides the means for defining a non-rectangular
235      * shape within those bounds for the purpose of hit detection.
236      *
237      * @param c the component where the <i>x,y</i> location is being queried;
238      *          this argument is often ignored,
239      *          but might be used if the UI object is stateless
240      *          and shared by multiple components
241      * @param x the <i>x</i> coordinate of the point
242      * @param y the <i>y</i> coordinate of the point
243      *
244      * @see javax.swing.JComponent#contains
245      * @see java.awt.Component#contains
246      */
247     @SuppressWarnings("deprecation")
contains(JComponent c, int x, int y)248     public boolean contains(JComponent c, int x, int y) {
249         return c.inside(x, y);
250     }
251 
252     /**
253      * Returns an instance of the UI delegate for the specified component.
254      * Each subclass must provide its own static <code>createUI</code>
255      * method that returns an instance of that UI delegate subclass.
256      * If the UI delegate subclass is stateless, it may return an instance
257      * that is shared by multiple components.  If the UI delegate is
258      * stateful, then it should return a new instance per component.
259      * The default implementation of this method throws an error, as it
260      * should never be invoked.
261      */
createUI(JComponent c)262     public static ComponentUI createUI(JComponent c) {
263         throw new Error("ComponentUI.createUI not implemented.");
264     }
265 
266     /**
267      * Returns the baseline.  The baseline is measured from the top of
268      * the component.  This method is primarily meant for
269      * <code>LayoutManager</code>s to align components along their
270      * baseline.  A return value less than 0 indicates this component
271      * does not have a reasonable baseline and that
272      * <code>LayoutManager</code>s should not align this component on
273      * its baseline.
274      * <p>
275      * This method returns -1.  Subclasses that have a meaningful baseline
276      * should override appropriately.
277      *
278      * @param c <code>JComponent</code> baseline is being requested for
279      * @param width the width to get the baseline for
280      * @param height the height to get the baseline for
281      * @throws NullPointerException if <code>c</code> is <code>null</code>
282      * @throws IllegalArgumentException if width or height is &lt; 0
283      * @return baseline or a value &lt; 0 indicating there is no reasonable
284      *                  baseline
285      * @see javax.swing.JComponent#getBaseline(int,int)
286      * @since 1.6
287      */
getBaseline(JComponent c, int width, int height)288     public int getBaseline(JComponent c, int width, int height) {
289         if (c == null) {
290             throw new NullPointerException("Component must be non-null");
291         }
292         if (width < 0 || height < 0) {
293             throw new IllegalArgumentException(
294                     "Width and height must be >= 0");
295         }
296         return -1;
297     }
298 
299     /**
300      * Returns an enum indicating how the baseline of he component
301      * changes as the size changes.  This method is primarily meant for
302      * layout managers and GUI builders.
303      * <p>
304      * This method returns <code>BaselineResizeBehavior.OTHER</code>.
305      * Subclasses that support a baseline should override appropriately.
306      *
307      * @param c <code>JComponent</code> to return baseline resize behavior for
308      * @return an enum indicating how the baseline changes as the component
309      *         size changes
310      * @throws NullPointerException if <code>c</code> is <code>null</code>
311      * @see javax.swing.JComponent#getBaseline(int, int)
312      * @since 1.6
313      */
getBaselineResizeBehavior( JComponent c)314     public Component.BaselineResizeBehavior getBaselineResizeBehavior(
315             JComponent c) {
316         if (c == null) {
317             throw new NullPointerException("Component must be non-null");
318         }
319         return Component.BaselineResizeBehavior.OTHER;
320     }
321 
322     /**
323      * Returns the number of accessible children in the object.  If all
324      * of the children of this object implement <code>Accessible</code>,
325      * this
326      * method should return the number of children of this object.
327      * UIs might wish to override this if they present areas on the
328      * screen that can be viewed as components, but actual components
329      * are not used for presenting those areas.
330      *
331      * Note: As of v1.3, it is recommended that developers call
332      * <code>Component.AccessibleAWTComponent.getAccessibleChildrenCount()</code> instead
333      * of this method.
334      *
335      * @see #getAccessibleChild
336      * @return the number of accessible children in the object
337      */
getAccessibleChildrenCount(JComponent c)338     public int getAccessibleChildrenCount(JComponent c) {
339         return SwingUtilities.getAccessibleChildrenCount(c);
340     }
341 
342     /**
343      * Returns the <code>i</code>th <code>Accessible</code> child of the object.
344      * UIs might need to override this if they present areas on the
345      * screen that can be viewed as components, but actual components
346      * are not used for presenting those areas.
347      *
348      * <p>
349      *
350      * Note: As of v1.3, it is recommended that developers call
351      * <code>Component.AccessibleAWTComponent.getAccessibleChild()</code> instead of
352      * this method.
353      *
354      * @see #getAccessibleChildrenCount
355      * @param i zero-based index of child
356      * @return the <code>i</code>th <code>Accessible</code> child of the object
357      */
getAccessibleChild(JComponent c, int i)358     public Accessible getAccessibleChild(JComponent c, int i) {
359         return SwingUtilities.getAccessibleChild(c, i);
360     }
361 }
362