001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema.ui.nodes;
043:
044: import java.awt.AlphaComposite;
045: import java.awt.Color;
046: import java.awt.Component;
047: import java.awt.Dimension;
048: import java.awt.Graphics;
049: import java.awt.Graphics2D;
050: import java.awt.Insets;
051: import java.awt.Point;
052: import java.awt.Rectangle;
053: import java.awt.event.ComponentListener;
054: import java.awt.event.ContainerListener;
055: import java.awt.event.FocusListener;
056: import java.awt.event.HierarchyBoundsListener;
057: import java.awt.event.HierarchyListener;
058: import java.awt.event.InputMethodListener;
059: import java.awt.event.MouseListener;
060: import java.awt.event.MouseMotionListener;
061: import java.awt.event.MouseWheelListener;
062: import java.beans.PropertyChangeListener;
063: import java.beans.VetoableChangeListener;
064: import javax.swing.JComponent;
065: import javax.swing.JTree;
066: import javax.swing.border.Border;
067: import javax.swing.event.AncestorListener;
068: import org.openide.explorer.view.NodeRenderer;
069:
070: /**
071: * Leave this class alone for now--I may come back to it later to spruce up
072: * the rendering of each tree node
073: *
074: * @author Todd Fast, todd.fast@sun.com
075: */
076: public class SchemaComponentNodeRenderer extends NodeRenderer {
077: /**
078: *
079: *
080: */
081: public SchemaComponentNodeRenderer() {
082: super ();
083: }
084:
085: /**
086: *
087: *
088: */
089: public Component getTreeCellRendererComponent(JTree tree,
090: Object value, boolean selected, boolean expanded,
091: boolean leaf, int row, boolean hasFocus) {
092: JComponent component = (JComponent) super
093: .getTreeCellRendererComponent(tree, value, selected,
094: expanded, leaf, row, hasFocus);
095: return new WrapperComponent(tree, component, true);
096: }
097:
098: ////////////////////////////////////////////////////////////////////////////
099: // Inner class
100: ////////////////////////////////////////////////////////////////////////////
101:
102: /**
103: *
104: *
105: */
106: protected class WrapperComponent extends JComponent {
107: static final long serialVersionUID = 1L;
108:
109: /**
110: *
111: *
112: */
113: public WrapperComponent(JTree tree, JComponent target,
114: boolean hasDetail) {
115: super ();
116: this .tree = tree;
117: this .target = target;
118: this .hasDetail = hasDetail;
119: }
120:
121: /**
122: *
123: *
124: */
125: public void paint(Graphics g) {
126: target.paint(g);
127:
128: Graphics2D g2d = (Graphics2D) g;
129:
130: if (!hasDetail)
131: return;
132:
133: Rectangle clip = g2d.getClipBounds();
134:
135: int newWidth = tree.getWidth() - (int) clip.getX();
136:
137: g2d.setClip(0 /*(int)clip.getX()*/, (int) clip.getY(),
138: newWidth /*(int)clip.getWidth()+10*/, (int) clip
139: .getHeight());
140:
141: // Draw the "has detail" triangle widget
142: final int INSET = 6;
143: final int WIDTH = newWidth; //getWidth()+8;
144: final int HEIGHT = getHeight();
145: final int HEIGHT_CORRECTION = HEIGHT % 2 + 1;
146: final int SIZE = HEIGHT - 2 * INSET;
147: final int X = WIDTH - SIZE - INSET;
148:
149: // This value must be odd
150: final int STEPS = HEIGHT - 2 * INSET + HEIGHT_CORRECTION;
151:
152: // Draw the shadow with 33% opacity
153: g2d.setComposite(AlphaComposite.getInstance(
154: AlphaComposite.SRC_OVER, 0.33f));
155:
156: // Draw the shadow
157: int right = 0;
158: for (int i = 0; i < STEPS + 1; i++) {
159: final int X_OFFSET = -1;
160: final int Y_OFFSET = -1;
161:
162: if (i == 0) {
163: g.drawLine(X + X_OFFSET, INSET + i + Y_OFFSET - 1,
164: X + right + X_OFFSET, INSET + i + Y_OFFSET
165: - 1);
166: }
167:
168: right += (i <= STEPS / 2) ? 2 : -2;
169: g.drawLine(X + X_OFFSET, INSET + i + Y_OFFSET, X
170: + right + X_OFFSET, INSET + i + Y_OFFSET);
171:
172: if (i == STEPS) {
173: g.drawLine(X + X_OFFSET, INSET + i + Y_OFFSET + 1,
174: X + right + X_OFFSET, INSET + i + Y_OFFSET
175: + 1);
176: }
177: }
178:
179: // Draw widget with 100% opacity
180: g2d.setComposite(AlphaComposite.getInstance(
181: AlphaComposite.SRC_OVER, 1.0f));
182:
183: int color = 255;
184: final int COLOR_INC = color / 6 / STEPS;
185:
186: // Draw the triangle
187: right = -1;
188: for (int i = 0; i < STEPS; i++) {
189: g.setColor(new Color(color, color, color));
190: color -= COLOR_INC;
191:
192: right += (i <= STEPS / 2) ? 2 : -2;
193: g.drawLine(X, INSET + i, X + right, INSET + i);
194: }
195: }
196:
197: public void addNotify() {
198: target.addNotify();
199: }
200:
201: public void removeNotify() {
202: target.removeNotify();
203: }
204:
205: public Dimension getPreferredSize() {
206: return target.getPreferredSize();
207: }
208:
209: // public String getText() {
210: // return target.getText();
211: // }
212:
213: public Border getBorder() {
214: return target.getBorder();
215: }
216:
217: public void setBorder(Border b) {
218: target.setBorder(b);
219: }
220:
221: public Insets getInsets() {
222: return target.getInsets();
223: }
224:
225: public void setEnabled(boolean b) {
226: target.setEnabled(b);
227: }
228:
229: public boolean isEnabled() {
230: return target.isEnabled();
231: }
232:
233: public void updateUI() {
234: target.updateUI();
235: }
236:
237: public Graphics getGraphics() {
238: return target.getGraphics();
239: }
240:
241: public Rectangle getBounds() {
242: return target.getBounds();
243: }
244:
245: public void setBounds(int x, int y, int w, int h) {
246: target.setBounds(x, y, w, h);
247: }
248:
249: @SuppressWarnings("deprecation")
250: public void reshape(int x, int y, int w, int h) {
251: target.reshape(x, y, w, h);
252: }
253:
254: public int getWidth() {
255: return target.getWidth();
256: }
257:
258: public int getHeight() {
259: return target.getHeight();
260: }
261:
262: public Point getLocation() {
263: return target.getLocation();
264: }
265:
266: public void validate() {
267: target.validate();
268: }
269:
270: // public void repaint(long tm, int x, int y, int w, int h) {
271: // target.repaint(tm,x,y,w,h);
272: // }
273: //
274: // public void repaint() {
275: // target.repaint();
276: // }
277:
278: public void invalidate() {
279: target.invalidate();
280: }
281:
282: public void revalidate() {
283: target.revalidate();
284: }
285:
286: public void addAncestorListener(AncestorListener l) {
287: target.addAncestorListener(l);
288: }
289:
290: public void addComponentListener(ComponentListener l) {
291: target.addComponentListener(l);
292: }
293:
294: public void addContainerListener(ContainerListener l) {
295: target.addContainerListener(l);
296: }
297:
298: public void addHierarchyListener(HierarchyListener l) {
299: target.addHierarchyListener(l);
300: }
301:
302: public void addHierarchyBoundsListener(HierarchyBoundsListener l) {
303: target.addHierarchyBoundsListener(l);
304: }
305:
306: public void addInputMethodListener(InputMethodListener l) {
307: target.addInputMethodListener(l);
308: }
309:
310: public void addFocusListener(FocusListener fl) {
311: target.addFocusListener(fl);
312: }
313:
314: public void addMouseListener(MouseListener ml) {
315: target.addMouseListener(ml);
316: }
317:
318: public void addMouseWheelListener(MouseWheelListener ml) {
319: target.addMouseWheelListener(ml);
320: }
321:
322: public void addMouseMotionListener(MouseMotionListener ml) {
323: target.addMouseMotionListener(ml);
324: }
325:
326: public void addVetoableChangeListener(VetoableChangeListener vl) {
327: target.addVetoableChangeListener(vl);
328: }
329:
330: public void addPropertyChangeListener(String s,
331: PropertyChangeListener l) {
332: target.addPropertyChangeListener(s, l);
333: }
334:
335: public void addPropertyChangeListener(PropertyChangeListener l) {
336: target.addPropertyChangeListener(l);
337: }
338:
339: private JTree tree;
340: private JComponent target;
341: private boolean hasDetail;
342: }
343: }
|