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