001: /*
002: * @(#)JCellRendererPane.java 10/25/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.accessibility.Accessible;
009: import javax.accessibility.AccessibleContext;
010: import javax.accessibility.AccessibleRole;
011: import javax.swing.*;
012: import java.awt.*;
013: import java.io.IOException;
014: import java.io.ObjectOutputStream;
015:
016: /**
017: * Copied from CellRendererPane and make it extending JComponent so that tooltips of renderer works.
018: */
019: public class JCellRendererPane extends JComponent implements Accessible {
020: /**
021: * Construct a CellRendererPane object.
022: */
023: public JCellRendererPane() {
024: super ();
025: setLayout(null);
026: setVisible(false);
027: }
028:
029: /**
030: * Overridden to avoid propagating a invalidate up the tree when the
031: * cell renderer child is configured.
032: */
033: @Override
034: public void invalidate() {
035: }
036:
037: /**
038: * Shouldn't be called.
039: */
040: @Override
041: public void paint(Graphics g) {
042: }
043:
044: /**
045: * Shouldn't be called.
046: */
047: @Override
048: public void update(Graphics g) {
049: }
050:
051: /**
052: * If the specified component is already a child of this then we don't
053: * bother doing anything - stacking order doesn't matter for cell
054: * renderer components (CellRendererPane doesn't paint anyway).<
055: */
056: @Override
057: protected void addImpl(Component x, Object constraints, int index) {
058: if (x.getParent() == this ) {
059: return;
060: } else {
061: super .addImpl(x, constraints, index);
062: }
063: }
064:
065: /**
066: * Paint a cell renderer component c on graphics object g. Before the component
067: * is drawn it's reparented to this (if that's necessary), it's bounds
068: * are set to w,h and the graphics object is (effectively) translated to x,y.
069: * If it's a JComponent, double buffering is temporarily turned off. After
070: * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if
071: * it's the last renderer component painted, it will not start consuming input.
072: * The Container p is the component we're actually drawing on, typically it's
073: * equal to this.getParent(). If shouldValidate is true the component c will be
074: * validated before painted.
075: */
076: public void paintComponent(Graphics g, Component c, Container p,
077: int x, int y, int w, int h, boolean shouldValidate) {
078: if (c == null) {
079: if (p != null) {
080: Color oldColor = g.getColor();
081: g.setColor(p.getBackground());
082: g.fillRect(x, y, w, h);
083: g.setColor(oldColor);
084: }
085: return;
086: }
087:
088: if (c.getParent() != this ) {
089: this .add(c);
090: }
091:
092: c.setBounds(x, y, w, h);
093:
094: if (shouldValidate) {
095: c.validate();
096: }
097:
098: boolean wasDoubleBuffered = false;
099: if ((c instanceof JComponent)
100: && ((JComponent) c).isDoubleBuffered()) {
101: wasDoubleBuffered = true;
102: ((JComponent) c).setDoubleBuffered(false);
103: }
104:
105: Graphics cg = g.create(x, y, w, h);
106: try {
107: c.paint(cg);
108: } finally {
109: cg.dispose();
110: }
111:
112: if (wasDoubleBuffered && (c instanceof JComponent)) {
113: ((JComponent) c).setDoubleBuffered(true);
114: }
115:
116: c.setBounds(-w, -h, 0, 0);
117: }
118:
119: /**
120: * Calls this.paintComponent(g, c, p, x, y, w, h, false).
121: */
122: public void paintComponent(Graphics g, Component c, Container p,
123: int x, int y, int w, int h) {
124: paintComponent(g, c, p, x, y, w, h, false);
125: }
126:
127: /**
128: * Calls this.paintComponent() with the rectangles x,y,width,height fields.
129: */
130: public void paintComponent(Graphics g, Component c, Container p,
131: Rectangle r) {
132: paintComponent(g, c, p, r.x, r.y, r.width, r.height);
133: }
134:
135: private void writeObject(ObjectOutputStream s) throws IOException {
136: removeAll();
137: s.defaultWriteObject();
138: }
139:
140: /////////////////
141: // Accessibility support
142: ////////////////
143:
144: protected AccessibleContext accessibleContext = null;
145:
146: /**
147: * Gets the AccessibleContext associated with this CellRendererPane.
148: * For CellRendererPanes, the AccessibleContext takes the form of an
149: * AccessibleCellRendererPane.
150: * A new AccessibleCellRendererPane instance is created if necessary.
151: *
152: * @return an AccessibleCellRendererPane that serves as the
153: * AccessibleContext of this CellRendererPane
154: */
155: @Override
156: public AccessibleContext getAccessibleContext() {
157: if (accessibleContext == null) {
158: accessibleContext = new AccessibleCellRendererPane();
159: }
160: return accessibleContext;
161: }
162:
163: /**
164: * This class implements accessibility support for the
165: * <code>CellRendererPane</code> class.
166: */
167: protected class AccessibleCellRendererPane extends
168: AccessibleAWTContainer {
169: // AccessibleContext methods
170: //
171: /**
172: * Get the role of this object.
173: *
174: * @return an instance of AccessibleRole describing the role of the
175: * object
176: * @see javax.accessibility.AccessibleRole
177: */
178: @Override
179: public AccessibleRole getAccessibleRole() {
180: return AccessibleRole.PANEL;
181: }
182: } // inner class AccessibleCellRendererPane
183: }
|