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 = new Object();
1396:
1397: private static final Object COLLAPSED = new Object();
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:
1520:
1521:
1522: updateUI();
1523: setModel(model);
1524: }
1525:
1526:
1531: public JTree(TreeNode root)
1532: {
1533: this(root, false);
1534: }
1535:
1536:
1544: public JTree(TreeNode root, boolean asksAllowChildren)
1545: {
1546: this(new DefaultTreeModel(root, asksAllowChildren));
1547: }
1548:
1549:
1554: public JTree(Vector value)
1555: {
1556: this(createTreeModel(value));
1557: }
1558:
1559: public int getRowForPath(TreePath path)
1560: {
1561: TreeUI ui = getUI();
1562:
1563: if (ui != null)
1564: return ui.getRowForPath(this, path);
1565:
1566: return -1;
1567: }
1568:
1569: public TreePath getPathForRow(int row)
1570: {
1571: TreeUI ui = getUI();
1572: return ui != null ? ui.getPathForRow(this, row) : null;
1573: }
1574:
1575:
1583: protected TreePath[] getPathBetweenRows(int index0, int index1)
1584: {
1585: TreeUI ui = getUI();
1586:
1587: if (ui == null)
1588: return null;
1589:
1590: int minIndex = Math.min(index0, index1);
1591: int maxIndex = Math.max(index0, index1);
1592: TreePath[] paths = new TreePath[maxIndex - minIndex + 1];
1593:
1594: for (int i = minIndex; i <= maxIndex; ++i)
1595: paths[i - minIndex] = ui.getPathForRow(this, i);
1596:
1597: return paths;
1598: }
1599:
1600:
1605: protected static TreeModel createTreeModel(Object value)
1606: {
1607: return new DefaultTreeModel(new DynamicUtilTreeNode(value, value));
1608: }
1609:
1610:
1615: public TreeUI getUI()
1616: {
1617: return (TreeUI) ui;
1618: }
1619:
1620:
1625: public void setUI(TreeUI ui)
1626: {
1627: super.setUI(ui);
1628: }
1629:
1630:
1633: public void updateUI()
1634: {
1635: setUI((TreeUI) UIManager.getUI(this));
1636: }
1637:
1638:
1643: public String getUIClassID()
1644: {
1645: return "TreeUI";
1646: }
1647:
1648:
1654: public AccessibleContext getAccessibleContext()
1655: {
1656: return new AccessibleJTree();
1657: }
1658:
1659:
1664: public Dimension getPreferredScrollableViewportSize()
1665: {
1666: return getPreferredSize();
1667: }
1668:
1669:
1682: public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation,
1683: int direction)
1684: {
1685: int delta;
1686:
1687:
1688: if (orientation == SwingConstants.VERTICAL)
1689: {
1690:
1691: int row = getClosestRowForLocation(visibleRect.x, visibleRect.y + 1);
1692: row = row + direction;
1693: if (row < 0)
1694: row = 0;
1695:
1696: Rectangle newTop = getRowBounds(row);
1697: delta = newTop.y - visibleRect.y;
1698: }
1699: else
1700: delta = direction * rowHeight == 0 ? 20 : rowHeight;
1701: return delta;
1702: }
1703:
1704: public int getScrollableBlockIncrement(Rectangle visibleRect,
1705: int orientation, int direction)
1706: {
1707: return getScrollableUnitIncrement(visibleRect, orientation, direction);
1708: }
1709:
1710: public boolean getScrollableTracksViewportHeight()
1711: {
1712: if (getParent() instanceof JViewport)
1713: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1714: return false;
1715: }
1716:
1717: public boolean getScrollableTracksViewportWidth()
1718: {
1719: if (getParent() instanceof JViewport)
1720: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1721: return false;
1722: }
1723:
1724:
1729: public void addTreeExpansionListener(TreeExpansionListener listener)
1730: {
1731: listenerList.add(TreeExpansionListener.class, listener);
1732: }
1733:
1734:
1739: public void removeTreeExpansionListener(TreeExpansionListener listener)
1740: {
1741: listenerList.remove(TreeExpansionListener.class, listener);
1742: }
1743:
1744:
1749: public TreeExpansionListener[] getTreeExpansionListeners()
1750: {
1751: return (TreeExpansionListener[]) getListeners(TreeExpansionListener.class);
1752: }
1753:
1754:
1759: public void fireTreeCollapsed(TreePath path)
1760: {
1761: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1762: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1763:
1764: for (int index = 0; index < listeners.length; ++index)
1765: listeners[index].treeCollapsed(event);
1766: }
1767:
1768:
1773: public void fireTreeExpanded(TreePath path)
1774: {
1775: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1776: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1777:
1778: for (int index = 0; index < listeners.length; ++index)
1779: listeners[index].treeExpanded(event);
1780: }
1781:
1782:
1787: public void addTreeSelectionListener(TreeSelectionListener listener)
1788: {
1789: listenerList.add(TreeSelectionListener.class, listener);
1790: }
1791:
1792:
1797: public void removeTreeSelectionListener(TreeSelectionListener listener)
1798: {
1799: listenerList.remove(TreeSelectionListener.class, listener);
1800: }
1801:
1802:
1807: public TreeSelectionListener[] getTreeSelectionListeners()
1808: {
1809: return (TreeSelectionListener[])
1810: getListeners(TreeSelectionListener.class);
1811: }
1812:
1813:
1818: protected void fireValueChanged(TreeSelectionEvent event)
1819: {
1820: TreeSelectionListener[] listeners = getTreeSelectionListeners();
1821:
1822: for (int index = 0; index < listeners.length; ++index)
1823: listeners[index].valueChanged(event);
1824: }
1825:
1826:
1831: public void addTreeWillExpandListener(TreeWillExpandListener listener)
1832: {
1833: listenerList.add(TreeWillExpandListener.class, listener);
1834: }
1835:
1836:
1841: public void removeTreeWillExpandListener(TreeWillExpandListener listener)
1842: {
1843: listenerList.remove(TreeWillExpandListener.class, listener);
1844: }
1845:
1846:
1851: public TreeWillExpandListener[] getTreeWillExpandListeners()
1852: {
1853: return (TreeWillExpandListener[])
1854: getListeners(TreeWillExpandListener.class);
1855: }
1856:
1857:
1862: public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException
1863: {
1864: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1865: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1866:
1867: for (int index = 0; index < listeners.length; ++index)
1868: listeners[index].treeWillCollapse(event);
1869: }
1870:
1871:
1876: public void fireTreeWillExpand(TreePath path) throws ExpandVetoException
1877: {
1878: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1879: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1880:
1881: for (int index = 0; index < listeners.length; ++index)
1882: listeners[index].treeWillExpand(event);
1883: }
1884:
1885:
1890: public TreeModel getModel()
1891: {
1892: return treeModel;
1893: }
1894:
1895:
1900: public void setModel(TreeModel model)
1901: {
1902: if (treeModel == model)
1903: return;
1904:
1905:
1906: if (treeModel != null && treeModelListener != null)
1907: treeModel.removeTreeModelListener(treeModelListener);
1908:
1909:
1910: if (treeModelListener == null)
1911: treeModelListener = createTreeModelListener();
1912: if (model != null)
1913: model.addTreeModelListener(treeModelListener);
1914:
1915: TreeModel oldValue = treeModel;
1916: treeModel = model;
1917: clearToggledPaths();
1918:
1919: if (treeModel != null)
1920: {
1921: if (treeModelListener == null)
1922: treeModelListener = createTreeModelListener();
1923: if (treeModelListener != null)
1924: treeModel.addTreeModelListener(treeModelListener);
1925: Object root = treeModel.getRoot();
1926: if (root != null && !treeModel.isLeaf(root))
1927: {
1928: nodeStates.put(new TreePath(root), Boolean.TRUE);
1929: }
1930: }
1931:
1932: firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model);
1933: }
1934:
1935:
1941: public boolean isEditable()
1942: {
1943: return editable;
1944: }
1945:
1946:
1952: public void setEditable(boolean flag)
1953: {
1954: if (editable == flag)
1955: return;
1956:
1957: boolean oldValue = editable;
1958: editable = flag;
1959: firePropertyChange(EDITABLE_PROPERTY, oldValue, editable);
1960: }
1961:
1962:
1968: public boolean isRootVisible()
1969: {
1970: return rootVisible;
1971: }
1972:
1973: public void setRootVisible(boolean flag)
1974: {
1975: if (rootVisible == flag)
1976: return;
1977:
1978:
1979: if (rootVisible && !flag)
1980: {
1981: TreeSelectionModel model = getSelectionModel();
1982:
1983: TreePath rootPath = getPathForRow(0);
1984: model.removeSelectionPath(rootPath);
1985: }
1986:
1987: boolean oldValue = rootVisible;
1988: rootVisible = flag;
1989: firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, flag);
1990:
1991: }
1992:
1993: public boolean getShowsRootHandles()
1994: {
1995: return showsRootHandles;
1996: }
1997:
1998: public void setShowsRootHandles(boolean flag)
1999: {
2000: clientShowsRootHandlesSet = true;
2001:
2002: if (showsRootHandles == flag)
2003: return;
2004:
2005: boolean oldValue = showsRootHandles;
2006: showsRootHandles = flag;
2007: firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue, flag);
2008: }
2009:
2010: public TreeCellEditor getCellEditor()
2011: {
2012: return cellEditor;
2013: }
2014:
2015: public void setCellEditor(TreeCellEditor editor)
2016: {
2017: if (cellEditor == editor)
2018: return;
2019:
2020: TreeCellEditor oldValue = cellEditor;
2021: cellEditor = editor;
2022: firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor);
2023: }
2024:
2025: public TreeCellRenderer getCellRenderer()
2026: {
2027: return cellRenderer;
2028: }
2029:
2030: public void setCellRenderer(TreeCellRenderer newRenderer)
2031: {
2032: if (cellRenderer == newRenderer)
2033: return;
2034:
2035: TreeCellRenderer oldValue = cellRenderer;
2036: cellRenderer = newRenderer;
2037: firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, newRenderer);
2038: }
2039:
2040: public TreeSelectionModel getSelectionModel()
2041: {
2042: return selectionModel;
2043: }
2044:
2045: public void setSelectionModel(TreeSelectionModel model)
2046: {
2047: if (selectionModel == model)
2048: return;
2049:
2050: if (selectionModel != null)
2051: selectionModel.removeTreeSelectionListener(selectionRedirector);
2052:
2053: TreeSelectionModel oldValue = selectionModel;
2054: selectionModel = model;
2055:
2056: if (selectionModel != null)
2057: selectionModel.addTreeSelectionListener(selectionRedirector);
2058:
2059: firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
2060: revalidate();
2061: repaint();
2062: }
2063:
2064: public int getVisibleRowCount()
2065: {
2066: return visibleRowCount;
2067: }
2068:
2069: public void setVisibleRowCount(int rows)
2070: {
2071: if (visibleRowCount == rows)
2072: return;
2073:
2074: int oldValue = visibleRowCount;
2075: visibleRowCount = rows;
2076: firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, rows);
2077: }
2078:
2079: public boolean isLargeModel()
2080: {
2081: return largeModel;
2082: }
2083:
2084: public void setLargeModel(boolean large)
2085: {
2086: if (largeModel == large)
2087: return;
2088:
2089: boolean oldValue = largeModel;
2090: largeModel = large;
2091: firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large);
2092: }
2093:
2094: public int getRowHeight()
2095: {
2096: return rowHeight;
2097: }
2098:
2099: public void setRowHeight(int height)
2100: {
2101: clientRowHeightSet = true;
2102:
2103: if (rowHeight == height)
2104: return;
2105:
2106: int oldValue = rowHeight;
2107: rowHeight = height;
2108: firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height);
2109: }
2110:
2111: public boolean isFixedRowHeight()
2112: {
2113: return rowHeight > 0;
2114: }
2115:
2116: public boolean getInvokesStopCellEditing()
2117: {
2118: return invokesStopCellEditing;
2119: }
2120:
2121: public void setInvokesStopCellEditing(boolean invoke)
2122: {
2123: if (invokesStopCellEditing == invoke)
2124: return;
2125:
2126: boolean oldValue = invokesStopCellEditing;
2127: invokesStopCellEditing = invoke;
2128: firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY,
2129: oldValue, invoke);
2130: }
2131:
2132:
2135: public int getToggleClickCount()
2136: {
2137: return toggleClickCount;
2138: }
2139:
2140:
2143: public void setToggleClickCount(int count)
2144: {
2145: if (toggleClickCount == count)
2146: return;
2147:
2148: int oldValue = toggleClickCount;
2149: toggleClickCount = count;
2150: firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count);
2151: }
2152:
2153: public void scrollPathToVisible(TreePath path)
2154: {
2155: if (path == null)
2156: return;
2157: Rectangle rect = getPathBounds(path);
2158: scrollRectToVisible(rect);
2159: }
2160:
2161: public void scrollRowToVisible(int row)
2162: {
2163: scrollPathToVisible(getPathForRow(row));
2164: }
2165:
2166: public boolean getScrollsOnExpand()
2167: {
2168: return scrollsOnExpand;
2169: }
2170:
2171: public void setScrollsOnExpand(boolean scroll)
2172: {
2173: clientScrollsOnExpandSet = true;
2174: if (scrollsOnExpand == scroll)
2175: return;
2176:
2177: boolean oldValue = scrollsOnExpand;
2178: scrollsOnExpand = scroll;
2179: firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll);
2180: }
2181:
2182: public void setSelectionPath(TreePath path)
2183: {
2184: selectionModel.setSelectionPath(path);
2185: }
2186:
2187: public void setSelectionPaths(TreePath[] paths)
2188: {
2189: selectionModel.setSelectionPaths(paths);
2190: }
2191:
2192: public void setSelectionRow(int row)
2193: {
2194: TreePath path = getPathForRow(row);
2195:
2196: if (path != null)
2197: selectionModel.setSelectionPath(path);
2198: }
2199:
2200: public void setSelectionRows(int[] rows)
2201: {
2202:
2203: if (rows == null || getUI() == null)
2204: return;
2205:
2206: TreePath[] paths = new TreePath[rows.length];
2207:
2208: for (int i = rows.length - 1; i >= 0; --i)
2209: paths[i] = getPathForRow(rows[i]);
2210:
2211: setSelectionPaths(paths);
2212: }
2213:
2214: public void setSelectionInterval(int index0, int index1)
2215: {
2216: TreePath[] paths = getPathBetweenRows(index0, index1);
2217:
2218: if (paths != null)
2219: setSelectionPaths(paths);
2220: }
2221:
2222: public void addSelectionPath(TreePath path)
2223: {
2224: selectionModel.addSelectionPath(path);
2225: }
2226:
2227: public void addSelectionPaths(TreePath[] paths)
2228: {
2229: selectionModel.addSelectionPaths(paths);
2230: }
2231:
2232: public void addSelectionRow(int row)
2233: {
2234: TreePath path = getPathForRow(row);
2235:
2236: if (path != null)
2237: selectionModel.addSelectionPath(path);
2238: }
2239:
2240: public void addSelectionRows(int[] rows)
2241: {
2242:
2243: if (rows == null || getUI() == null)
2244: return;
2245:
2246: TreePath[] paths = new TreePath[rows.length];
2247:
2248: for (int i = rows.length - 1; i >= 0; --i)
2249: paths[i] = getPathForRow(rows[i]);
2250:
2251: addSelectionPaths(paths);
2252: }
2253:
2254:
2262: public void addSelectionInterval(int index0, int index1)
2263: {
2264: TreePath[] paths = getPathBetweenRows(index0, index1);
2265:
2266: if (paths != null)
2267: addSelectionPaths(paths);
2268: }
2269:
2270: public void removeSelectionPath(TreePath path)
2271: {
2272: selectionModel.removeSelectionPath(path);
2273: }
2274:
2275: public void removeSelectionPaths(TreePath[] paths)
2276: {
2277: selectionModel.removeSelectionPaths(paths);
2278: }
2279:
2280: public void removeSelectionRow(int row)
2281: {
2282: TreePath path = getPathForRow(row);
2283:
2284: if (path != null)
2285: selectionModel.removeSelectionPath(path);
2286: }
2287:
2288: public void removeSelectionRows(int[] rows)
2289: {
2290: if (rows == null || getUI() == null)
2291: return;
2292:
2293: TreePath[] paths = new TreePath[rows.length];
2294:
2295: for (int i = rows.length - 1; i >= 0; --i)
2296: paths[i] = getPathForRow(rows[i]);
2297:
2298: removeSelectionPaths(paths);
2299: }
2300:
2301: public void removeSelectionInterval(int index0, int index1)
2302: {
2303: TreePath[] paths = getPathBetweenRows(index0, index1);
2304:
2305: if (paths != null)
2306: removeSelectionPaths(paths);
2307: }
2308:
2309: public void clearSelection()
2310: {
2311: selectionModel.clearSelection();
2312: setLeadSelectionPath(null);
2313: }
2314:
2315: public TreePath getLeadSelectionPath()
2316: {
2317: if (selectionModel == null)
2318: return null;
2319: else
2320: return selectionModel.getLeadSelectionPath();
2321: }
2322:
2323:
2326: public void setLeadSelectionPath(TreePath path)
2327: {
2328: if (selectionModel != null)
2329: {
2330: TreePath oldValue = selectionModel.getLeadSelectionPath();
2331: if (path.equals(oldValue))
2332: return;
2333:
2334:
2335: if (path != null)
2336: {
2337: repaint(getPathBounds(path));
2338: selectionModel.addSelectionPath(path);
2339: }
2340:
2341: if (oldValue != null)
2342: repaint(getPathBounds(oldValue));
2343:
2344: firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
2345: }
2346: }
2347:
2348:
2351: public TreePath getAnchorSelectionPath()
2352: {
2353: return anchorSelectionPath;
2354: }
2355:
2356:
2359: public void setAnchorSelectionPath(TreePath path)
2360: {
2361: if (anchorSelectionPath == path)
2362: return;
2363:
2364: TreePath oldValue = anchorSelectionPath;
2365: anchorSelectionPath = path;
2366: firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, path);
2367: }
2368:
2369: public int getLeadSelectionRow()
2370: {
2371: return selectionModel.getLeadSelectionRow();
2372: }
2373:
2374: public int getMaxSelectionRow()
2375: {
2376: return selectionModel.getMaxSelectionRow();
2377: }
2378:
2379: public int getMinSelectionRow()
2380: {
2381: return selectionModel.getMinSelectionRow();
2382: }
2383:
2384: public int getSelectionCount()
2385: {
2386: return selectionModel.getSelectionCount();
2387: }
2388:
2389: public TreePath getSelectionPath()
2390: {
2391: return selectionModel.getSelectionPath();
2392: }
2393:
2394: public TreePath[] getSelectionPaths()
2395: {
2396: return selectionModel.getSelectionPaths();
2397: }
2398:
2399: public int[] getSelectionRows()
2400: {
2401: return selectionModel.getSelectionRows();
2402: }
2403:
2404: public boolean isPathSelected(TreePath path)
2405: {
2406: return selectionModel.isPathSelected(path);
2407: }
2408:
2409: public boolean isRowSelected(int row)
2410: {
2411: return selectionModel.isPathSelected(getPathForRow(row));
2412: }
2413:
2414: public boolean isSelectionEmpty()
2415: {
2416: return selectionModel.isSelectionEmpty();
2417: }
2418:
2419:
2426: public boolean getDragEnabled()
2427: {
2428: return dragEnabled;
2429: }
2430:
2431:
2438: public void setDragEnabled(boolean enabled)
2439: {
2440: dragEnabled = enabled;
2441: }
2442:
2443: public int getRowCount()
2444: {
2445: TreeUI ui = getUI();
2446:
2447: if (ui != null)
2448: return ui.getRowCount(this);
2449:
2450: return 0;
2451: }
2452:
2453: public void collapsePath(TreePath path)
2454: {
2455: try
2456: {
2457: fireTreeWillCollapse(path);
2458: }
2459: catch (ExpandVetoException ev)
2460: {
2461:
2462: }
2463: setExpandedState(path, false);
2464: fireTreeCollapsed(path);
2465: }
2466:
2467: public void collapseRow(int row)
2468: {
2469: if (row < 0 || row >= getRowCount())
2470: return;
2471:
2472: TreePath path = getPathForRow(row);
2473:
2474: if (path != null)
2475: collapsePath(path);
2476: }
2477:
2478: public void expandPath(TreePath path)
2479: {
2480:
2481:
2482: if (path == null || isExpanded(path))
2483: return;
2484:
2485: try
2486: {
2487: fireTreeWillExpand(path);
2488: }
2489: catch (ExpandVetoException ev)
2490: {
2491:
2492: }
2493:
2494: setExpandedState(path, true);
2495: fireTreeExpanded(path);
2496: }
2497:
2498: public void expandRow(int row)
2499: {
2500: if (row < 0 || row >= getRowCount())
2501: return;
2502:
2503: TreePath path = getPathForRow(row);
2504:
2505: if (path != null)
2506: expandPath(path);
2507: }
2508:
2509: public boolean isCollapsed(TreePath path)
2510: {
2511: return !isExpanded(path);
2512: }
2513:
2514: public boolean isCollapsed(int row)
2515: {
2516: if (row < 0 || row >= getRowCount())
2517: return false;
2518:
2519: TreePath path = getPathForRow(row);
2520:
2521: if (path != null)
2522: return isCollapsed(path);
2523:
2524: return false;
2525: }
2526:
2527: public boolean isExpanded(TreePath path)
2528: {
2529: if (path == null)
2530: return false;
2531:
2532: Object state = nodeStates.get(path);
2533:
2534: if ((state == null) || (state != EXPANDED))
2535: return false;
2536:
2537: TreePath parent = path.getParentPath();
2538:
2539: if (parent != null)
2540: return isExpanded(parent);
2541:
2542: return true;
2543: }
2544:
2545: public boolean isExpanded(int row)
2546: {
2547: if (row < 0 || row >= getRowCount())
2548: return false;
2549:
2550: TreePath path = getPathForRow(row);
2551:
2552: if (path != null)
2553: return isExpanded(path);
2554:
2555: return false;
2556: }
2557:
2558:
2561: public boolean getExpandsSelectedPaths()
2562: {
2563: return expandsSelectedPaths;
2564: }
2565:
2566:
2569: public void setExpandsSelectedPaths(boolean flag)
2570: {
2571: if (expandsSelectedPaths == flag)
2572: return;
2573:
2574: boolean oldValue = expandsSelectedPaths;
2575: expandsSelectedPaths = flag;
2576: firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue, flag);
2577: }
2578:
2579: public Rectangle getPathBounds(TreePath path)
2580: {
2581: TreeUI ui = getUI();
2582:
2583: if (ui == null)
2584: return null;
2585:
2586: return ui.getPathBounds(this, path);
2587: }
2588:
2589: public Rectangle getRowBounds(int row)
2590: {
2591: TreePath path = getPathForRow(row);
2592:
2593: if (path != null)
2594: return getPathBounds(path);
2595:
2596: return null;
2597: }
2598:
2599: public boolean isEditing()
2600: {
2601: TreeUI ui = getUI();
2602:
2603: if (ui != null)
2604: return ui.isEditing(this);
2605:
2606: return false;
2607: }
2608:
2609: public boolean stopEditing()
2610: {
2611: TreeUI ui = getUI();
2612:
2613: if (isEditing())
2614: if (ui != null)
2615: return ui.stopEditing(this);
2616:
2617: return false;
2618: }
2619:
2620: public void cancelEditing()
2621: {
2622: TreeUI ui = getUI();
2623:
2624: if (isEditing())
2625: if (ui != null)
2626: ui.cancelEditing(this);
2627: }
2628:
2629: public void startEditingAtPath(TreePath path)
2630: {
2631: TreeUI ui = getUI();
2632:
2633: if (ui != null)
2634: ui.startEditingAtPath(this, path);
2635: }
2636:
2637: public TreePath getEditingPath()
2638: {
2639: TreeUI ui = getUI();
2640:
2641: if (ui != null)
2642: return ui.getEditingPath(this);
2643:
2644: return null;
2645: }
2646:
2647: public TreePath getPathForLocation(int x, int y)
2648: {
2649: TreePath path = getClosestPathForLocation(x, y);
2650:
2651: if (path != null)
2652: {
2653: Rectangle rect = getPathBounds(path);
2654:
2655: if ((rect != null) && rect.contains(x, y))
2656: return path;
2657: }
2658:
2659: return null;
2660: }
2661:
2662: public int getRowForLocation(int x, int y)
2663: {
2664: TreePath path = getPathForLocation(x, y);
2665:
2666: if (path != null)
2667: return getRowForPath(path);
2668:
2669: return -1;
2670: }
2671:
2672: public TreePath getClosestPathForLocation(int x, int y)
2673: {
2674: TreeUI ui = getUI();
2675:
2676: if (ui != null)
2677: return ui.getClosestPathForLocation(this, x, y);
2678:
2679: return null;
2680: }
2681:
2682: public int getClosestRowForLocation(int x, int y)
2683: {
2684: TreePath path = getClosestPathForLocation(x, y);
2685:
2686: if (path != null)
2687: return getRowForPath(path);
2688:
2689: return -1;
2690: }
2691:
2692: public Object getLastSelectedPathComponent()
2693: {
2694: TreePath path = getSelectionPath();
2695:
2696: if (path != null)
2697: return path.getLastPathComponent();
2698:
2699: return null;
2700: }
2701:
2702: private void doExpandParents(TreePath path, boolean state)
2703: {
2704: TreePath parent = path.getParentPath();
2705:
2706: if (!isExpanded(parent) && parent != null)
2707: doExpandParents(parent, false);
2708:
2709: nodeStates.put(path, state ? EXPANDED : COLLAPSED);
2710: }
2711:
2712: protected void setExpandedState(TreePath path, boolean state)
2713: {
2714: if (path == null)
2715: return;
2716:
2717: doExpandParents(path, state);
2718: }
2719:
2720: protected void clearToggledPaths()
2721: {
2722: nodeStates.clear();
2723: }
2724:
2725: protected Enumeration getDescendantToggledPaths(TreePath parent)
2726: {
2727: if (parent == null)
2728: return null;
2729:
2730: Enumeration nodes = nodeStates.keys();
2731: Vector result = new Vector();
2732:
2733: while (nodes.hasMoreElements())
2734: {
2735: TreePath path = (TreePath) nodes.nextElement();
2736:
2737: if (path.isDescendant(parent))
2738: result.addElement(path);
2739: }
2740:
2741: return result.elements();
2742: }
2743:
2744: public boolean hasBeenExpanded(TreePath path)
2745: {
2746: if (path == null)
2747: return false;
2748:
2749: return nodeStates.get(path) != null;
2750: }
2751:
2752: public boolean isVisible(TreePath path)
2753: {
2754: if (path == null)
2755: return false;
2756:
2757: TreePath parent = path.getParentPath();
2758:
2759: if (parent == null)
2760: return true;
2761:
2762: return isExpanded(parent);
2763: }
2764:
2765: public void makeVisible(TreePath path)
2766: {
2767: if (path == null)
2768: return;
2769:
2770: expandPath(path.getParentPath());
2771: }
2772:
2773: public boolean isPathEditable(TreePath path)
2774: {
2775: return isEditable();
2776: }
2777:
2778:
2783: protected TreeModelListener createTreeModelListener()
2784: {
2785: return new TreeModelHandler();
2786: }
2787:
2788:
2794: protected static TreeModel getDefaultTreeModel()
2795: {
2796: DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");
2797: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child node 1");
2798: DefaultMutableTreeNode child11 =
2799: new DefaultMutableTreeNode("Child node 1.1");
2800: DefaultMutableTreeNode child12 =
2801: new DefaultMutableTreeNode("Child node 1.2");
2802: DefaultMutableTreeNode child13 =
2803: new DefaultMutableTreeNode("Child node 1.3");
2804: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child node 2");
2805: DefaultMutableTreeNode child21 =
2806: new DefaultMutableTreeNode("Child node 2.1");
2807: DefaultMutableTreeNode child22 =
2808: new DefaultMutableTreeNode("Child node 2.2");
2809: DefaultMutableTreeNode child23 =
2810: new DefaultMutableTreeNode("Child node 2.3");
2811: DefaultMutableTreeNode child24 =
2812: new DefaultMutableTreeNode("Child node 2.4");
2813:
2814: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child node 3");
2815: root.add(child1);
2816: root.add(child2);
2817: root.add(child3);
2818: child1.add(child11);
2819: child1.add(child12);
2820: child1.add(child13);
2821: child2.add(child21);
2822: child2.add(child22);
2823: child2.add(child23);
2824: child2.add(child24);
2825: return new DefaultTreeModel(root);
2826: }
2827:
2828:
2843: public String convertValueToText(Object value, boolean selected,
2844: boolean expanded, boolean leaf, int row, boolean hasFocus)
2845: {
2846: return value.toString();
2847: }
2848:
2849:
2856: protected String paramString()
2857: {
2858:
2859:
2860:
2861: return "";
2862: }
2863:
2864:
2874: public Enumeration getExpandedDescendants(TreePath path)
2875: {
2876: Enumeration paths = nodeStates.keys();
2877: Vector relevantPaths = new Vector();
2878: while (paths.hasMoreElements())
2879: {
2880: TreePath nextPath = (TreePath) paths.nextElement();
2881: if (nodeStates.get(nextPath) == EXPANDED
2882: && path.isDescendant(nextPath))
2883: {
2884: relevantPaths.add(nextPath);
2885: }
2886: }
2887: return relevantPaths.elements();
2888: }
2889:
2890:
2908: public TreePath getNextMatch(String prefix, int startingRow,
2909: Position.Bias bias)
2910: {
2911: if (prefix == null)
2912: throw new IllegalArgumentException("The argument 'prefix' must not be"
2913: + " null.");
2914: if (startingRow < 0)
2915: throw new IllegalArgumentException("The argument 'startingRow' must not"
2916: + " be less than zero.");
2917:
2918: int size = getRowCount();
2919: if (startingRow > size)
2920: throw new IllegalArgumentException("The argument 'startingRow' must not"
2921: + " be greater than the number of"
2922: + " elements in the TreeModel.");
2923:
2924: TreePath foundPath = null;
2925: if (bias == Position.Bias.Forward)
2926: {
2927: for (int i = startingRow; i < size; i++)
2928: {
2929: TreePath path = getPathForRow(i);
2930: Object o = path.getLastPathComponent();
2931:
2932:
2933: String item = convertValueToText(o, isRowSelected(i),
2934: isExpanded(i), treeModel.isLeaf(o),
2935: i, false);
2936: if (item.startsWith(prefix))
2937: {
2938: foundPath = path;
2939: break;
2940: }
2941: }
2942: }
2943: else
2944: {
2945: for (int i = startingRow; i >= 0; i--)
2946: {
2947: TreePath path = getPathForRow(i);
2948: Object o = path.getLastPathComponent();
2949:
2950:
2951: String item = convertValueToText(o, isRowSelected(i),
2952: isExpanded(i), treeModel.isLeaf(o), i, false);
2953: if (item.startsWith(prefix))
2954: {
2955: foundPath = path;
2956: break;
2957: }
2958: }
2959: }
2960: return foundPath;
2961: }
2962:
2963:
2978: protected boolean removeDescendantSelectedPaths(TreePath path,
2979: boolean includeSelected)
2980: {
2981: boolean removedSomething = false;
2982: TreePath[] selected = getSelectionPaths();
2983: for (int index = 0; index < selected.length; index++)
2984: {
2985: if ((selected[index] == path && includeSelected)
2986: || (selected[index].isDescendant(path)))
2987: {
2988: removeSelectionPath(selected[index]);
2989: removedSomething = true;
2990: }
2991: }
2992: return removedSomething;
2993: }
2994:
2995:
3002: protected void removeDescendantToggledPaths(Enumeration toRemove)
3003: {
3004: while (toRemove.hasMoreElements())
3005: {
3006: TreePath current = (TreePath) toRemove.nextElement();
3007: Enumeration descendants = getDescendantToggledPaths(current);
3008:
3009: while (descendants.hasMoreElements())
3010: {
3011: TreePath currentDes = (TreePath) descendants.nextElement();
3012: if (isExpanded(currentDes))
3013: nodeStates.remove(currentDes);
3014: }
3015: }
3016: }
3017:
3018:
3034: public void treeDidChange()
3035: {
3036: repaint();
3037: }
3038:
3039:
3053: void setUIProperty(String propertyName, Object value)
3054: {
3055: if (propertyName.equals("rowHeight"))
3056: {
3057: if (! clientRowHeightSet)
3058: {
3059: setRowHeight(((Integer) value).intValue());
3060: clientRowHeightSet = false;
3061: }
3062: }
3063: else if (propertyName.equals("scrollsOnExpand"))
3064: {
3065: if (! clientScrollsOnExpandSet)
3066: {
3067: setScrollsOnExpand(((Boolean) value).booleanValue());
3068: clientScrollsOnExpandSet = false;
3069: }
3070: }
3071: else if (propertyName.equals("showsRootHandles"))
3072: {
3073: if (! clientShowsRootHandlesSet)
3074: {
3075: setShowsRootHandles(((Boolean) value).booleanValue());
3076: clientShowsRootHandlesSet = false;
3077: }
3078: }
3079: else
3080: {
3081: super.setUIProperty(propertyName, value);
3082: }
3083: }
3084: }