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. package javax.swing.plaf;
  8. import javax.swing.JComponent;
  9. import javax.swing.SwingUtilities;
  10. import javax.accessibility.Accessible;
  11. import java.awt.Container;
  12. import java.awt.Dimension;
  13. import java.awt.Graphics;
  14. import java.awt.Insets;
  15. /**
  16. * The base class for all UI delegate objects in the Swing pluggable
  17. * look and feel architecture. The UI delegate object for a Swing
  18. * component is responsible for implementing the aspects of the
  19. * component that depend on the look and feel.
  20. * The <code>JComponent</code> class
  21. * invokes methods from this class in order to delegate operations
  22. * (painting, layout calculations, etc.) that may vary depending on the
  23. * look and feel installed. <b>Client programs should not invoke methods
  24. * on this class directly.</b>
  25. *
  26. * @see javax.swing.JComponent
  27. * @see javax.swing.UIManager
  28. *
  29. */
  30. public abstract class ComponentUI {
  31. /**
  32. * Sole constructor. (For invocation by subclass constructors,
  33. * typically implicit.)
  34. */
  35. public ComponentUI() {
  36. }
  37. /**
  38. * Configures the specified component appropriate for the look and feel.
  39. * This method is invoked when the <code>ComponentUI</code> instance is being installed
  40. * as the UI delegate on the specified component. This method should
  41. * completely configure the component for the look and feel,
  42. * including the following:
  43. * <ol>
  44. * <li>Install any default property values for color, fonts, borders,
  45. * icons, opacity, etc. on the component. Whenever possible,
  46. * property values initialized by the client program should <i>not</i>
  47. * be overridden.
  48. * <li>Install a <code>LayoutManager</code> on the component if necessary.
  49. * <li>Create/add any required sub-components to the component.
  50. * <li>Create/install event listeners on the component.
  51. * <li>Create/install a <code>PropertyChangeListener</code> on the component in order
  52. * to detect and respond to component property changes appropriately.
  53. * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
  54. * <li>Initialize any appropriate instance data.
  55. * </ol>
  56. * @param c the component where this UI delegate is being installed
  57. *
  58. * @see #uninstallUI
  59. * @see javax.swing.JComponent#setUI
  60. * @see javax.swing.JComponent#updateUI
  61. */
  62. public void installUI(JComponent c) {
  63. }
  64. /**
  65. * Reverses configuration which was done on the specified component during
  66. * <code>installUI</code>. This method is invoked when this
  67. * <code>UIComponent</code> instance is being removed as the UI delegate
  68. * for the specified component. This method should undo the
  69. * configuration performed in <code>installUI</code>, being careful to
  70. * leave the <code>JComponent</code> instance in a clean state (no
  71. * extraneous listeners, look-and-feel-specific property objects, etc.).
  72. * This should include the following:
  73. * <ol>
  74. * <li>Remove any UI-set borders from the component.
  75. * <li>Remove any UI-set layout managers on the component.
  76. * <li>Remove any UI-added sub-components from the component.
  77. * <li>Remove any UI-added event/property listeners from the component.
  78. * <li>Remove any UI-installed keyboard UI from the component.
  79. * <li>Nullify any allocated instance data objects to allow for GC.
  80. * </ol>
  81. * @param c the component from which this UI delegate is being removed;
  82. * this argument is often ignored,
  83. * but might be used if the UI object is stateless
  84. * and shared by multiple components
  85. *
  86. * @see #installUI
  87. * @see javax.swing.JComponent#updateUI
  88. */
  89. public void uninstallUI(JComponent c) {
  90. }
  91. /**
  92. * Paints the specified component appropriate for the look and feel.
  93. * This method is invoked from the <code>ComponentUI.update</code> method when
  94. * the specified component is being painted. Subclasses should override
  95. * this method and use the specified <code>Graphics</code> object to
  96. * render the content of the component.
  97. *
  98. * @param g the <code>Graphics</code> context in which to paint
  99. * @param c the component being painted;
  100. * this argument is often ignored,
  101. * but might be used if the UI object is stateless
  102. * and shared by multiple components
  103. *
  104. * @see #update
  105. */
  106. public void paint(Graphics g, JComponent c) {
  107. }
  108. /**
  109. * Notifies this UI delegate that it's time to paint the specified
  110. * component. This method is invoked by <code>JComponent</code>
  111. * when the specified component is being painted.
  112. * By default this method will fill the specified component with
  113. * its background color (if its <code>opaque</code> property is
  114. * <code>true</code>) and then immediately call <code>paint</code>.
  115. * In general this method need not be overridden by subclasses;
  116. * all look-and-feel rendering code should reside in the <code>paint</code>
  117. * method.
  118. *
  119. * @param g the <code>Graphics</code> context in which to paint
  120. * @param c the component being painted;
  121. * this argument is often ignored,
  122. * but might be used if the UI object is stateless
  123. * and shared by multiple components
  124. *
  125. * @see #paint
  126. * @see javax.swing.JComponent#paintComponent
  127. */
  128. public void update(Graphics g, JComponent c) {
  129. if (c.isOpaque()) {
  130. g.setColor(c.getBackground());
  131. g.fillRect(0, 0, c.getWidth(),c.getHeight());
  132. }
  133. paint(g, c);
  134. }
  135. /**
  136. * Returns the specified component's preferred size appropriate for
  137. * the look and feel. If <code>null</code> is returned, the preferred
  138. * size will be calculated by the component's layout manager instead
  139. * (this is the preferred approach for any component with a specific
  140. * layout manager installed). The default implementation of this
  141. * method returns <code>null</code>.
  142. *
  143. * @param c the component whose preferred size is being queried;
  144. * this argument is often ignored,
  145. * but might be used if the UI object is stateless
  146. * and shared by multiple components
  147. *
  148. * @see javax.swing.JComponent#getPreferredSize
  149. * @see java.awt.LayoutManager#preferredLayoutSize
  150. */
  151. public Dimension getPreferredSize(JComponent c) {
  152. return null;
  153. }
  154. /**
  155. * Returns the specified component's minimum size appropriate for
  156. * the look and feel. If <code>null</code> is returned, the minimum
  157. * size will be calculated by the component's layout manager instead
  158. * (this is the preferred approach for any component with a specific
  159. * layout manager installed). The default implementation of this
  160. * method invokes <code>getPreferredSize</code> and returns that value.
  161. *
  162. * @param c the component whose minimum size is being queried;
  163. * this argument is often ignored,
  164. * but might be used if the UI object is stateless
  165. * and shared by multiple components
  166. *
  167. * @return a <code>Dimension</code> object or <code>null</code>
  168. *
  169. * @see javax.swing.JComponent#getMinimumSize
  170. * @see java.awt.LayoutManager#minimumLayoutSize
  171. * @see #getPreferredSize
  172. */
  173. public Dimension getMinimumSize(JComponent c) {
  174. return getPreferredSize(c);
  175. }
  176. /**
  177. * Returns the specified component's maximum size appropriate for
  178. * the look and feel. If <code>null</code> is returned, the maximum
  179. * size will be calculated by the component's layout manager instead
  180. * (this is the preferred approach for any component with a specific
  181. * layout manager installed). The default implementation of this
  182. * method invokes <code>getPreferredSize</code> and returns that value.
  183. *
  184. * @param c the component whose maximum size is being queried;
  185. * this argument is often ignored,
  186. * but might be used if the UI object is stateless
  187. * and shared by multiple components
  188. * @return a <code>Dimension</code> object or <code>null</code>
  189. *
  190. * @see javax.swing.JComponent#getMaximumSize
  191. * @see java.awt.LayoutManager2#maximumLayoutSize
  192. */
  193. public Dimension getMaximumSize(JComponent c) {
  194. return getPreferredSize(c);
  195. }
  196. /**
  197. * Returns <code>true</code> if the specified <i>x,y</i> location is
  198. * contained within the look and feel's defined shape of the specified
  199. * component. <code>x</code> and <code>y</code> are defined to be relative
  200. * to the coordinate system of the specified component. Although
  201. * a component's <code>bounds</code> is constrained to a rectangle,
  202. * this method provides the means for defining a non-rectangular
  203. * shape within those bounds for the purpose of hit detection.
  204. *
  205. * @param c the component where the <i>x,y</i> location is being queried;
  206. * this argument is often ignored,
  207. * but might be used if the UI object is stateless
  208. * and shared by multiple components
  209. * @param x the <i>x</i> coordinate of the point
  210. * @param y the <i>y</i> coordinate of the point
  211. *
  212. * @see javax.swing.JComponent#contains
  213. * @see java.awt.Component#contains
  214. */
  215. public boolean contains(JComponent c, int x, int y) {
  216. return c.inside(x, y);
  217. }
  218. /**
  219. * Returns an instance of the UI delegate for the specified component.
  220. * Each subclass must provide its own static <code>createUI</code>
  221. * method that returns an instance of that UI delegate subclass.
  222. * If the UI delegate subclass is stateless, it may return an instance
  223. * that is shared by multiple components. If the UI delegate is
  224. * stateful, then it should return a new instance per component.
  225. * The default implementation of this method throws an error, as it
  226. * should never be invoked.
  227. */
  228. public static ComponentUI createUI(JComponent c) {
  229. throw new Error("ComponentUI.createUI not implemented.");
  230. }
  231. /**
  232. * Returns the number of accessible children in the object. If all
  233. * of the children of this object implement <code>Accessible</code>,
  234. * this
  235. * method should return the number of children of this object.
  236. * UIs might wish to override this if they present areas on the
  237. * screen that can be viewed as components, but actual components
  238. * are not used for presenting those areas.
  239. *
  240. * Note: As of v1.3, it is recommended that developers call
  241. * <code>Component.AccessibleAWTComponent.getAccessibleChildrenCount()</code> instead
  242. * of this method.
  243. *
  244. * @see #getAccessibleChild
  245. * @return the number of accessible children in the object
  246. */
  247. public int getAccessibleChildrenCount(JComponent c) {
  248. return SwingUtilities.getAccessibleChildrenCount(c);
  249. }
  250. /**
  251. * Returns the <code>i</code>th <code>Accessible</code> child of the object.
  252. * UIs might need to override this if they present areas on the
  253. * screen that can be viewed as components, but actual components
  254. * are not used for presenting those areas.
  255. *
  256. * <p>
  257. *
  258. * Note: As of v1.3, it is recommended that developers call
  259. * <code>Component.AccessibleAWTComponent.getAccessibleChild()</code> instead of
  260. * this method.
  261. *
  262. * @see #getAccessibleChildrenCount
  263. * @param i zero-based index of child
  264. * @return the <code>i</code>th <code>Accessible</code> child of the object
  265. */
  266. public Accessible getAccessibleChild(JComponent c, int i) {
  267. return SwingUtilities.getAccessibleChild(c, i);
  268. }
  269. }