38 |
|
|
39 |
package javax.swing.plaf.basic; |
package javax.swing.plaf.basic; |
40 |
|
|
41 |
|
import java.awt.Color; |
42 |
|
import java.awt.Component; |
43 |
|
import java.awt.Dimension; |
44 |
|
import java.awt.Graphics; |
45 |
import java.awt.Rectangle; |
import java.awt.Rectangle; |
46 |
|
|
47 |
|
import javax.swing.JComponent; |
48 |
|
import javax.swing.UIDefaults; |
49 |
|
import javax.swing.UIManager; |
50 |
|
import javax.swing.plaf.ComponentUI; |
51 |
import javax.swing.JTree; |
import javax.swing.JTree; |
52 |
import javax.swing.plaf.TreeUI; |
import javax.swing.plaf.TreeUI; |
53 |
|
import javax.swing.tree.DefaultTreeCellRenderer; |
54 |
import javax.swing.tree.TreePath; |
import javax.swing.tree.TreePath; |
55 |
|
import javax.swing.tree.TreeModel; |
56 |
|
|
57 |
/** |
/** |
58 |
* A delegate providing the user interface for <code>JTree</code> |
* A delegate providing the user interface for <code>JTree</code> |
67 |
public class BasicTreeUI |
public class BasicTreeUI |
68 |
extends TreeUI |
extends TreeUI |
69 |
{ |
{ |
70 |
|
|
71 |
/** |
/** |
72 |
* Determines the geometric extent of the label that is |
* Determines the geometric extent of the label that is |
73 |
* drawn for a path. |
* drawn for a path. |
202 |
return true; // FIXME: not implemented |
return true; // FIXME: not implemented |
203 |
} |
} |
204 |
|
|
|
|
|
205 |
/** |
/** |
206 |
* Cancels editing a tree cell, discarding any entered value. |
* Cancels editing a tree cell, discarding any entered value. |
207 |
* If no editing session is active, nothing happens. The cell |
* If no editing session is active, nothing happens. The cell |
243 |
{ |
{ |
244 |
return null; // FIXME: not implemented |
return null; // FIXME: not implemented |
245 |
} |
} |
246 |
|
|
247 |
|
public static ComponentUI createUI(JComponent c) |
248 |
|
{ |
249 |
|
return new BasicTreeUI(); |
250 |
|
} |
251 |
|
|
252 |
|
int rightChildIndent; |
253 |
|
int leftChildIndent; |
254 |
|
int rowHeight; |
255 |
|
Color hashColor; |
256 |
|
|
257 |
|
protected void installDefaults(JTree tree) |
258 |
|
{ |
259 |
|
UIDefaults defaults = UIManager.getLookAndFeelDefaults(); |
260 |
|
|
261 |
|
tree.setFont(defaults.getFont("Tree.font")); |
262 |
|
tree.setForeground(defaults.getColor("Tree.foreground")); |
263 |
|
tree.setBackground(defaults.getColor("Tree.background")); |
264 |
|
tree.setOpaque(true); |
265 |
|
|
266 |
|
hashColor = defaults.getColor("Tree.hash"); |
267 |
|
rightChildIndent = defaults.getInt("Tree.rightChildIndent"); |
268 |
|
leftChildIndent = defaults.getInt("Tree.leftChildIndent"); |
269 |
|
rowHeight = defaults.getInt("Tree.rowHeight"); |
270 |
|
} |
271 |
|
|
272 |
|
protected void installKeyboardActions() |
273 |
|
{ |
274 |
|
} |
275 |
|
|
276 |
|
protected void installListeners() |
277 |
|
{ |
278 |
|
} |
279 |
|
|
280 |
|
public void installUI(JComponent c) |
281 |
|
{ |
282 |
|
installDefaults((JTree) c); |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
|
protected void uninstallDefaults(JTree tree) |
287 |
|
{ |
288 |
|
tree.setFont(null); |
289 |
|
tree.setForeground(null); |
290 |
|
tree.setBackground(null); |
291 |
|
|
292 |
|
tree.setCellRenderer(null); |
293 |
|
} |
294 |
|
|
295 |
|
public void uninstallUI(JComponent c) |
296 |
|
{ |
297 |
|
uninstallDefaults((JTree) c); |
298 |
|
} |
299 |
|
|
300 |
|
public Dimension getPreferredSize(JComponent c) |
301 |
|
{ |
302 |
|
return new Dimension(200,200); |
303 |
|
} |
304 |
|
|
305 |
|
protected void paintLeaf(Graphics g, int x, int y, JTree tree, Object leaf) |
306 |
|
{ |
307 |
|
Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, |
308 |
|
leaf, |
309 |
|
false, // selected |
310 |
|
false, // expanded |
311 |
|
true, // leaf |
312 |
|
0, // row |
313 |
|
false // hasFocus |
314 |
|
); |
315 |
|
g.translate(x, y); |
316 |
|
c.paint(g); |
317 |
|
g.translate(-x, -y); |
318 |
|
} |
319 |
|
|
320 |
|
protected void paintNonLeaf(Graphics g, int x, int y, JTree tree, Object nonLeaf) |
321 |
|
{ |
322 |
|
Component c = tree.getCellRenderer().getTreeCellRendererComponent(tree, |
323 |
|
nonLeaf, |
324 |
|
false, // selected |
325 |
|
false, // expanded |
326 |
|
false, // leaf |
327 |
|
0, // row |
328 |
|
false // hasFocus |
329 |
|
); |
330 |
|
g.translate(x, y); |
331 |
|
c.paint(g); |
332 |
|
g.translate(-x, -y); |
333 |
|
} |
334 |
|
|
335 |
|
protected int paintRecursive(Graphics g, |
336 |
|
int indentation, |
337 |
|
int descent, |
338 |
|
int childNumber, |
339 |
|
int depth, |
340 |
|
JTree tree, |
341 |
|
TreeModel mod, |
342 |
|
Object curr) |
343 |
|
{ |
344 |
|
Rectangle clip = g.getClipBounds(); |
345 |
|
if (indentation > clip.x + clip.width + rightChildIndent || |
346 |
|
descent > clip.y + clip.height + rowHeight) |
347 |
|
return descent; |
348 |
|
|
349 |
|
|
350 |
|
int halfHeight = rowHeight / 2; |
351 |
|
int halfWidth = rightChildIndent / 2; |
352 |
|
int y0 = descent + halfHeight; |
353 |
|
|
354 |
|
if (mod.isLeaf(curr)) |
355 |
|
{ |
356 |
|
paintLeaf(g, indentation, descent, tree, curr); |
357 |
|
descent += rowHeight; |
358 |
|
} |
359 |
|
else |
360 |
|
{ |
361 |
|
if (depth > 0 || tree.isRootVisible()) |
362 |
|
{ |
363 |
|
paintNonLeaf(g, indentation, descent, tree, curr); |
364 |
|
descent += rowHeight; |
365 |
|
y0 += halfHeight; |
366 |
|
} |
367 |
|
int max = mod.getChildCount(curr); |
368 |
|
for (int i = 0; i < max; ++i) |
369 |
|
{ |
370 |
|
g.setColor(hashColor); |
371 |
|
g.drawLine(indentation + halfWidth, descent + halfHeight, |
372 |
|
indentation + rightChildIndent, descent + halfHeight); |
373 |
|
descent = paintRecursive(g, |
374 |
|
indentation + rightChildIndent, descent, |
375 |
|
i, depth+1, |
376 |
|
tree, mod, mod.getChild(curr, i)); |
377 |
|
} |
378 |
|
} |
379 |
|
|
380 |
|
int y1 = descent - halfHeight; |
381 |
|
if (y0 != y1) |
382 |
|
{ |
383 |
|
g.setColor(hashColor); |
384 |
|
g.drawLine(indentation + halfWidth, y0, |
385 |
|
indentation + halfWidth, y1); |
386 |
|
} |
387 |
|
|
388 |
|
return descent; |
389 |
|
} |
390 |
|
|
391 |
|
public void paint(Graphics g, JComponent c) |
392 |
|
{ |
393 |
|
JTree tree = (JTree) c; |
394 |
|
TreeModel mod = tree.getModel(); |
395 |
|
g.translate(10, 10); |
396 |
|
paintRecursive(g, 0, 0, 0, 0, tree, mod, mod.getRoot()); |
397 |
|
g.translate(-10, -10); |
398 |
|
} |
399 |
} |
} |