author | malenkov |
Tue, 29 Oct 2013 17:01:06 +0400 | |
changeset 21278 | ef8a3a2a72f2 |
parent 20451 | 4cedf4e1560a |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.metal; |
|
27 |
||
28 |
import javax.swing.*; |
|
29 |
import javax.swing.event.*; |
|
30 |
import java.awt.*; |
|
31 |
import java.awt.event.*; |
|
32 |
import java.beans.*; |
|
33 |
import java.io.*; |
|
34 |
import java.util.*; |
|
35 |
import javax.swing.plaf.*; |
|
36 |
import javax.swing.tree.*; |
|
37 |
||
38 |
import javax.swing.plaf.basic.*; |
|
39 |
||
40 |
/** |
|
41 |
* The metal look and feel implementation of <code>TreeUI</code>. |
|
42 |
* <p> |
|
43 |
* <code>MetalTreeUI</code> allows for configuring how to |
|
44 |
* visually render the spacing and delineation between nodes. The following |
|
45 |
* hints are supported: |
|
46 |
* |
|
47 |
* <table summary="Descriptions of supported hints: Angled, Horizontal, and None"> |
|
48 |
* <tr> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
49 |
* <th><p style="text-align:left">Angled</p></th> |
2 | 50 |
* <td>A line is drawn connecting the child to the parent. For handling |
51 |
* of the root node refer to |
|
52 |
* {@link javax.swing.JTree#setRootVisible} and |
|
53 |
* {@link javax.swing.JTree#setShowsRootHandles}. |
|
54 |
* </td> |
|
55 |
* </tr> |
|
56 |
* <tr> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
57 |
* <th><p style="text-align:left">Horizontal</p></th> |
2 | 58 |
* <td>A horizontal line is drawn dividing the children of the root node.</td> |
59 |
* </tr> |
|
60 |
* <tr> |
|
20451
4cedf4e1560a
8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents:
5506
diff
changeset
|
61 |
* <th><p style="text-align:left">None</p></th> |
2 | 62 |
* <td>Do not draw any visual indication between nodes.</td> |
63 |
* </tr> |
|
64 |
* </table> |
|
65 |
* |
|
66 |
* <p> |
|
21278 | 67 |
* As it is typically impractical to obtain the <code>TreeUI</code> from |
2 | 68 |
* the <code>JTree</code> and cast to an instance of <code>MetalTreeUI</code> |
69 |
* you enable this property via the client property |
|
70 |
* <code>JTree.lineStyle</code>. For example, to switch to |
|
71 |
* <code>Horizontal</code> style you would do: |
|
72 |
* <code>tree.putClientProperty("JTree.lineStyle", "Horizontal");</code> |
|
73 |
* <p> |
|
74 |
* The default is <code>Angled</code>. |
|
75 |
* |
|
76 |
* @author Tom Santos |
|
77 |
* @author Steve Wilson (value add stuff) |
|
78 |
*/ |
|
79 |
public class MetalTreeUI extends BasicTreeUI { |
|
80 |
||
81 |
private static Color lineColor; |
|
82 |
||
83 |
private static final String LINE_STYLE = "JTree.lineStyle"; |
|
84 |
||
85 |
private static final String LEG_LINE_STYLE_STRING = "Angled"; |
|
86 |
private static final String HORIZ_STYLE_STRING = "Horizontal"; |
|
87 |
private static final String NO_STYLE_STRING = "None"; |
|
88 |
||
89 |
private static final int LEG_LINE_STYLE = 2; |
|
90 |
private static final int HORIZ_LINE_STYLE = 1; |
|
91 |
private static final int NO_LINE_STYLE = 0; |
|
92 |
||
93 |
private int lineStyle = LEG_LINE_STYLE; |
|
94 |
private PropertyChangeListener lineStyleListener = new LineListener(); |
|
95 |
||
96 |
// Boilerplate |
|
97 |
public static ComponentUI createUI(JComponent x) { |
|
98 |
return new MetalTreeUI(); |
|
99 |
} |
|
100 |
||
101 |
public MetalTreeUI() |
|
102 |
{ |
|
103 |
super(); |
|
104 |
} |
|
105 |
||
106 |
protected int getHorizontalLegBuffer() |
|
107 |
{ |
|
108 |
return 3; |
|
109 |
} |
|
110 |
||
111 |
public void installUI( JComponent c ) { |
|
112 |
super.installUI( c ); |
|
113 |
lineColor = UIManager.getColor( "Tree.line" ); |
|
114 |
||
115 |
Object lineStyleFlag = c.getClientProperty( LINE_STYLE ); |
|
116 |
decodeLineStyle(lineStyleFlag); |
|
117 |
c.addPropertyChangeListener(lineStyleListener); |
|
118 |
||
119 |
} |
|
120 |
||
121 |
public void uninstallUI( JComponent c) { |
|
122 |
c.removePropertyChangeListener(lineStyleListener); |
|
123 |
super.uninstallUI(c); |
|
124 |
} |
|
125 |
||
126 |
/** this function converts between the string passed into the client property |
|
127 |
* and the internal representation (currently and int) |
|
128 |
* |
|
129 |
*/ |
|
130 |
protected void decodeLineStyle(Object lineStyleFlag) { |
|
131 |
if ( lineStyleFlag == null || |
|
132 |
lineStyleFlag.equals(LEG_LINE_STYLE_STRING)){ |
|
133 |
lineStyle = LEG_LINE_STYLE; // default case |
|
134 |
} else { |
|
135 |
if ( lineStyleFlag.equals(NO_STYLE_STRING) ) { |
|
136 |
lineStyle = NO_LINE_STYLE; |
|
137 |
} else if ( lineStyleFlag.equals(HORIZ_STYLE_STRING) ) { |
|
138 |
lineStyle = HORIZ_LINE_STYLE; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
} |
|
143 |
||
144 |
protected boolean isLocationInExpandControl(int row, int rowLevel, |
|
145 |
int mouseX, int mouseY) { |
|
146 |
if(tree != null && !isLeaf(row)) { |
|
147 |
int boxWidth; |
|
148 |
||
149 |
if(getExpandedIcon() != null) |
|
150 |
boxWidth = getExpandedIcon().getIconWidth() + 6; |
|
151 |
else |
|
152 |
boxWidth = 8; |
|
153 |
||
154 |
Insets i = tree.getInsets(); |
|
155 |
int boxLeftX = (i != null) ? i.left : 0; |
|
156 |
||
157 |
||
158 |
boxLeftX += (((rowLevel + depthOffset - 1) * totalChildIndent) + |
|
159 |
getLeftChildIndent()) - boxWidth/2; |
|
160 |
||
161 |
int boxRightX = boxLeftX + boxWidth; |
|
162 |
||
163 |
return mouseX >= boxLeftX && mouseX <= boxRightX; |
|
164 |
} |
|
165 |
return false; |
|
166 |
} |
|
167 |
||
168 |
public void paint(Graphics g, JComponent c) { |
|
169 |
super.paint( g, c ); |
|
170 |
||
171 |
||
172 |
// Paint the lines |
|
173 |
if (lineStyle == HORIZ_LINE_STYLE && !largeModel) { |
|
174 |
paintHorizontalSeparators(g,c); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
protected void paintHorizontalSeparators(Graphics g, JComponent c) { |
|
179 |
g.setColor( lineColor ); |
|
180 |
||
181 |
Rectangle clipBounds = g.getClipBounds(); |
|
182 |
||
183 |
int beginRow = getRowForPath(tree, getClosestPathForLocation |
|
184 |
(tree, 0, clipBounds.y)); |
|
185 |
int endRow = getRowForPath(tree, getClosestPathForLocation |
|
186 |
(tree, 0, clipBounds.y + clipBounds.height - 1)); |
|
187 |
||
188 |
if ( beginRow <= -1 || endRow <= -1 ) { |
|
189 |
return; |
|
190 |
} |
|
191 |
||
192 |
for ( int i = beginRow; i <= endRow; ++i ) { |
|
193 |
TreePath path = getPathForRow(tree, i); |
|
194 |
||
195 |
if(path != null && path.getPathCount() == 2) { |
|
196 |
Rectangle rowBounds = getPathBounds(tree,getPathForRow |
|
197 |
(tree, i)); |
|
198 |
||
199 |
// Draw a line at the top |
|
200 |
if(rowBounds != null) |
|
201 |
g.drawLine(clipBounds.x, rowBounds.y, |
|
202 |
clipBounds.x + clipBounds.width, rowBounds.y); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
} |
|
207 |
||
208 |
protected void paintVerticalPartOfLeg(Graphics g, Rectangle clipBounds, |
|
209 |
Insets insets, TreePath path) { |
|
210 |
if (lineStyle == LEG_LINE_STYLE) { |
|
211 |
super.paintVerticalPartOfLeg(g, clipBounds, insets, path); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
protected void paintHorizontalPartOfLeg(Graphics g, Rectangle clipBounds, |
|
216 |
Insets insets, Rectangle bounds, |
|
217 |
TreePath path, int row, |
|
218 |
boolean isExpanded, |
|
219 |
boolean hasBeenExpanded, boolean |
|
220 |
isLeaf) { |
|
221 |
if (lineStyle == LEG_LINE_STYLE) { |
|
222 |
super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds, |
|
223 |
path, row, isExpanded, |
|
224 |
hasBeenExpanded, isLeaf); |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
/** This class listens for changes in line style */ |
|
229 |
class LineListener implements PropertyChangeListener { |
|
230 |
public void propertyChange(PropertyChangeEvent e) { |
|
231 |
String name = e.getPropertyName(); |
|
232 |
if ( name.equals( LINE_STYLE ) ) { |
|
233 |
decodeLineStyle(e.getNewValue()); |
|
234 |
} |
|
235 |
} |
|
236 |
} // end class PaletteListener |
|
237 |
||
238 |
} |