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 || childValue.getClass()
1147: .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: JTree.this.repaint();
1283: }
1284: }
1285:
1286:
1289: protected static class EmptySelectionModel extends DefaultTreeSelectionModel
1290: {
1291:
1292: private static final long serialVersionUID = -5815023306225701477L;
1293:
1294:
1297: protected static final EmptySelectionModel sharedInstance =
1298: new EmptySelectionModel();
1299:
1300:
1303: protected EmptySelectionModel()
1304: {
1305:
1306: }
1307:
1308:
1313: public static EmptySelectionModel sharedInstance()
1314: {
1315: return sharedInstance;
1316: }
1317:
1318:
1323: public void setSelectionPaths(TreePath[] paths)
1324: {
1325:
1326: }
1327:
1328:
1333: public void addSelectionPaths(TreePath[] paths)
1334: {
1335:
1336: }
1337:
1338:
1343: public void removeSelectionPaths(TreePath[] paths)
1344: {
1345:
1346: }
1347: }
1348:
1349: private static final long serialVersionUID = 7559816092864483649L;
1350:
1351: public static final String CELL_EDITOR_PROPERTY = "cellEditor";
1352:
1353: public static final String CELL_RENDERER_PROPERTY = "cellRenderer";
1354:
1355: public static final String EDITABLE_PROPERTY = "editable";
1356:
1357: public static final String INVOKES_STOP_CELL_EDITING_PROPERTY =
1358: "invokesStopCellEditing";
1359:
1360: public static final String LARGE_MODEL_PROPERTY = "largeModel";
1361:
1362: public static final String ROOT_VISIBLE_PROPERTY = "rootVisible";
1363:
1364: public static final String ROW_HEIGHT_PROPERTY = "rowHeight";
1365:
1366: public static final String SCROLLS_ON_EXPAND_PROPERTY = "scrollsOnExpand";
1367:
1368: public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
1369:
1370: public static final String SHOWS_ROOT_HANDLES_PROPERTY = "showsRootHandles";
1371:
1372: public static final String TOGGLE_CLICK_COUNT_PROPERTY = "toggleClickCount";
1373:
1374: public static final String TREE_MODEL_PROPERTY = "model";
1375:
1376: public static final String VISIBLE_ROW_COUNT_PROPERTY = "visibleRowCount";
1377:
1378:
1379: public static final String ANCHOR_SELECTION_PATH_PROPERTY =
1380: "anchorSelectionPath";
1381:
1382:
1383: public static final String LEAD_SELECTION_PATH_PROPERTY = "leadSelectionPath";
1384:
1385:
1386: public static final String EXPANDS_SELECTED_PATHS_PROPERTY =
1387: "expandsSelectedPaths";
1388:
1389: private static final Object EXPANDED = new Object();
1390:
1391: private static final Object COLLAPSED = new Object();
1392:
1393: private boolean dragEnabled;
1394:
1395: private boolean expandsSelectedPaths;
1396:
1397: private TreePath anchorSelectionPath;
1398:
1399: private TreePath leadSelectionPath;
1400:
1401:
1406: private Hashtable nodeStates = new Hashtable();
1407:
1408: protected transient TreeCellEditor cellEditor;
1409:
1410: protected transient TreeCellRenderer cellRenderer;
1411:
1412: protected boolean editable;
1413:
1414: protected boolean invokesStopCellEditing;
1415:
1416: protected boolean largeModel;
1417:
1418: protected boolean rootVisible;
1419:
1420: protected int rowHeight;
1421:
1422: protected boolean scrollsOnExpand;
1423:
1424: protected transient TreeSelectionModel selectionModel;
1425:
1426: protected boolean showsRootHandles;
1427:
1428: protected int toggleClickCount;
1429:
1430: protected transient TreeModel treeModel;
1431:
1432: protected int visibleRowCount;
1433:
1434:
1437: protected transient TreeModelListener treeModelListener;
1438:
1439:
1442: protected TreeSelectionRedirector selectionRedirector =
1443: new TreeSelectionRedirector();
1444:
1445:
1448: public JTree()
1449: {
1450: this(createTreeModel(null));
1451: }
1452:
1453:
1458: public JTree(Hashtable value)
1459: {
1460: this(createTreeModel(value));
1461: }
1462:
1463:
1468: public JTree(Object[] value)
1469: {
1470: this(createTreeModel(value));
1471: }
1472:
1473:
1478: public JTree(TreeModel model)
1479: {
1480: updateUI();
1481: setRootVisible(true);
1482: setModel(model);
1483: setSelectionModel(EmptySelectionModel.sharedInstance());
1484: }
1485:
1486:
1491: public JTree(TreeNode root)
1492: {
1493: this(root, false);
1494: }
1495:
1496:
1504: public JTree(TreeNode root, boolean asksAllowChildren)
1505: {
1506: this(new DefaultTreeModel(root, asksAllowChildren));
1507: }
1508:
1509:
1514: public JTree(Vector value)
1515: {
1516: this(createTreeModel(value));
1517: }
1518:
1519: public int getRowForPath(TreePath path)
1520: {
1521: TreeUI ui = getUI();
1522:
1523: if (ui != null)
1524: return ui.getRowForPath(this, path);
1525:
1526: return -1;
1527: }
1528:
1529: public TreePath getPathForRow(int row)
1530: {
1531: TreeUI ui = getUI();
1532: return ui != null ? ui.getPathForRow(this, row) : null;
1533: }
1534:
1535: protected TreePath[] getPathBetweenRows(int index0, int index1)
1536: {
1537: TreeUI ui = getUI();
1538:
1539: if (ui == null)
1540: return null;
1541:
1542: int minIndex = Math.min(index0, index1);
1543: int maxIndex = Math.max(index0, index1);
1544: TreePath[] paths = new TreePath[maxIndex - minIndex + 1];
1545:
1546: for (int i = minIndex; i <= maxIndex; ++i)
1547: paths[i - minIndex] = ui.getPathForRow(this, i);
1548:
1549: return paths;
1550: }
1551:
1552:
1557: protected static TreeModel createTreeModel(Object value)
1558: {
1559: return new DefaultTreeModel(new DynamicUtilTreeNode(value, value));
1560: }
1561:
1562:
1567: public TreeUI getUI()
1568: {
1569: return (TreeUI) ui;
1570: }
1571:
1572:
1577: public void setUI(TreeUI ui)
1578: {
1579: super.setUI(ui);
1580: }
1581:
1582:
1585: public void updateUI()
1586: {
1587: setUI((TreeUI) UIManager.getUI(this));
1588: }
1589:
1590:
1595: public String getUIClassID()
1596: {
1597: return "TreeUI";
1598: }
1599:
1600:
1606: public AccessibleContext getAccessibleContext()
1607: {
1608: return new AccessibleJTree();
1609: }
1610:
1611:
1616: public Dimension getPreferredScrollableViewportSize()
1617: {
1618: return new Dimension (getPreferredSize().width, getVisibleRowCount()*getRowHeight());
1619: }
1620:
1621: public int getScrollableUnitIncrement(Rectangle visibleRect,
1622: int orientation, int direction)
1623: {
1624: return 1;
1625: }
1626:
1627: public int getScrollableBlockIncrement(Rectangle visibleRect,
1628: int orientation, int direction)
1629: {
1630: return 1;
1631: }
1632:
1633: public boolean getScrollableTracksViewportHeight()
1634: {
1635: if (getParent() instanceof JViewport)
1636: return ((JViewport) getParent()).getHeight() > getPreferredSize().height;
1637: return false;
1638: }
1639:
1640: public boolean getScrollableTracksViewportWidth()
1641: {
1642: if (getParent() instanceof JViewport)
1643: return ((JViewport) getParent()).getWidth() > getPreferredSize().width;
1644: return false;
1645: }
1646:
1647:
1652: public void addTreeExpansionListener(TreeExpansionListener listener)
1653: {
1654: listenerList.add(TreeExpansionListener.class, listener);
1655: }
1656:
1657:
1662: public void removeTreeExpansionListener(TreeExpansionListener listener)
1663: {
1664: listenerList.remove(TreeExpansionListener.class, listener);
1665: }
1666:
1667:
1672: public TreeExpansionListener[] getTreeExpansionListeners()
1673: {
1674: return (TreeExpansionListener[]) getListeners(TreeExpansionListener.class);
1675: }
1676:
1677:
1682: public void fireTreeCollapsed(TreePath path)
1683: {
1684: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1685: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1686:
1687: for (int index = 0; index < listeners.length; ++index)
1688: listeners[index].treeCollapsed(event);
1689: }
1690:
1691:
1696: public void fireTreeExpanded(TreePath path)
1697: {
1698: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1699: TreeExpansionListener[] listeners = getTreeExpansionListeners();
1700:
1701: for (int index = 0; index < listeners.length; ++index)
1702: listeners[index].treeExpanded(event);
1703: }
1704:
1705:
1710: public void addTreeSelectionListener(TreeSelectionListener listener)
1711: {
1712: listenerList.add(TreeSelectionListener.class, listener);
1713: }
1714:
1715:
1720: public void removeTreeSelectionListener(TreeSelectionListener listener)
1721: {
1722: listenerList.remove(TreeSelectionListener.class, listener);
1723: }
1724:
1725:
1730: public TreeSelectionListener[] getTreeSelectionListeners()
1731: {
1732: return (TreeSelectionListener[])
1733: getListeners(TreeSelectionListener.class);
1734: }
1735:
1736:
1741: protected void fireValueChanged(TreeSelectionEvent event)
1742: {
1743: TreeSelectionListener[] listeners = getTreeSelectionListeners();
1744:
1745: for (int index = 0; index < listeners.length; ++index)
1746: listeners[index].valueChanged(event);
1747: }
1748:
1749:
1754: public void addTreeWillExpandListener(TreeWillExpandListener listener)
1755: {
1756: listenerList.add(TreeWillExpandListener.class, listener);
1757: }
1758:
1759:
1764: public void removeTreeWillExpandListener(TreeWillExpandListener listener)
1765: {
1766: listenerList.remove(TreeWillExpandListener.class, listener);
1767: }
1768:
1769:
1774: public TreeWillExpandListener[] getTreeWillExpandListeners()
1775: {
1776: return (TreeWillExpandListener[])
1777: getListeners(TreeWillExpandListener.class);
1778: }
1779:
1780:
1785: public void fireTreeWillCollapse(TreePath path) throws ExpandVetoException
1786: {
1787: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1788: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1789:
1790: for (int index = 0; index < listeners.length; ++index)
1791: listeners[index].treeWillCollapse(event);
1792: }
1793:
1794:
1799: public void fireTreeWillExpand(TreePath path) throws ExpandVetoException
1800: {
1801: TreeExpansionEvent event = new TreeExpansionEvent(this, path);
1802: TreeWillExpandListener[] listeners = getTreeWillExpandListeners();
1803:
1804: for (int index = 0; index < listeners.length; ++index)
1805: listeners[index].treeWillExpand(event);
1806: }
1807:
1808:
1813: public TreeModel getModel()
1814: {
1815: return treeModel;
1816: }
1817:
1818:
1823: public void setModel(TreeModel model)
1824: {
1825: if (treeModel == model)
1826: return;
1827:
1828:
1829: if (treeModelListener == null)
1830: treeModelListener = createTreeModelListener();
1831: if (model != null)
1832: model.addTreeModelListener(treeModelListener);
1833:
1834: TreeModel oldValue = treeModel;
1835: treeModel = model;
1836:
1837: firePropertyChange(TREE_MODEL_PROPERTY, oldValue, model);
1838: updateUI();
1839: }
1840:
1841:
1847: public boolean isEditable()
1848: {
1849: return editable;
1850: }
1851:
1852:
1858: public void setEditable(boolean flag)
1859: {
1860: if (editable == flag)
1861: return;
1862:
1863: boolean oldValue = editable;
1864: editable = flag;
1865: firePropertyChange(EDITABLE_PROPERTY, oldValue, editable);
1866: }
1867:
1868:
1874: public boolean isRootVisible()
1875: {
1876: return rootVisible;
1877: }
1878:
1879: public void setRootVisible(boolean flag)
1880: {
1881: if (rootVisible == flag)
1882: return;
1883:
1884: boolean oldValue = rootVisible;
1885: rootVisible = flag;
1886: firePropertyChange(ROOT_VISIBLE_PROPERTY, oldValue, flag);
1887: }
1888:
1889: public boolean getShowsRootHandles()
1890: {
1891: return showsRootHandles;
1892: }
1893:
1894: public void setShowsRootHandles(boolean flag)
1895: {
1896: if (showsRootHandles == flag)
1897: return;
1898:
1899: boolean oldValue = showsRootHandles;
1900: showsRootHandles = flag;
1901: firePropertyChange(SHOWS_ROOT_HANDLES_PROPERTY, oldValue, flag);
1902: }
1903:
1904: public TreeCellEditor getCellEditor()
1905: {
1906: return cellEditor;
1907: }
1908:
1909: public void setCellEditor(TreeCellEditor editor)
1910: {
1911: if (cellEditor == editor)
1912: return;
1913:
1914: TreeCellEditor oldValue = cellEditor;
1915: cellEditor = editor;
1916: firePropertyChange(CELL_EDITOR_PROPERTY, oldValue, editor);
1917: }
1918:
1919: public TreeCellRenderer getCellRenderer()
1920: {
1921: return cellRenderer;
1922: }
1923:
1924: public void setCellRenderer(TreeCellRenderer newRenderer)
1925: {
1926: if (cellRenderer == newRenderer)
1927: return;
1928:
1929: TreeCellRenderer oldValue = cellRenderer;
1930: cellRenderer = newRenderer;
1931: firePropertyChange(CELL_RENDERER_PROPERTY, oldValue, newRenderer);
1932: }
1933:
1934: public TreeSelectionModel getSelectionModel()
1935: {
1936: return selectionModel;
1937: }
1938:
1939: public void setSelectionModel(TreeSelectionModel model)
1940: {
1941: if (selectionModel == model)
1942: return;
1943:
1944: if (selectionModel != null)
1945: selectionModel.removeTreeSelectionListener(selectionRedirector);
1946:
1947: TreeSelectionModel oldValue = selectionModel;
1948: selectionModel = model;
1949:
1950: if (selectionModel != null)
1951: selectionModel.addTreeSelectionListener(selectionRedirector);
1952:
1953: firePropertyChange(SELECTION_MODEL_PROPERTY, oldValue, model);
1954: revalidate();
1955: repaint();
1956: }
1957:
1958: public int getVisibleRowCount()
1959: {
1960: return visibleRowCount;
1961: }
1962:
1963: public void setVisibleRowCount(int rows)
1964: {
1965: if (visibleRowCount == rows)
1966: return;
1967:
1968: int oldValue = visibleRowCount;
1969: visibleRowCount = rows;
1970: firePropertyChange(VISIBLE_ROW_COUNT_PROPERTY, oldValue, rows);
1971: }
1972:
1973: public boolean isLargeModel()
1974: {
1975: return largeModel;
1976: }
1977:
1978: public void setLargeModel(boolean large)
1979: {
1980: if (largeModel == large)
1981: return;
1982:
1983: boolean oldValue = largeModel;
1984: largeModel = large;
1985: firePropertyChange(LARGE_MODEL_PROPERTY, oldValue, large);
1986: }
1987:
1988: public int getRowHeight()
1989: {
1990: return rowHeight;
1991: }
1992:
1993: public void setRowHeight(int height)
1994: {
1995: if (rowHeight == height)
1996: return;
1997:
1998: int oldValue = rowHeight;
1999: rowHeight = height;
2000: firePropertyChange(ROW_HEIGHT_PROPERTY, oldValue, height);
2001: }
2002:
2003: public boolean isFixedRowHeight()
2004: {
2005: return rowHeight > 0;
2006: }
2007:
2008: public boolean getInvokesStopCellEditing()
2009: {
2010: return invokesStopCellEditing;
2011: }
2012:
2013: public void setInvokesStopCellEditing(boolean invoke)
2014: {
2015: if (invokesStopCellEditing == invoke)
2016: return;
2017:
2018: boolean oldValue = invokesStopCellEditing;
2019: invokesStopCellEditing = invoke;
2020: firePropertyChange(INVOKES_STOP_CELL_EDITING_PROPERTY,
2021: oldValue, invoke);
2022: }
2023:
2024:
2027: public int getToggleClickCount()
2028: {
2029: return toggleClickCount;
2030: }
2031:
2032:
2035: public void setToggleClickCount(int count)
2036: {
2037: if (toggleClickCount == count)
2038: return;
2039:
2040: int oldValue = toggleClickCount;
2041: toggleClickCount = count;
2042: firePropertyChange(TOGGLE_CLICK_COUNT_PROPERTY, oldValue, count);
2043: }
2044:
2045: public void scrollPathToVisible(TreePath path)
2046: {
2047: if (path == null)
2048: return;
2049:
2050: Object[] oPath = path.getPath();
2051: TreePath temp = new TreePath(oPath[0]);
2052: boolean stop = false;
2053: int i = 1;
2054: while (!stop)
2055: {
2056: while (isVisible(temp))
2057: if (i < oPath.length)
2058: temp = temp.pathByAddingChild(oPath[i++]);
2059: else
2060: {
2061: stop = true;
2062: break;
2063: }
2064: makeVisible(temp);
2065: }
2066: Rectangle rect = getPathBounds(path);
2067: scrollRectToVisible(rect);
2068: setSelectionPath(temp);
2069: }
2070:
2071: public void scrollRowToVisible(int row)
2072: {
2073: scrollPathToVisible(getPathForRow(row));
2074: }
2075:
2076: public boolean getScrollsOnExpand()
2077: {
2078: return scrollsOnExpand;
2079: }
2080:
2081: public void setScrollsOnExpand(boolean scroll)
2082: {
2083: if (scrollsOnExpand == scroll)
2084: return;
2085:
2086: boolean oldValue = scrollsOnExpand;
2087: scrollsOnExpand = scroll;
2088: firePropertyChange(SCROLLS_ON_EXPAND_PROPERTY, oldValue, scroll);
2089: }
2090:
2091: public void setSelectionPath(TreePath path)
2092: {
2093: selectionModel.setSelectionPath(path);
2094: }
2095:
2096: public void setSelectionPaths(TreePath[] paths)
2097: {
2098: selectionModel.setSelectionPaths(paths);
2099: }
2100:
2101: public void setSelectionRow(int row)
2102: {
2103: TreePath path = getPathForRow(row);
2104:
2105: if (path != null)
2106: selectionModel.setSelectionPath(path);
2107: }
2108:
2109: public void setSelectionRows(int[] rows)
2110: {
2111:
2112: if (rows == null || getUI() == null)
2113: return;
2114:
2115: TreePath[] paths = new TreePath[rows.length];
2116:
2117: for (int i = rows.length - 1; i >= 0; --i)
2118: paths[i] = getPathForRow(rows[i]);
2119:
2120: setSelectionPaths(paths);
2121: }
2122:
2123: public void setSelectionInterval(int index0, int index1)
2124: {
2125: TreePath[] paths = getPathBetweenRows(index0, index1);
2126:
2127: if (paths != null)
2128: setSelectionPaths(paths);
2129: }
2130:
2131: public void addSelectionPath(TreePath path)
2132: {
2133: selectionModel.addSelectionPath(path);
2134: }
2135:
2136: public void addSelectionPaths(TreePath[] paths)
2137: {
2138: selectionModel.addSelectionPaths(paths);
2139: }
2140:
2141: public void addSelectionRow(int row)
2142: {
2143: TreePath path = getPathForRow(row);
2144:
2145: if (path != null)
2146: selectionModel.addSelectionPath(path);
2147: }
2148:
2149: public void addSelectionRows(int[] rows)
2150: {
2151:
2152: if (rows == null || getUI() == null)
2153: return;
2154:
2155: TreePath[] paths = new TreePath[rows.length];
2156:
2157: for (int i = rows.length - 1; i >= 0; --i)
2158: paths[i] = getPathForRow(rows[i]);
2159:
2160: addSelectionPaths(paths);
2161: }
2162:
2163: public void addSelectionInterval(int index0, int index1)
2164: {
2165: TreePath[] paths = getPathBetweenRows(index0, index1);
2166:
2167: if (paths != null)
2168: addSelectionPaths(paths);
2169: }
2170:
2171: public void removeSelectionPath(TreePath path)
2172: {
2173: selectionModel.removeSelectionPath(path);
2174: }
2175:
2176: public void removeSelectionPaths(TreePath[] paths)
2177: {
2178: selectionModel.removeSelectionPaths(paths);
2179: }
2180:
2181: public void removeSelectionRow(int row)
2182: {
2183: TreePath path = getPathForRow(row);
2184:
2185: if (path != null)
2186: selectionModel.removeSelectionPath(path);
2187: }
2188:
2189: public void removeSelectionRows(int[] rows)
2190: {
2191: if (rows == null || getUI() == null)
2192: return;
2193:
2194: TreePath[] paths = new TreePath[rows.length];
2195:
2196: for (int i = rows.length - 1; i >= 0; --i)
2197: paths[i] = getPathForRow(rows[i]);
2198:
2199: removeSelectionPaths(paths);
2200: }
2201:
2202: public void removeSelectionInterval(int index0, int index1)
2203: {
2204: TreePath[] paths = getPathBetweenRows(index0, index1);
2205:
2206: if (paths != null)
2207: removeSelectionPaths(paths);
2208: }
2209:
2210: public void clearSelection()
2211: {
2212: selectionModel.clearSelection();
2213: setLeadSelectionPath(null);
2214: }
2215:
2216: public TreePath getLeadSelectionPath()
2217: {
2218: return leadSelectionPath;
2219: }
2220:
2221:
2224: public void setLeadSelectionPath(TreePath path)
2225: {
2226: if (leadSelectionPath == path)
2227: return;
2228:
2229: TreePath oldValue = leadSelectionPath;
2230: leadSelectionPath = path;
2231: firePropertyChange(LEAD_SELECTION_PATH_PROPERTY, oldValue, path);
2232: }
2233:
2234:
2237: public TreePath getAnchorSelectionPath()
2238: {
2239: return anchorSelectionPath;
2240: }
2241:
2242:
2245: public void setAnchorSelectionPath(TreePath path)
2246: {
2247: if (anchorSelectionPath == path)
2248: return;
2249:
2250: TreePath oldValue = anchorSelectionPath;
2251: anchorSelectionPath = path;
2252: firePropertyChange(ANCHOR_SELECTION_PATH_PROPERTY, oldValue, path);
2253: }
2254:
2255: public int getLeadSelectionRow()
2256: {
2257: return selectionModel.getLeadSelectionRow();
2258: }
2259:
2260: public int getMaxSelectionRow()
2261: {
2262: return selectionModel.getMaxSelectionRow();
2263: }
2264:
2265: public int getMinSelectionRow()
2266: {
2267: return selectionModel.getMinSelectionRow();
2268: }
2269:
2270: public int getSelectionCount()
2271: {
2272: return selectionModel.getSelectionCount();
2273: }
2274:
2275: public TreePath getSelectionPath()
2276: {
2277: return selectionModel.getSelectionPath();
2278: }
2279:
2280: public TreePath[] getSelectionPaths()
2281: {
2282: return selectionModel.getSelectionPaths();
2283: }
2284:
2285: public int[] getSelectionRows()
2286: {
2287: return selectionModel.getSelectionRows();
2288: }
2289:
2290: public boolean isPathSelected(TreePath path)
2291: {
2292: return selectionModel.isPathSelected(path);
2293: }
2294:
2295: public boolean isRowSelected(int row)
2296: {
2297: return selectionModel.isPathSelected(getPathForRow(row));
2298: }
2299:
2300: public boolean isSelectionEmpty()
2301: {
2302: return selectionModel.isSelectionEmpty();
2303: }
2304:
2305:
2312: public boolean getDragEnabled()
2313: {
2314: return dragEnabled;
2315: }
2316:
2317:
2324: public void setDragEnabled(boolean enabled)
2325: {
2326: dragEnabled = enabled;
2327: }
2328:
2329: public int getRowCount()
2330: {
2331: TreeUI ui = getUI();
2332:
2333: if (ui != null)
2334: return ui.getRowCount(this);
2335:
2336: return 0;
2337: }
2338:
2339: public void collapsePath(TreePath path)
2340: {
2341: try
2342: {
2343: fireTreeWillCollapse(path);
2344: }
2345: catch (ExpandVetoException ev)
2346: {
2347:
2348: }
2349: setExpandedState(path, false);
2350: fireTreeCollapsed(path);
2351: }
2352:
2353: public void collapseRow(int row)
2354: {
2355: if (row < 0 || row >= getRowCount())
2356: return;
2357:
2358: TreePath path = getPathForRow(row);
2359:
2360: if (path != null)
2361: collapsePath(path);
2362: }
2363:
2364: public void expandPath(TreePath path)
2365: {
2366:
2367: if ((path == null) || (treeModel.isLeaf(path.getLastPathComponent())))
2368: return;
2369:
2370: try
2371: {
2372: fireTreeWillExpand(path);
2373: }
2374: catch (ExpandVetoException ev)
2375: {
2376:
2377: }
2378:
2379: setExpandedState(path, true);
2380: fireTreeExpanded(path);
2381: }
2382:
2383: public void expandRow(int row)
2384: {
2385: if (row < 0 || row >= getRowCount())
2386: return;
2387:
2388: TreePath path = getPathForRow(row);
2389:
2390: if (path != null)
2391: expandPath(path);
2392: }
2393:
2394: public boolean isCollapsed(TreePath path)
2395: {
2396: return !isExpanded(path);
2397: }
2398:
2399: public boolean isCollapsed(int row)
2400: {
2401: if (row < 0 || row >= getRowCount())
2402: return false;
2403:
2404: TreePath path = getPathForRow(row);
2405:
2406: if (path != null)
2407: return isCollapsed(path);
2408:
2409: return false;
2410: }
2411:
2412: public boolean isExpanded(TreePath path)
2413: {
2414: if (path == null)
2415: return false;
2416:
2417: Object state = nodeStates.get(path);
2418:
2419: if ((state == null) || (state != EXPANDED))
2420: return false;
2421:
2422: TreePath parent = path.getParentPath();
2423:
2424: if (parent != null)
2425: return isExpanded(parent);
2426:
2427: return true;
2428: }
2429:
2430: public boolean isExpanded(int row)
2431: {
2432: if (row < 0 || row >= getRowCount())
2433: return false;
2434:
2435: TreePath path = getPathForRow(row);
2436:
2437: if (path != null)
2438: return isExpanded(path);
2439:
2440: return false;
2441: }
2442:
2443:
2446: public boolean getExpandsSelectedPaths()
2447: {
2448: return expandsSelectedPaths;
2449: }
2450:
2451:
2454: public void setExpandsSelectedPaths(boolean flag)
2455: {
2456: if (expandsSelectedPaths == flag)
2457: return;
2458:
2459: boolean oldValue = expandsSelectedPaths;
2460: expandsSelectedPaths = flag;
2461: firePropertyChange(EXPANDS_SELECTED_PATHS_PROPERTY, oldValue, flag);
2462: }
2463:
2464: public Rectangle getPathBounds(TreePath path)
2465: {
2466: TreeUI ui = getUI();
2467:
2468: if (ui == null)
2469: return null;
2470:
2471: return ui.getPathBounds(this, path);
2472: }
2473:
2474: public Rectangle getRowBounds(int row)
2475: {
2476: TreePath path = getPathForRow(row);
2477:
2478: if (path != null)
2479: return getPathBounds(path);
2480:
2481: return null;
2482: }
2483:
2484: public boolean isEditing()
2485: {
2486: TreeUI ui = getUI();
2487:
2488: if (ui != null)
2489: return ui.isEditing(this);
2490:
2491: return false;
2492: }
2493:
2494: public boolean stopEditing()
2495: {
2496: TreeUI ui = getUI();
2497:
2498: if (ui != null)
2499: return ui.stopEditing(this);
2500:
2501: return false;
2502: }
2503:
2504: public void cancelEditing()
2505: {
2506: TreeUI ui = getUI();
2507:
2508: if (ui != null)
2509: ui.cancelEditing(this);
2510: }
2511:
2512: public void startEditingAtPath(TreePath path)
2513: {
2514: TreeUI ui = getUI();
2515:
2516: if (ui != null)
2517: ui.startEditingAtPath(this, path);
2518: }
2519:
2520: public TreePath getEditingPath()
2521: {
2522: TreeUI ui = getUI();
2523:
2524: if (ui != null)
2525: return ui.getEditingPath(this);
2526:
2527: return null;
2528: }
2529:
2530: public TreePath getPathForLocation(int x, int y)
2531: {
2532: TreePath path = getClosestPathForLocation(x, y);
2533:
2534: if (path != null)
2535: {
2536: Rectangle rect = getPathBounds(path);
2537:
2538: if ((rect != null) && rect.contains(x, y))
2539: return path;
2540: }
2541:
2542: return null;
2543: }
2544:
2545: public int getRowForLocation(int x, int y)
2546: {
2547: TreePath path = getPathForLocation(x, y);
2548:
2549: if (path != null)
2550: return getRowForPath(path);
2551:
2552: return -1;
2553: }
2554:
2555: public TreePath getClosestPathForLocation(int x, int y)
2556: {
2557: TreeUI ui = getUI();
2558:
2559: if (ui != null)
2560: return ui.getClosestPathForLocation(this, x, y);
2561:
2562: return null;
2563: }
2564:
2565: public int getClosestRowForLocation(int x, int y)
2566: {
2567: TreePath path = getClosestPathForLocation(x, y);
2568:
2569: if (path != null)
2570: return getRowForPath(path);
2571:
2572: return -1;
2573: }
2574:
2575: public Object getLastSelectedPathComponent()
2576: {
2577: TreePath path = getSelectionPath();
2578:
2579: if (path != null)
2580: return path.getLastPathComponent();
2581:
2582: return null;
2583: }
2584:
2585: private void doExpandParents(TreePath path, boolean state)
2586: {
2587: TreePath parent = path.getParentPath();
2588:
2589: if (!isExpanded(parent) && parent != null)
2590: doExpandParents(parent, false);
2591:
2592: nodeStates.put(path, state ? EXPANDED : COLLAPSED);
2593: }
2594:
2595: protected void setExpandedState(TreePath path, boolean state)
2596: {
2597: if (path == null)
2598: return;
2599: TreePath parent = path.getParentPath();
2600:
2601: doExpandParents(path, state);
2602: }
2603:
2604: protected void clearToggledPaths()
2605: {
2606: nodeStates.clear();
2607: }
2608:
2609: protected Enumeration getDescendantToggledPaths(TreePath parent)
2610: {
2611: if (parent == null)
2612: return null;
2613:
2614: Enumeration nodes = nodeStates.keys();
2615: Vector result = new Vector();
2616:
2617: while (nodes.hasMoreElements())
2618: {
2619: TreePath path = (TreePath) nodes.nextElement();
2620:
2621: if (path.isDescendant(parent))
2622: result.addElement(path);
2623: }
2624:
2625: return result.elements();
2626: }
2627:
2628: public boolean hasBeenExpanded(TreePath path)
2629: {
2630: if (path == null)
2631: return false;
2632:
2633: return nodeStates.get(path) != null;
2634: }
2635:
2636: public boolean isVisible(TreePath path)
2637: {
2638: if (path == null)
2639: return false;
2640:
2641: TreePath parent = path.getParentPath();
2642:
2643: if (parent == null)
2644: return true;
2645:
2646: return isExpanded(parent);
2647: }
2648:
2649: public void makeVisible(TreePath path)
2650: {
2651: if (path == null)
2652: return;
2653:
2654: expandPath(path.getParentPath());
2655: }
2656:
2657: public boolean isPathEditable(TreePath path)
2658: {
2659: return isEditable();
2660: }
2661:
2662:
2667: protected TreeModelListener createTreeModelListener()
2668: {
2669: return new TreeModelHandler();
2670: }
2671:
2672:
2678: protected static TreeModel getDefaultTreeModel()
2679: {
2680: DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root node");
2681: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child node 1");
2682: DefaultMutableTreeNode child11 =
2683: new DefaultMutableTreeNode("Child node 1.1");
2684: DefaultMutableTreeNode child12 =
2685: new DefaultMutableTreeNode("Child node 1.2");
2686: DefaultMutableTreeNode child13 =
2687: new DefaultMutableTreeNode("Child node 1.3");
2688: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child node 2");
2689: DefaultMutableTreeNode child21 =
2690: new DefaultMutableTreeNode("Child node 2.1");
2691: DefaultMutableTreeNode child22 =
2692: new DefaultMutableTreeNode("Child node 2.2");
2693: DefaultMutableTreeNode child23 =
2694: new DefaultMutableTreeNode("Child node 2.3");
2695: DefaultMutableTreeNode child24 =
2696: new DefaultMutableTreeNode("Child node 2.4");
2697:
2698: DefaultMutableTreeNode child3 = new DefaultMutableTreeNode("Child node 3");
2699: root.add(child1);
2700: root.add(child2);
2701: root.add(child3);
2702: child1.add(child11);
2703: child1.add(child12);
2704: child1.add(child13);
2705: child2.add(child21);
2706: child2.add(child22);
2707: child2.add(child23);
2708: child2.add(child24);
2709: return new DefaultTreeModel(root);
2710: }
2711:
2712:
2727: public String convertValueToText(Object value, boolean selected,
2728: boolean expanded, boolean leaf, int row, boolean hasFocus)
2729: {
2730: return value.toString();
2731: }
2732:
2733:
2740: public String paramString()
2741: {
2742:
2743:
2744:
2745: return "";
2746: }
2747:
2748:
2758: public Enumeration getExpandedDescendants(TreePath path)
2759: {
2760: Enumeration paths = nodeStates.keys();
2761: Vector relevantPaths = new Vector();
2762: while (paths.hasMoreElements())
2763: {
2764: TreePath nextPath = (TreePath) paths.nextElement();
2765: if (nodeStates.get(nextPath) == EXPANDED
2766: && path.isDescendant(nextPath))
2767: {
2768: relevantPaths.add(nextPath);
2769: }
2770: }
2771: return relevantPaths.elements();
2772: }
2773:
2774:
2792: public TreePath getNextMatch(String prefix, int startingRow,
2793: Position.Bias bias)
2794: {
2795: if (prefix == null)
2796: throw new IllegalArgumentException("The argument 'prefix' must not be"
2797: + " null.");
2798: if (startingRow < 0)
2799: throw new IllegalArgumentException("The argument 'startingRow' must not"
2800: + " be less than zero.");
2801:
2802: int size = getRowCount();
2803: if (startingRow > size)
2804: throw new IllegalArgumentException("The argument 'startingRow' must not"
2805: + " be greater than the number of"
2806: + " elements in the TreeModel.");
2807:
2808: TreePath foundPath = null;
2809: if (bias == Position.Bias.Forward)
2810: {
2811: for (int i = startingRow; i < size; i++)
2812: {
2813: TreePath path = getPathForRow(i);
2814: Object o = path.getLastPathComponent();
2815:
2816:
2817: String item = convertValueToText(o, isRowSelected(i),
2818: isExpanded(i), treeModel.isLeaf(o),
2819: i, false);
2820: if (item.startsWith(prefix))
2821: {
2822: foundPath = path;
2823: break;
2824: }
2825: }
2826: }
2827: else
2828: {
2829: for (int i = startingRow; i >= 0; i--)
2830: {
2831: TreePath path = getPathForRow(i);
2832: Object o = path.getLastPathComponent();
2833:
2834:
2835: String item = convertValueToText(o, isRowSelected(i),
2836: isExpanded(i), treeModel.isLeaf(o), i, false);
2837: if (item.startsWith(prefix))
2838: {
2839: foundPath = path;
2840: break;
2841: }
2842: }
2843: }
2844: return foundPath;
2845: }
2846:
2847:
2862: protected boolean removeDescendantSelectedPaths(TreePath path,
2863: boolean includeSelected)
2864: {
2865: boolean removedSomething = false;
2866: TreePath[] selected = getSelectionPaths();
2867: for (int index = 0; index < selected.length; index++)
2868: {
2869: if ((selected[index] == path && includeSelected)
2870: || (selected[index].isDescendant(path)))
2871: {
2872: removeSelectionPath(selected[index]);
2873: removedSomething = true;
2874: }
2875: }
2876: return removedSomething;
2877: }
2878:
2879:
2886: protected void removeDescendantToggledPaths(Enumeration toRemove)
2887: {
2888: while (toRemove.hasMoreElements())
2889: {
2890: TreePath current = (TreePath) toRemove.nextElement();
2891: Enumeration descendants = getDescendantToggledPaths(current);
2892:
2893: while (descendants.hasMoreElements())
2894: {
2895: TreePath currentDes = (TreePath) descendants.nextElement();
2896: if (isExpanded(currentDes))
2897: nodeStates.remove(currentDes);
2898: }
2899: }
2900: }
2901:
2902:
2908: public void treeDidChange()
2909: {
2910: repaint();
2911: }
2912: }