001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.openide.explorer.view;
043:
044: import java.awt.BorderLayout;
045: import java.beans.PropertyVetoException;
046: import java.lang.ref.WeakReference;
047: import java.lang.reflect.InvocationTargetException;
048: import javax.swing.JFrame;
049: import javax.swing.JPanel;
050: import javax.swing.JTree;
051: import javax.swing.SwingUtilities;
052: import javax.swing.tree.TreePath;
053: import org.netbeans.junit.NbTestCase;
054: import org.openide.explorer.ExplorerManager;
055: import org.openide.nodes.AbstractNode;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Node;
058:
059: /**
060: * Tests for class BeanTreeViewTest
061: */
062: public class BeanTreeViewTest extends NbTestCase {
063:
064: private static final int NO_OF_NODES = 3;
065:
066: public BeanTreeViewTest(String name) {
067: super (name);
068: }
069:
070: public void testFirstChildRemovalCausesSelectionOfSibling()
071: throws Throwable {
072: doChildRemovalTest("foo");
073: }
074:
075: public void testSecondChildRemovalCausesSelectionOfSibling()
076: throws Throwable {
077: doChildRemovalTest("bar");
078: }
079:
080: public void testThirdChildRemovalCausesSelectionOfSibling()
081: throws Throwable {
082: doChildRemovalTest("bla");
083: }
084:
085: private static Object holder;
086:
087: private void doChildRemovalTest(final String name) throws Throwable {
088:
089: class AWTTst implements Runnable {
090:
091: AbstractNode root = new AbstractNode(new Children.Array());
092: Node[] children = { createLeaf("foo"), createLeaf("bar"),
093: createLeaf("bla") };
094: Panel p = new Panel();
095: BeanTreeView btv = new BeanTreeView();
096: JFrame f = new JFrame();
097: JTree tree = btv.tree;
098: Node operateOn;
099:
100: {
101: root.setName("test root");
102: root.getChildren().add(children);
103: p.getExplorerManager().setRootContext(root);
104: p.add(BorderLayout.CENTER, btv);
105: f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
106: f.getContentPane().add(BorderLayout.CENTER, p);
107: f.setVisible(true);
108: }
109:
110: public void run() {
111:
112: for (int i = 0;; i++) {
113: if (name.equals(children[i].getName())) {
114: // this should select a sibling of the removed node
115: operateOn = children[i];
116: break;
117: }
118: }
119:
120: try {
121: p.getExplorerManager().setSelectedNodes(
122: new Node[] { operateOn });
123: } catch (PropertyVetoException e) {
124: fail("Unexpected PropertyVetoException from ExplorerManager.setSelectedNodes()");
125: }
126:
127: TreePath[] paths = tree.getSelectionPaths();
128: assertNotNull(
129: "Before removal: one node should be selected, but there are none.",
130: paths);
131: assertEquals(
132: "Before removal: one node should be selected, but there are none.",
133: 1, paths.length);
134: assertEquals(
135: "Before removal: one node should be selected, but there are none.",
136: operateOn, Visualizer.findNode(paths[0]
137: .getLastPathComponent()));
138: assertEquals(
139: "Before removal: one node should be selected, but there are none.",
140: operateOn, Visualizer.findNode(tree
141: .getAnchorSelectionPath()
142: .getLastPathComponent()));
143:
144: // this should select a sibling of the removed node
145: root.getChildren().remove(new Node[] { operateOn });
146: assertNotNull(
147: "After removal: one node should be selected, but there are none.",
148: tree.getSelectionPath());
149: children = null;
150: }
151:
152: public void tryGc() {
153: WeakReference<Node> wref = new WeakReference<Node>(
154: operateOn);
155: operateOn = null;
156: assertGC("Node should be released.", wref);
157: }
158: }
159: AWTTst awt = new AWTTst();
160: holder = awt;
161: try {
162: SwingUtilities.invokeAndWait(awt);
163: } catch (InvocationTargetException ex) {
164: throw ex.getTargetException();
165: }
166: awt.tryGc();
167: }
168:
169: private static Node createLeaf(String name) {
170: AbstractNode n = new AbstractNode(Children.LEAF);
171: n.setName(name);
172: return n;
173: }
174:
175: private static class Panel extends JPanel implements
176: ExplorerManager.Provider {
177: private ExplorerManager em = new ExplorerManager();
178:
179: public ExplorerManager getExplorerManager() {
180: return em;
181: }
182: }
183: }
|