KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > ExplorerActionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer;
21
22 import java.util.logging.Level JavaDoc;
23 import org.netbeans.junit.*;
24 import junit.textui.TestRunner;
25 import org.openide.explorer.*;
26 import org.openide.nodes.Node;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.AbstractNode;
29 import java.util.Collections JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.awt.BorderLayout JavaDoc;
32 import org.openide.explorer.view.BeanTreeView;
33 import javax.swing.JLabel JavaDoc;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.io.NbMarshalledObject;
36
37 /** Testing behaviour of ExplorerActions in order to fix 33566
38  */

39 public class ExplorerActionsTest extends NbTestCase {
40     static {
41         // initialize special TopComponent.Registry
42
Object JavaDoc x = ActionsInfraHid.UT;
43     }
44     
45     private static javax.swing.Action JavaDoc delete = org.openide.util.actions.SystemAction.get (
46         org.openide.actions.DeleteAction.class
47     );
48     
49     public ExplorerActionsTest (String JavaDoc name) {
50         super(name);
51     }
52     
53     protected Level JavaDoc logLevel() {
54         return Level.FINER;
55     }
56
57     protected boolean runInEQ() {
58         return true;
59     }
60
61     public void testGlobalStateInExplorerActionsIsImportant () throws Exception JavaDoc {
62         EP panel = new EP (null);
63         ExplorerPanel.setConfirmDelete(false);
64         
65         doDelete (panel);
66     }
67     
68     public void testGlobalStateCanBeOverriden () throws Exception JavaDoc {
69         ExplorerActions actions = new ExplorerActions ();
70         actions.setConfirmDelete (false);
71         
72         ExplorerPanel.setConfirmDelete(true);
73         EP panel = new EP (actions);
74
75         doDelete (panel);
76     }
77     
78     public void testGlobalStateOnDeserializedPanel () throws Exception JavaDoc {
79         EP panel = new EP (null);
80         ExplorerPanel.setConfirmDelete(false);
81         setupExplorerManager (panel.getExplorerManager());
82         
83         NbMarshalledObject mar = new NbMarshalledObject (panel);
84         Object JavaDoc obj = mar.get ();
85         EP deserializedPanel = (EP) obj;
86         
87         // activate the actions
88
ActionsInfraHid.UT.setActivated (deserializedPanel);
89         deserializedPanel.componentActivated();
90         
91         ActionsInfraHid.UT.setCurrentNodes (deserializedPanel.getExplorerManager().getRootContext ().getChildren ().getNodes ());
92         
93         // deletes without asking a question, if the question appears something
94
// is wrong
95
delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, ""));
96     }
97     
98     /** Performs a delete */
99     
100     private void doDelete (EP panel) throws Exception JavaDoc {
101         setupExplorerManager (panel.getExplorerManager());
102         // activate the actions
103
ActionsInfraHid.UT.setActivated (panel);
104         panel.componentActivated();
105         
106         ActionsInfraHid.UT.setCurrentNodes (panel.getExplorerManager().getSelectedNodes());
107         assertTrue ("Delete is allowed", delete.isEnabled());
108         
109         // deletes without asking a question, if the question appears something
110
// is wrong
111
delete.actionPerformed(new java.awt.event.ActionEvent JavaDoc (this, 0, ""));
112     }
113     
114     private static class RootNode extends AbstractNode {
115         public RootNode () {
116             super (new Children.Array ());
117         }
118         public Node.Handle getHandle () {
119             return new H ();
120         }
121         private static class H implements Node.Handle {
122             H() {}
123             static final long serialVersionUID = -5158460093499159177L;
124             public Node getNode () throws java.io.IOException JavaDoc {
125                 Node n = new RootNode ();
126                 n.getChildren().add (new Node[] {
127                     new Del ("H1"), new Del ("H2")
128                 });
129                 return n;
130             }
131         }
132     }
133
134     private static class Del extends AbstractNode {
135         public Del (String JavaDoc name) {
136             super (Children.LEAF);
137             setName (name);
138         }
139         public boolean canDestroy () {
140             return true;
141         }
142     }
143     
144     /** Setups an explorer manager to be ready to delete something.
145      * @param em manager
146      */

147     private static void setupExplorerManager (ExplorerManager em) throws Exception JavaDoc {
148         AbstractNode root = new RootNode ();
149         Node[] arr = new Node[] {
150             new Del ("1"), new Del ("2")
151         };
152         root.getChildren().add (arr);
153         
154         em.setRootContext(root);
155         em.setSelectedNodes(root.getChildren().getNodes());
156         
157         assertEquals (
158             "Same nodes selected",
159             Arrays.asList (arr),
160             Arrays.asList (root.getChildren ().getNodes ())
161         );
162     }
163     
164     /** Special ExplorerPanel that has method how to actiavate itself.
165      */

166     private static class EP extends ExplorerPanel {
167         private ExplorerActions actions;
168         Node rootNode = null;
169         
170         public EP () {
171         }
172
173         public EP (ExplorerActions actions) {
174             this.actions = actions;
175         }
176         
177         public void componentActivated () {
178             super.componentActivated ();
179             if (actions != null) {
180                 actions.attach(getExplorerManager ());
181             }
182         }
183         
184     }
185 }
186
Popular Tags