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.windows;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.KeyEvent;
046: import java.security.KeyStore;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import javax.swing.AbstractAction;
052: import javax.swing.Action;
053: import javax.swing.ActionMap;
054: import javax.swing.JComponent;
055: import javax.swing.KeyStroke;
056: import javax.swing.text.Keymap;
057: import org.netbeans.junit.MockServices;
058: import org.netbeans.junit.NbTestCase;
059: import org.openide.nodes.Node;
060: import org.openide.util.ContextAwareAction;
061: import org.openide.util.Lookup;
062: import org.openide.util.LookupEvent;
063: import org.openide.util.lookup.AbstractLookup;
064: import org.openide.util.lookup.InstanceContent;
065:
066: /** Tests behaviour of GlobalContextProviderImpl
067: * and its cooperation with activated and current nodes.
068: *
069: * @author Jaroslav Tulach
070: */
071: public class ContextAwareActionInTopComponentTest extends NbTestCase {
072:
073: private TopComponent tc;
074: private MyContextAwareAction myGlobalAction = new MyContextAwareAction();
075: private KeyStroke KEY_STROKE = KeyStroke.getKeyStroke(
076: KeyEvent.VK_W, KeyEvent.ALT_DOWN_MASK
077: + KeyEvent.CTRL_DOWN_MASK
078: + KeyEvent.SHIFT_DOWN_MASK);
079:
080: public ContextAwareActionInTopComponentTest(
081: java.lang.String testName) {
082: super (testName);
083: }
084:
085: protected void setUp() throws Exception {
086: tc = new TopComponent();
087: tc.requestActive();
088:
089: MockServices.setServices(MyKeymap.class);
090: Keymap km = Lookup.getDefault().lookup(Keymap.class);
091: km.addActionForKeyStroke(KEY_STROKE, myGlobalAction);
092: }
093:
094: public void testGlobalActionDisabled() throws Exception {
095: myGlobalAction.setEnabled(false);
096:
097: final org.openide.nodes.Node n = new org.openide.nodes.AbstractNode(
098: org.openide.nodes.Children.LEAF);
099: tc.setActivatedNodes(new Node[] { n });
100:
101: KeyEvent e = new KeyEvent(tc, KeyEvent.KEY_TYPED, 0, 0, 0);
102: assertTrue(tc.processKeyBinding(KEY_STROKE, e,
103: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, true));
104: assertTrue(myGlobalAction.actionWasPerformed);
105: }
106:
107: public void testGlobalActionSurvivedFocusChange() throws Exception {
108: myGlobalAction.setEnabled(true);
109:
110: final org.openide.nodes.Node n = new org.openide.nodes.AbstractNode(
111: org.openide.nodes.Children.LEAF);
112: tc.setActivatedNodes(null);
113:
114: KeyEvent e = new KeyEvent(tc, KeyEvent.KEY_TYPED, 0, 0, 0);
115: assertTrue(tc.processKeyBinding(KEY_STROKE, e,
116: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, true));
117: assertTrue(myGlobalAction.actionWasPerformed);
118: }
119:
120: public void testGlobalActionDoesNotSurviveFocusChange()
121: throws Exception {
122: myGlobalAction.setEnabled(true);
123:
124: final org.openide.nodes.Node n = new org.openide.nodes.AbstractNode(
125: org.openide.nodes.Children.LEAF);
126: tc.setActivatedNodes(new Node[0]);
127:
128: KeyEvent e = new KeyEvent(tc, KeyEvent.KEY_TYPED, 0, 0, 0);
129: assertTrue(tc.processKeyBinding(KEY_STROKE, e,
130: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, true));
131: assertFalse(myGlobalAction.actionWasPerformed);
132: }
133:
134: /**
135: * Context-aware action that is enabled only if there are any activated nodes.
136: *
137: */
138: private static class MyContextAwareAction extends AbstractAction
139: implements ContextAwareAction {
140:
141: private static boolean actionWasPerformed = false;
142:
143: public MyContextAwareAction() {
144: actionWasPerformed = false;
145: }
146:
147: public void actionPerformed(ActionEvent arg0) {
148: actionWasPerformed = true;
149: }
150:
151: public Action createContextAwareInstance(Lookup actionContext) {
152: MyContextAwareAction action = new MyContextAwareAction();
153: action.setEnabled(null != actionContext.lookup(Node.class));
154: return action;
155: }
156: }
157:
158: public static class MyKeymap implements Keymap {
159:
160: private Map<KeyStroke, Action> ks2a = new HashMap<KeyStroke, Action>();
161:
162: public String getName() {
163: throw new UnsupportedOperationException(
164: "Not supported yet.");
165: }
166:
167: public Action getDefaultAction() {
168: throw new UnsupportedOperationException(
169: "Not supported yet.");
170: }
171:
172: public void setDefaultAction(Action arg0) {
173: throw new UnsupportedOperationException(
174: "Not supported yet.");
175: }
176:
177: public Action getAction(KeyStroke arg0) {
178: return ks2a.get(arg0);
179: }
180:
181: public KeyStroke[] getBoundKeyStrokes() {
182: throw new UnsupportedOperationException(
183: "Not supported yet.");
184: }
185:
186: public Action[] getBoundActions() {
187: throw new UnsupportedOperationException(
188: "Not supported yet.");
189: }
190:
191: public KeyStroke[] getKeyStrokesForAction(Action arg0) {
192: throw new UnsupportedOperationException(
193: "Not supported yet.");
194: }
195:
196: public boolean isLocallyDefined(KeyStroke arg0) {
197: throw new UnsupportedOperationException(
198: "Not supported yet.");
199: }
200:
201: public void addActionForKeyStroke(KeyStroke arg0, Action arg1) {
202: ks2a.put(arg0, arg1);
203: }
204:
205: public void removeKeyStrokeBinding(KeyStroke arg0) {
206: throw new UnsupportedOperationException(
207: "Not supported yet.");
208: }
209:
210: public void removeBindings() {
211: throw new UnsupportedOperationException(
212: "Not supported yet.");
213: }
214:
215: public Keymap getResolveParent() {
216: throw new UnsupportedOperationException(
217: "Not supported yet.");
218: }
219:
220: public void setResolveParent(Keymap arg0) {
221: throw new UnsupportedOperationException(
222: "Not supported yet.");
223: }
224: }
225: }
|