0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Anton Avtamonov, Alexander T. Simbirtsev
0019: * @version $Revision$
0020: */package javax.swing;
0021:
0022: import java.awt.Component;
0023: import java.awt.Dimension;
0024: import java.awt.Font;
0025: import java.awt.FontMetrics;
0026: import java.awt.Point;
0027: import java.awt.Rectangle;
0028: import java.awt.event.MouseEvent;
0029: import java.util.ArrayList;
0030: import java.util.Enumeration;
0031: import java.util.EventListener;
0032: import java.util.Hashtable;
0033: import java.util.LinkedList;
0034: import java.util.List;
0035: import java.util.Vector;
0036: import javax.swing.event.TreeExpansionEvent;
0037: import javax.swing.event.TreeExpansionListener;
0038: import javax.swing.event.TreeModelEvent;
0039: import javax.swing.event.TreeModelListener;
0040: import javax.swing.event.TreeSelectionEvent;
0041: import javax.swing.event.TreeSelectionListener;
0042: import javax.swing.event.TreeWillExpandListener;
0043: import javax.swing.plaf.TreeUI;
0044: import javax.swing.text.Position;
0045: import javax.swing.tree.DefaultMutableTreeNode;
0046: import javax.swing.tree.DefaultTreeCellEditor;
0047: import javax.swing.tree.DefaultTreeCellRenderer;
0048: import javax.swing.tree.DefaultTreeModel;
0049: import javax.swing.tree.DefaultTreeSelectionModel;
0050: import javax.swing.tree.ExpandVetoException;
0051: import javax.swing.tree.RowMapper;
0052: import javax.swing.tree.TreeCellEditor;
0053: import javax.swing.tree.TreeCellRenderer;
0054: import javax.swing.tree.TreeModel;
0055: import javax.swing.tree.TreeNode;
0056: import javax.swing.tree.TreePath;
0057: import javax.swing.tree.TreeSelectionModel;
0058: import javax.swing.tree.VariableHeightLayoutCache;
0059:
0060: @SuppressWarnings("serial")
0061: public class JTreeTest extends SwingTestCase {
0062: protected JTree tree;
0063:
0064: @Override
0065: protected void setUp() throws Exception {
0066: super .setUp();
0067: tree = new JTree();
0068: tree.setCellRenderer(new DefaultTreeCellRenderer() {
0069: @Override
0070: public FontMetrics getFontMetrics(Font f) {
0071: return JTreeTest.this .getFontMetrics(f, 10, 10);
0072: }
0073: });
0074: propertyChangeController = new PropertyChangeController();
0075: tree.addPropertyChangeListener(propertyChangeController);
0076: }
0077:
0078: @Override
0079: protected void tearDown() throws Exception {
0080: tree = null;
0081: propertyChangeController = null;
0082: super .tearDown();
0083: }
0084:
0085: public void testTreeSelectionRedirector() throws Exception {
0086: assertNull(tree.selectionRedirector);
0087: DefaultTreeSelectionModel model = (DefaultTreeSelectionModel) tree
0088: .getSelectionModel();
0089: assertFalse(hasListener(model.getTreeSelectionListeners(),
0090: JTree.TreeSelectionRedirector.class));
0091: final Marker marker = new Marker();
0092: tree.addTreeSelectionListener(new TreeSelectionListener() {
0093: public void valueChanged(final TreeSelectionEvent e) {
0094: marker.setAuxiliary(e);
0095: }
0096: });
0097: assertNotNull(tree.selectionRedirector);
0098: assertTrue(hasListener(model.getTreeSelectionListeners(),
0099: JTree.TreeSelectionRedirector.class));
0100: TreePath path = new TreePath("root");
0101: TreePath oldLead = new TreePath("old_lead");
0102: TreePath newLead = new TreePath("new_lead");
0103: tree.selectionRedirector.valueChanged(new TreeSelectionEvent(
0104: "any_source", path, true, oldLead, newLead));
0105: assertNotNull(marker.getAuxiliary());
0106: TreeSelectionEvent redirectedEvent = (TreeSelectionEvent) marker
0107: .getAuxiliary();
0108: assertSame(tree, redirectedEvent.getSource());
0109: assertSame(path, redirectedEvent.getPath());
0110: assertTrue(redirectedEvent.isAddedPath());
0111: assertSame(oldLead, redirectedEvent.getOldLeadSelectionPath());
0112: assertSame(newLead, redirectedEvent.getNewLeadSelectionPath());
0113: DefaultTreeSelectionModel newModel = new DefaultTreeSelectionModel();
0114: tree.setSelectionModel(newModel);
0115: assertTrue(hasListener(newModel.getTreeSelectionListeners(),
0116: JTree.TreeSelectionRedirector.class));
0117: tree.setSelectionModel(null);
0118: assertTrue(hasListener(((DefaultTreeSelectionModel) tree
0119: .getSelectionModel()).getTreeSelectionListeners(),
0120: JTree.TreeSelectionRedirector.class));
0121: }
0122:
0123: public void testJTree() {
0124: assertFalse(tree.editable);
0125: assertFalse(tree.invokesStopCellEditing);
0126: assertFalse(tree.largeModel);
0127: assertTrue(tree.rootVisible);
0128: assertEquals(0, tree.rowHeight);
0129: assertTrue(tree.scrollsOnExpand);
0130: assertFalse(tree.showsRootHandles);
0131: assertEquals(2, tree.toggleClickCount);
0132: assertEquals(20, tree.visibleRowCount);
0133: assertNotNull(tree.selectionModel);
0134: assertNull(tree.selectionRedirector);
0135: assertNotNull(tree.cellRenderer);
0136: assertNull(tree.cellEditor);
0137: assertNotNull(tree.treeModel);
0138: assertNotNull(tree.treeModelListener);
0139: }
0140:
0141: public void testJTreeTreeModel() {
0142: DefaultTreeModel model = new DefaultTreeModel(
0143: new DefaultMutableTreeNode("root"));
0144: tree = new JTree(model);
0145: assertFalse(tree.editable);
0146: assertFalse(tree.invokesStopCellEditing);
0147: assertFalse(tree.largeModel);
0148: assertTrue(tree.rootVisible);
0149: assertEquals(0, tree.rowHeight);
0150: assertTrue(tree.scrollsOnExpand);
0151: assertFalse(tree.showsRootHandles);
0152: assertEquals(2, tree.toggleClickCount);
0153: assertEquals(20, tree.visibleRowCount);
0154: assertNotNull(tree.selectionModel);
0155: assertNull(tree.selectionRedirector);
0156: assertEquals(model, tree.treeModel);
0157: assertFalse(((DefaultTreeModel) tree.treeModel)
0158: .asksAllowsChildren());
0159: assertNotNull(tree.treeModelListener);
0160: tree = new JTree((TreeModel) null);
0161: assertFalse(tree.editable);
0162: assertFalse(tree.invokesStopCellEditing);
0163: assertFalse(tree.largeModel);
0164: assertTrue(tree.rootVisible);
0165: assertEquals(0, tree.rowHeight);
0166: assertTrue(tree.scrollsOnExpand);
0167: assertFalse(tree.showsRootHandles);
0168: assertEquals(2, tree.toggleClickCount);
0169: assertEquals(20, tree.visibleRowCount);
0170: assertNotNull(tree.selectionModel);
0171: assertNull(tree.selectionRedirector);
0172: assertNull(tree.treeModel);
0173: if (isHarmony()) {
0174: assertNotNull(tree.treeModelListener);
0175: } else {
0176: assertNull(tree.treeModelListener);
0177: }
0178: }
0179:
0180: public void testJTreeTreeNodeBoolean() {
0181: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0182: tree = new JTree(root, true);
0183: assertFalse(tree.editable);
0184: assertFalse(tree.invokesStopCellEditing);
0185: assertFalse(tree.largeModel);
0186: assertTrue(tree.rootVisible);
0187: assertEquals(0, tree.rowHeight);
0188: assertTrue(tree.scrollsOnExpand);
0189: assertFalse(tree.showsRootHandles);
0190: assertEquals(2, tree.toggleClickCount);
0191: assertEquals(20, tree.visibleRowCount);
0192: assertNotNull(tree.selectionModel);
0193: assertNull(tree.selectionRedirector);
0194: assertNotNull(tree.treeModel);
0195: assertEquals(root, tree.treeModel.getRoot());
0196: assertTrue(((DefaultTreeModel) tree.treeModel)
0197: .asksAllowsChildren());
0198: assertNotNull(tree.treeModelListener);
0199: tree = new JTree(root, false);
0200: assertFalse(tree.editable);
0201: assertFalse(tree.invokesStopCellEditing);
0202: assertFalse(tree.largeModel);
0203: assertTrue(tree.rootVisible);
0204: assertEquals(0, tree.rowHeight);
0205: assertTrue(tree.scrollsOnExpand);
0206: assertFalse(tree.showsRootHandles);
0207: assertEquals(2, tree.toggleClickCount);
0208: assertEquals(20, tree.visibleRowCount);
0209: assertNotNull(tree.selectionModel);
0210: assertNull(tree.selectionRedirector);
0211: assertNotNull(tree.treeModel);
0212: assertEquals(root, tree.treeModel.getRoot());
0213: assertFalse(((DefaultTreeModel) tree.treeModel)
0214: .asksAllowsChildren());
0215: assertNotNull(tree.treeModelListener);
0216: tree = new JTree(null, false);
0217: assertFalse(tree.editable);
0218: assertFalse(tree.invokesStopCellEditing);
0219: assertFalse(tree.largeModel);
0220: assertTrue(tree.rootVisible);
0221: assertEquals(0, tree.rowHeight);
0222: assertTrue(tree.scrollsOnExpand);
0223: assertFalse(tree.showsRootHandles);
0224: assertEquals(2, tree.toggleClickCount);
0225: assertEquals(20, tree.visibleRowCount);
0226: assertNotNull(tree.selectionModel);
0227: assertNull(tree.selectionRedirector);
0228: assertNotNull(tree.treeModel);
0229: assertNull(tree.treeModel.getRoot());
0230: assertFalse(((DefaultTreeModel) tree.treeModel)
0231: .asksAllowsChildren());
0232: assertNotNull(tree.treeModelListener);
0233: }
0234:
0235: public void testJTreeTreeNode() {
0236: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0237: tree = new JTree(root);
0238: assertFalse(tree.editable);
0239: assertFalse(tree.invokesStopCellEditing);
0240: assertFalse(tree.largeModel);
0241: assertTrue(tree.rootVisible);
0242: assertEquals(0, tree.rowHeight);
0243: assertTrue(tree.scrollsOnExpand);
0244: assertFalse(tree.showsRootHandles);
0245: assertEquals(2, tree.toggleClickCount);
0246: assertEquals(20, tree.visibleRowCount);
0247: assertNotNull(tree.selectionModel);
0248: assertNull(tree.selectionRedirector);
0249: assertNotNull(tree.treeModel);
0250: assertEquals(root, tree.treeModel.getRoot());
0251: assertFalse(((DefaultTreeModel) tree.treeModel)
0252: .asksAllowsChildren());
0253: assertNotNull(tree.treeModelListener);
0254: tree = new JTree((TreeNode) null);
0255: assertFalse(tree.editable);
0256: assertFalse(tree.invokesStopCellEditing);
0257: assertFalse(tree.largeModel);
0258: assertTrue(tree.rootVisible);
0259: assertEquals(0, tree.rowHeight);
0260: assertTrue(tree.scrollsOnExpand);
0261: assertFalse(tree.showsRootHandles);
0262: assertEquals(2, tree.toggleClickCount);
0263: assertEquals(20, tree.visibleRowCount);
0264: assertNotNull(tree.selectionModel);
0265: assertNull(tree.selectionRedirector);
0266: assertNotNull(tree.treeModel);
0267: assertNull(tree.treeModel.getRoot());
0268: assertFalse(((DefaultTreeModel) tree.treeModel)
0269: .asksAllowsChildren());
0270: assertNotNull(tree.treeModelListener);
0271: }
0272:
0273: public void testJTreeObjectArray() {
0274: Object[] nodes = new Object[] { "node1", "node2", "node3" };
0275: tree = new JTree(nodes);
0276: assertFalse(tree.editable);
0277: assertFalse(tree.invokesStopCellEditing);
0278: assertFalse(tree.largeModel);
0279: assertFalse(tree.rootVisible);
0280: assertEquals(0, tree.rowHeight);
0281: assertTrue(tree.scrollsOnExpand);
0282: assertTrue(tree.showsRootHandles);
0283: assertEquals(2, tree.toggleClickCount);
0284: assertEquals(20, tree.visibleRowCount);
0285: assertNotNull(tree.selectionModel);
0286: assertNull(tree.selectionRedirector);
0287: assertNotNull(tree.treeModel);
0288: assertNotNull(tree.treeModel.getRoot());
0289: assertEquals(3, tree.treeModel.getChildCount(tree.treeModel
0290: .getRoot()));
0291: assertFalse(((DefaultTreeModel) tree.treeModel)
0292: .asksAllowsChildren());
0293: assertNotNull(tree.treeModelListener);
0294: tree = new JTree((Object[]) null);
0295: assertFalse(tree.editable);
0296: assertFalse(tree.invokesStopCellEditing);
0297: assertFalse(tree.largeModel);
0298: assertFalse(tree.rootVisible);
0299: assertEquals(0, tree.rowHeight);
0300: assertTrue(tree.scrollsOnExpand);
0301: assertTrue(tree.showsRootHandles);
0302: assertEquals(2, tree.toggleClickCount);
0303: assertEquals(20, tree.visibleRowCount);
0304: assertNotNull(tree.selectionModel);
0305: assertNull(tree.selectionRedirector);
0306: assertNotNull(tree.treeModel);
0307: assertNotNull(tree.treeModel.getRoot());
0308: assertEquals(0, tree.treeModel.getChildCount(tree.treeModel
0309: .getRoot()));
0310: assertFalse(((DefaultTreeModel) tree.treeModel)
0311: .asksAllowsChildren());
0312: assertNotNull(tree.treeModelListener);
0313: }
0314:
0315: public void testJTreeVector() {
0316: Vector<String> nodes = new Vector<String>();
0317: nodes.add("node1");
0318: nodes.add("node2");
0319: nodes.add("node3");
0320: tree = new JTree(nodes);
0321: assertFalse(tree.editable);
0322: assertFalse(tree.invokesStopCellEditing);
0323: assertFalse(tree.largeModel);
0324: assertFalse(tree.rootVisible);
0325: assertEquals(0, tree.rowHeight);
0326: assertTrue(tree.scrollsOnExpand);
0327: assertTrue(tree.showsRootHandles);
0328: assertEquals(2, tree.toggleClickCount);
0329: assertEquals(20, tree.visibleRowCount);
0330: assertNotNull(tree.selectionModel);
0331: assertNull(tree.selectionRedirector);
0332: assertNotNull(tree.treeModel);
0333: assertNotNull(tree.treeModel.getRoot());
0334: assertEquals(3, tree.treeModel.getChildCount(tree.treeModel
0335: .getRoot()));
0336: assertFalse(((DefaultTreeModel) tree.treeModel)
0337: .asksAllowsChildren());
0338: assertNotNull(tree.treeModelListener);
0339: tree = new JTree((Vector) null);
0340: assertFalse(tree.editable);
0341: assertFalse(tree.invokesStopCellEditing);
0342: assertFalse(tree.largeModel);
0343: assertFalse(tree.rootVisible);
0344: assertEquals(0, tree.rowHeight);
0345: assertTrue(tree.scrollsOnExpand);
0346: assertTrue(tree.showsRootHandles);
0347: assertEquals(2, tree.toggleClickCount);
0348: assertEquals(20, tree.visibleRowCount);
0349: assertNotNull(tree.selectionModel);
0350: assertNull(tree.selectionRedirector);
0351: assertNotNull(tree.treeModel);
0352: assertNotNull(tree.treeModel.getRoot());
0353: assertEquals(0, tree.treeModel.getChildCount(tree.treeModel
0354: .getRoot()));
0355: assertFalse(((DefaultTreeModel) tree.treeModel)
0356: .asksAllowsChildren());
0357: assertNotNull(tree.treeModelListener);
0358: }
0359:
0360: public void testJTreeHashtable() {
0361: Hashtable<String, String> nodes = new Hashtable<String, String>();
0362: nodes.put("node1", "node1");
0363: nodes.put("node2", "node2");
0364: nodes.put("node3", "node3");
0365: tree = new JTree(nodes);
0366: assertFalse(tree.editable);
0367: assertFalse(tree.invokesStopCellEditing);
0368: assertFalse(tree.largeModel);
0369: assertFalse(tree.rootVisible);
0370: assertEquals(0, tree.rowHeight);
0371: assertTrue(tree.scrollsOnExpand);
0372: assertTrue(tree.showsRootHandles);
0373: assertEquals(2, tree.toggleClickCount);
0374: assertEquals(20, tree.visibleRowCount);
0375: assertNotNull(tree.selectionModel);
0376: assertNull(tree.selectionRedirector);
0377: assertNotNull(tree.treeModel);
0378: assertNotNull(tree.treeModel.getRoot());
0379: assertEquals(3, tree.treeModel.getChildCount(tree.treeModel
0380: .getRoot()));
0381: assertFalse(((DefaultTreeModel) tree.treeModel)
0382: .asksAllowsChildren());
0383: assertNotNull(tree.treeModelListener);
0384: tree = new JTree((Hashtable) null);
0385: assertFalse(tree.editable);
0386: assertFalse(tree.invokesStopCellEditing);
0387: assertFalse(tree.largeModel);
0388: assertFalse(tree.rootVisible);
0389: assertEquals(0, tree.rowHeight);
0390: assertTrue(tree.scrollsOnExpand);
0391: assertTrue(tree.showsRootHandles);
0392: assertEquals(2, tree.toggleClickCount);
0393: assertEquals(20, tree.visibleRowCount);
0394: assertNotNull(tree.selectionModel);
0395: assertNull(tree.selectionRedirector);
0396: assertNotNull(tree.treeModel);
0397: assertNotNull(tree.treeModel.getRoot());
0398: assertEquals(0, tree.treeModel.getChildCount(tree.treeModel
0399: .getRoot()));
0400: assertFalse(((DefaultTreeModel) tree.treeModel)
0401: .asksAllowsChildren());
0402: assertNotNull(tree.treeModelListener);
0403: }
0404:
0405: public void testGetAccessibleContext() {
0406: assertNull(tree.accessibleContext);
0407: assertNotNull(tree.getAccessibleContext());
0408: assertSame(tree.accessibleContext, tree.getAccessibleContext());
0409: }
0410:
0411: public void testGetToolTipTextMouseEvent() {
0412: tree.setToolTipText("tip");
0413: tree.cellRenderer = new DefaultTreeCellRenderer() {
0414: @Override
0415: public String getToolTipText() {
0416: return "renderer tip";
0417: }
0418: };
0419: assertEquals("renderer tip", tree
0420: .getToolTipText(new MouseEvent(tree, 0, 0, 0, 0, 0, 0,
0421: false)));
0422: assertNull(tree.getToolTipText(null));
0423: }
0424:
0425: public void testGetUIClassID() {
0426: assertEquals("TreeUI", tree.getUIClassID());
0427: }
0428:
0429: public void testSetAddRemoveSelectionInterval() {
0430: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0431: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0432: "child1");
0433: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
0434: "child11");
0435: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
0436: "child111");
0437: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
0438: "child2");
0439: DefaultMutableTreeNode child21 = new DefaultMutableTreeNode(
0440: "child21");
0441: root.add(child1);
0442: child1.add(child11);
0443: child11.add(child111);
0444: root.add(child2);
0445: child2.add(child21);
0446: tree.setModel(new DefaultTreeModel(root));
0447: TreePath rootPath = new TreePath(root);
0448: TreePath path1 = rootPath.pathByAddingChild(child1);
0449: TreePath path11 = path1.pathByAddingChild(child11);
0450: TreePath path2 = rootPath.pathByAddingChild(child2);
0451: assertEquals(0, tree.getSelectionCount());
0452: tree.addSelectionInterval(0, 0);
0453: assertEquals(1, tree.getSelectionCount());
0454: tree.addSelectionInterval(1, 1);
0455: assertEquals(2, tree.getSelectionCount());
0456: tree.addSelectionInterval(-20, 20);
0457: assertEqualsIgnoreOrder(
0458: new TreePath[] { rootPath, path1, path2 }, tree
0459: .getSelectionPaths());
0460: tree.expandPath(path1);
0461: assertEqualsIgnoreOrder(
0462: new TreePath[] { rootPath, path1, path2 }, tree
0463: .getSelectionPaths());
0464: tree.addSelectionInterval(1, 2);
0465: assertEqualsIgnoreOrder(new TreePath[] { rootPath, path1,
0466: path2, path11 }, tree.getSelectionPaths());
0467: tree.removeSelectionInterval(0, 1);
0468: assertEqualsIgnoreOrder(new TreePath[] { path11, path2 }, tree
0469: .getSelectionPaths());
0470: tree.removeSelectionInterval(2, 2);
0471: assertEqualsIgnoreOrder(new TreePath[] { path2 }, tree
0472: .getSelectionPaths());
0473: tree.removeSelectionInterval(0, 2);
0474: assertEqualsIgnoreOrder(new TreePath[] { path2 }, tree
0475: .getSelectionPaths());
0476: tree.removeSelectionInterval(-1, 10);
0477: assertNull(tree.getSelectionPaths());
0478: tree.setSelectionInterval(3, 1);
0479: assertEqualsIgnoreOrder(
0480: new TreePath[] { path1, path11, path2 }, tree
0481: .getSelectionPaths());
0482: tree.setSelectionInterval(2, 2);
0483: assertEqualsIgnoreOrder(new TreePath[] { path11 }, tree
0484: .getSelectionPaths());
0485: }
0486:
0487: public void testAddRemoveSelectionPathPathsRowRows() {
0488: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0489: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0490: "child1");
0491: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
0492: "child11");
0493: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
0494: "child111");
0495: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
0496: "child2");
0497: DefaultMutableTreeNode child21 = new DefaultMutableTreeNode(
0498: "child21");
0499: root.add(child1);
0500: child1.add(child11);
0501: child11.add(child111);
0502: root.add(child2);
0503: child2.add(child21);
0504: tree.setModel(new DefaultTreeModel(root));
0505: TreePath rootPath = new TreePath(root);
0506: TreePath path1 = rootPath.pathByAddingChild(child1);
0507: TreePath path11 = path1.pathByAddingChild(child11);
0508: TreePath path111 = path11.pathByAddingChild(child111);
0509: TreePath path2 = rootPath.pathByAddingChild(child2);
0510: TreePath path21 = path2.pathByAddingChild(child21);
0511: assertEquals(0, tree.getSelectionCount());
0512: tree.addSelectionPath(path1);
0513: assertEqualsIgnoreOrder(new TreePath[] { path1 }, tree
0514: .getSelectionPaths());
0515: tree.addSelectionPaths(new TreePath[] { rootPath, path11 });
0516: assertEqualsIgnoreOrder(new TreePath[] { path1, rootPath,
0517: path11 }, tree.getSelectionPaths());
0518: TreePath unexistedPath = rootPath
0519: .pathByAddingChild(new DefaultMutableTreeNode(
0520: "unexisted"));
0521: tree.addSelectionPath(unexistedPath);
0522: assertEqualsIgnoreOrder(new TreePath[] { path1, rootPath,
0523: path11, unexistedPath }, tree.getSelectionPaths());
0524: tree.removeSelectionPaths(new TreePath[] { rootPath,
0525: unexistedPath });
0526: assertEqualsIgnoreOrder(new TreePath[] { path11, path1 }, tree
0527: .getSelectionPaths());
0528: tree.removeSelectionPath(path11);
0529: assertEqualsIgnoreOrder(new TreePath[] { path1 }, tree
0530: .getSelectionPaths());
0531: tree.expandPath(path2);
0532: tree.addSelectionRows(new int[] { 3, 4 });
0533: assertEqualsIgnoreOrder(
0534: new TreePath[] { path1, path2, path21 }, tree
0535: .getSelectionPaths());
0536: tree.expandPath(path11);
0537: tree.addSelectionRows(new int[] { 0, 3 });
0538: assertEqualsIgnoreOrder(new TreePath[] { path1, path2, path21,
0539: rootPath, path111 }, tree.getSelectionPaths());
0540: tree.removeSelectionRows(new int[] { 0, 2 });
0541: assertEqualsIgnoreOrder(new TreePath[] { path111, path2, path1,
0542: path21 }, tree.getSelectionPaths());
0543: tree.removeSelectionRow(3);
0544: assertEqualsIgnoreOrder(
0545: new TreePath[] { path2, path1, path21 }, tree
0546: .getSelectionPaths());
0547: }
0548:
0549: public void testAddGetRemoveTreeExpansionListener() {
0550: class ConcreteTreeExpansionListener implements
0551: TreeExpansionListener {
0552: public void treeCollapsed(TreeExpansionEvent e) {
0553: }
0554:
0555: public void treeExpanded(TreeExpansionEvent e) {
0556: }
0557: }
0558: ;
0559: TreeExpansionListener TreeExpansionListener1 = new ConcreteTreeExpansionListener();
0560: TreeExpansionListener TreeExpansionListener2 = new ConcreteTreeExpansionListener();
0561: TreeExpansionListener TreeExpansionListener3 = new ConcreteTreeExpansionListener();
0562: EventListener[] listenersArray = null;
0563: listenersArray = tree.getTreeExpansionListeners();
0564: int initialValue = listenersArray.length;
0565: tree.addTreeExpansionListener(TreeExpansionListener1);
0566: tree.addTreeExpansionListener(TreeExpansionListener2);
0567: tree.addTreeExpansionListener(TreeExpansionListener2);
0568: listenersArray = tree.getTreeExpansionListeners();
0569: assertEquals(initialValue + 3, listenersArray.length);
0570: tree.removeTreeExpansionListener(TreeExpansionListener1);
0571: tree.addTreeExpansionListener(TreeExpansionListener3);
0572: tree.addTreeExpansionListener(TreeExpansionListener3);
0573: listenersArray = tree.getTreeExpansionListeners();
0574: assertEquals(initialValue + 4, listenersArray.length);
0575: tree.removeTreeExpansionListener(TreeExpansionListener3);
0576: tree.removeTreeExpansionListener(TreeExpansionListener3);
0577: listenersArray = tree.getTreeExpansionListeners();
0578: assertEquals(initialValue + 2, listenersArray.length);
0579: tree.removeTreeExpansionListener(TreeExpansionListener2);
0580: tree.removeTreeExpansionListener(TreeExpansionListener2);
0581: listenersArray = tree.getTreeExpansionListeners();
0582: assertEquals(initialValue, listenersArray.length);
0583: }
0584:
0585: public void testAddGetRemoveTreeSelectionListener() {
0586: class ConcreteTreeSelectionListener implements
0587: TreeSelectionListener {
0588: public void valueChanged(TreeSelectionEvent e) {
0589: }
0590: }
0591: ;
0592: TreeSelectionListener TreeSelectionListener1 = new ConcreteTreeSelectionListener();
0593: TreeSelectionListener TreeSelectionListener2 = new ConcreteTreeSelectionListener();
0594: TreeSelectionListener TreeSelectionListener3 = new ConcreteTreeSelectionListener();
0595: EventListener[] listenersArray = null;
0596: listenersArray = tree.getTreeSelectionListeners();
0597: int initialValue = listenersArray.length;
0598: tree.addTreeSelectionListener(TreeSelectionListener1);
0599: tree.addTreeSelectionListener(TreeSelectionListener2);
0600: tree.addTreeSelectionListener(TreeSelectionListener2);
0601: listenersArray = tree.getTreeSelectionListeners();
0602: assertEquals(initialValue + 3, listenersArray.length);
0603: tree.removeTreeSelectionListener(TreeSelectionListener1);
0604: tree.addTreeSelectionListener(TreeSelectionListener3);
0605: tree.addTreeSelectionListener(TreeSelectionListener3);
0606: listenersArray = tree.getTreeSelectionListeners();
0607: assertEquals(initialValue + 4, listenersArray.length);
0608: tree.removeTreeSelectionListener(TreeSelectionListener3);
0609: tree.removeTreeSelectionListener(TreeSelectionListener3);
0610: listenersArray = tree.getTreeSelectionListeners();
0611: assertEquals(initialValue + 2, listenersArray.length);
0612: tree.removeTreeSelectionListener(TreeSelectionListener2);
0613: tree.removeTreeSelectionListener(TreeSelectionListener2);
0614: listenersArray = tree.getTreeSelectionListeners();
0615: assertEquals(initialValue, listenersArray.length);
0616: }
0617:
0618: public void testAddGetRemoveTreeWillExpandListener() {
0619: class ConcreteTreeWillExpandListener implements
0620: TreeWillExpandListener {
0621: public void treeWillCollapse(TreeExpansionEvent e)
0622: throws ExpandVetoException {
0623: }
0624:
0625: public void treeWillExpand(TreeExpansionEvent e)
0626: throws ExpandVetoException {
0627: }
0628: }
0629: ;
0630: TreeWillExpandListener TreeWillExpandListener1 = new ConcreteTreeWillExpandListener();
0631: TreeWillExpandListener TreeWillExpandListener2 = new ConcreteTreeWillExpandListener();
0632: TreeWillExpandListener TreeWillExpandListener3 = new ConcreteTreeWillExpandListener();
0633: EventListener[] listenersArray = null;
0634: listenersArray = tree.getTreeWillExpandListeners();
0635: int initialValue = listenersArray.length;
0636: tree.addTreeWillExpandListener(TreeWillExpandListener1);
0637: tree.addTreeWillExpandListener(TreeWillExpandListener2);
0638: tree.addTreeWillExpandListener(TreeWillExpandListener2);
0639: listenersArray = tree.getTreeWillExpandListeners();
0640: assertEquals(initialValue + 3, listenersArray.length);
0641: tree.removeTreeWillExpandListener(TreeWillExpandListener1);
0642: tree.addTreeWillExpandListener(TreeWillExpandListener3);
0643: tree.addTreeWillExpandListener(TreeWillExpandListener3);
0644: listenersArray = tree.getTreeWillExpandListeners();
0645: assertEquals(initialValue + 4, listenersArray.length);
0646: tree.removeTreeWillExpandListener(TreeWillExpandListener3);
0647: tree.removeTreeWillExpandListener(TreeWillExpandListener3);
0648: listenersArray = tree.getTreeWillExpandListeners();
0649: assertEquals(initialValue + 2, listenersArray.length);
0650: tree.removeTreeWillExpandListener(TreeWillExpandListener2);
0651: tree.removeTreeWillExpandListener(TreeWillExpandListener2);
0652: listenersArray = tree.getTreeWillExpandListeners();
0653: assertEquals(initialValue, listenersArray.length);
0654: }
0655:
0656: public void testClearSelection() {
0657: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0658: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0659: "child1");
0660: root.add(child1);
0661: tree.setModel(new DefaultTreeModel(root));
0662: TreePath rootPath = new TreePath(root);
0663: TreePath path1 = rootPath.pathByAddingChild(child1);
0664: assertEquals(0, tree.getSelectionCount());
0665: assertEquals(0, tree.getSelectionModel().getSelectionCount());
0666: tree.addSelectionPaths(new TreePath[] { rootPath, path1 });
0667: assertEquals(2, tree.getSelectionCount());
0668: assertEquals(2, tree.getSelectionModel().getSelectionCount());
0669: tree.clearSelection();
0670: assertEquals(0, tree.getSelectionCount());
0671: assertEquals(0, tree.getSelectionModel().getSelectionCount());
0672: }
0673:
0674: public void testClearToggledPaths() {
0675: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0676: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0677: "child1");
0678: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
0679: "child11");
0680: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
0681: "child111");
0682: root.add(child1);
0683: child1.add(child11);
0684: child11.add(child111);
0685: tree.setModel(new DefaultTreeModel(root));
0686: TreePath rootPath = new TreePath(root);
0687: TreePath path1 = rootPath.pathByAddingChild(child1);
0688: assertTrue(tree.isExpanded(rootPath));
0689: assertFalse(tree.isExpanded(path1));
0690: tree.clearToggledPaths();
0691: assertFalse(tree.isExpanded(rootPath));
0692: assertFalse(tree.isExpanded(path1));
0693: tree.expandPath(path1);
0694: tree.clearToggledPaths();
0695: assertFalse(tree.isExpanded(rootPath));
0696: assertFalse(tree.isExpanded(path1));
0697: }
0698:
0699: public void testConvertValueToText() {
0700: assertEquals("", tree.convertValueToText(null, false, false,
0701: true, 0, false));
0702: assertEquals("any", tree.convertValueToText("any", false,
0703: false, true, 0, false));
0704: assertEquals("5", tree.convertValueToText(new Integer("5"),
0705: false, false, true, 0, false));
0706: }
0707:
0708: public void testCreateTreeModel() {
0709: Object obj1 = new Object[] { "node1", "node2", "node3" };
0710: Vector<String> obj2 = new Vector<String>();
0711: obj2.add("node1");
0712: obj2.add("node2");
0713: obj2.add("node3");
0714: Hashtable<String, String> obj3 = new Hashtable<String, String>();
0715: obj3.put("node1", "value1");
0716: obj3.put("node2", "value3");
0717: obj3.put("node3", "value3");
0718: Object obj4 = "object";
0719: List<String> obj5 = new ArrayList<String>();
0720: obj5.add("node1");
0721: obj5.add("node2");
0722: obj5.add("node3");
0723: Object obj6 = new int[] { 1, 2, 3 };
0724: Vector<Vector<String>> obj7 = new Vector<Vector<String>>();
0725: Vector<String> obj71 = new Vector<String>();
0726: obj71.add("node1");
0727: obj71.add("node2");
0728: obj7.add(obj71);
0729: TreeModel model = JTree.createTreeModel(obj1);
0730: assertTrue(model instanceof DefaultTreeModel);
0731: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0732: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0733: .getUserObject());
0734: assertEquals(3, model.getChildCount(model.getRoot()));
0735: assertTrue(model.getChild(model.getRoot(), 0) instanceof JTree.DynamicUtilTreeNode);
0736: model = JTree.createTreeModel(obj2);
0737: assertTrue(model instanceof DefaultTreeModel);
0738: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0739: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0740: .getUserObject());
0741: assertEquals(3, model.getChildCount(model.getRoot()));
0742: assertTrue(model.getChild(model.getRoot(), 0) instanceof JTree.DynamicUtilTreeNode);
0743: model = JTree.createTreeModel(obj3);
0744: assertTrue(model instanceof DefaultTreeModel);
0745: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0746: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0747: .getUserObject());
0748: assertEquals(3, model.getChildCount(model.getRoot()));
0749: assertTrue(model.getChild(model.getRoot(), 0) instanceof JTree.DynamicUtilTreeNode);
0750: assertTrue(((String) ((DefaultMutableTreeNode) ((DefaultMutableTreeNode) model
0751: .getRoot()).getChildAt(0)).getUserObject())
0752: .startsWith("node"));
0753: model = JTree.createTreeModel(obj4);
0754: assertTrue(model instanceof DefaultTreeModel);
0755: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0756: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0757: .getUserObject());
0758: assertEquals(0, model.getChildCount(model.getRoot()));
0759: model = JTree.createTreeModel(obj5);
0760: assertTrue(model instanceof DefaultTreeModel);
0761: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0762: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0763: .getUserObject());
0764: assertEquals(0, model.getChildCount(model.getRoot()));
0765: model = JTree.createTreeModel(obj6);
0766: assertTrue(model instanceof DefaultTreeModel);
0767: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0768: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0769: .getUserObject());
0770: assertEquals(0, model.getChildCount(model.getRoot()));
0771: model = JTree.createTreeModel(obj7);
0772: assertTrue(model instanceof DefaultTreeModel);
0773: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0774: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0775: .getUserObject());
0776: assertEquals(1, model.getChildCount(model.getRoot()));
0777: DefaultMutableTreeNode child = (DefaultMutableTreeNode) ((DefaultMutableTreeNode) model
0778: .getRoot()).getChildAt(0);
0779: assertTrue(child instanceof JTree.DynamicUtilTreeNode);
0780: assertEquals(obj71, child.getUserObject());
0781: assertEquals(2, child.getChildCount());
0782: assertTrue(model.getChild(child, 0) instanceof JTree.DynamicUtilTreeNode);
0783: model = JTree.createTreeModel(null);
0784: assertTrue(model instanceof DefaultTreeModel);
0785: assertTrue(model.getRoot() instanceof DefaultMutableTreeNode);
0786: assertEquals("root", ((DefaultMutableTreeNode) model.getRoot())
0787: .getUserObject());
0788: assertEquals(0, model.getChildCount(model.getRoot()));
0789: }
0790:
0791: public void testCreateTreeModelListener() {
0792: TreeModelListener listener1 = tree.createTreeModelListener();
0793: TreeModelListener listener2 = tree.createTreeModelListener();
0794: assertTrue(listener1 instanceof JTree.TreeModelHandler);
0795: assertNotSame(listener1, listener2);
0796: }
0797:
0798: public void testTreeModelHandler() throws Exception {
0799: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
0800: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode();
0801: root.add(child1);
0802: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode();
0803: child1.add(child11);
0804: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode();
0805: child11.add(child111);
0806: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode();
0807: root.add(child2);
0808: DefaultMutableTreeNode child21 = new DefaultMutableTreeNode();
0809: child2.add(child21);
0810: tree.setModel(new DefaultTreeModel(root));
0811: TreePath rootPath = new TreePath(root);
0812: TreePath path1 = rootPath.pathByAddingChild(child1);
0813: TreePath path11 = path1.pathByAddingChild(child11);
0814: TreePath path111 = path11.pathByAddingChild(child111);
0815: TreePath path2 = rootPath.pathByAddingChild(child2);
0816: TreePath path21 = path1.pathByAddingChild(child21);
0817: tree.setExpandedState(path11, true);
0818: assertTrue(tree.isExpanded(rootPath));
0819: assertTrue(tree.isExpanded(path1));
0820: assertTrue(tree.isExpanded(path11));
0821: assertFalse(tree.isExpanded(path111));
0822: assertFalse(tree.isExpanded(path2));
0823: assertFalse(tree.isExpanded(path21));
0824: tree.treeModelListener.treeNodesChanged(new TreeModelEvent(
0825: tree, path1));
0826: assertTrue(tree.isExpanded(rootPath));
0827: assertTrue(tree.isExpanded(path1));
0828: assertTrue(tree.isExpanded(path11));
0829: assertFalse(tree.isExpanded(path111));
0830: assertFalse(tree.isExpanded(path2));
0831: assertFalse(tree.isExpanded(path21));
0832: TreePath path12 = path1
0833: .pathByAddingChild(new DefaultMutableTreeNode());
0834: tree.treeModelListener.treeNodesInserted(new TreeModelEvent(
0835: tree, path1, new int[] { 1 }, new Object[] { path12 }));
0836: assertTrue(tree.isExpanded(rootPath));
0837: assertTrue(tree.isExpanded(path1));
0838: assertTrue(tree.isExpanded(path11));
0839: assertFalse(tree.isExpanded(path12));
0840: assertFalse(tree.isExpanded(path111));
0841: assertFalse(tree.isExpanded(path2));
0842: assertFalse(tree.isExpanded(path21));
0843: tree.treeModelListener
0844: .treeNodesRemoved(new TreeModelEvent(tree, path1,
0845: new int[] { 0 }, new Object[] { child11 }));
0846: assertTrue(tree.isExpanded(rootPath));
0847: assertTrue(tree.isExpanded(path1));
0848: assertFalse(tree.isExpanded(path11));
0849: assertFalse(tree.isExpanded(path111));
0850: assertFalse(tree.isExpanded(path2));
0851: assertFalse(tree.isExpanded(path21));
0852: tree.setExpandedState(path11, true);
0853: tree.treeModelListener.treeNodesRemoved(new TreeModelEvent(
0854: tree, path1));
0855: assertTrue(tree.isExpanded(rootPath));
0856: assertTrue(tree.isExpanded(path1));
0857: assertTrue(tree.isExpanded(path11));
0858: assertFalse(tree.isExpanded(path111));
0859: assertFalse(tree.isExpanded(path2));
0860: assertFalse(tree.isExpanded(path21));
0861: tree.setExpandedState(path2, true);
0862: tree.treeModelListener.treeNodesRemoved(new TreeModelEvent(
0863: tree, rootPath,
0864: new int[] { 0 /*index is not important*/},
0865: new Object[] { child2 }));
0866: assertTrue(tree.isExpanded(rootPath));
0867: assertTrue(tree.isExpanded(path1));
0868: assertTrue(tree.isExpanded(path11));
0869: assertFalse(tree.isExpanded(path111));
0870: assertFalse(tree.isExpanded(path2));
0871: assertFalse(tree.isExpanded(path21));
0872: tree.setExpandedState(path11, true);
0873: tree.setExpandedState(path2, true);
0874: assertTrue(tree.isExpanded(rootPath));
0875: assertTrue(tree.isExpanded(path1));
0876: assertTrue(tree.isExpanded(path11));
0877: assertFalse(tree.isExpanded(path111));
0878: assertTrue(tree.isExpanded(path2));
0879: assertFalse(tree.isExpanded(path21));
0880: tree.treeModelListener.treeStructureChanged(new TreeModelEvent(
0881: tree, rootPath));
0882: assertTrue(tree.isExpanded(rootPath));
0883: assertFalse(tree.isExpanded(path1));
0884: assertFalse(tree.isExpanded(path11));
0885: assertFalse(tree.isExpanded(path111));
0886: assertFalse(tree.isExpanded(path2));
0887: assertFalse(tree.isExpanded(path21));
0888: tree.setExpandedState(path11, true);
0889: tree.setExpandedState(path2, true);
0890: tree.treeModelListener.treeStructureChanged(new TreeModelEvent(
0891: "any", rootPath, new int[] { 0 },
0892: new Object[] { child1 }));
0893: assertTrue(tree.isExpanded(rootPath));
0894: assertFalse(tree.isExpanded(path1));
0895: assertFalse(tree.isExpanded(path11));
0896: assertFalse(tree.isExpanded(path111));
0897: assertFalse(tree.isExpanded(path2));
0898: assertFalse(tree.isExpanded(path21));
0899: }
0900:
0901: public void testExpandCollapsePathRow() {
0902: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0903: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0904: "child1");
0905: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
0906: "child11");
0907: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
0908: "child111");
0909: root.add(child1);
0910: child1.add(child11);
0911: child11.add(child111);
0912: tree.setModel(new DefaultTreeModel(root));
0913: TreePath rootPath = new TreePath(root);
0914: TreePath path1 = rootPath.pathByAddingChild(child1);
0915: TreePath path11 = path1.pathByAddingChild(child11);
0916: TreePath path111 = path11.pathByAddingChild(child111);
0917: assertTrue(tree.isExpanded(rootPath));
0918: assertFalse(tree.isExpanded(path1));
0919: assertFalse(tree.isExpanded(path11));
0920: assertFalse(tree.isExpanded(path111));
0921: tree.expandPath(path11);
0922: assertTrue(tree.isExpanded(rootPath));
0923: assertTrue(tree.isExpanded(path1));
0924: assertTrue(tree.isExpanded(path11));
0925: assertFalse(tree.isExpanded(path111));
0926: tree.collapseRow(0);
0927: assertFalse(tree.isExpanded(rootPath));
0928: assertFalse(tree.isExpanded(path1));
0929: assertFalse(tree.isExpanded(path11));
0930: assertFalse(tree.isExpanded(path111));
0931: tree.expandPath(path111);
0932: assertFalse(tree.isExpanded(rootPath));
0933: assertFalse(tree.isExpanded(path1));
0934: assertFalse(tree.isExpanded(path11));
0935: assertFalse(tree.isExpanded(path111));
0936: tree.expandPath(path11);
0937: assertTrue(tree.isExpanded(rootPath));
0938: assertTrue(tree.isExpanded(path1));
0939: assertTrue(tree.isExpanded(path11));
0940: assertFalse(tree.isExpanded(path111));
0941: tree.collapsePath(path1);
0942: assertTrue(tree.isExpanded(rootPath));
0943: assertFalse(tree.isExpanded(path1));
0944: assertFalse(tree.isExpanded(path11));
0945: assertFalse(tree.isExpanded(path111));
0946: tree.collapsePath(path11);
0947: assertTrue(tree.isExpanded(rootPath));
0948: assertTrue(tree.isExpanded(path1));
0949: assertFalse(tree.isExpanded(path11));
0950: assertFalse(tree.isExpanded(path111));
0951: tree.collapsePath(path111);
0952: assertTrue(tree.isExpanded(rootPath));
0953: assertTrue(tree.isExpanded(path1));
0954: assertTrue(tree.isExpanded(path11));
0955: assertFalse(tree.isExpanded(path111));
0956: tree.collapseRow(1);
0957: assertTrue(tree.isExpanded(rootPath));
0958: assertFalse(tree.isExpanded(path1));
0959: assertFalse(tree.isExpanded(path11));
0960: assertFalse(tree.isExpanded(path111));
0961: tree.expandRow(1);
0962: assertTrue(tree.isExpanded(rootPath));
0963: assertTrue(tree.isExpanded(path1));
0964: assertTrue(tree.isExpanded(path11));
0965: assertFalse(tree.isExpanded(path111));
0966: tree.collapseRow(2);
0967: assertTrue(tree.isExpanded(rootPath));
0968: assertTrue(tree.isExpanded(path1));
0969: assertFalse(tree.isExpanded(path11));
0970: assertFalse(tree.isExpanded(path111));
0971: tree.expandRow(10);
0972: assertTrue(tree.isExpanded(rootPath));
0973: assertTrue(tree.isExpanded(path1));
0974: assertFalse(tree.isExpanded(path11));
0975: assertFalse(tree.isExpanded(path111));
0976: tree.collapseRow(10);
0977: assertTrue(tree.isExpanded(rootPath));
0978: assertTrue(tree.isExpanded(path1));
0979: assertFalse(tree.isExpanded(path11));
0980: assertFalse(tree.isExpanded(path111));
0981: tree.collapseRow(-1);
0982: assertTrue(tree.isExpanded(rootPath));
0983: assertTrue(tree.isExpanded(path1));
0984: assertFalse(tree.isExpanded(path11));
0985: assertFalse(tree.isExpanded(path111));
0986: DefaultMutableTreeNode unexisted = new DefaultMutableTreeNode(
0987: "unexisted");
0988: TreePath unexistedPath = rootPath.pathByAddingChild(unexisted);
0989: tree.expandPath(unexistedPath);
0990: assertFalse(tree.isExpanded(unexistedPath));
0991: unexisted.add(new DefaultMutableTreeNode());
0992: tree.expandPath(unexistedPath);
0993: assertTrue(tree.isExpanded(unexistedPath));
0994: }
0995:
0996: public void testHasBeenExpanded() {
0997: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
0998: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
0999: "child1");
1000: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1001: "child11");
1002: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1003: "child111");
1004: root.add(child1);
1005: child1.add(child11);
1006: child11.add(child111);
1007: tree.setModel(new DefaultTreeModel(root));
1008: TreePath rootPath = new TreePath(root);
1009: TreePath path1 = rootPath.pathByAddingChild(child1);
1010: TreePath path11 = path1.pathByAddingChild(child11);
1011: assertTrue(tree.hasBeenExpanded(rootPath));
1012: assertFalse(tree.hasBeenExpanded(path1));
1013: assertFalse(tree.hasBeenExpanded(path11));
1014: tree.expandPath(path11);
1015: assertTrue(tree.hasBeenExpanded(rootPath));
1016: assertTrue(tree.hasBeenExpanded(path1));
1017: assertTrue(tree.hasBeenExpanded(path11));
1018: tree.expandPath(path1);
1019: assertTrue(tree.hasBeenExpanded(rootPath));
1020: assertTrue(tree.hasBeenExpanded(path1));
1021: assertTrue(tree.hasBeenExpanded(path11));
1022: ((DefaultTreeModel) tree.getModel())
1023: .removeNodeFromParent(child11);
1024: assertTrue(tree.hasBeenExpanded(rootPath));
1025: if (isHarmony()) {
1026: assertTrue(tree.hasBeenExpanded(path1));
1027: } else {
1028: assertFalse(tree.hasBeenExpanded(path1));
1029: }
1030: assertFalse(tree.hasBeenExpanded(path11));
1031: assertFalse(tree.hasBeenExpanded(null));
1032: }
1033:
1034: public void testFireTreeExpandedCollapsedWillExpandCollapse()
1035: throws Exception {
1036: final Marker expandMarker = new Marker();
1037: final Marker collapseMarker = new Marker();
1038: tree.addTreeExpansionListener(new TreeExpansionListener() {
1039: public void treeExpanded(final TreeExpansionEvent event) {
1040: expandMarker.setOccurred();
1041: expandMarker.setAuxiliary(event);
1042: }
1043:
1044: public void treeCollapsed(final TreeExpansionEvent event) {
1045: collapseMarker.setOccurred();
1046: collapseMarker.setAuxiliary(event);
1047: }
1048: });
1049: final Marker willExpandMarker = new Marker();
1050: final Marker willCollapseMarker = new Marker();
1051: tree.addTreeWillExpandListener(new TreeWillExpandListener() {
1052: public void treeWillExpand(final TreeExpansionEvent event)
1053: throws ExpandVetoException {
1054: willExpandMarker.setOccurred();
1055: willExpandMarker.setAuxiliary(event);
1056: }
1057:
1058: public void treeWillCollapse(final TreeExpansionEvent event)
1059: throws ExpandVetoException {
1060: willCollapseMarker.setOccurred();
1061: willCollapseMarker.setAuxiliary(event);
1062: }
1063: });
1064: TreePath eventPath = new TreePath(new DefaultMutableTreeNode(
1065: "anyRoot"))
1066: .pathByAddingChild(new DefaultMutableTreeNode("anyNode"));
1067: tree.setExpandedState(eventPath, true);
1068: assertTrue(willExpandMarker.isOccurred());
1069: assertTrue(expandMarker.isOccurred());
1070: assertFalse(willCollapseMarker.isOccurred());
1071: assertFalse(collapseMarker.isOccurred());
1072: assertSame(eventPath, ((TreeExpansionEvent) willExpandMarker
1073: .getAuxiliary()).getPath());
1074: assertSame(eventPath, ((TreeExpansionEvent) expandMarker
1075: .getAuxiliary()).getPath());
1076: expandMarker.reset();
1077: willExpandMarker.reset();
1078: collapseMarker.reset();
1079: willCollapseMarker.reset();
1080: tree.setExpandedState(eventPath, false);
1081: assertFalse(willExpandMarker.isOccurred());
1082: assertFalse(expandMarker.isOccurred());
1083: assertTrue(willCollapseMarker.isOccurred());
1084: assertTrue(collapseMarker.isOccurred());
1085: assertSame(eventPath, ((TreeExpansionEvent) willCollapseMarker
1086: .getAuxiliary()).getPath());
1087: assertSame(eventPath, ((TreeExpansionEvent) collapseMarker
1088: .getAuxiliary()).getPath());
1089: expandMarker.reset();
1090: willExpandMarker.reset();
1091: collapseMarker.reset();
1092: willCollapseMarker.reset();
1093: tree.fireTreeExpanded(eventPath);
1094: tree.fireTreeCollapsed(eventPath);
1095: tree.fireTreeWillExpand(eventPath);
1096: tree.fireTreeWillCollapse(eventPath);
1097: assertTrue(willExpandMarker.isOccurred());
1098: assertTrue(expandMarker.isOccurred());
1099: assertTrue(willCollapseMarker.isOccurred());
1100: assertTrue(collapseMarker.isOccurred());
1101: assertSame(eventPath, ((TreeExpansionEvent) willExpandMarker
1102: .getAuxiliary()).getPath());
1103: assertSame(eventPath, ((TreeExpansionEvent) expandMarker
1104: .getAuxiliary()).getPath());
1105: assertSame(eventPath, ((TreeExpansionEvent) willCollapseMarker
1106: .getAuxiliary()).getPath());
1107: assertSame(eventPath, ((TreeExpansionEvent) collapseMarker
1108: .getAuxiliary()).getPath());
1109: }
1110:
1111: public void testFireValueChanged() {
1112: final Marker changeMarker = new Marker();
1113: tree.addTreeSelectionListener(new TreeSelectionListener() {
1114: public void valueChanged(final TreeSelectionEvent e) {
1115: changeMarker.setOccurred();
1116: changeMarker.setAuxiliary(e);
1117: }
1118: });
1119: TreePath eventPath = new TreePath(new DefaultMutableTreeNode(
1120: "anyRoot"))
1121: .pathByAddingChild(new DefaultMutableTreeNode("anyNode"));
1122: tree.getSelectionModel().addSelectionPath(eventPath);
1123: assertTrue(changeMarker.isOccurred());
1124: assertSame(eventPath, ((TreeSelectionEvent) changeMarker
1125: .getAuxiliary()).getPath());
1126: changeMarker.reset();
1127: tree.fireValueChanged(null);
1128: assertTrue(changeMarker.isOccurred());
1129: assertNull(changeMarker.getAuxiliary());
1130: }
1131:
1132: public void testGetSetAnchorSelectionPath() {
1133: TreePath path = new TreePath("unexisted");
1134: tree.setAnchorSelectionPath(path);
1135: assertSame(path, tree.getAnchorSelectionPath());
1136: tree.setAnchorSelectionPath(null);
1137: assertNull(tree.getAnchorSelectionPath());
1138: tree.getSelectionModel().addSelectionPath(path);
1139: assertSame(path, tree.getAnchorSelectionPath());
1140: }
1141:
1142: //This is part of UI functionality
1143: public void testGetClosestPathForLocation() {
1144: assertNotNull(tree.getClosestPathForLocation(1000, 1000));
1145: }
1146:
1147: //This is part of UI functionality
1148: public void testGetClosestRowForLocation() {
1149: assertEquals(0, tree.getClosestRowForLocation(-1000, -1000));
1150: }
1151:
1152: public void testGetDefaultTreeModel() {
1153: TreeModel model1 = JTree.getDefaultTreeModel();
1154: TreeModel model2 = JTree.getDefaultTreeModel();
1155: assertTrue(model1 instanceof DefaultTreeModel);
1156: assertNotSame(model1, model2);
1157: assertTrue(model1.getRoot() instanceof DefaultMutableTreeNode);
1158: DefaultMutableTreeNode root = (DefaultMutableTreeNode) model1
1159: .getRoot();
1160: assertEquals(3, root.getChildCount());
1161: assertEquals("JTree", root.getUserObject());
1162: assertTrue(root.getChildAt(0) instanceof DefaultMutableTreeNode);
1163: DefaultMutableTreeNode node = (DefaultMutableTreeNode) root
1164: .getChildAt(0);
1165: if (isHarmony()) {
1166: assertEquals("towns", node.getUserObject());
1167: assertEquals(4, node.getChildCount());
1168: assertEquals("Saint-Petersburg",
1169: ((DefaultMutableTreeNode) node.getChildAt(0))
1170: .getUserObject());
1171: assertEquals("New-York", ((DefaultMutableTreeNode) node
1172: .getChildAt(1)).getUserObject());
1173: assertEquals("Munchen", ((DefaultMutableTreeNode) node
1174: .getChildAt(2)).getUserObject());
1175: assertEquals("Oslo", ((DefaultMutableTreeNode) node
1176: .getChildAt(3)).getUserObject());
1177: } else {
1178: assertEquals("colors", node.getUserObject());
1179: assertEquals(4, node.getChildCount());
1180: assertEquals("blue", ((DefaultMutableTreeNode) node
1181: .getChildAt(0)).getUserObject());
1182: assertEquals("violet", ((DefaultMutableTreeNode) node
1183: .getChildAt(1)).getUserObject());
1184: assertEquals("red", ((DefaultMutableTreeNode) node
1185: .getChildAt(2)).getUserObject());
1186: assertEquals("yellow", ((DefaultMutableTreeNode) node
1187: .getChildAt(3)).getUserObject());
1188: }
1189: assertTrue(root.getChildAt(1) instanceof DefaultMutableTreeNode);
1190: node = (DefaultMutableTreeNode) root.getChildAt(1);
1191: if (isHarmony()) {
1192: assertEquals("animals", node.getUserObject());
1193: assertEquals(4, node.getChildCount());
1194: assertEquals("dog", ((DefaultMutableTreeNode) node
1195: .getChildAt(0)).getUserObject());
1196: assertEquals("tiger", ((DefaultMutableTreeNode) node
1197: .getChildAt(1)).getUserObject());
1198: assertEquals("wolf", ((DefaultMutableTreeNode) node
1199: .getChildAt(2)).getUserObject());
1200: assertEquals("bear", ((DefaultMutableTreeNode) node
1201: .getChildAt(3)).getUserObject());
1202: } else {
1203: assertEquals("sports", node.getUserObject());
1204: assertEquals(4, node.getChildCount());
1205: assertEquals("basketball", ((DefaultMutableTreeNode) node
1206: .getChildAt(0)).getUserObject());
1207: assertEquals("soccer", ((DefaultMutableTreeNode) node
1208: .getChildAt(1)).getUserObject());
1209: assertEquals("football", ((DefaultMutableTreeNode) node
1210: .getChildAt(2)).getUserObject());
1211: assertEquals("hockey", ((DefaultMutableTreeNode) node
1212: .getChildAt(3)).getUserObject());
1213: }
1214: assertTrue(root.getChildAt(2) instanceof DefaultMutableTreeNode);
1215: node = (DefaultMutableTreeNode) root.getChildAt(2);
1216: if (isHarmony()) {
1217: assertEquals("computers", node.getUserObject());
1218: assertEquals(4, node.getChildCount());
1219: assertEquals("notebook", ((DefaultMutableTreeNode) node
1220: .getChildAt(0)).getUserObject());
1221: assertEquals("desktop", ((DefaultMutableTreeNode) node
1222: .getChildAt(1)).getUserObject());
1223: assertEquals("server", ((DefaultMutableTreeNode) node
1224: .getChildAt(2)).getUserObject());
1225: assertEquals("mainframe", ((DefaultMutableTreeNode) node
1226: .getChildAt(3)).getUserObject());
1227: } else {
1228: assertEquals("food", node.getUserObject());
1229: assertEquals(4, node.getChildCount());
1230: assertEquals("hot dogs", ((DefaultMutableTreeNode) node
1231: .getChildAt(0)).getUserObject());
1232: assertEquals("pizza", ((DefaultMutableTreeNode) node
1233: .getChildAt(1)).getUserObject());
1234: assertEquals("ravioli", ((DefaultMutableTreeNode) node
1235: .getChildAt(2)).getUserObject());
1236: assertEquals("bananas", ((DefaultMutableTreeNode) node
1237: .getChildAt(3)).getUserObject());
1238: }
1239: }
1240:
1241: public void testGetDescendantToggledPaths() {
1242: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1243: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1244: "child1");
1245: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1246: "child11");
1247: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1248: "child111");
1249: root.add(child1);
1250: child1.add(child11);
1251: child11.add(child111);
1252: tree.setModel(new DefaultTreeModel(root));
1253: TreePath rootPath = new TreePath(root);
1254: TreePath path1 = rootPath.pathByAddingChild(child1);
1255: TreePath path11 = path1.pathByAddingChild(child11);
1256: assertNull(tree.getDescendantToggledPaths(null));
1257: checkInEnumeration(tree.getDescendantToggledPaths(rootPath),
1258: new Object[] { rootPath });
1259: checkInEnumeration(tree.getDescendantToggledPaths(path1),
1260: new Object[] {});
1261: checkInEnumeration(tree.getDescendantToggledPaths(path11),
1262: new Object[] {});
1263: tree.expandPath(path11);
1264: checkInEnumeration(tree.getDescendantToggledPaths(rootPath),
1265: new Object[] { path1, rootPath, path11 });
1266: checkInEnumeration(tree.getDescendantToggledPaths(path11),
1267: new Object[] { path11 });
1268: tree.collapsePath(path11);
1269: checkInEnumeration(tree.getDescendantToggledPaths(rootPath),
1270: new Object[] { path1, rootPath, path11 });
1271: tree.collapsePath(rootPath);
1272: checkInEnumeration(tree.getDescendantToggledPaths(rootPath),
1273: new Object[] { path1, rootPath, path11 });
1274: }
1275:
1276: public void testGetDragEnabled() {
1277: assertFalse(tree.getDragEnabled());
1278: tree.setDragEnabled(true);
1279: assertTrue(tree.getDragEnabled());
1280: assertFalse(propertyChangeController.isChanged());
1281: }
1282:
1283: public void testGetExpandedDescendants() {
1284: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1285: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1286: "child1");
1287: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1288: "child11");
1289: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1290: "child111");
1291: root.add(child1);
1292: child1.add(child11);
1293: child11.add(child111);
1294: tree.setModel(new DefaultTreeModel(root));
1295: TreePath rootPath = new TreePath(root);
1296: TreePath path1 = rootPath.pathByAddingChild(child1);
1297: TreePath path11 = path1.pathByAddingChild(child11);
1298: assertNull(tree.getExpandedDescendants(null));
1299: checkInEnumeration(tree.getExpandedDescendants(rootPath),
1300: new Object[] { rootPath });
1301: assertNull(tree.getExpandedDescendants(path1));
1302: assertNull(tree.getExpandedDescendants(path11));
1303: tree.expandPath(path11);
1304: checkInEnumeration(tree.getExpandedDescendants(rootPath),
1305: new Object[] { path1, rootPath, path11 });
1306: tree.collapsePath(path11);
1307: checkInEnumeration(tree.getExpandedDescendants(rootPath),
1308: new Object[] { path1, rootPath });
1309: tree.collapsePath(rootPath);
1310: assertNull(tree.getExpandedDescendants(rootPath));
1311: }
1312:
1313: public void testGetSetExpandsSelectedPaths() {
1314: assertTrue(tree.getExpandsSelectedPaths());
1315: tree.setExpandsSelectedPaths(false);
1316: assertFalse(tree.getExpandsSelectedPaths());
1317: assertTrue(propertyChangeController
1318: .isChanged("expandsSelectedPaths"));
1319: }
1320:
1321: public void testGetSetInvokesStopCellEditing() {
1322: assertFalse(tree.getInvokesStopCellEditing());
1323: tree.setInvokesStopCellEditing(true);
1324: assertTrue(tree.getInvokesStopCellEditing());
1325: assertTrue(propertyChangeController
1326: .isChanged("invokesStopCellEditing"));
1327: }
1328:
1329: public void testGetLastSelectedPathComponent() {
1330: assertNull(tree.getLastSelectedPathComponent());
1331: tree.setSelectionRow(1);
1332: assertTrue(tree.getLastSelectedPathComponent() instanceof DefaultMutableTreeNode);
1333: if (isHarmony()) {
1334: assertEquals("towns", tree.getLastSelectedPathComponent()
1335: .toString());
1336: } else {
1337: assertEquals("colors", tree.getLastSelectedPathComponent()
1338: .toString());
1339: }
1340: }
1341:
1342: public void testGetLeadSelectionRow() {
1343: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1344: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1345: "child1");
1346: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1347: "child11");
1348: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1349: "child111");
1350: root.add(child1);
1351: child1.add(child11);
1352: child11.add(child111);
1353: tree.setModel(new DefaultTreeModel(root));
1354: TreePath rootPath = new TreePath(root);
1355: TreePath path1 = rootPath.pathByAddingChild(child1);
1356: TreePath path11 = path1.pathByAddingChild(child11);
1357: assertEquals(-1, tree.getLeadSelectionRow());
1358: tree.setSelectionRow(10);
1359: assertEquals(-1, tree.getLeadSelectionRow());
1360: tree.setExpandsSelectedPaths(false);
1361: tree.setSelectionPath(path11);
1362: assertEquals(-1, tree.getLeadSelectionRow());
1363: tree.setLeadSelectionPath(path11);
1364: assertEquals(-1, tree.getLeadSelectionRow());
1365: tree.setLeadSelectionPath(path1);
1366: assertEquals(1, tree.getLeadSelectionRow());
1367: }
1368:
1369: public void testGetMinMaxSelectionRow() {
1370: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1371: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1372: "child1");
1373: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1374: "child11");
1375: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
1376: "child2");
1377: root.add(child1);
1378: child1.add(child11);
1379: root.add(child2);
1380: tree.setModel(new DefaultTreeModel(root));
1381: TreePath rootPath = new TreePath(root);
1382: TreePath path1 = rootPath.pathByAddingChild(child1);
1383: TreePath path11 = path1.pathByAddingChild(child11);
1384: TreePath path2 = rootPath.pathByAddingChild(child2);
1385: tree.setSelectionPaths(new TreePath[] { path2, path11 });
1386: assertEquals(2, tree.getMinSelectionRow());
1387: assertEquals(3, tree.getMaxSelectionRow());
1388: tree.collapsePath(path1);
1389: assertEqualsIgnoreOrder(new TreePath[] { path2, path1 }, tree
1390: .getSelectionPaths());
1391: assertEquals(1, tree.getMinSelectionRow());
1392: assertEquals(2, tree.getMaxSelectionRow());
1393: tree.expandPath(path1);
1394: assertEqualsIgnoreOrder(new TreePath[] { path2, path1 }, tree
1395: .getSelectionPaths());
1396: assertEquals(1, tree.getMinSelectionRow());
1397: assertEquals(3, tree.getMaxSelectionRow());
1398: }
1399:
1400: public void testGetNextMatch() {
1401: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1402: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1403: "child1");
1404: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1405: "child11");
1406: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
1407: "child2");
1408: root.add(child1);
1409: child1.add(child11);
1410: root.add(child2);
1411: tree.setModel(new DefaultTreeModel(root));
1412: TreePath rootPath = new TreePath(root);
1413: TreePath path1 = rootPath.pathByAddingChild(child1);
1414: TreePath path11 = path1.pathByAddingChild(child11);
1415: TreePath path2 = rootPath.pathByAddingChild(child2);
1416: assertEquals(rootPath, tree.getNextMatch("ro", 0,
1417: Position.Bias.Forward));
1418: assertEquals(rootPath, tree.getNextMatch("ro", 2,
1419: Position.Bias.Forward));
1420: assertEquals(rootPath, tree.getNextMatch("ro", 2,
1421: Position.Bias.Backward));
1422: assertEquals(path1, tree.getNextMatch("ch", 0,
1423: Position.Bias.Forward));
1424: assertEquals(path1, tree.getNextMatch("ch", 1,
1425: Position.Bias.Forward));
1426: assertEquals(path1, tree.getNextMatch("ch", 1,
1427: Position.Bias.Backward));
1428: assertEquals(path2, tree.getNextMatch("ch", 2,
1429: Position.Bias.Forward));
1430: assertEquals(path1, tree.getNextMatch("child1", 2,
1431: Position.Bias.Forward));
1432: assertEquals(path2, tree.getNextMatch("child2", 1,
1433: Position.Bias.Backward));
1434: assertNull(tree.getNextMatch("child11", 1,
1435: Position.Bias.Backward));
1436: tree.expandRow(1);
1437: assertEquals(path11, tree.getNextMatch("child11", 1,
1438: Position.Bias.Backward));
1439: assertEquals(path1, tree.getNextMatch("child1", 0,
1440: Position.Bias.Forward));
1441: assertEquals(path11, tree.getNextMatch("child1", 0,
1442: Position.Bias.Backward));
1443: assertEquals(path11, tree.getNextMatch("child1", 0, null));
1444: assertEquals(path11, tree.getNextMatch("ChiLD1", 0, null));
1445: assertNull(tree
1446: .getNextMatch("childX", 1, Position.Bias.Forward));
1447: testExceptionalCase(new IllegalArgumentCase() {
1448: @Override
1449: public void exceptionalAction() throws Exception {
1450: tree.getNextMatch("any", -1, Position.Bias.Forward);
1451: }
1452: });
1453: testExceptionalCase(new IllegalArgumentCase() {
1454: @Override
1455: public void exceptionalAction() throws Exception {
1456: tree.getNextMatch("any", 10, Position.Bias.Forward);
1457: }
1458: });
1459: testExceptionalCase(new IllegalArgumentCase() {
1460: @Override
1461: public void exceptionalAction() throws Exception {
1462: tree.getNextMatch(null, 1, Position.Bias.Forward);
1463: }
1464: });
1465: }
1466:
1467: public void testGetPathBetweenRows() {
1468: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1469: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1470: "child1");
1471: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1472: "child11");
1473: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
1474: "child2");
1475: root.add(child1);
1476: child1.add(child11);
1477: root.add(child2);
1478: tree.setModel(new DefaultTreeModel(root));
1479: TreePath rootPath = new TreePath(root);
1480: TreePath path1 = rootPath.pathByAddingChild(child1);
1481: TreePath path11 = path1.pathByAddingChild(child11);
1482: TreePath path2 = rootPath.pathByAddingChild(child2);
1483: assertEquals(new TreePath[] { path1 }, tree.getPathBetweenRows(
1484: 1, 1));
1485: assertEquals(new TreePath[] { rootPath, path1 }, tree
1486: .getPathBetweenRows(0, 1));
1487: assertEquals(new TreePath[] { path1, path2 }, tree
1488: .getPathBetweenRows(1, 2));
1489: assertEquals(new TreePath[] { rootPath, path1, path2 }, tree
1490: .getPathBetweenRows(0, 2));
1491: if (isHarmony()) {
1492: assertEquals(new TreePath[] { rootPath, path1, path2 },
1493: tree.getPathBetweenRows(-1, 3));
1494: } else {
1495: assertEquals(new TreePath[] { null, rootPath, path1, path2,
1496: null }, tree.getPathBetweenRows(-1, 3));
1497: }
1498: tree.expandPath(path1);
1499: assertEquals(new TreePath[] { rootPath, path1, path11 }, tree
1500: .getPathBetweenRows(0, 2));
1501: }
1502:
1503: //This is UI behavior. Should not be deeply tested here
1504: public void testGetPathBounds() {
1505: Object root = tree.getModel().getRoot();
1506: TreePath pathToRoot = new TreePath(root);
1507: assertEquals(tree.getPathBounds(pathToRoot), tree.getUI()
1508: .getPathBounds(tree, pathToRoot));
1509: TreePath pathToChild = pathToRoot.pathByAddingChild(tree
1510: .getModel().getChild(root, 0));
1511: assertEquals(tree.getPathBounds(pathToChild), tree.getUI()
1512: .getPathBounds(tree, pathToChild));
1513: }
1514:
1515: //This is UI behavior. Should not be deeply tested here
1516: public void testGetRowBounds() {
1517: Object root = tree.getModel().getRoot();
1518: TreePath pathToRoot = new TreePath(root);
1519: assertEquals(tree.getRowBounds(0), tree.getUI().getPathBounds(
1520: tree, pathToRoot));
1521: TreePath pathToChild = pathToRoot.pathByAddingChild(tree
1522: .getModel().getChild(root, 0));
1523: assertEquals(tree.getRowBounds(1), tree.getUI().getPathBounds(
1524: tree, pathToChild));
1525: assertNull(tree.getRowBounds(-1));
1526: assertNull(tree.getRowBounds(10));
1527: }
1528:
1529: public void testGetPathForLocation() {
1530: assertEquals(new TreePath(tree.getModel().getRoot()), tree
1531: .getPathForLocation(5, 5));
1532: assertNull(tree.getPathForLocation(500, 5));
1533: }
1534:
1535: public void testGetRowForLocation() {
1536: assertEquals(0, tree.getRowForLocation(5, 5));
1537: assertEquals(-1, tree.getRowForLocation(500, 5));
1538: }
1539:
1540: //This is UI behavior. Should not be deeply tested here
1541: public void testGetRowForPath() {
1542: Object root = tree.getModel().getRoot();
1543: TreePath pathToRoot = new TreePath(root);
1544: assertEquals(0, tree.getRowForPath(pathToRoot));
1545: Object child = tree.getModel().getChild(root, 0);
1546: TreePath pathToChild = pathToRoot.pathByAddingChild(child);
1547: assertEquals(1, tree.getRowForPath(pathToChild));
1548: Object childChild = tree.getModel().getChild(child, 0);
1549: TreePath pathToChildChild = pathToChild
1550: .pathByAddingChild(childChild);
1551: assertEquals(-1, tree.getRowForPath(pathToChildChild));
1552: }
1553:
1554: //This is UI behavior. Should not be deeply tested here
1555: public void testGetPathForRow() {
1556: assertNotNull(tree.getPathForRow(0));
1557: assertNull(tree.getPathForRow(-1));
1558: assertNull(tree.getPathForRow(10));
1559: }
1560:
1561: public void testGetRowCount() {
1562: assertEquals(4, tree.getRowCount());
1563: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
1564: "tree root");
1565: DefaultMutableTreeNode child = new DefaultMutableTreeNode(
1566: "child");
1567: root.add(child);
1568: child.add(new DefaultMutableTreeNode("child_child"));
1569: tree.setModel(new DefaultTreeModel(root));
1570: assertEquals(2, tree.getRowCount());
1571: tree.setModel(null);
1572: assertEquals(0, tree.getRowCount());
1573: }
1574:
1575: public void testGetSetRowHeight() {
1576: assertEquals(0, tree.getRowHeight());
1577: tree.setRowHeight(20);
1578: assertEquals(20, tree.getRowHeight());
1579: assertTrue(propertyChangeController.isChanged("rowHeight"));
1580: propertyChangeController.reset();
1581: tree.setRowHeight(-10);
1582: assertEquals(-10, tree.getRowHeight());
1583: assertTrue(propertyChangeController.isChanged("rowHeight"));
1584: }
1585:
1586: public void testGetSetScrollsOnExpand() {
1587: assertTrue(tree.getScrollsOnExpand());
1588: tree.setScrollsOnExpand(false);
1589: assertFalse(tree.getScrollsOnExpand());
1590: assertTrue(propertyChangeController
1591: .isChanged("scrollsOnExpand"));
1592: }
1593:
1594: public void testGetSelectionCount() {
1595: assertEquals(0, tree.getSelectionCount());
1596: Object root = tree.getModel().getRoot();
1597: TreePath pathToRoot = new TreePath(root);
1598: Object child = tree.getModel().getChild(root, 0);
1599: TreePath pathToChild = pathToRoot.pathByAddingChild(child);
1600: Object childChild = tree.getModel().getChild(child, 0);
1601: TreePath pathToChildChild = pathToChild
1602: .pathByAddingChild(childChild);
1603: tree.addSelectionPath(pathToRoot);
1604: assertEquals(1, tree.getSelectionCount());
1605: tree.addSelectionPath(pathToChild);
1606: assertEquals(2, tree.getSelectionCount());
1607: tree.setExpandsSelectedPaths(false);
1608: tree.addSelectionPath(pathToChildChild);
1609: assertEquals(3, tree.getSelectionCount());
1610: tree.clearSelection();
1611: assertEquals(0, tree.getSelectionCount());
1612: tree.setAnchorSelectionPath(pathToChildChild);
1613: tree.setLeadSelectionPath(pathToRoot);
1614: assertEquals(0, tree.getSelectionCount());
1615: }
1616:
1617: public void testGetSelectionModel() {
1618: assertNotNull(tree.getSelectionModel());
1619: assertTrue(tree.getSelectionModel() instanceof DefaultTreeSelectionModel);
1620: assertNotNull(tree.getSelectionModel().getRowMapper());
1621: }
1622:
1623: public void testGetSetSelectionPaths() {
1624: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1625: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1626: "child1");
1627: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1628: "child11");
1629: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1630: "child111");
1631: root.add(child1);
1632: child1.add(child11);
1633: child11.add(child111);
1634: tree.setModel(new DefaultTreeModel(root));
1635: TreePath rootPath = new TreePath(root);
1636: TreePath path1 = rootPath.pathByAddingChild(child1);
1637: TreePath path11 = path1.pathByAddingChild(child11);
1638: TreePath path111 = path11.pathByAddingChild(child111);
1639: assertNull(tree.getSelectionPaths());
1640: tree.setSelectionPath(path1);
1641: tree.addSelectionPath(rootPath);
1642: assertEqualsIgnoreOrder(new TreePath[] { path1, rootPath },
1643: tree.getSelectionPaths());
1644: tree.setSelectionPath(path11);
1645: assertEqualsIgnoreOrder(new TreePath[] { path11 }, tree
1646: .getSelectionPaths());
1647: tree.addSelectionPaths(new TreePath[] { rootPath, path111 });
1648: assertEqualsIgnoreOrder(new TreePath[] { path11, rootPath,
1649: path111 }, tree.getSelectionPaths());
1650: tree.collapsePath(path1);
1651: assertEqualsIgnoreOrder(new TreePath[] { rootPath, path1 },
1652: tree.getSelectionPaths());
1653: tree.setSelectionPaths(new TreePath[] { path111 });
1654: assertEqualsIgnoreOrder(new TreePath[] { path111 }, tree
1655: .getSelectionPaths());
1656: tree.setSelectionPaths(null);
1657: assertNull(tree.getSelectionPaths());
1658: }
1659:
1660: public void testGetSetShowsRootHandles() {
1661: assertFalse(tree.getShowsRootHandles());
1662: tree.setShowsRootHandles(true);
1663: assertTrue(tree.getShowsRootHandles());
1664: assertTrue(propertyChangeController
1665: .isChanged("showsRootHandles"));
1666: }
1667:
1668: public void testGetToggleClickCount() {
1669: assertEquals(2, tree.getToggleClickCount());
1670: tree.setToggleClickCount(10);
1671: assertEquals(10, tree.getToggleClickCount());
1672: assertTrue(propertyChangeController
1673: .isChanged("toggleClickCount"));
1674: }
1675:
1676: public void testGetUI() {
1677: assertNotNull("ui is returned ", tree.getUI());
1678: }
1679:
1680: public void testIsSetEditable() {
1681: assertFalse(tree.isEditable());
1682: tree.setEditable(true);
1683: assertTrue(tree.isEditable());
1684: assertTrue(propertyChangeController.isChanged("editable"));
1685: }
1686:
1687: public void testIsExpanded() {
1688: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1689: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1690: "child1");
1691: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1692: "child11");
1693: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1694: "child111");
1695: root.add(child1);
1696: child1.add(child11);
1697: child11.add(child111);
1698: tree.setModel(new DefaultTreeModel(root));
1699: TreePath rootPath = new TreePath(root);
1700: TreePath path1 = rootPath.pathByAddingChild(child1);
1701: TreePath path11 = path1.pathByAddingChild(child11);
1702: assertTrue(tree.isExpanded(rootPath));
1703: assertTrue(tree.isExpanded(0));
1704: assertFalse(tree.isExpanded(path1));
1705: assertFalse(tree.isExpanded(1));
1706: tree.expandPath(path1);
1707: assertTrue(tree.isExpanded(rootPath));
1708: assertTrue(tree.isExpanded(0));
1709: assertTrue(tree.isExpanded(path1));
1710: assertTrue(tree.isExpanded(1));
1711: assertFalse(tree.isExpanded(path11));
1712: assertFalse(tree.isExpanded(2));
1713: }
1714:
1715: public void testIsFixedRowHeight() {
1716: assertFalse(tree.isFixedRowHeight());
1717: tree.setRowHeight(10);
1718: assertTrue(tree.isFixedRowHeight());
1719: tree.setRowHeight(-10);
1720: assertFalse(tree.isFixedRowHeight());
1721: }
1722:
1723: public void testIsLargeModel() {
1724: assertFalse(tree.isLargeModel());
1725: tree.setLargeModel(true);
1726: assertTrue(tree.isLargeModel());
1727: assertTrue(propertyChangeController.isChanged("largeModel"));
1728: }
1729:
1730: public void testIsPathEditable() {
1731: assertFalse(tree.isPathEditable(null));
1732: tree.setEditable(true);
1733: assertTrue(tree.isPathEditable(null));
1734: }
1735:
1736: public void testSetIsRootVisible() {
1737: assertTrue(tree.isRootVisible());
1738: tree.setRootVisible(false);
1739: assertFalse(tree.isRootVisible());
1740: assertTrue(propertyChangeController.isChanged("rootVisible"));
1741: }
1742:
1743: public void testIsPathSelected() {
1744: TreePath unexistedPath = new TreePath(
1745: new DefaultMutableTreeNode("any root"));
1746: assertFalse(tree.isPathSelected(null));
1747: assertFalse(tree.isPathSelected(unexistedPath));
1748: tree.setSelectionPath(unexistedPath);
1749: assertTrue(tree.isPathSelected(unexistedPath));
1750: }
1751:
1752: public void testIsRowSelected() {
1753: assertFalse(tree.isRowSelected(0));
1754: assertFalse(tree.isRowSelected(10));
1755: tree.setSelectionRow(0);
1756: assertTrue(tree.isRowSelected(0));
1757: }
1758:
1759: public void testIsSelectionEmpty() {
1760: assertTrue(tree.isSelectionEmpty());
1761: tree.setSelectionRow(0);
1762: assertFalse(tree.isSelectionEmpty());
1763: tree.clearSelection();
1764: assertTrue(tree.isSelectionEmpty());
1765: }
1766:
1767: public void testIsVisible() {
1768: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1769: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1770: "child1");
1771: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1772: "child11");
1773: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1774: "child111");
1775: root.add(child1);
1776: child1.add(child11);
1777: child11.add(child111);
1778: tree.setModel(new DefaultTreeModel(root));
1779: TreePath rootPath = new TreePath(root);
1780: TreePath path1 = rootPath.pathByAddingChild(child1);
1781: TreePath path11 = path1.pathByAddingChild(child11);
1782: TreePath path111 = path11.pathByAddingChild(child111);
1783: assertTrue(tree.isVisible(rootPath));
1784: assertTrue(tree.isVisible(path1));
1785: assertFalse(tree.isVisible(path11));
1786: assertFalse(tree.isVisible(path111));
1787: tree.expandPath(path11);
1788: assertTrue(tree.isVisible(rootPath));
1789: assertTrue(tree.isVisible(path1));
1790: assertTrue(tree.isVisible(path11));
1791: assertTrue(tree.isVisible(path111));
1792: }
1793:
1794: public void testMakeVisible() {
1795: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1796: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1797: "child1");
1798: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1799: "child11");
1800: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1801: "child111");
1802: root.add(child1);
1803: child1.add(child11);
1804: child11.add(child111);
1805: tree.setModel(new DefaultTreeModel(root));
1806: TreePath rootPath = new TreePath(root);
1807: TreePath path1 = rootPath.pathByAddingChild(child1);
1808: TreePath path11 = path1.pathByAddingChild(child11);
1809: TreePath path111 = path11.pathByAddingChild(child111);
1810: assertFalse(tree.isExpanded(path1));
1811: tree.makeVisible(path1);
1812: assertFalse(tree.isExpanded(path1));
1813: tree.makeVisible(path111);
1814: assertTrue(tree.isExpanded(path1));
1815: assertTrue(tree.isExpanded(path11));
1816: }
1817:
1818: public void testRemoveDescendantSelectedPaths() {
1819: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1820: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1821: "child1");
1822: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1823: "child11");
1824: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1825: "child111");
1826: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
1827: "child2");
1828: DefaultMutableTreeNode child21 = new DefaultMutableTreeNode(
1829: "child21");
1830: root.add(child1);
1831: child1.add(child11);
1832: child11.add(child111);
1833: root.add(child2);
1834: child2.add(child21);
1835: tree.setModel(new DefaultTreeModel(root));
1836: TreePath rootPath = new TreePath(root);
1837: TreePath path1 = rootPath.pathByAddingChild(child1);
1838: TreePath path11 = path1.pathByAddingChild(child11);
1839: TreePath path21 = rootPath.pathByAddingChild(child21);
1840: tree.setSelectionPaths(new TreePath[] { rootPath, path11,
1841: path21 });
1842: assertEqualsIgnoreOrder(new TreePath[] { rootPath, path11,
1843: path21 }, tree.getSelectionPaths());
1844: tree.removeDescendantSelectedPaths(path11, false);
1845: assertEqualsIgnoreOrder(new TreePath[] { rootPath, path11,
1846: path21 }, tree.getSelectionPaths());
1847: tree.removeDescendantSelectedPaths(path11, true);
1848: assertEqualsIgnoreOrder(new TreePath[] { rootPath, path21 },
1849: tree.getSelectionPaths());
1850: tree.removeDescendantSelectedPaths(rootPath, false);
1851: assertEqualsIgnoreOrder(new TreePath[] { rootPath }, tree
1852: .getSelectionPaths());
1853: tree.removeDescendantSelectedPaths(rootPath, true);
1854: assertNull(tree.getSelectionPaths());
1855: }
1856:
1857: public void testRemoveDescendantToggledPaths() {
1858: DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
1859: DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(
1860: "child1");
1861: DefaultMutableTreeNode child11 = new DefaultMutableTreeNode(
1862: "child11");
1863: DefaultMutableTreeNode child111 = new DefaultMutableTreeNode(
1864: "child111");
1865: DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(
1866: "child2");
1867: DefaultMutableTreeNode child21 = new DefaultMutableTreeNode(
1868: "child21");
1869: root.add(child1);
1870: child1.add(child11);
1871: child11.add(child111);
1872: root.add(child2);
1873: child2.add(child21);
1874: tree.setModel(new DefaultTreeModel(root));
1875: TreePath rootPath = new TreePath(root);
1876: TreePath path1 = rootPath.pathByAddingChild(child1);
1877: TreePath path11 = path1.pathByAddingChild(child11);
1878: TreePath path2 = rootPath.pathByAddingChild(child2);
1879: assertTrue(tree.isExpanded(rootPath));
1880: assertFalse(tree.isExpanded(path1));
1881: assertFalse(tree.isExpanded(path2));
1882: tree.expandPath(path1);
1883: tree.expandPath(path2);
1884: assertTrue(tree.isExpanded(rootPath));
1885: assertTrue(tree.isExpanded(path1));
1886: assertFalse(tree.isExpanded(path11));
1887: assertTrue(tree.isExpanded(path2));
1888: tree
1889: .removeDescendantToggledPaths(createTestEnumeration(new TreePath[] {
1890: path2, path1 }));
1891: assertTrue(tree.isExpanded(rootPath));
1892: assertFalse(tree.isExpanded(path1));
1893: assertFalse(tree.isExpanded(path11));
1894: assertFalse(tree.isExpanded(path2));
1895: tree.expandPath(path11);
1896: tree.expandPath(path2);
1897: tree
1898: .removeDescendantToggledPaths(createTestEnumeration(new TreePath[] {
1899: path2, path11 }));
1900: assertTrue(tree.isExpanded(rootPath));
1901: assertTrue(tree.isExpanded(path1));
1902: assertFalse(tree.isExpanded(path11));
1903: assertFalse(tree.isExpanded(path2));
1904: }
1905:
1906: public void testSetCellEditor() {
1907: PropertyChangeController listener = new PropertyChangeController();
1908: DefaultTreeCellEditor editor1 = new DefaultTreeCellEditor(tree,
1909: (DefaultTreeCellRenderer) tree.getCellRenderer());
1910: DefaultTreeCellEditor editor2 = new DefaultTreeCellEditor(tree,
1911: (DefaultTreeCellRenderer) tree.getCellRenderer());
1912: tree.addPropertyChangeListener(listener);
1913: TreeCellEditor oldEditor = tree.getCellEditor();
1914: tree.setCellEditor(editor1);
1915: listener.checkPropertyFired(tree, "cellEditor", oldEditor,
1916: editor1);
1917: assertEquals("cellEditor", editor1, tree.getCellEditor());
1918: tree.setEditable(true);
1919: listener.reset();
1920: tree.setCellEditor(editor2);
1921: listener.checkPropertyFired(tree, "cellEditor", editor1,
1922: editor2);
1923: assertEquals("cellEditor", editor2, tree.getCellEditor());
1924: assertTrue(tree.isEditable());
1925: listener.reset();
1926: tree.setCellEditor(editor2);
1927: assertFalse("event's not been fired ", listener.isChanged());
1928: listener.reset();
1929: tree.setCellEditor(null);
1930: listener.checkPropertyFired(tree, "cellEditor", editor2, null);
1931: // it's being controlled by UI via listener
1932: assertNotNull("cellEditor", tree.getCellEditor());
1933: assertNotSame("cellEditor", oldEditor, tree.getCellEditor());
1934: assertNotSame("cellEditor", editor2, tree.getCellEditor());
1935: assertTrue(tree.isEditable());
1936: listener.reset();
1937: }
1938:
1939: public void testGetCellEditor() {
1940: assertNull(tree.getCellEditor());
1941: }
1942:
1943: public void testSetCellRenderer() {
1944: PropertyChangeController listener = new PropertyChangeController();
1945: TreeCellRenderer renderer1 = new DefaultTreeCellRenderer();
1946: TreeCellRenderer renderer2 = new DefaultTreeCellRenderer();
1947: tree.addPropertyChangeListener(listener);
1948: TreeCellRenderer oldRenderer = tree.getCellRenderer();
1949: tree.setCellRenderer(renderer1);
1950: listener.checkPropertyFired(tree, "cellRenderer", oldRenderer,
1951: renderer1);
1952: assertEquals("cellRenderer", renderer1, tree.getCellRenderer());
1953: listener.reset();
1954: tree.setCellRenderer(renderer2);
1955: listener.checkPropertyFired(tree, "cellRenderer", renderer1,
1956: renderer2);
1957: assertEquals("cellRenderer", renderer2, tree.getCellRenderer());
1958: listener.reset();
1959: tree.setCellRenderer(renderer2);
1960: assertFalse("event's not been fired ", listener.isChanged());
1961: listener.reset();
1962: tree.setCellRenderer(null);
1963: listener.checkPropertyFired(tree, "cellRenderer", renderer2,
1964: null);
1965: // it's being controlled by UI via listener
1966: assertNotNull("cellRenderer", tree.getCellRenderer());
1967: assertNotSame("cellRenderer", oldRenderer, tree
1968: .getCellRenderer());
1969: assertNotSame("cellRenderer", renderer2, tree.getCellRenderer());
1970: listener.reset();
1971: }
1972:
1973: public void testGetCellRenderer() {
1974: assertNotNull(tree.getCellRenderer());
1975: assertTrue(tree.getCellRenderer() instanceof DefaultTreeCellRenderer);
1976: }
1977:
1978: public void testSetExpandedState() {
1979: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
1980: DefaultMutableTreeNode node1 = new DefaultMutableTreeNode();
1981: DefaultMutableTreeNode node11 = new DefaultMutableTreeNode();
1982: DefaultMutableTreeNode node111 = new DefaultMutableTreeNode();
1983: root.add(node1);
1984: node1.add(node11);
1985: node11.add(node111);
1986: DefaultTreeModel model = new DefaultTreeModel(root);
1987: tree.setModel(model);
1988: TreePath rootPath = new TreePath(root);
1989: TreePath path1 = rootPath.pathByAddingChild(node1);
1990: TreePath path11 = path1.pathByAddingChild(node11);
1991: TreePath path111 = path11.pathByAddingChild(node111);
1992: assertTrue(tree.isExpanded(rootPath));
1993: assertFalse(tree.isExpanded(path1));
1994: assertFalse(tree.isExpanded(path11));
1995: tree.setExpandedState(path1, true);
1996: assertTrue(tree.isExpanded(rootPath));
1997: assertTrue(tree.isExpanded(path1));
1998: assertFalse(tree.isExpanded(path11));
1999: tree.setExpandedState(rootPath, false);
2000: assertFalse(tree.isExpanded(rootPath));
2001: assertFalse(tree.isExpanded(path1));
2002: assertFalse(tree.isExpanded(path11));
2003: tree.setExpandedState(rootPath, true);
2004: assertTrue(tree.isExpanded(rootPath));
2005: assertTrue(tree.isExpanded(path1));
2006: assertFalse(tree.isExpanded(path11));
2007: tree.setExpandedState(rootPath, false);
2008: assertFalse(tree.isExpanded(rootPath));
2009: assertFalse(tree.isExpanded(path1));
2010: assertFalse(tree.isExpanded(path11));
2011: tree.setExpandedState(path1, false);
2012: assertTrue(tree.isExpanded(rootPath));
2013: assertFalse(tree.isExpanded(path1));
2014: assertFalse(tree.isExpanded(path11));
2015: tree.setExpandedState(path11, false);
2016: assertTrue(tree.isExpanded(rootPath));
2017: assertTrue(tree.isExpanded(path1));
2018: assertFalse(tree.isExpanded(path11));
2019: tree.setExpandedState(path111, true);
2020: assertTrue(tree.isExpanded(rootPath));
2021: assertTrue(tree.isExpanded(path1));
2022: assertTrue(tree.isExpanded(path11));
2023: assertTrue(tree.isExpanded(path111));
2024: TreePath unexistedPath = path111
2025: .pathByAddingChild(new DefaultMutableTreeNode());
2026: tree.setExpandedState(unexistedPath, true);
2027: assertTrue(tree.isExpanded(unexistedPath));
2028: tree.setExpandedState(unexistedPath, false);
2029: assertFalse(tree.isExpanded(unexistedPath));
2030: tree.setExpandedState(path111, false);
2031: assertTrue(tree.isExpanded(path1));
2032: assertTrue(tree.isExpanded(path11));
2033: assertFalse(tree.isExpanded(path111));
2034: tree.addTreeWillExpandListener(new TreeWillExpandListener() {
2035: public void treeWillExpand(TreeExpansionEvent event)
2036: throws ExpandVetoException {
2037: throw new ExpandVetoException(event);
2038: }
2039:
2040: public void treeWillCollapse(TreeExpansionEvent event)
2041: throws ExpandVetoException {
2042: }
2043: });
2044: tree.setExpandedState(path111, true);
2045: assertTrue(tree.isExpanded(path1));
2046: assertTrue(tree.isExpanded(path11));
2047: assertFalse(tree.isExpanded(path111));
2048: tree.addTreeWillExpandListener(new TreeWillExpandListener() {
2049: public void treeWillExpand(TreeExpansionEvent event)
2050: throws ExpandVetoException {
2051: }
2052:
2053: public void treeWillCollapse(TreeExpansionEvent event)
2054: throws ExpandVetoException {
2055: throw new ExpandVetoException(event);
2056: }
2057: });
2058: tree.setExpandedState(path11, false);
2059: assertTrue(tree.isExpanded(path1));
2060: assertTrue(tree.isExpanded(path11));
2061: assertFalse(tree.isExpanded(path111));
2062: tree.setExpandedState(null, false);
2063: assertTrue(tree.isExpanded(rootPath));
2064: assertTrue(tree.isExpanded(path1));
2065: assertTrue(tree.isExpanded(path11));
2066: assertFalse(tree.isExpanded(path111));
2067: }
2068:
2069: public void testGetSetLeadSelectionPath() {
2070: TreePath path = new TreePath("unexisted");
2071: tree.setLeadSelectionPath(path);
2072: assertSame(path, tree.getLeadSelectionPath());
2073: assertNull(tree.getSelectionModel().getLeadSelectionPath());
2074: tree.setLeadSelectionPath(null);
2075: assertNull(tree.getLeadSelectionPath());
2076: tree.getSelectionModel().addSelectionPath(path);
2077: assertSame(path, tree.getSelectionModel()
2078: .getLeadSelectionPath());
2079: assertSame(path, tree.getLeadSelectionPath());
2080: }
2081:
2082: public void testGetSetModel() {
2083: DefaultTreeModel oldModel = (DefaultTreeModel) tree.getModel();
2084: assertEquals(2, oldModel.getTreeModelListeners().length);
2085: DefaultTreeModel newModel = new DefaultTreeModel(
2086: new DefaultMutableTreeNode("some root"));
2087: tree.setModel(newModel);
2088: assertTrue(propertyChangeController.isChanged("model"));
2089: assertEquals(0, oldModel.getTreeModelListeners().length);
2090: assertEquals(2, newModel.getTreeModelListeners().length);
2091: tree.setModel(null);
2092: assertNull(tree.getModel());
2093: }
2094:
2095: public void testSetSelectionModel() {
2096: assertTrue(tree.getSelectionModel() instanceof DefaultTreeSelectionModel);
2097: RowMapper rowMapper1 = new RowMapper() {
2098: public int[] getRowsForPaths(TreePath[] path) {
2099: return null;
2100: }
2101: };
2102: RowMapper rowMapper2 = new RowMapper() {
2103: public int[] getRowsForPaths(TreePath[] path) {
2104: return null;
2105: }
2106: };
2107: TreeSelectionModel model1 = new DefaultTreeSelectionModel();
2108: TreeSelectionModel model2 = new DefaultTreeSelectionModel();
2109: RowMapper mapper = tree.getSelectionModel().getRowMapper();
2110: tree.setSelectionModel(model1);
2111: assertTrue(propertyChangeController.isChanged("selectionModel"));
2112: assertEquals(model1, tree.getSelectionModel());
2113: assertSame(mapper, tree.getSelectionModel().getRowMapper());
2114: tree.setSelectionModel(model2);
2115: assertEquals(model2, tree.getSelectionModel());
2116: assertSame(mapper, tree.getSelectionModel().getRowMapper());
2117: propertyChangeController.reset();
2118: TreeSelectionModel oldModel = tree.getSelectionModel();
2119: tree.setSelectionModel(null);
2120: assertNotNull(tree.getSelectionModel());
2121: assertTrue(tree.getSelectionModel() instanceof JTree.EmptySelectionModel);
2122: assertSame(mapper, tree.getSelectionModel().getRowMapper());
2123: assertTrue(propertyChangeController.isChanged("selectionModel"));
2124: propertyChangeController.checkPropertyFired(tree,
2125: "selectionModel", oldModel, tree.getSelectionModel());
2126: TreeSelectionModel emptyModel = tree.getSelectionModel();
2127: tree.setSelectionModel(model2);
2128: tree.setSelectionModel(null);
2129: assertSame(emptyModel, tree.getSelectionModel());
2130: model1.setRowMapper(rowMapper1);
2131: tree.setSelectionModel(model1);
2132: assertSame(mapper, model1.getRowMapper());
2133: model1.setRowMapper(null);
2134: model2.setRowMapper(rowMapper2);
2135: tree.setSelectionModel(model2);
2136: assertSame(mapper, model2.getRowMapper());
2137: assertTrue(mapper instanceof VariableHeightLayoutCache);
2138: }
2139:
2140: public void testGetSetSelectionPath() {
2141: assertNull(tree.getSelectionPath());
2142: TreePath path = new TreePath("non-existed");
2143: tree.setSelectionPath(path);
2144: assertSame(path, tree.getSelectionPath());
2145: assertSame(path, tree.getSelectionModel().getSelectionPath());
2146: assertEquals(1, tree.getSelectionCount());
2147: assertNull(tree.getSelectionRows());
2148: Object root = tree.getModel().getRoot();
2149: Object child = tree.getModel().getChild(root, 0);
2150: path = new TreePath(root).pathByAddingChild(child);
2151: tree.setSelectionPath(path);
2152: assertSame(path, tree.getSelectionPath());
2153: assertEquals(1, tree.getSelectionCount());
2154: assertEquals(1, tree.getSelectionRows().length);
2155: assertEquals(1, tree.getSelectionRows()[0]);
2156: }
2157:
2158: public void testGetSetSelectionRow() {
2159: assertNull(tree.getSelectionRows());
2160: tree.setSelectionRow(2);
2161: assertEquals(1, tree.getSelectionRows().length);
2162: assertEquals(2, tree.getSelectionRows()[0]);
2163: assertEquals(2, tree.getSelectionModel().getSelectionRows()[0]);
2164: tree.setSelectionRow(10000);
2165: assertNull(tree.getSelectionRows());
2166: TreePath path = new TreePath(tree.getModel().getRoot());
2167: tree.getSelectionModel().setSelectionPath(path);
2168: assertEquals(path, tree.getSelectionPath());
2169: assertEquals(1, tree.getSelectionRows().length);
2170: assertEquals(1,
2171: tree.getSelectionModel().getSelectionRows().length);
2172: assertEquals(0, tree.getSelectionRows()[0]);
2173: }
2174:
2175: public void testGetSetSelectionRows() {
2176: assertNull(tree.getSelectionRows());
2177: tree.setSelectionRows(null);
2178: assertNull(tree.getSelectionRows());
2179: tree.setSelectionRows(new int[] { 0, 1 });
2180: assertEquals(2, tree.getSelectionRows().length);
2181: assertEquals(2,
2182: tree.getSelectionModel().getSelectionRows().length);
2183: tree.setSelectionRows(new int[] { 100 });
2184: assertNull(tree.getSelectionRows());
2185: assertNull(tree.getSelectionPaths());
2186: }
2187:
2188: public void testSetUITreeUI() {
2189: assertNotNull(tree.getUI());
2190: TreeUI newUI = new TreeUI() {
2191: @Override
2192: public void cancelEditing(JTree tree) {
2193: }
2194:
2195: @Override
2196: public TreePath getClosestPathForLocation(JTree tree,
2197: int x, int y) {
2198: return null;
2199: }
2200:
2201: @Override
2202: public TreePath getEditingPath(JTree tree) {
2203: return null;
2204: }
2205:
2206: @Override
2207: public Rectangle getPathBounds(JTree tree, TreePath path) {
2208: return null;
2209: }
2210:
2211: @Override
2212: public TreePath getPathForRow(JTree tree, int row) {
2213: return null;
2214: }
2215:
2216: @Override
2217: public int getRowCount(JTree tree) {
2218: return 0;
2219: }
2220:
2221: @Override
2222: public int getRowForPath(JTree tree, TreePath path) {
2223: return 0;
2224: }
2225:
2226: @Override
2227: public boolean isEditing(JTree tree) {
2228: return false;
2229: }
2230:
2231: @Override
2232: public void startEditingAtPath(JTree tree, TreePath path) {
2233: }
2234:
2235: @Override
2236: public boolean stopEditing(JTree path) {
2237: return false;
2238: }
2239: };
2240: tree.setUI(newUI);
2241: assertEquals(newUI, tree.getUI());
2242: }
2243:
2244: public void testGetSetVisibleRowCount() {
2245: assertEquals(20, tree.getVisibleRowCount());
2246: tree.setVisibleRowCount(10);
2247: assertEquals(10, tree.getVisibleRowCount());
2248: assertTrue(propertyChangeController
2249: .isChanged("visibleRowCount"));
2250: tree.setVisibleRowCount(-5);
2251: assertEquals(-5, tree.getVisibleRowCount());
2252: }
2253:
2254: //UI behavior. Should not be deeply tested here
2255: public void testIsEditingStartEditingAtPathGetEditingPath() {
2256: assertFalse(tree.isEditing());
2257: assertFalse(tree.isEditable());
2258: TreePath rootPath = new TreePath(tree.getModel().getRoot());
2259: tree.startEditingAtPath(rootPath);
2260: assertFalse(tree.isEditing());
2261: assertNull(tree.getEditingPath());
2262: tree.setEditable(true);
2263: tree.startEditingAtPath(rootPath);
2264: assertTrue(tree.isEditing());
2265: assertSame(rootPath, tree.getEditingPath());
2266: assertTrue(tree.stopEditing());
2267: assertFalse(tree.isEditing());
2268: assertNull(tree.getEditingPath());
2269: assertFalse(tree.stopEditing());
2270: tree.startEditingAtPath(rootPath);
2271: tree.cancelEditing();
2272: assertFalse(tree.isEditing());
2273: assertNull(tree.getEditingPath());
2274: }
2275:
2276: public void testGetPreferredScrollableViewportSize() {
2277: tree.setPreferredSize(new Dimension(100, 200));
2278: tree.setVisibleRowCount(0);
2279: assertEquals(new Dimension(100, 0), tree
2280: .getPreferredScrollableViewportSize());
2281: tree.setVisibleRowCount(1);
2282: assertEquals(new Dimension(100, 18), tree
2283: .getPreferredScrollableViewportSize());
2284: tree.setCellRenderer(new DefaultTreeCellRenderer() {
2285: @Override
2286: public Component getTreeCellRendererComponent(
2287: final JTree tree, final Object value,
2288: final boolean selected, final boolean expanded,
2289: final boolean leaf, final int row,
2290: final boolean hasFocus) {
2291: JComponent result = (JComponent) super
2292: .getTreeCellRendererComponent(tree, value,
2293: selected, expanded, leaf, row, hasFocus);
2294: result.setPreferredSize(new Dimension(25,
2295: 10 * (row + 1)));
2296: result.setBounds(10, 20, 30, 40);
2297: return result;
2298: }
2299: });
2300: assertEquals(new Dimension(100, 10), tree
2301: .getPreferredScrollableViewportSize());
2302: tree.setModel(null);
2303: assertEquals(new Dimension(100, 16), tree
2304: .getPreferredScrollableViewportSize());
2305: tree.setRowHeight(20);
2306: tree.setVisibleRowCount(1);
2307: assertEquals(new Dimension(100, 20), tree
2308: .getPreferredScrollableViewportSize());
2309: tree.setVisibleRowCount(2);
2310: assertEquals(new Dimension(100, 40), tree
2311: .getPreferredScrollableViewportSize());
2312: }
2313:
2314: public void testGetScrollableBlockIncrement() {
2315: assertEquals(30, tree.getScrollableBlockIncrement(
2316: new Rectangle(10, 20, 30, 40),
2317: SwingConstants.HORIZONTAL, 1));
2318: assertEquals(30, tree.getScrollableBlockIncrement(
2319: new Rectangle(10, 20, 30, 40),
2320: SwingConstants.HORIZONTAL, -1));
2321: assertEquals(40, tree.getScrollableBlockIncrement(
2322: new Rectangle(10, 20, 30, 40), SwingConstants.VERTICAL,
2323: 1));
2324: assertEquals(40, tree.getScrollableBlockIncrement(
2325: new Rectangle(10, 20, 30, 40), SwingConstants.VERTICAL,
2326: -1));
2327: }
2328:
2329: public void testGetScrollableUnitIncrement() {
2330: assertEquals(4, tree.getScrollableUnitIncrement(new Rectangle(
2331: 10, 20, 30, 40), SwingConstants.HORIZONTAL, 1));
2332: assertEquals(4, tree.getScrollableUnitIncrement(new Rectangle(
2333: 100, 200, 300, 400), SwingConstants.HORIZONTAL, -1));
2334: assertEquals(8, tree.getScrollableUnitIncrement(new Rectangle(
2335: 10, 10, 30, 40), SwingConstants.VERTICAL, 1));
2336: assertEquals(10, tree.getScrollableUnitIncrement(new Rectangle(
2337: 10, 10, 30, 40), SwingConstants.VERTICAL, -1));
2338: assertEquals(16, tree.getScrollableUnitIncrement(new Rectangle(
2339: 10, 20, 30, 40), SwingConstants.VERTICAL, 1));
2340: assertEquals(2, tree.getScrollableUnitIncrement(new Rectangle(
2341: 10, 20, 30, 40), SwingConstants.VERTICAL, -1));
2342: assertEquals(-128, tree.getScrollableUnitIncrement(
2343: new Rectangle(10, 200, 30, 40),
2344: SwingConstants.VERTICAL, 1));
2345: assertEquals(146, tree.getScrollableUnitIncrement(
2346: new Rectangle(10, 200, 30, 40),
2347: SwingConstants.VERTICAL, -1));
2348: tree.setModel(null);
2349: assertEquals(0, tree.getScrollableUnitIncrement(new Rectangle(
2350: 10, 200, 30, 40), SwingConstants.VERTICAL, 1));
2351: assertEquals(4, tree.getScrollableUnitIncrement(new Rectangle(
2352: 10, 200, 30, 40), SwingConstants.HORIZONTAL, 1));
2353: }
2354:
2355: public void testGetScrollableTracksViewportHeight() {
2356: assertFalse(tree.getScrollableTracksViewportHeight());
2357: JViewport vp = new JViewport();
2358: vp.setView(tree);
2359: vp.setBounds(10, 10, 1000, tree.getPreferredSize().height);
2360: assertFalse(tree.getScrollableTracksViewportHeight());
2361: vp.setBounds(10, 10, 1000, tree.getPreferredSize().height + 1);
2362: assertTrue(tree.getScrollableTracksViewportHeight());
2363: }
2364:
2365: public void testGetScrollableTracksViewportWidth() {
2366: assertFalse(tree.getScrollableTracksViewportWidth());
2367: JViewport vp = new JViewport();
2368: vp.setView(tree);
2369: vp.setBounds(10, 10, tree.getPreferredSize().width, 1000);
2370: assertFalse(tree.getScrollableTracksViewportWidth());
2371: vp.setBounds(10, 10, tree.getPreferredSize().width + 1, 1000);
2372: assertTrue(tree.getScrollableTracksViewportWidth());
2373: }
2374:
2375: public void testScrollRowPathToVisible() {
2376: JViewport vp = new JViewport();
2377: vp.setView(tree);
2378: vp.setBounds(0, 0, 200, 20);
2379: vp.setViewPosition(new Point(0, 0));
2380: tree.scrollRowToVisible(3);
2381: assertTrue(vp.getViewRect().contains(tree.getRowBounds(3)));
2382: tree.scrollRowToVisible(1);
2383: assertTrue(vp.getViewRect().contains(tree.getRowBounds(1)));
2384: Object root = tree.getModel().getRoot();
2385: Object child0 = tree.getModel().getChild(root, 0);
2386: Object child2 = tree.getModel().getChild(root, 2);
2387: Object child00 = tree.getModel().getChild(child0, 0);
2388: TreePath rootPath = new TreePath(root);
2389: TreePath path0 = rootPath.pathByAddingChild(child0);
2390: TreePath path2 = rootPath.pathByAddingChild(child2);
2391: tree.scrollPathToVisible(path2);
2392: assertTrue(vp.getViewRect().contains(tree.getPathBounds(path2)));
2393: TreePath path00invisible = path0.pathByAddingChild(child00);
2394: assertFalse(tree.isVisible(path00invisible));
2395: tree.scrollPathToVisible(path00invisible);
2396: assertTrue(tree.isVisible(path00invisible));
2397: assertTrue(vp.getViewRect().contains(
2398: tree.getPathBounds(path00invisible)));
2399: tree.scrollPathToVisible(null);
2400: assertTrue(vp.getViewRect().contains(
2401: tree.getPathBounds(path00invisible)));
2402: tree.scrollRowToVisible(-10);
2403: assertTrue(vp.getViewRect().contains(
2404: tree.getPathBounds(path00invisible)));
2405: }
2406:
2407: private void checkInEnumeration(
2408: final Enumeration<? extends Object> e,
2409: final Object[] expected) {
2410: assertNotNull(e);
2411: List<Object> actual = new LinkedList<Object>();
2412: while (e.hasMoreElements()) {
2413: actual.add(e.nextElement());
2414: }
2415: assertEquals(expected.length, actual.size());
2416: for (int i = 0; i < expected.length; i++) {
2417: if (!actual.contains(expected[i])) {
2418: fail("Element " + expected[i]
2419: + " doesn't exist in enumeration");
2420: }
2421: }
2422: }
2423:
2424: private void assertEqualsIgnoreOrder(final Object[] expected,
2425: final Object[] actual) {
2426: if (expected == null && actual == null) {
2427: return;
2428: }
2429: if (expected == null || actual == null) {
2430: fail("arrays are different");
2431: }
2432: assertEquals(expected.length, actual.length);
2433: for (int i = 0; i < expected.length; i++) {
2434: boolean found = false;
2435: for (int j = 0; j < actual.length; j++) {
2436: if (expected[i].equals(actual[j])) {
2437: found = true;
2438: continue;
2439: }
2440: }
2441: assertTrue("actual does not contain " + expected[i]
2442: + " element", found);
2443: }
2444: }
2445:
2446: private Enumeration<TreePath> createTestEnumeration(
2447: final Object[] base) {
2448: return new Enumeration<TreePath>() {
2449: private int index;
2450:
2451: public TreePath nextElement() {
2452: return (TreePath) base[index++];
2453: }
2454:
2455: public boolean hasMoreElements() {
2456: return index < base.length;
2457: }
2458: };
2459: }
2460: }
|