KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalTreeUI


1 /*
2  * @(#)MetalTreeUI.java 1.23 03/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 javax.swing.plaf.metal;
9
10 import javax.swing.*;
11 import javax.swing.event.*;
12 import java.awt.*;
13 import java.awt.event.*;
14 import java.beans.*;
15 import java.io.*;
16 import java.util.*;
17 import javax.swing.plaf.*;
18 import javax.swing.tree.*;
19
20 import javax.swing.plaf.basic.*;
21
22 /**
23  * The metal look and feel implementation of <code>TreeUI</code>.
24  * <p>
25  * <code>MetalTreeUI</code> allows for configuring how to
26  * visually render the spacing and delineation between nodes. The following
27  * hints are supported:
28  *
29  * <table summary="Descriptions of supported hints: Angled, Horizontal, and None">
30  * <tr>
31  * <th><p align="left">Angled</p></th>
32  * <td>A line is drawn connecting the child to the parent. For handling
33  * of the root node refer to
34  * {@link javax.swing.JTree#setRootVisible} and
35  * {@link javax.swing.JTree#setShowsRootHandles}.
36  * </td>
37  * </tr>
38  * <tr>
39  * <th><p align="left">Horizontal</p></th>
40  * <td>A horizontal line is drawn dividing the children of the root node.</td>
41  * </tr>
42  * <tr>
43  * <th><p align="left">None</p></th>
44  * <td>Do not draw any visual indication between nodes.</td>
45  * </tr>
46  * </table>
47  *
48  * <p>
49  * As it is typically impratical to obtain the <code>TreeUI</code> from
50  * the <code>JTree</code> and cast to an instance of <code>MetalTreeUI</code>
51  * you enable this property via the client property
52  * <code>JTree.lineStyle</code>. For example, to switch to
53  * <code>Horizontal</code> style you would do:
54  * <code>tree.putClientProperty("JTree.lineStyle", "Horizontal");</code>
55  * <p>
56  * The default is <code>Angled</code>.
57  *
58  * @version 1.23 12/19/03
59  * @author Tom Santos
60  * @author Steve Wilson (value add stuff)
61  */

62 public class MetalTreeUI extends BasicTreeUI {
63
64     private static Color lineColor;
65   
66     private static final String JavaDoc LINE_STYLE = "JTree.lineStyle";
67
68     private static final String JavaDoc LEG_LINE_STYLE_STRING = "Angled";
69     private static final String JavaDoc HORIZ_STYLE_STRING = "Horizontal";
70     private static final String JavaDoc NO_STYLE_STRING = "None";
71
72     private static final int LEG_LINE_STYLE = 2;
73     private static final int HORIZ_LINE_STYLE = 1;
74     private static final int NO_LINE_STYLE = 0;
75
76     private int lineStyle = LEG_LINE_STYLE;
77     private PropertyChangeListener lineStyleListener = new LineListener();
78
79     // Boilerplate
80
public static ComponentUI createUI(JComponent x) {
81     return new MetalTreeUI JavaDoc();
82     }
83
84     public MetalTreeUI()
85     {
86     super();
87     }
88
89     protected int getHorizontalLegBuffer()
90       {
91     return 4;
92       }
93
94     public void installUI( JComponent c ) {
95         super.installUI( c );
96     lineColor = UIManager.getColor( "Tree.line" );
97
98     Object JavaDoc lineStyleFlag = c.getClientProperty( LINE_STYLE );
99     decodeLineStyle(lineStyleFlag);
100     c.addPropertyChangeListener(lineStyleListener);
101
102     }
103
104     public void uninstallUI( JComponent c) {
105          c.removePropertyChangeListener(lineStyleListener);
106      super.uninstallUI(c);
107     }
108
109     /** this function converts between the string passed into the client property
110       * and the internal representation (currently and int)
111       *
112       */

113     protected void decodeLineStyle(Object JavaDoc lineStyleFlag) {
114       if ( lineStyleFlag == null ||
115                     lineStyleFlag.equals(LEG_LINE_STYLE_STRING)){
116     lineStyle = LEG_LINE_STYLE; // default case
117
} else {
118       if ( lineStyleFlag.equals(NO_STYLE_STRING) ) {
119           lineStyle = NO_LINE_STYLE;
120       } else if ( lineStyleFlag.equals(HORIZ_STYLE_STRING) ) {
121           lineStyle = HORIZ_LINE_STYLE;
122       }
123       }
124
125     }
126
127     protected boolean isLocationInExpandControl(int row, int rowLevel,
128                         int mouseX, int mouseY) {
129     if(tree != null && !isLeaf(row)) {
130         int boxWidth;
131
132         if(getExpandedIcon() != null)
133         boxWidth = getExpandedIcon().getIconWidth() + 6;
134         else
135         boxWidth = 8;
136
137         Insets i = tree.getInsets();
138         int boxLeftX = (i != null) ? i.left : 0;
139
140
141         boxLeftX += (((rowLevel + depthOffset - 1) * totalChildIndent) +
142             getLeftChildIndent()) - boxWidth/2;
143
144         int boxRightX = boxLeftX + boxWidth;
145     
146         return mouseX >= boxLeftX && mouseX <= boxRightX;
147     }
148     return false;
149     }
150
151     public void paint(Graphics g, JComponent c) {
152         super.paint( g, c );
153  
154
155     // Paint the lines
156
if (lineStyle == HORIZ_LINE_STYLE && !largeModel) {
157         paintHorizontalSeparators(g,c);
158     }
159     }
160
161     protected void paintHorizontalSeparators(Graphics g, JComponent c) {
162         g.setColor( lineColor );
163
164     Rectangle clipBounds = g.getClipBounds();
165
166     int beginRow = getRowForPath(tree, getClosestPathForLocation
167                      (tree, 0, clipBounds.y));
168     int endRow = getRowForPath(tree, getClosestPathForLocation
169                  (tree, 0, clipBounds.y + clipBounds.height - 1));
170
171     if ( beginRow <= -1 || endRow <= -1 ) {
172         return;
173     }
174
175     for ( int i = beginRow; i <= endRow; ++i ) {
176         TreePath path = getPathForRow(tree, i);
177
178         if(path != null && path.getPathCount() == 2) {
179         Rectangle rowBounds = getPathBounds(tree,getPathForRow
180                               (tree, i));
181
182             // Draw a line at the top
183
if(rowBounds != null)
184             g.drawLine(clipBounds.x, rowBounds.y,
185                    clipBounds.x + clipBounds.width, rowBounds.y);
186         }
187     }
188
189     }
190
191     protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds,
192                       Insets insets, TreePath path) {
193     if (lineStyle == LEG_LINE_STYLE) {
194         super.paintVerticalPartOfLeg(g, clipBounds, insets, path);
195     }
196     }
197
198     protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds,
199                         Insets insets, Rectangle bounds,
200                         TreePath path, int row,
201                         boolean isExpanded,
202                         boolean hasBeenExpanded, boolean
203                         isLeaf) {
204     if (lineStyle == LEG_LINE_STYLE) {
205         super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds,
206                        path, row, isExpanded,
207                        hasBeenExpanded, isLeaf);
208     }
209     }
210
211     /** This class listens for changes in line style */
212     class LineListener implements PropertyChangeListener {
213         public void propertyChange(PropertyChangeEvent e) {
214         String JavaDoc name = e.getPropertyName();
215         if ( name.equals( LINE_STYLE ) ) {
216             decodeLineStyle(e.getNewValue());
217         }
218     }
219     } // end class PaletteListener
220

221 }
222
Popular Tags