KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > windows > WindowsTreeUI


1 /*
2  * @(#)WindowsTreeUI.java 1.25 06/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.windows;
9
10 import java.awt.*;
11 import java.awt.event.*;
12
13 import java.io.*;
14 import java.util.*;
15
16 import javax.swing.plaf.basic.*;
17 import javax.swing.*;
18 import javax.swing.plaf.*;
19
20 import javax.swing.tree.*;
21
22 import com.sun.java.swing.plaf.windows.TMSchema.*;
23 import com.sun.java.swing.plaf.windows.XPStyle.Skin;
24
25
26 /**
27  * A Windows tree.
28  * <p>
29  * <strong>Warning:</strong>
30  * Serialized objects of this class will not be compatible with
31  * future Swing releases. The current serialization support is appropriate
32  * for short term storage or RMI between applications running the same
33  * version of Swing. A future release of Swing will provide support for
34  * long term persistence.
35  *
36  * @version 1.25 12/19/06
37  * @author Scott Violet
38  */

39 public class WindowsTreeUI extends BasicTreeUI {
40
41     public static ComponentUI createUI( JComponent c )
42       {
43     return new WindowsTreeUI();
44       }
45
46
47     /**
48       * Ensures that the rows identified by beginRow through endRow are
49       * visible.
50       */

51     protected void ensureRowsAreVisible(int beginRow, int endRow) {
52     if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
53             Rectangle visRect = tree.getVisibleRect();
54         if(beginRow == endRow) {
55         Rectangle scrollBounds = getPathBounds(tree, getPathForRow
56                                (tree, beginRow));
57
58         if(scrollBounds != null) {
59                     scrollBounds.x = visRect.x;
60                     scrollBounds.width = visRect.width;
61             tree.scrollRectToVisible(scrollBounds);
62         }
63         }
64         else {
65         Rectangle beginRect = getPathBounds(tree, getPathForRow
66                               (tree, beginRow));
67         Rectangle testRect = beginRect;
68         int beginY = beginRect.y;
69         int maxY = beginY + visRect.height;
70
71         for(int counter = beginRow + 1; counter <= endRow; counter++) {
72             testRect = getPathBounds(tree,
73                          getPathForRow(tree, counter));
74             if((testRect.y + testRect.height) > maxY)
75             counter = endRow;
76         }
77         tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
78                           testRect.y + testRect.height-
79                           beginY));
80         }
81     }
82     }
83
84     static protected final int HALF_SIZE = 4;
85     static protected final int SIZE = 9;
86
87     /**
88      * Returns the default cell renderer that is used to do the
89      * stamping of each node.
90      */

91     protected TreeCellRenderer createDefaultCellRenderer() {
92     return new WindowsTreeCellRenderer();
93     }
94
95     /**
96      * The minus sign button icon
97      * <p>
98      * <strong>Warning:</strong>
99      * Serialized objects of this class will not be compatible with
100      * future Swing releases. The current serialization support is appropriate
101      * for short term storage or RMI between applications running the same
102      * version of Swing. A future release of Swing will provide support for
103      * long term persistence.
104      */

105     public static class ExpandedIcon implements Icon, Serializable {
106
107         static public Icon createExpandedIcon() {
108         return new ExpandedIcon();
109     }
110
111         Skin getSkin(Component c) {
112         XPStyle xp = XPStyle.getXP();
113             return (xp != null) ? xp.getSkin(c, Part.TVP_GLYPH) : null;
114     }
115
116     public void paintIcon(Component c, Graphics g, int x, int y) {
117             Skin skin = getSkin(c);
118         if (skin != null) {
119                 skin.paintSkin(g, x, y, State.OPENED);
120         return;
121         }
122
123         Color backgroundColor = c.getBackground();
124
125         if(backgroundColor != null)
126         g.setColor(backgroundColor);
127         else
128         g.setColor(Color.white);
129         g.fillRect(x, y, SIZE-1, SIZE-1);
130         g.setColor(Color.gray);
131         g.drawRect(x, y, SIZE-1, SIZE-1);
132         g.setColor(Color.black);
133         g.drawLine(x + 2, y + HALF_SIZE, x + (SIZE - 3), y + HALF_SIZE);
134     }
135
136     public int getIconWidth() {
137             Skin skin = getSkin(null);
138         return (skin != null) ? skin.getWidth() : SIZE;
139     }
140
141     public int getIconHeight() {
142             Skin skin = getSkin(null);
143         return (skin != null) ? skin.getHeight() : SIZE;
144     }
145     }
146
147     /**
148      * The plus sign button icon
149      * <p>
150      * <strong>Warning:</strong>
151      * Serialized objects of this class will not be compatible with
152      * future Swing releases. The current serialization support is appropriate
153      * for short term storage or RMI between applications running the same
154      * version of Swing. A future release of Swing will provide support for
155      * long term persistence.
156      */

157     public static class CollapsedIcon extends ExpandedIcon {
158         static public Icon createCollapsedIcon() {
159         return new CollapsedIcon();
160     }
161
162     public void paintIcon(Component c, Graphics g, int x, int y) {
163             Skin skin = getSkin(c);
164         if (skin != null) {
165                 skin.paintSkin(g, x, y, State.CLOSED);
166         } else {
167         super.paintIcon(c, g, x, y);
168         g.drawLine(x + HALF_SIZE, y + 2, x + HALF_SIZE, y + (SIZE - 3));
169         }
170     }
171     }
172
173     public class WindowsTreeCellRenderer extends DefaultTreeCellRenderer {
174
175     /**
176      * Configures the renderer based on the passed in components.
177      * The value is set from messaging the tree with
178      * <code>convertValueToText</code>, which ultimately invokes
179      * <code>toString</code> on <code>value</code>.
180      * The foreground color is set based on the selection and the icon
181      * is set based on on leaf and expanded.
182      */

183     public Component getTreeCellRendererComponent(JTree tree, Object JavaDoc value,
184                               boolean sel,
185                               boolean expanded,
186                               boolean leaf, int row,
187                               boolean hasFocus) {
188         super.getTreeCellRendererComponent(tree, value, sel,
189                            expanded, leaf, row,
190                            hasFocus);
191         // Windows displays the open icon when the tree item selected.
192
if (!tree.isEnabled()) {
193         setEnabled(false);
194         if (leaf) {
195             setDisabledIcon(getLeafIcon());
196         } else if (sel) {
197             setDisabledIcon(getOpenIcon());
198         } else {
199             setDisabledIcon(getClosedIcon());
200         }
201         }
202         else {
203         setEnabled(true);
204         if (leaf) {
205             setIcon(getLeafIcon());
206         } else if (sel) {
207             setIcon(getOpenIcon());
208         } else {
209             setIcon(getClosedIcon());
210         }
211         }
212         return this;
213     }
214     
215     }
216
217 }
218
Popular Tags