1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55:
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84: import ;
85:
86: public class JTree extends JComponent implements Scrollable, Accessible
87: {
88:
89:
94: protected class AccessibleJTree extends JComponent.AccessibleJComponent
95: implements AccessibleSelection, TreeSelectionListener, TreeModelListener,
96: TreeExpansionListener
97: {
98:
99:
103: protected class AccessibleJTreeNode extends AccessibleContext
104: implements Accessible, AccessibleComponent, AccessibleSelection,
105: AccessibleAction
106: {
107:
108: private JTree tree;
109: private TreePath tp;
110: private Accessible acc;
111: private AccessibleStateSet states;
112: private Vector selectionList;
113: private Vector actionList;
114: private TreeModel mod;
115: private Cursor cursor;
116:
117:
124: public AccessibleJTreeNode(JTree t, TreePath p, Accessible ap)
125: {
126: states = new AccessibleStateSet();
127: selectionList = new Vector();
128: actionList = new Vector();
129: mod = tree.getModel();
130: cursor = JTree.this.getCursor();
131:
132: tree = t;
133: tp = p;
134: acc = ap;
135:
136:
137:
138: TreePath[] selected = tree.getSelectionPaths();
139: for (int i = 0; i < selected.length; i++)
140: {
141: TreePath sel = selected[i];
142: if ((sel.getParentPath()).equals(tp))
143: selectionList.add(sel);
144: }
145:
146:
147:
148: actionList.add("EXPAND");
149: actionList.add("COLLAPSE");
150: actionList.add("EDIT");
151: actionList.add("SELECT");
152: actionList.add("DESELECT");
153: }
154:
155:
161: public void addAccessibleSelection(int i)
162: {
163: if (mod != null)
164: {
165: Object child = mod.getChild(tp.getLastPathComponent(), i);
166: if (child != null)
167: {
168: if (!states.contains(AccessibleState.MULTISELECTABLE))
169: clearAccessibleSelection();
170: selectionList.add(child);
171: tree.addSelectionPath(tp.pathByAddingChild(child));
172: }
173: }
174: }
175:
176:
182: public void addFocusListener(FocusListener l)
183: {
184: tree.addFocusListener(l);
185: }
186:
187:
192: public void addPropertyChangeListener(PropertyChangeListener l)
193: {
194:
195: }
196:
197:
201: public void clearAccessibleSelection()
202: {
203: selectionList.clear();
204: }
205:
206:
214: public boolean contains(Point p)
215: {
216: return getBounds().contains(p);
217: }
218:
219:
225: public boolean doAccessibleAction(int i)
226: {
227: if (i >= actionList.size() || i < 0)
228: return false;
229:
230: if (actionList.get(i).equals("EXPAND"))
231: tree.expandPath(tp);
232: else if (actionList.get(i).equals("COLLAPSE"))
233: tree.collapsePath(tp);
234: else if (actionList.get(i).equals("SELECT"))
235: tree.addSelectionPath(tp);
236: else if (actionList.get(i).equals("DESELECT"))
237: tree.removeSelectionPath(tp);
238: else if (actionList.get(i).equals("EDIT"))
239: tree.startEditingAtPath(tp);
240: else
241: return false;
242: return true;
243: }
244:
245:
250: public AccessibleAction getAccessibleAction()
251: {
252: return this;
253: }
254:
255:
260: public int getAccessibleActionCount()
261: {
262: return actionList.size();
263: }
264:
265:
271: public String getAccessibleActionDescription(int i)
272: {
273: if (i < 0 || i >= actionList.size())
274: return (actionList.get(i)).toString();
275: return super.getAccessibleDescription();
276: }
277:
278:
285: public Accessible getAccessibleAt(Point p)
286: {
287: TreePath acc = tree.getClosestPathForLocation(p.x, p.y);
288: if (acc != null)
289: return new AccessibleJTreeNode(tree, acc, this);
290: return null;
291: }
292:
293:
299: public Accessible getAccessibleChild(int i)
300: {
301: if (mod != null)
302: {
303: Object child = mod.getChild(tp.getLastPathComponent(), i);
304: if (child != null)
305: return new AccessibleJTreeNode(tree, tp.pathByAddingChild(child),
306: acc);
307: }
308: return null;
309: }
310:
311:
316: public int getAccessibleChildrenCount()
317: {
318: TreeModel mod = getModel();
319: if (mod != null)
320: return mod.getChildCount(tp.getLastPathComponent());
321: return 0;
322: }
323:
324:
329: public AccessibleComponent getAccessibleComponent()
330: {
331: return this;
332: }
333:
334:
339: public AccessibleContext getAccessibleContext()
340: {
341: return this;
342: }
343:
344:
349: public String getAccessibleDescription()
350: {
351: return super.getAccessibleDescription();
352: }
353:
354:
359: public int getAccessibleIndexInParent()
360: {
361: AccessibleContext parent = getAccessibleParent().getAccessibleContext();
362: if (parent != null)
363: for (int i = 0; i < parent.getAccessibleChildrenCount(); i++)
364: {
365: if ((parent.getAccessibleChild(i)).equals(this))
366: return i;
367: }
368: return -1;
369: }
370:
371:
376: public String getAccessibleName()
377: {
378: return super.getAccessibleName();
379: }
380:
381:
386: public Accessible getAccessibleParent()
387: {
388: return super.getAccessibleParent();
389: }
390:
391:
396: public AccessibleRole getAccessibleRole()
397: {
398: return AccessibleJTree.this.getAccessibleRole();
399: }
400:
401:
406: public AccessibleSelection getAccessibleSelection()
407: {
408: return this;
409: }
410:
411:
417: public Accessible getAccessibleSelection(int i)
418: {
419: if (i > 0 && i < getAccessibleSelectionCount())
420: return new AccessibleJTreeNode(tree,
421: tp.pathByAddingChild(selectionList.get(i)), acc);
422: return null;
423: }
424:
425:
430: public int getAccessibleSelectionCount()
431: {
432: return selectionList.size();
433: }
434:
435:
440: public AccessibleStateSet getAccessibleStateSet()
441: {
442: if (isVisible())
443: states.add(AccessibleState.VISIBLE);
444: if (tree.isCollapsed(tp))
445: states.add(AccessibleState.COLLAPSED);
446: if (tree.isEditable())
447: states.add(AccessibleState.EDITABLE);
448: if (mod != null &&
449: !mod.isLeaf(tp.getLastPathComponent()))
450: states.add(AccessibleState.EXPANDABLE);
451: if (tree.isExpanded(tp))
452: states.add(AccessibleState.EXPANDED);
453: if (isFocusable())
454: states.add(AccessibleState.FOCUSABLE);
455: if (hasFocus())
456: states.add(AccessibleState.FOCUSED);
457: if (tree.getSelectionModel().getSelectionMode() !=
458: TreeSelectionModel.SINGLE_TREE_SELECTION)
459: states.add(AccessibleState.MULTISELECTABLE);
460: if (tree.isOpaque())
461: states.add(AccessibleState.OPAQUE);
462: if (tree.isPathSelected(tp))
463: states.add(AccessibleState.SELECTED);
464: if (isShowing())
465: states.add(AccessibleState.SHOWING);
466:
467: states.add(AccessibleState.SELECTABLE);
468: return states;
469: }
470:
471:
476: public AccessibleText getAccessibleText()
477: {
478: return super.getAccessibleText();
479: }
480:
481:
486: public AccessibleValue getAccessibleValue()
487: {
488: return super.getAccessibleValue();
489: }
490:
491:
496: public Color getBackground()
497: {
498: return tree.getBackground();
499: }
500:
501:
506: public Rectangle getBounds()
507: {
508: return tree.getPathBounds(tp);
509: }
510:
511:
516: public Cursor getCursor()
517: {
518: return cursor;
519: }
520:
521:
526: public Font getFont()
527: {
528: return tree.getFont();
529: }
530:
531:
537: public FontMetrics getFontMetrics(Font f)
538: {
539: return tree.getFontMetrics(f);
540: }
541:
542:
547: public Color getForeground()
548: {
549: return tree.getForeground();
550: }
551:
552:
557: public Locale getLocale()
558: {
559: return tree.getLocale();
560: }
561:
562:
569: public Point getLocation()
570: {
571: return getLocationInJTree();
572: }
573:
574:
579: protected Point getLocationInJTree()
580: {
581: Rectangle bounds = tree.getPathBounds(tp);
582: return new Point(bounds.x, bounds.y);
583: }
584:
585:
590: public Point getLocationOnScreen()
591: {
592: Point loc = getLocation();
593: SwingUtilities.convertPointToScreen(loc, tree);
594: return loc;
595: }
596:
597:
602: public Dimension getSize()
603: {
604: Rectangle b = getBounds();
605: return b.getSize();
606: }
607:
608:
614: public boolean isAccessibleChildSelected(int i)
615: {
616: Object child = mod.getChild(tp.getLastPathComponent(), i);
617: if (child != null)
618: return tree.isPathSelected(tp.pathByAddingChild(child));
619: return false;
620: }
621:
622:
627: public boolean isEnabled()
628: {
629: return tree.isEnabled();
630: }
631:
632:
637: public boolean isFocusTraversable()
638: {
639: return true;
640: }
641:
642:
648: public boolean isShowing()
649: {
650: return isVisible() && tree.isShowing();
651: }
652:
653:
658: public boolean isVisible()
659: {
660: return tree.isVisible(tp);
661: }
662:
663:
669: public void removeAccessibleSelection(int i)
670: {
671: if (mod != null)
672: {
673: Object child = mod.getChild(tp.getLastPathComponent(), i);
674: if (child != null)
675: {
676: if (!states.contains(AccessibleState.MULTISELECTABLE))
677: clearAccessibleSelection();
678: if (selectionList.contains(child))
679: {
680: selectionList.remove(child);
681: tree.removeSelectionPath(tp.pathByAddingChild(child));
682: }
683: }
684: }
685: }
686:
687:
693: public void removeFocusListener(FocusListener l)
694: {
695: tree.removeFocusListener(l);
696: }
697:
698:
703: public void removePropertyChangeListener(PropertyChangeListener l)
704: {
705:
706: }
707:
708:
711: public void requestFocus()
712: {
713: tree.requestFocus();
714: }
715:
716:
720: public void selectAllAccessibleSelection()
721: {
722: Object parent = tp.getLastPathComponent();
723: if (mod != null)
724: {
725: for (int i = 0; i < mod.getChildCount(parent); i++)
726: {
727: Object child = mod.getChild(parent, i);
728: if (child != null)
729: {
730: if (!states.contains(AccessibleState.MULTISELECTABLE))
731: clearAccessibleSelection();
732: if (selectionList.contains(child))
733: {
734: selectionList.add(child);
735: tree.addSelectionPath(tp.pathByAddingChild(child));
736: }
737: }
738: }
739: }
740: }
741:
742:
747: public void setAccessibleDescription(String s)
748: {
749: super.setAccessibleDescription(s);
750: }
751:
752:
757: public void setAccessibleName(String s)
758: {
759: super.setAccessibleName(s);
760: }
761:
762:
767: public void setBackground(Color c)
768: {
769:
770: }
771:
772:
777: public void setBounds(Rectangle r)
778: {
779:
780: }
781:
782:
787: public void setCursor(Cursor c)
788: {
789: cursor = c;
790: }
791:
792:
797: public void setEnabled(boolean b)
798: {
799:
800: }
801:
802:
807: public void setFont(Font f)
808: {
809:
810: }
811:
812:
817: public void setForeground(Color c)
818: {
819:
820: }
821:
822:
827: public void setLocation(Point p)
828: {
829:
830: }
831:
832:
837: public void setSize(Dimension d)
838: {
839:
840: }
841:
842:
847: public void setVisible(boolean b)
848: {
849:
850: }
851: }
852:
853:
856: public AccessibleJTree()
857: {
858:
859: }
860:
861:
866: public void addAccessibleSelection(int i)
867: {
868: addSelectionInterval(i, i);
869: }
870:
871:
874: public void clearAccessibleSelection()
875: {
876: clearSelection();
877: }
878:
879:
882: public void fireVisibleDataPropertyChange()
883: {
884: treeDidChange();
885: }
886:
887:
894: public Accessible getAccessibleAt(Point p)
895: {
896: TreePath tp = getClosestPathForLocation(p.x, p.y);
897: if (tp != null)
898: return new AccessibleJTreeNode(JTree.this, tp, null);
899: return null;
900: }
901:
902:
908: public Accessible getAccessibleChild(int i)
909: {
910: return null;
911: }
912:
913:
918: public int getAccessibleChildrenCount()
919: {
920: TreeModel model = getModel();
921: if (model != null)
922: return model.getChildCount(model.getRoot());
923: return 0;
924: }
925:
926:
931: public int getAccessibleIndexInParent()
932: {
933: return 0;
934: }
935:
936:
941: public AccessibleRole getAccessibleRole()
942: {
943: return AccessibleRole.TREE;
944: }
945:
946:
951: public AccessibleSelection getAccessibleSelection()
952: {
953: TreeModel mod = getModel();
954: if (mod != null)
955: return (new AccessibleJTreeNode(JTree.this,
956: new TreePath(mod.getRoot()), null)).getAccessibleSelection();
957: return null;
958: }
959:
960:
965: public Accessible getAccessibleSelection(int i)
966: {
967: TreeModel mod = getModel();
968: if (mod != null)
969: return (new AccessibleJTreeNode(JTree.this,
970: new TreePath(mod.getRoot()), null)).getAccessibleSelection(i);
971: return null;
972: }
973:
974:
979: public int getAccessibleSelectionCount()
980: {
981: return getSelectionCount();
982: }
983:
984:
990: public boolean isAccessibleChildSelected(int i)
991: {
992:
993: return false;
994: }
995:
996:
1002: public void removeAccessibleSelection(int i)
1003: {
1004: removeSelectionInterval(i, i);
1005: }
1006:
1007:
1011: public void selectAllAccessibleSelection()
1012: {
1013: if (getSelectionModel().getSelectionMode() !=
1014: TreeSelectionModel.SINGLE_TREE_SELECTION)
1015: addSelectionInterval(0, getVisibleRowCount());
1016: }
1017:
1018:
1023: public void treeCollapsed(TreeExpansionEvent e)
1024: {
1025: fireTreeCollapsed(e.getPath());
1026: }
1027:
1028:
1033: public void treeExpanded(TreeExpansionEvent e)
1034: {
1035: fireTreeExpanded(e.getPath());
1036: }
1037:
1038:
1043: public void treeNodesChanged(TreeModelEvent e)
1044: {
1045:
1046: }
1047:
1048:
1053: public void treeNodesInserted(TreeModelEvent e)
1054: {
1055:
1056: }
1057:
1058:
1063: public void treeNodesRemoved(TreeModelEvent e)
1064: {
1065:
1066: }
1067:
1068:
1073: public void treeStructureChanged(TreeModelEvent e)
1074: {
1075:
1076: }
1077:
1078:
1083: public void valueChanged(TreeSelectionEvent e)
1084: {
1085: fireValueChanged(e);
1086: }
1087: }
1088:
1089: public static class DynamicUtilTreeNode extends DefaultMutableTreeNode
1090: {
1091: protected Object childValue;
1092:
1093: protected boolean loadedChildren;
1094:
1095:
1099: protected boolean hasChildren;
1100:
1101: public DynamicUtilTreeNode(Object value, Object children)
1102: {
1103: super(value);
1104: childValue = children;
1105: loadedChildren = false;
1106: }
1107:
1108: public int getChildCount()
1109: {
1110: loadChildren();
1111: return super.getChildCount();
1112: }
1113:
1114: protected void loadChildren()
1115: {
1116: if (!loadedChildren)
1117: {
1118: createChildren(this, childValue);
1119: loadedChildren = true;
1120: }
1121: }
1122:
1123: public Enumeration children()
1124: {
1125: loadChildren();
1126: return super.children();
1127: }
1128:
1129:
1137: public TreeNode getChildAt(int pos)
1138: {
1139: loadChildren();
1140: return super.getChildAt(pos);
1141: }
1142:
1143: public boolean isLeaf()
1144: {
1145: return childValue == null || !(childValue instanceof Hashtable
1146: || childValue instanceof Vector
1147: || childValue.getClass().isArray());
1148: }
1149:
1150: public static void createChildren(DefaultMutableTreeNode parent,
1151: Object children)
1152: {
1153: if (children instanceof Hashtable)
1154: {
1155: Hashtable tab = (Hashtable) children;
1156: Enumeration e = tab.keys();
1157: while (e.hasMoreElements())
1158: {
1159: Object key = e.nextElement();
1160: Object val = tab.get(key);
1161: parent.add(new DynamicUtilTreeNode(key, val));
1162: }
1163: }
1164: else if (children instanceof Vector)
1165: {
1166: Iterator i = ((Vector) children).iterator();
1167: while (i.hasNext())
1168: {
1169: Object n = i.next();
1170: parent.add(new DynamicUtilTreeNode(n, n));
1171: }
1172: }
1173: else if (children != null && children.getClass().isArray())
1174: {
1175: Object[] arr = (Object[]) children;
1176: for (int i = 0; i < arr.length; ++i)
1177: parent.add(new DynamicUtilTreeNode(arr[i], arr[i]));
1178: }
1179: }
1180: }
1181:
1182:
1186: protected class TreeModelHandler implements TreeModelListener
1187: {
1188:
1189:
1192: protected TreeModelHandler()
1193: {
1194:
1195: }
1196:
1197:
1207: public void treeNodesChanged(TreeModelEvent ev)
1208: {
1209:
1210: }
1211:
1212:
1219: public void treeNodesInserted(TreeModelEvent ev)
1220: {
1221:
1222: }
1223:
1224:
1231: public void treeNodesRemoved(TreeModelEvent ev)
1232: {
1233:
1234:
1235: }
1236:
1237:
1244: public void treeStructureChanged(TreeModelEvent ev)
1245: {
1246:
1247: TreePath path = ev.getTreePath();
1248: setExpandedState(path, isExpanded(path));
1249: }
1250: }
1251:
1252:
1258: protected class TreeSelectionRedirector implements TreeSelectionListener,
1259: Serializable
1260: {
1261:
1262: private static final long serialVersionUID = -3505069663646241664L;
1263:
1264:
1267: protected TreeSelectionRedirector()
1268: {
1269:
1270: }
1271:
1272:
1277: public void valueChanged(TreeSelectionEvent ev)
1278: {
1279: TreeSelectionEvent rewritten =
1280: (TreeSelectionEvent) ev.cloneWithSource(JTree.this);
1281: fireValueChanged(rewritten);
1282:
1283:
1284: TreePath[] changed = ev.getPaths();
1285: for (int i = 0; i < changed.length; i++)
1286: {
1287: repaint(getPathBounds(changed[i]));
1288: }
1289: }
1290: }
1291:
1292:
1295: protected static class EmptySelectionModel extends DefaultTreeSelectionModel
1296: {
1297:
1298: private static final long serialVersionUID = -5815023306225701477L;
1299:
1300:
1303: protected static final EmptySelectionModel sharedInstance =
1304: new EmptySelectionModel();
1305:
1306:
1309: protected EmptySelectionModel()
1310: {
1311:
1312: }
1313:
1314:
1319: public static EmptySelectionModel sharedInstance()
1320: {
1321: return sharedInstance;
1322: }
1323:
1324:
1329: public void setSelectionPaths(TreePath[] paths)
1330: {
1331:
1332: }
1333:
1334:
1339: public void addSelectionPaths(TreePath[] paths)
1340: {
1341:
1342: }
1343:
1344:
1349: public void removeSelectionPaths(TreePath[] paths)
1350: {
1351:
1352: }
1353: }
1354:
1355: private static final long serialVersionUID = 7559816092864483649L;
1356:
1357: public static final String CELL_EDITOR_PROPERTY = "cellEditor";
1358:
1359: public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
1360:
1361: public static final String EDITABLE_PROPERTY = "editable";
1362:
1363: public static final String INVOKES_STOP_CELL_EDITING_PROPERTY =
1364: "invokesStopCellEditing";
1365:
1366: public static final String LARGE_MODEL_PROPERTY = "largeModel";
1367:
1368: public static final String ROOT_VISIBLE_PROPERTY = "rootVisible";
1369:
1370: public static final String ROW_HEIGHT_PROPERTY = "rowHeight";
1371:
1372: public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
1373:
1374: public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
1375:
1376: public static final String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
1377:
1378: public static final String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
1379:
1380: public static final String TREE_MODEL_PROPERTY = "model";
1381:
1382: public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
1383:
1384:
1385: public static final String ANCHOR_SELECTION_PATH_PROPERTY =
1386: "anchorSelectionPath";
1387:
1388:
1389: public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
1390:
1391:
1392: public static final String EXPANDS_SELECTED_PATHS_PROPERTY =
1393: "expandsSelectedPaths";
1394:
1395: private static final Object EXPANDED = Boolean.TRUE;
1396:
1397: private static final Object COLLAPSED = Boolean.FALSE;
1398:
1399: private boolean dragEnabled;
1400:
1401: private boolean expandsSelectedPaths;
1402:
1403: private TreePath anchorSelectionPath;
1404:
1405:
1410: private Hashtable nodeStates = new Hashtable();
1411:
1412: protected transient TreeCellEditor cellEditor;
1413:
1414: protected transient TreeCellRenderer cellRenderer;
1415:
1416: protected boolean editable;
1417:
1418: protected boolean invokesStopCellEditing;
1419:
1420: protected boolean largeModel;
1421:
1422: protected boolean rootVisible;
1423:
1424: protected int rowHeight;
1425:
1426: protected boolean scrollsOnExpand;
1427:
1428: protected transient TreeSelectionModel selectionModel;
1429:
1430: protected boolean showsRootHandles;
1431:
1432: protected int toggleClickCount;
1433:
1434: protected transient TreeModel treeModel;
1435:
1436: protected int visibleRowCount;
1437:
1438:
1441: protected transient TreeModelListener treeModelListener;
1442:
1443:
1446: protected TreeSelectionRedirector selectionRedirector =
1447: new TreeSelectionRedirector();
1448:
1449:
1456: private boolean clientRowHeightSet = false;
1457:
1458:
1465: private boolean clientScrollsOnExpandSet = false;
1466:
1467:
1474: private boolean clientShowsRootHandlesSet = false;
1475:
1476:
1479: public JTree()
1480: {
1481: this(getDefaultTreeModel());
1482: }
1483:
1484:
1489: public JTree(Hashtable value)
1490: {
1491: this(createTreeModel(value));
1492: }
1493:
1494:
1499: public JTree(Object[] value)
1500: {
1501: this(createTreeModel(value));
1502: }
1503:
1504:
1509: public JTree(TreeModel model)
1510: {
1511: setRootVisible(true);
1512: setSelectionModel(new EmptySelectionModel());
1513: selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
1514:
1515:
1516: nodeStates = new Hashtable();
1517:
1518:
1519: cellRenderer = null;
1520:
1521:
1522:
1523:
1524:
1525: updateUI();
1526: setModel(model);
1527: }
1528:
1529:
1534: public JTree(TreeNode root)
1535: {
1536: this(root, false);
1537: }
1538:
1539:
1547: public JTree(TreeNode root, boolean asksAllowChildren)
1548: {
1549: this(new DefaultTreeModel(root, asksAllowChildren));
1550: }
1551:
1552:
1557: public JTree(Vector value)
1558: {
1559: this(createTreeModel(value));
1560: }
1561:
1562: public int getRowForPath(TreePath path)
1563: {
1564: TreeUI ui = getUI();
1565:
1566: if (ui != null)
1567: return ui.getRowForPath(this, path);
1568:
1569: return -1;
1570: }
1571:
1572: public TreePath getPathForRow(int row)
1573: {
1574: TreeUI ui = getUI();
1575: return ui != null ? ui.getPathForRow(this, row) : null;
1576: }
1577:
1578:
1586: protected TreePath[] getPathBetweenRows(int index0, int index1)
1587: {
1588: TreeUI ui = getUI();
1589:
1590: if (ui == null)
1591: return null;
1592:
1593: int minIndex = Math.min(index0, index1);
1594: int maxIndex = Math.max(index0, index1);
1595: TreePath[] paths = new TreePath[maxIndex - minIndex + 1];
1596:
1597: for (int i = minIndex; i <= maxIndex; ++i)
1598: paths[i - minIndex] = ui.getPathForRow(this, i);
1599:
1600: return paths;
1601: }
1602:
1603:
1608: protected static TreeModel createTreeModel(Object value)
1609: {
1610: return new DefaultTreeModel(new DynamicUtilTreeNode(value, value));
1611: }
1612:
1613:
1618: public TreeUI getUI()
1619: {
1620: return (TreeUI) ui;
1621: }
1622:
1623:
1628: public void setUI(TreeUI ui)
1629: {
1630: super.setUI(ui);
1631: }
1632:
1633:
1636: public void updateUI()
1637: {
1638: setUI((TreeUI) UIManager.getUI(this));
1639: }
1640:
1641:
1646: public String getUIClassID()
1647: {
1648: return "TreeUI";
1649: }
1650:
1651:
1657: public AccessibleContext getAccessibleContext()
1658: {
1659: return new AccessibleJTree();
1660: }
1661:
1662:
1667: public Dimension getPreferredScrollableViewportSize()
1668: {
1669: return getPreferredSize();
1670: }
1671:
1672:
1685: public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
1686: int direction)
1687: {
1688: int delta;
1689:
1690:
1691: if (orientation == SwingConstants.VERTICAL)
1692: {
1693:
1694: int row = getClosestRowForLocation(visibleRect.x, visibleRect.y + 1);
1695: row = row + direction;
1696: if (row < 0)
1697: row = 0;
1698:
1699: Rectangle newTop = getRowBounds(row);
1700: delta = newTop.y - visibleRect.y;
1701: }
1702: else
1703: delta = direction * rowHeight == 0 ? 20 : rowHeight;
1704: return delta;
1705: }
1706:
1707: public int getScrollableBlockIncrement(Rectangle visibleRect,
1708: int orientation, int direction)
1709: {
1710: return getScrollableUnitIncrement(visibleRect, orientation, direction);
1711: }
1712:
1713: public boolean getScrollableTracksViewportHeight()
1714: {
1715: if (getParent() instanceof JViewport)
1716: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1717: return false;
1718: }
1719:
1720: public boolean getScrollableTracksViewportWidth()
1721: {
1722: if (getParent() instanceof JViewport)
1723: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1724: return false;
1725: }
1726:
1727:
1732: public void addTreeExpansionListener(TreeExpansionListener listener)
1733: {
1734: listenerList.add(TreeExpansionListener.class, listener);
1735: }
1736:
1737:
1742: public void removeTreeExpansionListener(TreeExpansionListener listener)
1743: {
1744: listenerList.remove(TreeExpansionListener.class, listener);
1745: }
1746:
1747:
1752: public TreeExpansionListener[] getTreeExpansionListeners()
1753: {
1754: return (TreeExpansionListener[]) getListeners(TreeExpansionListener.class);
1755: }
1756:
1757:
1762: public void fireTreeCollapsed(TreePath path)
1763: {
1764: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1765: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1766:
1767: for (int index = 0; index < listeners.length; ++index)
1768: listeners[index].treeCollapsed(event);
1769: }
1770:
1771:
1776: public void fireTreeExpanded(TreePath path)
1777: {
1778: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1779: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1780:
1781: for (int index = 0; index < listeners.length; ++index)
1782: listeners[index].treeExpanded(event);
1783: }
1784:
1785:
1790: public void addTreeSelectionListener(TreeSelectionListener listener)
1791: {
1792: listenerList.add(TreeSelectionListener.class, listener);
1793: }
1794:
1795:
1800: public void removeTreeSelectionListener(TreeSelectionListener listener)
1801: {
1802: listenerList.remove(TreeSelectionListener.class, listener);
1803: }
1804:
1805:
1810: public TreeSelectionListener[] getTreeSelectionListeners()
1811: {
1812: return (TreeSelectionListener[])
1813: getListeners(TreeSelectionListener.class);
1814: }
1815:
1816:
1821: protected void fireValueChanged(TreeSelectionEvent event)
1822: {
1823: TreeSelectionListener[] listeners = getTreeSelectionListeners();
1824:
1825: for (int index = 0; index < listeners.length; ++index)
1826: listeners[index].valueChanged(event);
1827: }
1828:
1829:
1834: public void addTreeWillExpandListener(TreeWillExpandListener listener)
1835: {
1836: listenerList.add(TreeWillExpandListener.class, listener);
1837: }
1838:
1839:
1844: public void removeTreeWillExpandListener(TreeWillExpandListener listener)
1845: {
1846: listenerList.remove(TreeWillExpandListener.class, listener);
1847: }
1848:
1849:
1854: public TreeWillExpandListener[] getTreeWillExpandListeners()
1855: {
1856: return (TreeWillExpandListener[])
1857: getListeners(TreeWillExpandListener.class);
1858: }
1859:
1860:
1865: public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException
1866: {
1867: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1868: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1869:
1870: for (int index = 0; index < listeners.length; ++index)
1871: listeners[index].treeWillCollapse(event);
1872: }
1873:
1874:
1879: public void fireTreeWillExpand(TreePath path) throws ExpandVetoException
1880: {
1881: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1882: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1883:
1884: for (int index = 0; index < listeners.length; ++index)
1885: listeners[index].treeWillExpand(event);
1886: }
1887:
1888:
1893: public TreeModel getModel()
1894: {
1895: return treeModel;
1896: }
1897:
1898:
1903: public void setModel(TreeModel model)
1904: {
1905: if (treeModel == model)
1906: return;
1907:
1908:
1909: if (treeModel != null && treeModelListener != null)
1910: treeModel.removeTreeModelListener(treeModelListener);
1911:
1912:
1913: if (treeModelListener == null)
1914: treeModelListener = createTreeModelListener();
1915: if (model != null)
1916: model.addTreeModelListener(treeModelListener);
1917:
1918: TreeModel oldValue = treeModel;
1919: treeModel = model;
1920: clearToggledPaths();
1921:
1922: if (treeModel != null)
1923: {
1924: if (treeModelListener == null)
1925: treeModelListener = createTreeModelListener();
1926: if (treeModelListener != null)
1927: treeModel.addTreeModelListener(treeModelListener);
1928: Object root = treeModel.getRoot();
1929: if (root != null && !treeModel.isLeaf(root))
1930: {
1931: nodeStates.put(new TreePath(root), Boolean.TRUE);
1932: }
1933: }
1934:
1935: firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model);
1936: }
1937:
1938:
1944: public boolean isEditable()
1945: {
1946: return editable;
1947: }
1948:
1949:
1955: public void setEditable(boolean flag)
1956: {
1957: if (editable == flag)
1958: return;
1959:
1960: boolean oldValue = editable;
1961: editable = flag;
1962: firePropertyChange(EDITABLE_PROPERTY, oldValue, editable);
1963: }
1964:
1965:
1971: public boolean isRootVisible()
1972: {
1973: return rootVisible;
1974: }
1975:
1976: public void setRootVisible(boolean flag)
1977: {
1978: if (rootVisible == flag)
1979: return;
1980:
1981:
1982: if (rootVisible && !flag)
1983: {
1984: TreeSelectionModel model = getSelectionModel();
1985:
1986: TreePath rootPath = getPathForRow(0);
1987: model.removeSelectionPath(rootPath);
1988: }
1989:
1990: boolean oldValue = rootVisible;
1991: rootVisible = flag;
1992: firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, flag);
1993:
1994: }
1995:
1996: public boolean getShowsRootHandles()
1997: {
1998: return showsRootHandles;
1999: }
2000:
2001: public void setShowsRootHandles(boolean flag)
2002: {
2003: clientShowsRootHandlesSet = true;
2004:
2005: if (showsRootHandles == flag)
2006: return;
2007:
2008: boolean oldValue = showsRootHandles;
2009: showsRootHandles = flag;
2010: firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue, flag);
2011: }
2012:
2013: public TreeCellEditor getCellEditor()
2014: {
2015: return cellEditor;
2016: }
2017:
2018: public void setCellEditor(TreeCellEditor editor)
2019: {
2020: if (cellEditor == editor)
2021: return;
2022:
2023: TreeCellEditor oldValue = cellEditor;
2024: cellEditor = editor;
2025: firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor);
2026: }
2027:
2028: public TreeCellRenderer getCellRenderer()
2029: {
2030: return cellRenderer;
2031: }
2032:
2033: public void setCellRenderer(TreeCellRenderer newRenderer)
2034: {
2035: if (cellRenderer == newRenderer)
2036: return;
2037:
2038: TreeCellRenderer oldValue = cellRenderer;
2039: cellRenderer = newRenderer;
2040: firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, newRenderer);
2041: }
2042:
2043: public TreeSelectionModel getSelectionModel()
2044: {
2045: return selectionModel;
2046: }
2047:
2048: public void setSelectionModel(TreeSelectionModel model)
2049: {
2050: if (selectionModel == model)
2051: return;
2052:
2053: if (selectionModel != null)
2054: selectionModel.removeTreeSelectionListener(selectionRedirector);
2055:
2056: TreeSelectionModel oldValue = selectionModel;
2057: selectionModel = model;
2058:
2059: if (selectionModel != null)
2060: selectionModel.addTreeSelectionListener(selectionRedirector);
2061:
2062: firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
2063: revalidate();
2064: repaint();
2065: }
2066:
2067: public int getVisibleRowCount()
2068: {
2069: return visibleRowCount;
2070: }
2071:
2072: public void setVisibleRowCount(int rows)
2073: {
2074: if (visibleRowCount == rows)
2075: return;
2076:
2077: int oldValue = visibleRowCount;
2078: visibleRowCount = rows;
2079: firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, rows);
2080: }
2081:
2082: public boolean isLargeModel()
2083: {
2084: return largeModel;
2085: }
2086:
2087: public void setLargeModel(boolean large)
2088: {
2089: if (largeModel == large)
2090: return;
2091:
2092: boolean oldValue = largeModel;
2093: largeModel = large;
2094: firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large);
2095: }
2096:
2097: public int getRowHeight()
2098: {
2099: return rowHeight;
2100: }
2101:
2102: public void setRowHeight(int height)
2103: {
2104: clientRowHeightSet = true;
2105:
2106: if (rowHeight == height)
2107: return;
2108:
2109: int oldValue = rowHeight;
2110: rowHeight = height;
2111: firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height);
2112: }
2113:
2114: public boolean isFixedRowHeight()
2115: {
2116: return rowHeight > 0;
2117: }
2118:
2119: public boolean getInvokesStopCellEditing()
2120: {
2121: return invokesStopCellEditing;
2122: }
2123:
2124: public void setInvokesStopCellEditing(boolean invoke)
2125: {
2126: if (invokesStopCellEditing == invoke)
2127: return;
2128:
2129: boolean oldValue = invokesStopCellEditing;
2130: invokesStopCellEditing = invoke;
2131: firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY,
2132: oldValue, invoke);
2133: }
2134:
2135:
2138: public int getToggleClickCount()
2139: {
2140: return toggleClickCount;
2141: }
2142:
2143:
2146: public void setToggleClickCount(int count)
2147: {
2148: if (toggleClickCount == count)
2149: return;
2150:
2151: int oldValue = toggleClickCount;
2152: toggleClickCount = count;
2153: firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count);
2154: }
2155:
2156: public void scrollPathToVisible(TreePath path)
2157: {
2158: if (path == null)
2159: return;
2160: Rectangle rect = getPathBounds(path);
2161: scrollRectToVisible(rect);
2162: }
2163:
2164: public void scrollRowToVisible(int row)
2165: {
2166: scrollPathToVisible(getPathForRow(row));
2167: }
2168:
2169: public boolean getScrollsOnExpand()
2170: {
2171: return scrollsOnExpand;
2172: }
2173:
2174: public void setScrollsOnExpand(boolean scroll)
2175: {
2176: clientScrollsOnExpandSet = true;
2177: if (scrollsOnExpand == scroll)
2178: return;
2179:
2180: boolean oldValue = scrollsOnExpand;
2181: scrollsOnExpand = scroll;
2182: firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll);
2183: }
2184:
2185: public void setSelectionPath(TreePath path)
2186: {
2187: selectionModel.setSelectionPath(path);
2188: }
2189:
2190: public void setSelectionPaths(TreePath[] paths)
2191: {
2192: selectionModel.setSelectionPaths(paths);
2193: }
2194:
2195: public void setSelectionRow(int row)
2196: {
2197: TreePath path = getPathForRow(row);
2198:
2199: if (path != null)
2200: selectionModel.setSelectionPath(path);
2201: }
2202:
2203: public void setSelectionRows(int[] rows)
2204: {
2205:
2206: if (rows == null || getUI() == null)
2207: return;
2208:
2209: TreePath[] paths = new TreePath[rows.length];
2210:
2211: for (int i = rows.length - 1; i >= 0; --i)
2212: paths[i] = getPathForRow(rows[i]);
2213:
2214: setSelectionPaths(paths);
2215: }
2216:
2217: public void setSelectionInterval(int index0, int index1)
2218: {
2219: TreePath[] paths = getPathBetweenRows(index0, index1);
2220:
2221: if (paths != null)
2222: setSelectionPaths(paths);
2223: }
2224:
2225: public void addSelectionPath(TreePath path)
2226: {
2227: selectionModel.addSelectionPath(path);
2228: }
2229:
2230: public void addSelectionPaths(TreePath[] paths)
2231: {
2232: selectionModel.addSelectionPaths(paths);
2233: }
2234:
2235: public void addSelectionRow(int row)
2236: {
2237: TreePath path = getPathForRow(row);
2238:
2239: if (path != null)
2240: selectionModel.addSelectionPath(path);
2241: }
2242:
2243: public void addSelectionRows(int[] rows)
2244: {
2245:
2246: if (rows == null || getUI() == null)
2247: return;
2248:
2249: TreePath[] paths = new TreePath[rows.length];
2250:
2251: for (int i = rows.length - 1; i >= 0; --i)
2252: paths[i] = getPathForRow(rows[i]);
2253:
2254: addSelectionPaths(paths);
2255: }
2256:
2257:
2265: public void addSelectionInterval(int index0, int index1)
2266: {
2267: TreePath[] paths = getPathBetweenRows(index0, index1);
2268:
2269: if (paths != null)
2270: addSelectionPaths(paths);
2271: }
2272:
2273: public void removeSelectionPath(TreePath path)
2274: {
2275: selectionModel.removeSelectionPath(path);
2276: }
2277:
2278: public void removeSelectionPaths(TreePath[] paths)
2279: {
2280: selectionModel.removeSelectionPaths(paths);
2281: }
2282:
2283: public void removeSelectionRow(int row)
2284: {
2285: TreePath path = getPathForRow(row);
2286:
2287: if (path != null)
2288: selectionModel.removeSelectionPath(path);
2289: }
2290:
2291: public void removeSelectionRows(int[] rows)
2292: {
2293: if (rows == null || getUI() == null)
2294: return;
2295:
2296: TreePath[] paths = new TreePath[rows.length];
2297:
2298: for (int i = rows.length - 1; i >= 0; --i)
2299: paths[i] = getPathForRow(rows[i]);
2300:
2301: removeSelectionPaths(paths);
2302: }
2303:
2304: public void removeSelectionInterval(int index0, int index1)
2305: {
2306: TreePath[] paths = getPathBetweenRows(index0, index1);
2307:
2308: if (paths != null)
2309: removeSelectionPaths(paths);
2310: }
2311:
2312: public void clearSelection()
2313: {
2314: selectionModel.clearSelection();
2315: setLeadSelectionPath(null);
2316: }
2317:
2318: public TreePath getLeadSelectionPath()
2319: {
2320: if (selectionModel == null)
2321: return null;
2322: else
2323: return selectionModel.getLeadSelectionPath();
2324: }
2325:
2326:
2329: public void setLeadSelectionPath(TreePath path)
2330: {
2331: if (selectionModel != null)
2332: {
2333: TreePath oldValue = selectionModel.getLeadSelectionPath();
2334: if (path.equals(oldValue))
2335: return;
2336:
2337:
2338: if (path != null)
2339: {
2340: repaint(getPathBounds(path));
2341: selectionModel.addSelectionPath(path);
2342: }
2343:
2344: if (oldValue != null)
2345: repaint(getPathBounds(oldValue));
2346:
2347: firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
2348: }
2349: }
2350:
2351:
2354: public TreePath getAnchorSelectionPath()
2355: {
2356: return anchorSelectionPath;
2357: }
2358:
2359:
2362: public void setAnchorSelectionPath(TreePath path)
2363: {
2364: if (anchorSelectionPath == path)
2365: return;
2366:
2367: TreePath oldValue = anchorSelectionPath;
2368: anchorSelectionPath = path;
2369: firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, path);
2370: }
2371:
2372: public int getLeadSelectionRow()
2373: {
2374: return selectionModel.getLeadSelectionRow();
2375: }
2376:
2377: public int getMaxSelectionRow()
2378: {
2379: return selectionModel.getMaxSelectionRow();
2380: }
2381:
2382: public int getMinSelectionRow()
2383: {
2384: return selectionModel.getMinSelectionRow();
2385: }
2386:
2387: public int getSelectionCount()
2388: {
2389: return selectionModel.getSelectionCount();
2390: }
2391:
2392: public TreePath getSelectionPath()
2393: {
2394: return selectionModel.getSelectionPath();
2395: }
2396:
2397: public TreePath[] getSelectionPaths()
2398: {
2399: return selectionModel.getSelectionPaths();
2400: }
2401:
2402: public int[] getSelectionRows()
2403: {
2404: return selectionModel.getSelectionRows();
2405: }
2406:
2407: public boolean isPathSelected(TreePath path)
2408: {
2409: return selectionModel.isPathSelected(path);
2410: }
2411:
2412: public boolean isRowSelected(int row)
2413: {
2414: return selectionModel.isPathSelected(getPathForRow(row));
2415: }
2416:
2417: public boolean isSelectionEmpty()
2418: {
2419: return selectionModel.isSelectionEmpty();
2420: }
2421:
2422:
2429: public boolean getDragEnabled()
2430: {
2431: return dragEnabled;
2432: }
2433:
2434:
2441: public void setDragEnabled(boolean enabled)
2442: {
2443: dragEnabled = enabled;
2444: }
2445:
2446: public int getRowCount()
2447: {
2448: TreeUI ui = getUI();
2449:
2450: if (ui != null)
2451: return ui.getRowCount(this);
2452:
2453: return 0;
2454: }
2455:
2456: public void collapsePath(TreePath path)
2457: {
2458: try
2459: {
2460: fireTreeWillCollapse(path);
2461: }
2462: catch (ExpandVetoException ev)
2463: {
2464:
2465: }
2466: setExpandedState(path, false);
2467: fireTreeCollapsed(path);
2468: }
2469:
2470: public void collapseRow(int row)
2471: {
2472: if (row < 0 || row >= getRowCount())
2473: return;
2474:
2475: TreePath path = getPathForRow(row);
2476:
2477: if (path != null)
2478: collapsePath(path);
2479: }
2480:
2481: public void expandPath(TreePath path)
2482: {
2483:
2484:
2485: if (path == null || isExpanded(path))
2486: return;
2487:
2488: try
2489: {
2490: fireTreeWillExpand(path);
2491: }
2492: catch (ExpandVetoException ev)
2493: {
2494:
2495: }
2496:
2497: setExpandedState(path, true);
2498: fireTreeExpanded(path);
2499: }
2500:
2501: public void expandRow(int row)
2502: {
2503: if (row < 0 || row >= getRowCount())
2504: return;
2505:
2506: TreePath path = getPathForRow(row);
2507:
2508: if (path != null)
2509: expandPath(path);
2510: }
2511:
2512: public boolean isCollapsed(TreePath path)
2513: {
2514: return !isExpanded(path);
2515: }
2516:
2517: public boolean isCollapsed(int row)
2518: {
2519: if (row < 0 || row >= getRowCount())
2520: return false;
2521:
2522: TreePath path = getPathForRow(row);
2523:
2524: if (path != null)
2525: return isCollapsed(path);
2526:
2527: return false;
2528: }
2529:
2530: public boolean isExpanded(TreePath path)
2531: {
2532: if (path == null)
2533: return false;
2534:
2535: Object state = nodeStates.get(path);
2536:
2537: if ((state == null) || (state != EXPANDED))
2538: return false;
2539:
2540: TreePath parent = path.getParentPath();
2541:
2542: if (parent != null)
2543: return isExpanded(parent);
2544:
2545: return true;
2546: }
2547:
2548: public boolean isExpanded(int row)
2549: {
2550: if (row < 0 || row >= getRowCount())
2551: return false;
2552:
2553: TreePath path = getPathForRow(row);
2554:
2555: if (path != null)
2556: return isExpanded(path);
2557:
2558: return false;
2559: }
2560:
2561:
2564: public boolean getExpandsSelectedPaths()
2565: {
2566: return expandsSelectedPaths;
2567: }
2568:
2569:
2572: public void setExpandsSelectedPaths(boolean flag)
2573: {
2574: if (expandsSelectedPaths == flag)
2575: return;
2576:
2577: boolean oldValue = expandsSelectedPaths;
2578: expandsSelectedPaths = flag;
2579: firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue, flag);
2580: }
2581:
2582: public Rectangle getPathBounds(TreePath path)
2583: {
2584: TreeUI ui = getUI();
2585:
2586: if (ui == null)
2587: return null;
2588:
2589: return ui.getPathBounds(this, path);
2590: }
2591:
2592: public Rectangle getRowBounds(int row)
2593: {
2594: TreePath path = getPathForRow(row);
2595:
2596: if (path != null)
2597: return getPathBounds(path);
2598:
2599: return null;
2600: }
2601:
2602: public boolean isEditing()
2603: {
2604: TreeUI ui = getUI();
2605:
2606: if (ui != null)
2607: return ui.isEditing(this);
2608:
2609: return false;
2610: }
2611:
2612: public boolean stopEditing()
2613: {
2614: TreeUI ui = getUI();
2615:
2616: if (isEditing())
2617: if (ui != null)
2618: return ui.stopEditing(this);
2619:
2620: return false;
2621: }
2622:
2623: public void cancelEditing()
2624: {
2625: TreeUI ui = getUI();
2626:
2627: if (isEditing())
2628: if (ui != null)
2629: ui.cancelEditing(this);
2630: }
2631:
2632: public void startEditingAtPath(TreePath path)
2633: {
2634: TreeUI ui = getUI();
2635:
2636: if (ui != null)
2637: ui.startEditingAtPath(this, path);
2638: }
2639:
2640: public TreePath getEditingPath()
2641: {
2642: TreeUI ui = getUI();
2643:
2644: if (ui != null)
2645: return ui.getEditingPath(this);
2646:
2647: return null;
2648: }
2649:
2650: public TreePath getPathForLocation(int x, int y)
2651: {
2652: TreePath path = getClosestPathForLocation(x, y);
2653:
2654: if (path != null)
2655: {
2656: Rectangle rect = getPathBounds(path);
2657:
2658: if ((rect != null) && rect.contains(x, y))
2659: return path;
2660: }
2661:
2662: return null;
2663: }
2664:
2665: public int getRowForLocation(int x, int y)
2666: {
2667: TreePath path = getPathForLocation(x, y);
2668:
2669: if (path != null)
2670: return getRowForPath(path);
2671:
2672: return -1;
2673: }
2674:
2675: public TreePath getClosestPathForLocation(int x, int y)
2676: {
2677: TreeUI ui = getUI();
2678:
2679: if (ui != null)
2680: return ui.getClosestPathForLocation(this, x, y);
2681:
2682: return null;
2683: }
2684:
2685: public int getClosestRowForLocation(int x, int y)
2686: {
2687: TreePath path = getClosestPathForLocation(x, y);
2688:
2689: if (path != null)
2690: return getRowForPath(path);
2691:
2692: return -1;
2693: }
2694:
2695: public Object getLastSelectedPathComponent()
2696: {
2697: TreePath path = getSelectionPath();
2698:
2699: if (path != null)
2700: return path.getLastPathComponent();
2701:
2702: return null;
2703: }
2704:
2705: private void doExpandParents(TreePath path, boolean state)
2706: {
2707: TreePath parent = path.getParentPath();
2708:
2709: if (!isExpanded(parent) && parent != null)
2710: doExpandParents(parent, false);
2711:
2712: nodeStates.put(path, state ? EXPANDED : COLLAPSED);
2713: }
2714:
2715: protected void setExpandedState(TreePath path, boolean state)
2716: {
2717: if (path == null)
2718: return;
2719:
2720: doExpandParents(path, state);
2721: }
2722:
2723: protected void clearToggledPaths()
2724: {
2725: nodeStates.clear();
2726: }
2727:
2728: protected Enumeration getDescendantToggledPaths(TreePath parent)
2729: {
2730: if (parent == null)
2731: return null;
2732:
2733: Enumeration nodes = nodeStates.keys();
2734: Vector result = new Vector();
2735:
2736: while (nodes.hasMoreElements())
2737: {
2738: TreePath path = (TreePath) nodes.nextElement();
2739:
2740: if (path.isDescendant(parent))
2741: result.addElement(path);
2742: }
2743:
2744: return result.elements();
2745: }
2746:
2747: public boolean hasBeenExpanded(TreePath path)
2748: {
2749: if (path == null)
2750: return false;
2751:
2752: return nodeStates.get(path) != null;
2753: }
2754:
2755: public boolean isVisible(TreePath path)
2756: {
2757: if (path == null)
2758: return false;
2759:
2760: TreePath parent = path.getParentPath();
2761:
2762: if (parent == null)
2763: return true;
2764:
2765: return isExpanded(parent);
2766: }
2767:
2768: public void makeVisible(TreePath path)
2769: {
2770: if (path == null)
2771: return;
2772:
2773: expandPath(path.getParentPath());
2774: }
2775:
2776: public boolean isPathEditable(TreePath path)
2777: {
2778: return isEditable();
2779: }
2780:
2781:
2786: protected TreeModelListener createTreeModelListener()
2787: {
2788: return new TreeModelHandler();
2789: }
2790:
2791:
2797: protected static TreeModel getDefaultTreeModel()
2798: {
2799: DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");
2800: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child node 1");
2801: DefaultMutableTreeNode child11 =
2802: new DefaultMutableTreeNode("Child node 1.1");
2803: DefaultMutableTreeNode child12 =
2804: new DefaultMutableTreeNode("Child node 1.2");
2805: DefaultMutableTreeNode child13 =
2806: new DefaultMutableTreeNode("Child node 1.3");
2807: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child node 2");
2808: DefaultMutableTreeNode child21 =
2809: new DefaultMutableTreeNode("Child node 2.1");
2810: DefaultMutableTreeNode child22 =
2811: new DefaultMutableTreeNode("Child node 2.2");
2812: DefaultMutableTreeNode child23 =
2813: new DefaultMutableTreeNode("Child node 2.3");
2814: DefaultMutableTreeNode child24 =
2815: new DefaultMutableTreeNode("Child node 2.4");
2816:
2817: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child node 3");
2818: root.add(child1);
2819: root.add(child2);
2820: root.add(child3);
2821: child1.add(child11);
2822: child1.add(child12);
2823: child1.add(child13);
2824: child2.add(child21);
2825: child2.add(child22);
2826: child2.add(child23);
2827: child2.add(child24);
2828: return new DefaultTreeModel(root);
2829: }
2830:
2831:
2846: public String convertValueToText(Object value, boolean selected,
2847: boolean expanded, boolean leaf, int row, boolean hasFocus)
2848: {
2849: return value.toString();
2850: }
2851:
2852:
2859: protected String paramString()
2860: {
2861:
2862:
2863:
2864: return "";
2865: }
2866:
2867:
2877: public Enumeration getExpandedDescendants(TreePath path)
2878: {
2879: Enumeration paths = nodeStates.keys();
2880: Vector relevantPaths = new Vector();
2881: while (paths.hasMoreElements())
2882: {
2883: TreePath nextPath = (TreePath) paths.nextElement();
2884: if (nodeStates.get(nextPath) == EXPANDED
2885: && path.isDescendant(nextPath))
2886: {
2887: relevantPaths.add(nextPath);
2888: }
2889: }
2890: return relevantPaths.elements();
2891: }
2892:
2893:
2911: public TreePath getNextMatch(String prefix, int startingRow,
2912: Position.Bias bias)
2913: {
2914: if (prefix == null)
2915: throw new IllegalArgumentException("The argument 'prefix' must not be"
2916: + " null.");
2917: if (startingRow < 0)
2918: throw new IllegalArgumentException("The argument 'startingRow' must not"
2919: + " be less than zero.");
2920:
2921: int size = getRowCount();
2922: if (startingRow > size)
2923: throw new IllegalArgumentException("The argument 'startingRow' must not"
2924: + " be greater than the number of"
2925: + " elements in the TreeModel.");
2926:
2927: TreePath foundPath = null;
2928: if (bias == Position.Bias.Forward)
2929: {
2930: for (int i = startingRow; i < size; i++)
2931: {
2932: TreePath path = getPathForRow(i);
2933: Object o = path.getLastPathComponent();
2934:
2935:
2936: String item = convertValueToText(o, isRowSelected(i),
2937: isExpanded(i), treeModel.isLeaf(o),
2938: i, false);
2939: if (item.startsWith(prefix))
2940: {
2941: foundPath = path;
2942: break;
2943: }
2944: }
2945: }
2946: else
2947: {
2948: for (int i = startingRow; i >= 0; i--)
2949: {
2950: TreePath path = getPathForRow(i);
2951: Object o = path.getLastPathComponent();
2952:
2953:
2954: String item = convertValueToText(o, isRowSelected(i),
2955: isExpanded(i), treeModel.isLeaf(o), i, false);
2956: if (item.startsWith(prefix))
2957: {
2958: foundPath = path;
2959: break;
2960: }
2961: }
2962: }
2963: return foundPath;
2964: }
2965:
2966:
2981: protected boolean removeDescendantSelectedPaths(TreePath path,
2982: boolean includeSelected)
2983: {
2984: boolean removedSomething = false;
2985: TreePath[] selected = getSelectionPaths();
2986: for (int index = 0; index < selected.length; index++)
2987: {
2988: if ((selected[index] == path && includeSelected)
2989: || (selected[index].isDescendant(path)))
2990: {
2991: removeSelectionPath(selected[index]);
2992: removedSomething = true;
2993: }
2994: }
2995: return removedSomething;
2996: }
2997:
2998:
3005: protected void removeDescendantToggledPaths(Enumeration toRemove)
3006: {
3007: while (toRemove.hasMoreElements())
3008: {
3009: TreePath current = (TreePath) toRemove.nextElement();
3010: Enumeration descendants = getDescendantToggledPaths(current);
3011:
3012: while (descendants.hasMoreElements())
3013: {
3014: TreePath currentDes = (TreePath) descendants.nextElement();
3015: if (isExpanded(currentDes))
3016: nodeStates.remove(currentDes);
3017: }
3018: }
3019: }
3020:
3021:
3037: public void treeDidChange()
3038: {
3039: repaint();
3040: }
3041:
3042:
3056: void setUIProperty(String propertyName, Object value)
3057: {
3058: if (propertyName.equals("rowHeight"))
3059: {
3060: if (! clientRowHeightSet)
3061: {
3062: setRowHeight(((Integer) value).intValue());
3063: clientRowHeightSet = false;
3064: }
3065: }
3066: else if (propertyName.equals("scrollsOnExpand"))
3067: {
3068: if (! clientScrollsOnExpandSet)
3069: {
3070: setScrollsOnExpand(((Boolean) value).booleanValue());
3071: clientScrollsOnExpandSet = false;
3072: }
3073: }
3074: else if (propertyName.equals("showsRootHandles"))
3075: {
3076: if (! clientShowsRootHandlesSet)
3077: {
3078: setShowsRootHandles(((Boolean) value).booleanValue());
3079: clientShowsRootHandlesSet = false;
3080: }
3081: }
3082: else
3083: {
3084: super.setUIProperty(propertyName, value);
3085: }
3086: }
3087: }