This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 38475
Collapse All | Expand All

(-)src/org/openide/explorer/ExplorerUtils.java (-5 / +4 lines)
Lines 40-55 Link Here
40
<span class="keyword">implements</span> <span class="type">ExplorerManager.Provider</span>, <span class="type">Lookup.Provider</span> {
40
<span class="keyword">implements</span> <span class="type">ExplorerManager.Provider</span>, <span class="type">Lookup.Provider</span> {
41
    <span class="keyword">private</span> <span class="type">ExplorerManager</span> <span class="variable-name">manager</span>;
41
    <span class="keyword">private</span> <span class="type">ExplorerManager</span> <span class="variable-name">manager</span>;
42
    <span class="keyword">public</span> <span class="type">YourComponent</span>() {
42
    <span class="keyword">public</span> <span class="type">YourComponent</span>() {
43
        <span class="keyword">this</span> (<span class="keyword">new</span> <span class="type">ExplorerManager</span>(), <span class="keyword">new</span> <span class="type">ActionMap</span>());
44
    }
45
    <span class="keyword">private</span> <span class="type">YourComponent</span>(ExplorerManager em, ActionMap map) {
46
        <span class="comment">// following line tells the top component which lookup should be associated with it</span>
43
        <span class="comment">// following line tells the top component which lookup should be associated with it</span>
47
        <span class="keyword">super </span> (ExplorerUtils.createLookup (em, map));
44
        <span class="keyword">this</span>.manager = <span class="keyword">new</span> <span class="type">ExplorerManager</span>();
48
        <span class="keyword">this</span>.manager = em;
45
        <span class="type">ActionMap</span> map = <span class="keyword">this</span>.getActionMap ();
49
        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
46
        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
50
        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
47
        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
51
        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
48
        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
52
        map.put(<span class="string">"delete"</span>, ExplorerUtils.actionDelete(manager, <span class="constant">true</span>)); <span class="comment">// or false
49
        map.put(<span class="string">"delete"</span>, ExplorerUtils.actionDelete(manager, <span class="constant">true</span>)); <span class="comment">// or false
50
 *
51
        associateLookup (ExplorerUtils.createLookup (manager, map));
53
</span>    }
52
</span>    }
54
    <span class="keyword">public</span> <span class="type">ExplorerManager</span> <span class="function-name">getExplorerManager</span>() {
53
    <span class="keyword">public</span> <span class="type">ExplorerManager</span> <span class="function-name">getExplorerManager</span>() {
55
        <span class="keyword">return</span> manager;
54
        <span class="keyword">return</span> manager;
(-)src/org/openide/windows/DelegateActionMap.java (-3 / +21 lines)
Lines 29-45 Link Here
29
 */
29
 */
30
final class DelegateActionMap extends ActionMap {
30
final class DelegateActionMap extends ActionMap {
31
    private JComponent component;
31
    private JComponent component;
32
    private ActionMap delegate;
32
33
33
    public DelegateActionMap(JComponent c) {
34
    public DelegateActionMap(JComponent c) {
34
        this.component = c;
35
        this.component = c;
35
    }
36
    }
36
37
    
38
    public DelegateActionMap(TopComponent c, ActionMap delegate) {
39
        this.component = c;
40
        this.delegate = delegate;
41
    }
42
    
37
    public int size() {
43
    public int size() {
38
        return keys ().length;
44
        return keys ().length;
39
    }
45
    }
40
46
41
    public Action get(Object key) {
47
    public Action get(Object key) {
42
        javax.swing.ActionMap m = component.getActionMap ();
48
        javax.swing.ActionMap m = delegate == null ? component.getActionMap () : delegate;
43
        if (m != null) {
49
        if (m != null) {
44
            Action a = m.get (key);
50
            Action a = m.get (key);
45
            if (a != null) {
51
            if (a != null) {
Lines 94-112 Link Here
94
    // Not implemented
100
    // Not implemented
95
    //
101
    //
96
    public void remove(Object key) {
102
    public void remove(Object key) {
103
        if (delegate != null) {
104
            delegate.remove (key);
105
        }
97
    }        
106
    }        
98
107
99
    public void setParent(ActionMap map) {
108
    public void setParent(ActionMap map) {
109
        if (delegate != null) {
110
            delegate.setParent (map);
111
        }
100
    }
112
    }
101
113
102
    public void clear() {
114
    public void clear() {
115
        if (delegate != null) {
116
            delegate.clear ();
117
        }
103
    }
118
    }
104
119
105
    public void put(Object key, Action action) {
120
    public void put(Object key, Action action) {
121
        if (delegate != null) {
122
            delegate.put (key, action);
123
        }
106
    }
124
    }
107
125
108
    public ActionMap getParent() {
126
    public ActionMap getParent() {
109
        return null;
127
        return delegate == null ? null : delegate.getParent ();
110
    }
128
    }
111
129
112
}    
130
}    
(-)src/org/openide/windows/TopComponent.java (-22 / +31 lines)
Lines 147-156 Link Here
147
    public TopComponent (Lookup lookup) {
147
    public TopComponent (Lookup lookup) {
148
        if (lookup != null) {
148
        if (lookup != null) {
149
            setLookup (lookup, true);
149
            setLookup (lookup, true);
150
            ActionMap map = (ActionMap)lookup.lookup (ActionMap.class);
151
            if (map != null) {
152
                setActionMap (map);
153
            }
154
        }
150
        }
155
        
151
        
156
        enableEvents (java.awt.AWTEvent.KEY_EVENT_MASK);
152
        enableEvents (java.awt.AWTEvent.KEY_EVENT_MASK);
Lines 194-219 Link Here
194
    /** Initialized <code>ActionMap</code> of this <code>TopComponent</code>.
190
    /** Initialized <code>ActionMap</code> of this <code>TopComponent</code>.
195
     * @since 4.13 */
191
     * @since 4.13 */
196
    private void initActionMap() {
192
    private void initActionMap() {
197
        javax.swing.ActionMap am = getActionMap();
193
        javax.swing.ActionMap am = new DelegateActionMap (this, new ActionMap ());
198
        if(am != null) {
194
        if(this instanceof TopComponent.Cloneable) {
199
            if(this instanceof TopComponent.Cloneable) {
195
            am.put("cloneWindow", new javax.swing.AbstractAction() { // NOI18N
200
                am.put("cloneWindow", new javax.swing.AbstractAction() { // NOI18N
196
                public void actionPerformed(ActionEvent evt) {
201
                    public void actionPerformed(ActionEvent evt) {
197
                    TopComponent cloned = ((TopComponent.Cloneable)
202
                        TopComponent cloned = ((TopComponent.Cloneable)
198
                        TopComponent.this).cloneComponent();
203
                            TopComponent.this).cloneComponent();
199
                    cloned.open();
204
                        cloned.open();
200
                    cloned.requestActive();
205
                        cloned.requestActive();
201
                }
206
                    }
207
                });
208
            }
209
            am.put("closeWindow", new javax.swing.AbstractAction() { // NOI18N
210
               public void actionPerformed(ActionEvent evt) {
211
                   TopComponent.this.close();
212
               }
213
            });
202
            });
214
        }
203
        }
204
        am.put("closeWindow", new javax.swing.AbstractAction() { // NOI18N
205
           public void actionPerformed(ActionEvent evt) {
206
               TopComponent.this.close();
207
           }
208
        });
209
        
210
        setActionMap (am);
215
    }
211
    }
216
212
    
217
    /** Getter for class that allows obtaining of information about components.
213
    /** Getter for class that allows obtaining of information about components.
218
    * It allows to find out which component is selected, which nodes are 
214
    * It allows to find out which component is selected, which nodes are 
219
    * currently or has been activated and list of all components.
215
    * currently or has been activated and list of all components.
Lines 851-857 Link Here
851
     * be returned from {@link getLookup} method.
847
     * be returned from {@link getLookup} method.
852
     *
848
     *
853
     * @param lookup the lookup to associate
849
     * @param lookup the lookup to associate
854
     * @param sync synchronize return value of getActivatedNodes() with the 
850
     * @exception IllegalStateException if there already is a lookup registered
851
     *   with this component
852
     * @since JST-PENDING
853
     */
854
    protected final void associateLookup (Lookup lookup) {
855
        setLookup (lookup, true);
856
    }
857
    
858
    
859
    /** Associates the provided lookup with the component. So it will
860
     * be returned from {@link getLookup} method.
861
     *
862
     * @param lookup the lookup to associate
863
     * @param sync synchronize return value of getActivatedNodes() with the
855
     *   content of lookup?
864
     *   content of lookup?
856
     * @exception IllegalStateException if there already is a lookup registered
865
     * @exception IllegalStateException if there already is a lookup registered
857
     *   with this component
866
     *   with this component
(-)test/unit/src/org/openide/windows/TopComponentGetLookupOverridenTest.java (+119 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.windows;
15
16
import java.awt.KeyboardFocusManager;
17
import java.util.ArrayList;
18
import javax.swing.ActionMap;
19
import javax.swing.text.DefaultEditorKit;
20
21
import junit.framework.*;
22
23
import org.netbeans.junit.*;
24
import org.openide.explorer.*;
25
import org.openide.nodes.AbstractNode;
26
import org.openide.nodes.Children;
27
import org.openide.nodes.FilterNode;
28
import org.openide.nodes.Node;
29
import org.openide.util.Lookup;
30
import org.openide.util.lookup.AbstractLookup;
31
import org.openide.util.lookup.InstanceContent;
32
33
34
35
/** Tests behaviour of GlobalContextProviderImpl
36
 * and its cooperation with activated and current nodes when TopComponent is
37
 * using its own lookup as in examples of ExplorerUtils...
38
 *
39
 * @author Jaroslav Tulach
40
 */
41
public class TopComponentGetLookupOverridenTest extends TopComponentGetLookupTest {
42
    public TopComponentGetLookupOverridenTest (java.lang.String testName) {
43
        super(testName);
44
    }
45
    
46
    public static void main(java.lang.String[] args) {
47
        junit.textui.TestRunner.run(suite());
48
    }
49
    
50
    public static Test suite() {
51
        TestSuite suite = new NbTestSuite(TopComponentGetLookupOverridenTest.class);
52
        
53
        return suite;
54
    }
55
    
56
    /** Setup component with lookup.
57
     */
58
    protected void setUp () {
59
        top = new ListingYourComponent ();
60
        lookup = top.getLookup ();
61
    }
62
63
    private static class ListingYourComponent extends YourComponent 
64
    implements java.beans.PropertyChangeListener {
65
        public ListingYourComponent () {
66
            addPropertyChangeListener (this);
67
            getExplorerManager ().setRootContext (new AbstractNode (new Children.Array ()));
68
        }
69
        
70
        public void propertyChange (java.beans.PropertyChangeEvent ev) {
71
            ExplorerManager manager = getExplorerManager ();
72
            
73
            if ("activatedNodes".equals (ev.getPropertyName())) {
74
                try {
75
                    Node[] arr = getActivatedNodes ();
76
                    Children.Array ch = (Children.Array)manager.getRootContext ().getChildren ();
77
                    for (int i = 0; i < arr.length; i++) {
78
                        if (arr[i].getParentNode() != manager.getRootContext()) {
79
                            assertTrue ("If this fails we are in troubles", ch.add (new Node[] { arr[i] }));
80
                        }
81
                    }
82
                    manager.setSelectedNodes (getActivatedNodes ());
83
                } catch (java.beans.PropertyVetoException ex) {
84
                    ex.printStackTrace();
85
                    fail (ex.getMessage());
86
                }
87
            }
88
        }
89
    } // end of ListingYourComponent
90
    
91
    // The following class is copied from example in ExplorerUtils:
92
    //
93
    public static class YourComponent extends TopComponent
94
    implements ExplorerManager.Provider, Lookup.Provider {
95
        private ExplorerManager manager;
96
        public YourComponent() {
97
            this.manager = new ExplorerManager ();
98
            ActionMap map = getActionMap ();
99
            map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
100
            map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
101
            map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
102
            map.put("delete", ExplorerUtils.actionDelete(manager, true)); // or false
103
            
104
            associateLookup (ExplorerUtils.createLookup (manager, map));
105
        }
106
        public ExplorerManager getExplorerManager() {
107
            return manager;
108
        }
109
        // It is good idea to switch all listeners on and off when the
110
        // component is shown or hidden. In the case of TopComponent use:
111
        protected void componentActivated() {
112
            ExplorerUtils.activateActions(manager, true);
113
        }
114
        protected void componentDeactivated() {
115
            ExplorerUtils.activateActions(manager, false);
116
        }
117
    } // end of YourComponent
118
}  
119
    
(-)test/unit/src/org/openide/windows/TopComponentGetLookupTest.java (+6 lines)
Lines 63-68 Link Here
63
        lookup = top.getLookup ();
63
        lookup = top.getLookup ();
64
    }
64
    }
65
    
65
    
66
    protected boolean runInEQ () {
67
        return true;
68
    }
69
    
70
    
66
    
71
    
67
    /** Test to find nodes.
72
    /** Test to find nodes.
68
     */
73
     */
Lines 501-506 Link Here
501
            1,
506
            1,
502
            lookup.lookup(new Lookup.Template(CloseCookie.class)).allInstances().size());
507
            lookup.lookup(new Lookup.Template(CloseCookie.class)).allInstances().size());
503
    }
508
    }
509
    
504
    
510
    
505
    /** Listener to count number of changes.
511
    /** Listener to count number of changes.
506
     */
512
     */

Return to bug 38475