Index: src/org/openide/explorer/ExplorerUtils.java =================================================================== RCS file: /cvs/openide/src/org/openide/explorer/ExplorerUtils.java,v retrieving revision 1.3 diff -u -r1.3 ExplorerUtils.java --- src/org/openide/explorer/ExplorerUtils.java 27 Dec 2003 18:02:04 -0000 1.3 +++ src/org/openide/explorer/ExplorerUtils.java 11 Jan 2004 15:25:14 -0000 @@ -40,16 +40,15 @@ implements ExplorerManager.Provider, Lookup.Provider { private ExplorerManager manager; public YourComponent() { - this (new ExplorerManager(), new ActionMap()); - } - private YourComponent(ExplorerManager em, ActionMap map) { // following line tells the top component which lookup should be associated with it - super (ExplorerUtils.createLookup (em, map)); - this.manager = em; + this.manager = new ExplorerManager(); + ActionMap map = this.getActionMap (); map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager)); map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager)); map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager)); map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false + * + associateLookup (ExplorerUtils.createLookup (manager, map)); } public ExplorerManager getExplorerManager() { return manager; Index: src/org/openide/windows/DelegateActionMap.java =================================================================== RCS file: /cvs/openide/src/org/openide/windows/DelegateActionMap.java,v retrieving revision 1.4 diff -u -r1.4 DelegateActionMap.java --- src/org/openide/windows/DelegateActionMap.java 12 Aug 2003 09:38:16 -0000 1.4 +++ src/org/openide/windows/DelegateActionMap.java 11 Jan 2004 15:25:14 -0000 @@ -29,17 +29,23 @@ */ final class DelegateActionMap extends ActionMap { private JComponent component; + private ActionMap delegate; public DelegateActionMap(JComponent c) { this.component = c; } - + + public DelegateActionMap(TopComponent c, ActionMap delegate) { + this.component = c; + this.delegate = delegate; + } + public int size() { return keys ().length; } public Action get(Object key) { - javax.swing.ActionMap m = component.getActionMap (); + javax.swing.ActionMap m = delegate == null ? component.getActionMap () : delegate; if (m != null) { Action a = m.get (key); if (a != null) { @@ -94,19 +100,31 @@ // Not implemented // public void remove(Object key) { + if (delegate != null) { + delegate.remove (key); + } } public void setParent(ActionMap map) { + if (delegate != null) { + delegate.setParent (map); + } } public void clear() { + if (delegate != null) { + delegate.clear (); + } } public void put(Object key, Action action) { + if (delegate != null) { + delegate.put (key, action); + } } public ActionMap getParent() { - return null; + return delegate == null ? null : delegate.getParent (); } } Index: src/org/openide/windows/TopComponent.java =================================================================== RCS file: /cvs/openide/src/org/openide/windows/TopComponent.java,v retrieving revision 1.115 diff -u -r1.115 TopComponent.java --- src/org/openide/windows/TopComponent.java 8 Jan 2004 12:30:14 -0000 1.115 +++ src/org/openide/windows/TopComponent.java 11 Jan 2004 15:25:14 -0000 @@ -147,10 +147,6 @@ public TopComponent (Lookup lookup) { if (lookup != null) { setLookup (lookup, true); - ActionMap map = (ActionMap)lookup.lookup (ActionMap.class); - if (map != null) { - setActionMap (map); - } } enableEvents (java.awt.AWTEvent.KEY_EVENT_MASK); @@ -194,26 +190,26 @@ /** Initialized ActionMap of this TopComponent. * @since 4.13 */ private void initActionMap() { - javax.swing.ActionMap am = getActionMap(); - if(am != null) { - if(this instanceof TopComponent.Cloneable) { - am.put("cloneWindow", new javax.swing.AbstractAction() { // NOI18N - public void actionPerformed(ActionEvent evt) { - TopComponent cloned = ((TopComponent.Cloneable) - TopComponent.this).cloneComponent(); - cloned.open(); - cloned.requestActive(); - } - }); - } - am.put("closeWindow", new javax.swing.AbstractAction() { // NOI18N - public void actionPerformed(ActionEvent evt) { - TopComponent.this.close(); - } + javax.swing.ActionMap am = new DelegateActionMap (this, new ActionMap ()); + if(this instanceof TopComponent.Cloneable) { + am.put("cloneWindow", new javax.swing.AbstractAction() { // NOI18N + public void actionPerformed(ActionEvent evt) { + TopComponent cloned = ((TopComponent.Cloneable) + TopComponent.this).cloneComponent(); + cloned.open(); + cloned.requestActive(); + } }); } + am.put("closeWindow", new javax.swing.AbstractAction() { // NOI18N + public void actionPerformed(ActionEvent evt) { + TopComponent.this.close(); + } + }); + + setActionMap (am); } - + /** Getter for class that allows obtaining of information about components. * It allows to find out which component is selected, which nodes are * currently or has been activated and list of all components. @@ -851,7 +847,20 @@ * be returned from {@link getLookup} method. * * @param lookup the lookup to associate - * @param sync synchronize return value of getActivatedNodes() with the + * @exception IllegalStateException if there already is a lookup registered + * with this component + * @since JST-PENDING + */ + protected final void associateLookup (Lookup lookup) { + setLookup (lookup, true); + } + + + /** Associates the provided lookup with the component. So it will + * be returned from {@link getLookup} method. + * + * @param lookup the lookup to associate + * @param sync synchronize return value of getActivatedNodes() with the * content of lookup? * @exception IllegalStateException if there already is a lookup registered * with this component Index: test/unit/src/org/openide/windows/TopComponentGetLookupOverridenTest.java =================================================================== RCS file: test/unit/src/org/openide/windows/TopComponentGetLookupOverridenTest.java diff -N test/unit/src/org/openide/windows/TopComponentGetLookupOverridenTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/unit/src/org/openide/windows/TopComponentGetLookupOverridenTest.java 11 Jan 2004 15:25:15 -0000 @@ -0,0 +1,119 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.windows; + +import java.awt.KeyboardFocusManager; +import java.util.ArrayList; +import javax.swing.ActionMap; +import javax.swing.text.DefaultEditorKit; + +import junit.framework.*; + +import org.netbeans.junit.*; +import org.openide.explorer.*; +import org.openide.nodes.AbstractNode; +import org.openide.nodes.Children; +import org.openide.nodes.FilterNode; +import org.openide.nodes.Node; +import org.openide.util.Lookup; +import org.openide.util.lookup.AbstractLookup; +import org.openide.util.lookup.InstanceContent; + + + +/** Tests behaviour of GlobalContextProviderImpl + * and its cooperation with activated and current nodes when TopComponent is + * using its own lookup as in examples of ExplorerUtils... + * + * @author Jaroslav Tulach + */ +public class TopComponentGetLookupOverridenTest extends TopComponentGetLookupTest { + public TopComponentGetLookupOverridenTest (java.lang.String testName) { + super(testName); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static Test suite() { + TestSuite suite = new NbTestSuite(TopComponentGetLookupOverridenTest.class); + + return suite; + } + + /** Setup component with lookup. + */ + protected void setUp () { + top = new ListingYourComponent (); + lookup = top.getLookup (); + } + + private static class ListingYourComponent extends YourComponent + implements java.beans.PropertyChangeListener { + public ListingYourComponent () { + addPropertyChangeListener (this); + getExplorerManager ().setRootContext (new AbstractNode (new Children.Array ())); + } + + public void propertyChange (java.beans.PropertyChangeEvent ev) { + ExplorerManager manager = getExplorerManager (); + + if ("activatedNodes".equals (ev.getPropertyName())) { + try { + Node[] arr = getActivatedNodes (); + Children.Array ch = (Children.Array)manager.getRootContext ().getChildren (); + for (int i = 0; i < arr.length; i++) { + if (arr[i].getParentNode() != manager.getRootContext()) { + assertTrue ("If this fails we are in troubles", ch.add (new Node[] { arr[i] })); + } + } + manager.setSelectedNodes (getActivatedNodes ()); + } catch (java.beans.PropertyVetoException ex) { + ex.printStackTrace(); + fail (ex.getMessage()); + } + } + } + } // end of ListingYourComponent + + // The following class is copied from example in ExplorerUtils: + // + public static class YourComponent extends TopComponent + implements ExplorerManager.Provider, Lookup.Provider { + private ExplorerManager manager; + public YourComponent() { + this.manager = new ExplorerManager (); + ActionMap map = getActionMap (); + map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager)); + map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager)); + map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager)); + map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false + + associateLookup (ExplorerUtils.createLookup (manager, map)); + } + public ExplorerManager getExplorerManager() { + return manager; + } + // It is good idea to switch all listeners on and off when the + // component is shown or hidden. In the case of TopComponent use: + protected void componentActivated() { + ExplorerUtils.activateActions(manager, true); + } + protected void componentDeactivated() { + ExplorerUtils.activateActions(manager, false); + } + } // end of YourComponent +} + Index: test/unit/src/org/openide/windows/TopComponentGetLookupTest.java =================================================================== RCS file: /cvs/openide/test/unit/src/org/openide/windows/TopComponentGetLookupTest.java,v retrieving revision 1.17 diff -u -r1.17 TopComponentGetLookupTest.java --- test/unit/src/org/openide/windows/TopComponentGetLookupTest.java 13 Nov 2003 16:21:40 -0000 1.17 +++ test/unit/src/org/openide/windows/TopComponentGetLookupTest.java 11 Jan 2004 15:25:15 -0000 @@ -63,6 +63,11 @@ lookup = top.getLookup (); } + protected boolean runInEQ () { + return true; + } + + /** Test to find nodes. */ @@ -501,6 +506,7 @@ 1, lookup.lookup(new Lookup.Template(CloseCookie.class)).allInstances().size()); } + /** Listener to count number of changes. */