KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > CookieAction84636Test


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.util.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.cookies.OpenCookie;
30 import org.openide.cookies.SaveCookie;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.CookieSet;
34 import org.openide.nodes.FilterNode;
35 import org.openide.nodes.Node;
36 import org.openide.nodes.NodeEvent;
37 import org.openide.nodes.NodeListener;
38 import org.openide.nodes.NodeMemberEvent;
39 import org.openide.nodes.NodeReorderEvent;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.lookup.AbstractLookup;
42 import org.openide.util.lookup.ProxyLookup;
43
44 /** Check problem with recursive loop.
45  * @author Jaroslav Tulach
46  */

47 public class CookieAction84636Test extends NbTestCase
48 implements NodeListener, PropertyChangeListener JavaDoc {
49     
50     public CookieAction84636Test(String JavaDoc name) {
51         super(name);
52     }
53     
54     private SimpleCookieAction a1;
55     private CookieNode n1;
56     
57     protected void setUp() throws Exception JavaDoc {
58         a1 = SystemAction.get(SimpleCookieAction.class);
59         n1 = new CookieNode();
60         n1.setName("n1");
61     }
62     
63     protected boolean runInEQ() {
64         return true;
65     }
66     
67     public void testBasicUsage() throws Exception JavaDoc {
68         a1.addPropertyChangeListener(this);
69         
70         FilterNode f1 = new FilterNode(n1);
71
72         f1.addPropertyChangeListener(this);
73         f1.addNodeListener(this);
74         Action JavaDoc context = a1.createContextAwareInstance(f1.getLookup());
75         
76         
77     }
78
79     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
80     }
81
82     public void childrenAdded(NodeMemberEvent ev) {
83     }
84
85     public void childrenRemoved(NodeMemberEvent ev) {
86     }
87
88     public void childrenReordered(NodeReorderEvent ev) {
89     }
90
91     public void nodeDestroyed(NodeEvent ev) {
92     }
93
94     
95     public static class SimpleCookieAction extends CookieAction {
96         protected int mode() {
97             return MODE_EXACTLY_ONE;
98         }
99         protected Class JavaDoc[] cookieClasses() {
100             return new Class JavaDoc[] {OpenCookie.class};
101         }
102         public static final List JavaDoc runOn = new ArrayList JavaDoc(); // List<List<Node>>
103
protected void performAction(Node[] activatedNodes) {
104             runOn.add(Arrays.asList(activatedNodes));
105         }
106         public String JavaDoc getName() {
107             return "SimpleCookieAction";
108         }
109         public HelpCtx getHelpCtx() {
110             return null;
111         }
112         protected boolean asynchronous() {
113             return false;
114         }
115
116         private boolean called = false;
117         protected boolean enable(Node[] activatedNodes) {
118             boolean retValue;
119             if (called) {
120                 fail("Called already!");
121             }
122             called =true;
123             
124             retValue = super.enable(activatedNodes);
125             return retValue;
126         }
127     }
128     
129     private static final class CookieNode extends AbstractNode {
130         private static final class Open implements OpenCookie {
131             public void open() {
132                 // do nothing
133
}
134         }
135         public CookieNode() {
136             super(Children.LEAF);
137             getCookieSet().add(new Open());
138         }
139         public void setHasCookie(boolean b) {
140             if (b && getCookie(OpenCookie.class) == null) {
141                 getCookieSet().add(new Open());
142             } else if (!b) {
143                 OpenCookie o = getCookie(OpenCookie.class);
144                 if (o != null) {
145                     getCookieSet().remove(o);
146                 }
147             }
148         }
149     }
150     
151 }
152
153
Popular Tags