1. /*
  2. * @(#)CellRendererPane.java 1.37 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.*;
  11. import java.beans.PropertyChangeListener;
  12. import java.util.Locale;
  13. import java.util.Vector;
  14. import javax.accessibility.*;
  15. /**
  16. * This class is inserted in between cell renderers and the components that
  17. * use them. It just exists to thwart the repaint() and invalidate() methods
  18. * which would otherwise propagate up the tree when the renderer was configured.
  19. * It's used by the implementations of JTable, JTree, and JList. For example,
  20. * here's how CellRendererPane is used in the code the paints each row
  21. * in a JList:
  22. * <pre>
  23. * cellRendererPane = new CellRendererPane();
  24. * ...
  25. * Component rendererComponent = renderer.getListCellRendererComponent();
  26. * renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
  27. * cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
  28. * </pre>
  29. * <p>
  30. * A renderer component must override isShowing() and unconditionally return
  31. * true to work correctly because the Swing paint does nothing for components
  32. * with isShowing false.
  33. * <p>
  34. * <strong>Warning:</strong>
  35. * Serialized objects of this class will not be compatible with
  36. * future Swing releases. The current serialization support is
  37. * appropriate for short term storage or RMI between applications running
  38. * the same version of Swing. As of 1.4, support for long term storage
  39. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  40. * has been added to the <code>java.beans</code> package.
  41. * Please see {@link java.beans.XMLEncoder}.
  42. *
  43. * @version 1.37 01/23/03
  44. * @author Hans Muller
  45. */
  46. public class CellRendererPane extends Container implements Accessible
  47. {
  48. /**
  49. * Construct a CellRendererPane object.
  50. */
  51. public CellRendererPane() {
  52. super();
  53. setLayout(null);
  54. setVisible(false);
  55. }
  56. /**
  57. * Overridden to avoid propagating a invalidate up the tree when the
  58. * cell renderer child is configured.
  59. */
  60. public void invalidate() { }
  61. /**
  62. * Shouldn't be called.
  63. */
  64. public void paint(Graphics g) { }
  65. /**
  66. * Shouldn't be called.
  67. */
  68. public void update(Graphics g) { }
  69. /**
  70. * If the specified component is already a child of this then we don't
  71. * bother doing anything - stacking order doesn't matter for cell
  72. * renderer components (CellRendererPane doesn't paint anyway).<
  73. */
  74. protected void addImpl(Component x, Object constraints, int index) {
  75. if (x.getParent() == this) {
  76. return;
  77. }
  78. else {
  79. super.addImpl(x, constraints, index);
  80. }
  81. }
  82. /**
  83. * Paint a cell renderer component c on graphics object g. Before the component
  84. * is drawn it's reparented to this (if that's necessary), it's bounds
  85. * are set to w,h and the graphics object is (effectively) translated to x,y.
  86. * If it's a JComponent, double buffering is temporarily turned off. After
  87. * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
  88. * it's the last renderer component painted, it will not start consuming input.
  89. * The Container p is the component we're actually drawing on, typically it's
  90. * equal to this.getParent(). If shouldValidate is true the component c will be
  91. * validated before painted.
  92. */
  93. public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
  94. if (c == null) {
  95. if (p != null) {
  96. Color oldColor = g.getColor();
  97. g.setColor(p.getBackground());
  98. g.fillRect(x, y, w, h);
  99. g.setColor(oldColor);
  100. }
  101. return;
  102. }
  103. if (c.getParent() != this) {
  104. this.add(c);
  105. }
  106. c.setBounds(x, y, w, h);
  107. if(shouldValidate) {
  108. c.validate();
  109. }
  110. boolean wasDoubleBuffered = false;
  111. if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
  112. wasDoubleBuffered = true;
  113. ((JComponent)c).setDoubleBuffered(false);
  114. }
  115. Graphics cg = SwingGraphics.createSwingGraphics(g, x, y, w, h);
  116. try {
  117. c.paint(cg);
  118. }
  119. finally {
  120. cg.dispose();
  121. }
  122. if (wasDoubleBuffered && (c instanceof JComponent)) {
  123. ((JComponent)c).setDoubleBuffered(true);
  124. }
  125. c.setBounds(-w, -h, 0, 0);
  126. }
  127. /**
  128. * Calls this.paintComponent(g, c, p, x, y, w, h, false).
  129. */
  130. public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
  131. paintComponent(g, c, p, x, y, w, h, false);
  132. }
  133. /**
  134. * Calls this.paintComponent() with the rectangles x,y,width,height fields.
  135. */
  136. public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
  137. paintComponent(g, c, p, r.x, r.y, r.width, r.height);
  138. }
  139. private void writeObject(ObjectOutputStream s) throws IOException {
  140. removeAll();
  141. s.defaultWriteObject();
  142. }
  143. /////////////////
  144. // Accessibility support
  145. ////////////////
  146. protected AccessibleContext accessibleContext = null;
  147. /**
  148. * Gets the AccessibleContext associated with this CellRendererPane.
  149. * For CellRendererPanes, the AccessibleContext takes the form of an
  150. * AccessibleCellRendererPane.
  151. * A new AccessibleCellRendererPane instance is created if necessary.
  152. *
  153. * @return an AccessibleCellRendererPane that serves as the
  154. * AccessibleContext of this CellRendererPane
  155. */
  156. public AccessibleContext getAccessibleContext() {
  157. if (accessibleContext == null) {
  158. accessibleContext = new AccessibleCellRendererPane();
  159. }
  160. return accessibleContext;
  161. }
  162. /**
  163. * This class implements accessibility support for the
  164. * <code>CellRendererPane</code> class.
  165. */
  166. protected class AccessibleCellRendererPane extends AccessibleAWTContainer {
  167. // AccessibleContext methods
  168. //
  169. /**
  170. * Get the role of this object.
  171. *
  172. * @return an instance of AccessibleRole describing the role of the
  173. * object
  174. * @see AccessibleRole
  175. */
  176. public AccessibleRole getAccessibleRole() {
  177. return AccessibleRole.PANEL;
  178. }
  179. } // inner class AccessibleCellRendererPane
  180. }