KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > SchemaComponentNodeRenderer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.nodes;
21
22 import java.awt.AlphaComposite JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Graphics2D JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.Point JavaDoc;
30 import java.awt.Rectangle JavaDoc;
31 import java.awt.event.ComponentListener JavaDoc;
32 import java.awt.event.ContainerListener JavaDoc;
33 import java.awt.event.FocusListener JavaDoc;
34 import java.awt.event.HierarchyBoundsListener JavaDoc;
35 import java.awt.event.HierarchyListener JavaDoc;
36 import java.awt.event.InputMethodListener JavaDoc;
37 import java.awt.event.MouseListener JavaDoc;
38 import java.awt.event.MouseMotionListener JavaDoc;
39 import java.awt.event.MouseWheelListener JavaDoc;
40 import java.beans.PropertyChangeListener JavaDoc;
41 import java.beans.VetoableChangeListener JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JTree JavaDoc;
44 import javax.swing.border.Border JavaDoc;
45 import javax.swing.event.AncestorListener JavaDoc;
46 import org.openide.explorer.view.NodeRenderer;
47
48 /**
49  * Leave this class alone for now--I may come back to it later to spruce up
50  * the rendering of each tree node
51  *
52  * @author Todd Fast, todd.fast@sun.com
53  */

54 public class SchemaComponentNodeRenderer extends NodeRenderer
55 {
56     /**
57      *
58      *
59      */

60     public SchemaComponentNodeRenderer()
61     {
62         super();
63     }
64
65
66     /**
67      *
68      *
69      */

70     public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value,
71         boolean selected, boolean expanded, boolean leaf, int row,
72         boolean hasFocus)
73     {
74         JComponent JavaDoc component=(JComponent JavaDoc)
75             super.getTreeCellRendererComponent(tree,value,
76                 selected,expanded,leaf,row,hasFocus);
77         return new WrapperComponent(tree,component,true);
78     }
79
80
81
82
83     ////////////////////////////////////////////////////////////////////////////
84
// Inner class
85
////////////////////////////////////////////////////////////////////////////
86

87     /**
88      *
89      *
90      */

91     protected class WrapperComponent extends JComponent JavaDoc
92     {
93             static final long serialVersionUID = 1L;
94         /**
95          *
96          *
97          */

98         public WrapperComponent(JTree JavaDoc tree, JComponent JavaDoc target,
99             boolean hasDetail)
100         {
101             super();
102             this.tree=tree;
103             this.target=target;
104             this.hasDetail=hasDetail;
105         }
106
107
108         /**
109          *
110          *
111          */

112         public void paint(Graphics JavaDoc g)
113         {
114             target.paint(g);
115
116             Graphics2D JavaDoc g2d=(Graphics2D JavaDoc)g;
117
118             if (!hasDetail)
119                 return;
120
121             Rectangle JavaDoc clip=g2d.getClipBounds();
122
123             int newWidth = tree.getWidth() - (int) clip.getX();
124
125             g2d.setClip(0 /*(int)clip.getX()*/,(int)clip.getY(),
126                 newWidth /*(int)clip.getWidth()+10*/,(int)clip.getHeight());
127
128             // Draw the "has detail" triangle widget
129
final int INSET=6;
130             final int WIDTH=newWidth; //getWidth()+8;
131
final int HEIGHT=getHeight();
132             final int HEIGHT_CORRECTION=HEIGHT % 2 + 1;
133             final int SIZE=HEIGHT-2*INSET;
134             final int X=WIDTH-SIZE-INSET;
135
136             // This value must be odd
137
final int STEPS=HEIGHT - 2*INSET + HEIGHT_CORRECTION;
138
139             // Draw the shadow with 33% opacity
140
g2d.setComposite(
141                 AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.33f));
142
143             // Draw the shadow
144
int right=0;
145             for (int i=0; i<STEPS+1; i++)
146             {
147                 final int X_OFFSET=-1;
148                 final int Y_OFFSET=-1;
149
150                 if (i==0)
151                 {
152                     g.drawLine(X+X_OFFSET,INSET+i+Y_OFFSET-1,
153                         X+right+X_OFFSET,INSET+i+Y_OFFSET-1);
154                 }
155
156                 right+=(i <= STEPS/2) ? 2 : -2;
157                 g.drawLine(X+X_OFFSET,INSET+i+Y_OFFSET,
158                     X+right+X_OFFSET,INSET+i+Y_OFFSET);
159
160                 if (i==STEPS)
161                 {
162                     g.drawLine(X+X_OFFSET,INSET+i+Y_OFFSET+1,
163                         X+right+X_OFFSET,INSET+i+Y_OFFSET+1);
164                 }
165             }
166
167             // Draw widget with 100% opacity
168
g2d.setComposite(
169                 AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));
170
171             int color=255;
172             final int COLOR_INC=color/6/STEPS;
173
174             // Draw the triangle
175
right=-1;
176             for (int i=0; i<STEPS; i++)
177             {
178                 g.setColor(new Color JavaDoc(color,color,color));
179                 color-=COLOR_INC;
180
181                 right+=(i <= STEPS/2) ? 2 : -2;
182                 g.drawLine(X,INSET+i,X+right,INSET+i);
183             }
184         }
185
186         public void addNotify() {
187             target.addNotify();
188         }
189
190         public void removeNotify() {
191             target.removeNotify();
192         }
193
194         public Dimension JavaDoc getPreferredSize() {
195             return target.getPreferredSize();
196         }
197
198 // public String getText() {
199
// return target.getText();
200
// }
201

202         public Border JavaDoc getBorder() {
203             return target.getBorder();
204         }
205
206         public void setBorder(Border JavaDoc b) {
207             target.setBorder(b);
208         }
209
210         public Insets JavaDoc getInsets() {
211             return target.getInsets();
212         }
213
214         public void setEnabled(boolean b) {
215             target.setEnabled(b);
216         }
217
218         public boolean isEnabled() {
219             return target.isEnabled();
220         }
221
222         public void updateUI() {
223             target.updateUI();
224         }
225
226         public Graphics JavaDoc getGraphics() {
227             return target.getGraphics();
228         }
229
230         public Rectangle JavaDoc getBounds() {
231             return target.getBounds();
232         }
233
234         public void setBounds(int x, int y, int w, int h) {
235             target.setBounds(x,y,w,h);
236         }
237
238                 @SuppressWarnings JavaDoc("deprecation")
239         public void reshape(int x, int y, int w, int h) {
240             target.reshape(x,y,w,h);
241         }
242
243         public int getWidth() {
244             return target.getWidth();
245         }
246
247         public int getHeight() {
248             return target.getHeight();
249         }
250
251         public Point JavaDoc getLocation() {
252             return target.getLocation();
253         }
254
255         public void validate() {
256             target.validate();
257         }
258
259 // public void repaint(long tm, int x, int y, int w, int h) {
260
// target.repaint(tm,x,y,w,h);
261
// }
262
//
263
// public void repaint() {
264
// target.repaint();
265
// }
266

267         public void invalidate() {
268             target.invalidate();
269         }
270
271         public void revalidate() {
272             target.revalidate();
273         }
274
275         public void addAncestorListener(AncestorListener JavaDoc l) {
276             target.addAncestorListener(l);
277         }
278
279         public void addComponentListener(ComponentListener JavaDoc l) {
280             target.addComponentListener(l);
281         }
282
283         public void addContainerListener(ContainerListener JavaDoc l) {
284             target.addContainerListener(l);
285         }
286
287         public void addHierarchyListener(HierarchyListener JavaDoc l) {
288             target.addHierarchyListener(l);
289         }
290
291         public void addHierarchyBoundsListener(HierarchyBoundsListener JavaDoc l) {
292             target.addHierarchyBoundsListener(l);
293         }
294
295         public void addInputMethodListener(InputMethodListener JavaDoc l) {
296             target.addInputMethodListener(l);
297         }
298
299         public void addFocusListener(FocusListener JavaDoc fl) {
300             target.addFocusListener(fl);
301         }
302
303         public void addMouseListener(MouseListener JavaDoc ml) {
304             target.addMouseListener(ml);
305         }
306
307         public void addMouseWheelListener(MouseWheelListener JavaDoc ml) {
308             target.addMouseWheelListener(ml);
309         }
310
311         public void addMouseMotionListener(MouseMotionListener JavaDoc ml) {
312             target.addMouseMotionListener(ml);
313         }
314
315         public void addVetoableChangeListener(VetoableChangeListener JavaDoc vl) {
316             target.addVetoableChangeListener(vl);
317         }
318
319         public void addPropertyChangeListener(String JavaDoc s, PropertyChangeListener JavaDoc l) {
320             target.addPropertyChangeListener(s,l);
321         }
322
323         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
324             target.addPropertyChangeListener(l);
325         }
326
327         private JTree JavaDoc tree;
328         private JComponent JavaDoc target;
329         private boolean hasDetail;
330     }
331 }
332
Popular Tags