KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > ComponentUI


1 /*
2  * @(#)ComponentUI.java 1.24 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf;
9
10 import javax.swing.JComponent JavaDoc;
11 import javax.swing.SwingUtilities JavaDoc;
12 import javax.accessibility.Accessible JavaDoc;
13
14 import java.awt.Container JavaDoc;
15 import java.awt.Dimension JavaDoc;
16 import java.awt.Graphics JavaDoc;
17 import java.awt.Insets JavaDoc;
18
19
20 /**
21  * The base class for all UI delegate objects in the Swing pluggable
22  * look and feel architecture. The UI delegate object for a Swing
23  * component is responsible for implementing the aspects of the
24  * component that depend on the look and feel.
25  * The <code>JComponent</code> class
26  * invokes methods from this class in order to delegate operations
27  * (painting, layout calculations, etc.) that may vary depending on the
28  * look and feel installed. <b>Client programs should not invoke methods
29  * on this class directly.</b>
30  *
31  * @see javax.swing.JComponent
32  * @see javax.swing.UIManager
33  *
34  */

35 public abstract class ComponentUI {
36     /**
37      * Sole constructor. (For invocation by subclass constructors,
38      * typically implicit.)
39      */

40     public ComponentUI() {
41     }
42
43     /**
44      * Configures the specified component appropriate for the look and feel.
45      * This method is invoked when the <code>ComponentUI</code> instance is being installed
46      * as the UI delegate on the specified component. This method should
47      * completely configure the component for the look and feel,
48      * including the following:
49      * <ol>
50      * <li>Install any default property values for color, fonts, borders,
51      * icons, opacity, etc. on the component. Whenever possible,
52      * property values initialized by the client program should <i>not</i>
53      * be overridden.
54      * <li>Install a <code>LayoutManager</code> on the component if necessary.
55      * <li>Create/add any required sub-components to the component.
56      * <li>Create/install event listeners on the component.
57      * <li>Create/install a <code>PropertyChangeListener</code> on the component in order
58      * to detect and respond to component property changes appropriately.
59      * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
60      * <li>Initialize any appropriate instance data.
61      * </ol>
62      * @param c the component where this UI delegate is being installed
63      *
64      * @see #uninstallUI
65      * @see javax.swing.JComponent#setUI
66      * @see javax.swing.JComponent#updateUI
67      */

68     public void installUI(JComponent JavaDoc c) {
69     }
70
71     /**
72      * Reverses configuration which was done on the specified component during
73      * <code>installUI</code>. This method is invoked when this
74      * <code>UIComponent</code> instance is being removed as the UI delegate
75      * for the specified component. This method should undo the
76      * configuration performed in <code>installUI</code>, being careful to
77      * leave the <code>JComponent</code> instance in a clean state (no
78      * extraneous listeners, look-and-feel-specific property objects, etc.).
79      * This should include the following:
80      * <ol>
81      * <li>Remove any UI-set borders from the component.
82      * <li>Remove any UI-set layout managers on the component.
83      * <li>Remove any UI-added sub-components from the component.
84      * <li>Remove any UI-added event/property listeners from the component.
85      * <li>Remove any UI-installed keyboard UI from the component.
86      * <li>Nullify any allocated instance data objects to allow for GC.
87      * </ol>
88      * @param c the component from which this UI delegate is being removed;
89      * this argument is often ignored,
90      * but might be used if the UI object is stateless
91      * and shared by multiple components
92      *
93      * @see #installUI
94      * @see javax.swing.JComponent#updateUI
95      */

96     public void uninstallUI(JComponent JavaDoc c) {
97     }
98
99     /**
100      * Paints the specified component appropriate for the look and feel.
101      * This method is invoked from the <code>ComponentUI.update</code> method when
102      * the specified component is being painted. Subclasses should override
103      * this method and use the specified <code>Graphics</code> object to
104      * render the content of the component.
105      *
106      * @param g the <code>Graphics</code> context in which to paint
107      * @param c the component being painted;
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 #update
113      */

114     public void paint(Graphics JavaDoc g, JComponent JavaDoc c) {
115     }
116
117     /**
118      * Notifies this UI delegate that it's time to paint the specified
119      * component. This method is invoked by <code>JComponent</code>
120      * when the specified component is being painted.
121      * By default this method will fill the specified component with
122      * its background color (if its <code>opaque</code> property is
123      * <code>true</code>) and then immediately call <code>paint</code>.
124      * In general this method need not be overridden by subclasses;
125      * all look-and-feel rendering code should reside in the <code>paint</code>
126      * method.
127      *
128      * @param g the <code>Graphics</code> context in which to paint
129      * @param c the component being painted;
130      * this argument is often ignored,
131      * but might be used if the UI object is stateless
132      * and shared by multiple components
133      *
134      * @see #paint
135      * @see javax.swing.JComponent#paintComponent
136      */

137     public void update(Graphics JavaDoc g, JComponent JavaDoc c) {
138     if (c.isOpaque()) {
139         g.setColor(c.getBackground());
140         g.fillRect(0, 0, c.getWidth(),c.getHeight());
141     }
142     paint(g, c);
143     }
144
145     /**
146      * Returns the specified component's preferred size appropriate for
147      * the look and feel. If <code>null</code> is returned, the preferred
148      * size will be calculated by the component's layout manager instead
149      * (this is the preferred approach for any component with a specific
150      * layout manager installed). The default implementation of this
151      * method returns <code>null</code>.
152      *
153      * @param c the component whose preferred size is being queried;
154      * this argument is often ignored,
155      * but might be used if the UI object is stateless
156      * and shared by multiple components
157      *
158      * @see javax.swing.JComponent#getPreferredSize
159      * @see java.awt.LayoutManager#preferredLayoutSize
160      */

161     public Dimension JavaDoc getPreferredSize(JComponent JavaDoc c) {
162     return null;
163     }
164
165     /**
166      * Returns the specified component's minimum size appropriate for
167      * the look and feel. If <code>null</code> is returned, the minimum
168      * size will be calculated by the component's layout manager instead
169      * (this is the preferred approach for any component with a specific
170      * layout manager installed). The default implementation of this
171      * method invokes <code>getPreferredSize</code> and returns that value.
172      *
173      * @param c the component whose minimum size is being queried;
174      * this argument is often ignored,
175      * but might be used if the UI object is stateless
176      * and shared by multiple components
177      *
178      * @return a <code>Dimension</code> object or <code>null</code>
179      *
180      * @see javax.swing.JComponent#getMinimumSize
181      * @see java.awt.LayoutManager#minimumLayoutSize
182      * @see #getPreferredSize
183      */

184     public Dimension JavaDoc getMinimumSize(JComponent JavaDoc c) {
185     return getPreferredSize(c);
186     }
187
188     /**
189      * Returns the specified component's maximum size appropriate for
190      * the look and feel. If <code>null</code> is returned, the maximum
191      * size will be calculated by the component's layout manager instead
192      * (this is the preferred approach for any component with a specific
193      * layout manager installed). The default implementation of this
194      * method invokes <code>getPreferredSize</code> and returns that value.
195      *
196      * @param c the component whose maximum size is being queried;
197      * this argument is often ignored,
198      * but might be used if the UI object is stateless
199      * and shared by multiple components
200      * @return a <code>Dimension</code> object or <code>null</code>
201      *
202      * @see javax.swing.JComponent#getMaximumSize
203      * @see java.awt.LayoutManager2#maximumLayoutSize
204      */

205     public Dimension JavaDoc getMaximumSize(JComponent JavaDoc c) {
206     return getPreferredSize(c);
207     }
208
209     /**
210      * Returns <code>true</code> if the specified <i>x,y</i> location is
211      * contained within the look and feel's defined shape of the specified
212      * component. <code>x</code> and <code>y</code> are defined to be relative
213      * to the coordinate system of the specified component. Although
214      * a component's <code>bounds</code> is constrained to a rectangle,
215      * this method provides the means for defining a non-rectangular
216      * shape within those bounds for the purpose of hit detection.
217      *
218      * @param c the component where the <i>x,y</i> location is being queried;
219      * this argument is often ignored,
220      * but might be used if the UI object is stateless
221      * and shared by multiple components
222      * @param x the <i>x</i> coordinate of the point
223      * @param y the <i>y</i> coordinate of the point
224      *
225      * @see javax.swing.JComponent#contains
226      * @see java.awt.Component#contains
227      */

228     public boolean contains(JComponent JavaDoc c, int x, int y) {
229     return c.inside(x, y);
230     }
231
232     /**
233      * Returns an instance of the UI delegate for the specified component.
234      * Each subclass must provide its own static <code>createUI</code>
235      * method that returns an instance of that UI delegate subclass.
236      * If the UI delegate subclass is stateless, it may return an instance
237      * that is shared by multiple components. If the UI delegate is
238      * stateful, then it should return a new instance per component.
239      * The default implementation of this method throws an error, as it
240      * should never be invoked.
241      */

242     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c) {
243     throw new Error JavaDoc("ComponentUI.createUI not implemented.");
244     }
245
246     /**
247      * Returns the number of accessible children in the object. If all
248      * of the children of this object implement <code>Accessible</code>,
249      * this
250      * method should return the number of children of this object.
251      * UIs might wish to override this if they present areas on the
252      * screen that can be viewed as components, but actual components
253      * are not used for presenting those areas.
254      *
255      * Note: As of v1.3, it is recommended that developers call
256      * <code>Component.AccessibleAWTComponent.getAccessibleChildrenCount()</code> instead
257      * of this method.
258      *
259      * @see #getAccessibleChild
260      * @return the number of accessible children in the object
261      */

262     public int getAccessibleChildrenCount(JComponent JavaDoc c) {
263         return SwingUtilities.getAccessibleChildrenCount(c);
264     }
265
266     /**
267      * Returns the <code>i</code>th <code>Accessible</code> child of the object.
268      * UIs might need to override this if they present areas on the
269      * screen that can be viewed as components, but actual components
270      * are not used for presenting those areas.
271      *
272      * <p>
273      *
274      * Note: As of v1.3, it is recommended that developers call
275      * <code>Component.AccessibleAWTComponent.getAccessibleChild()</code> instead of
276      * this method.
277      *
278      * @see #getAccessibleChildrenCount
279      * @param i zero-based index of child
280      * @return the <code>i</code>th <code>Accessible</code> child of the object
281      */

282     public Accessible JavaDoc getAccessibleChild(JComponent JavaDoc c, int i) {
283         return SwingUtilities.getAccessibleChild(c, i);
284     }
285 }
286
287
Popular Tags