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 133009
Collapse All | Expand All

(-)a/o.n.core/src/org/netbeans/core/ShortcutsFolder.java (-5 / +67 lines)
Lines 41-52 Link Here
41
41
42
package org.netbeans.core;
42
package org.netbeans.core;
43
43
44
import java.awt.EventQueue;
45
import java.awt.event.ActionEvent;
46
import java.beans.PropertyChangeEvent;
47
import java.beans.PropertyChangeListener;
44
import java.io.IOException;
48
import java.io.IOException;
45
import java.util.Collection;
49
import java.util.Collection;
46
import java.util.Enumeration;
50
import java.util.Enumeration;
47
import java.util.LinkedList;
51
import java.util.LinkedList;
48
import java.util.logging.Level;
52
import java.util.logging.Level;
49
import java.util.logging.Logger;
53
import java.util.logging.Logger;
54
import javax.swing.AbstractAction;
50
import javax.swing.Action;
55
import javax.swing.Action;
51
import javax.swing.KeyStroke;
56
import javax.swing.KeyStroke;
52
import javax.swing.text.Keymap;
57
import javax.swing.text.Keymap;
Lines 162-168 Link Here
162
            InstanceCookie ic = dataObject.getCookie(InstanceCookie.class);
167
            InstanceCookie ic = dataObject.getCookie(InstanceCookie.class);
163
            if (ic == null) continue;
168
            if (ic == null) continue;
164
            try {
169
            try {
165
                Action action = (Action) ic.instanceCreate ();
170
                Action action = new LazyAction(ic);
166
                String shortcuts = dataObject.getName ();
171
                String shortcuts = dataObject.getName ();
167
                debug.fine("  " + shortcuts + " : " + action);
172
                debug.fine("  " + shortcuts + " : " + action);
168
                KeyStroke[] keyStrokes = Utilities.stringToKeys (shortcuts);
173
                KeyStroke[] keyStrokes = Utilities.stringToKeys (shortcuts);
Lines 174-183 Link Here
174
                //remember DataObjects used to create the Actions so that there are
179
                //remember DataObjects used to create the Actions so that there are
175
                //the same Action instances in the menu
180
                //the same Action instances in the menu
176
                dataObjects.add( dataObject );
181
                dataObjects.add( dataObject );
177
            } catch (ClassNotFoundException x) {
178
                Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING,
179
                        "{0} ignored; cannot load class {1}",
180
                        new Object[] {dataObject.getPrimaryFile().getPath(), ic.instanceName()});
181
            } catch (Exception ex) {
182
            } catch (Exception ex) {
182
                Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING, null, ex);
183
                Logger.getLogger(ShortcutsFolder.class.getName()).log(Level.WARNING, null, ex);
183
            }
184
            }
Lines 203-208 Link Here
203
        }
204
        }
204
        currentKeymap.addActionForKeyStroke (keyStrokes [k], action);
205
        currentKeymap.addActionForKeyStroke (keyStrokes [k], action);
205
    }
206
    }
207
208
    private static final class LazyAction extends AbstractAction 
209
    implements PropertyChangeListener {
210
        private InstanceCookie ic;
211
        private Action action;
212
        private Object acceleratorKey;
213
        
214
        LazyAction(InstanceCookie ic) {
215
            this.ic = ic;
216
        }
217
218
        private Action action() {
219
            assert EventQueue.isDispatchThread();
220
221
            if (action != null) {
222
                return action;
223
            }
224
            try {
225
                action = (Action) ic.instanceCreate();
226
                if (acceleratorKey != null) {
227
                    action.putValue(ACCELERATOR_KEY, acceleratorKey);
228
                }
229
                action.addPropertyChangeListener(this);
230
            } catch (Exception ex) {
231
                Exceptions.printStackTrace(ex);
232
            }
233
            return action;
234
        }
235
236
        @Override
237
        public Object getValue(String key) {
238
            return action().getValue(key);
239
        }
240
241
        @Override
242
        public void putValue(String key, Object value) {
243
            if (ACCELERATOR_KEY.equals(key) && action == null) {
244
                acceleratorKey = value;
245
                return;
246
            }
247
            action().putValue(key, value);
248
        }
249
250
        @Override
251
        public void setEnabled(boolean b) {
252
        }
253
254
        @Override
255
        public boolean isEnabled() {
256
            return action().isEnabled();
257
        }
258
259
        public void actionPerformed(ActionEvent e) {
260
            action().actionPerformed(e);
261
        }
262
263
        public void propertyChange(PropertyChangeEvent evt) {
264
            firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
265
        }
266
    }
267
206
    
268
    
207
    private class Listener extends FileChangeAdapter implements Runnable {
269
    private class Listener extends FileChangeAdapter implements Runnable {
208
        
270
        
(-)a/o.n.core/test/unit/src/org/netbeans/core/ShortcutsFolderTest.java (-7 / +43 lines)
Lines 40-48 Link Here
40
 */
40
 */
41
package org.netbeans.core;
41
package org.netbeans.core;
42
42
43
import java.awt.EventQueue;
43
import java.awt.Toolkit;
44
import java.awt.Toolkit;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.ActionEvent;
45
import java.awt.event.KeyEvent;
46
import java.awt.event.KeyEvent;
47
import java.beans.PropertyChangeEvent;
48
import java.beans.PropertyChangeListener;
46
import java.io.IOException;
49
import java.io.IOException;
47
import java.lang.ref.Reference;
50
import java.lang.ref.Reference;
48
import java.lang.ref.WeakReference;
51
import java.lang.ref.WeakReference;
Lines 95-103 Link Here
95
    public void testApplyChangeToFactoryActionIssue49597 () throws Exception {
98
    public void testApplyChangeToFactoryActionIssue49597 () throws Exception {
96
        final FileSystem fs = Repository.getDefault ().getDefaultFileSystem ();
99
        final FileSystem fs = Repository.getDefault ().getDefaultFileSystem ();
97
        final FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts");
100
        final FileObject shortcuts = fs.getRoot ().getFileObject ("Shortcuts");
98
        FileObject inst = FileUtil.createData (fs.getRoot (), "/Actions/Tools/TestAction.instance");
101
        FileObject inst = FileUtil.createData (fs.getRoot (), "/Actions/Tools/org-netbeans-core-ShortcutsFolderTest$TestAction.instance");
99
        TestAction action = new TestAction ();
100
        inst.setAttribute ("instanceCreate", action);
101
        
102
        
102
        Reference<?> ref = new WeakReference<Object>(inst);
103
        Reference<?> ref = new WeakReference<Object>(inst);
103
        inst = null;
104
        inst = null;
Lines 114-120 Link Here
114
            
115
            
115
            public void run() throws IOException {
116
            public void run() throws IOException {
116
                inst2 = FileUtil.createData (fs.getRoot (), "/Shortcuts/CA-F9.shadow");
117
                inst2 = FileUtil.createData (fs.getRoot (), "/Shortcuts/CA-F9.shadow");
117
                inst2.setAttribute ("originalFile", "/Actions/Tools/TestAction.instance");
118
                inst2.setAttribute ("originalFile", "/Actions/Tools/org-netbeans-core-ShortcutsFolderTest$TestAction.instance");
118
            }
119
            }
119
        }
120
        }
120
        R run = new R();
121
        R run = new R();
Lines 125-140 Link Here
125
126
126
        FileObject[] arr = shortcuts.getChildren ();
127
        FileObject[] arr = shortcuts.getChildren ();
127
        err.log("children are here");
128
        err.log("children are here");
129
        assertEquals("No test action created yet", 0, TestAction.cnt);
128
        
130
        
129
        assertEquals ("One element is there", 1, arr.length);
131
        assertEquals ("One element is there", 1, arr.length);
130
        org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (arr[0]);
132
        org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (arr[0]);
131
        err.log("Object is here" + obj);
133
        err.log("Object is here" + obj);
134
        assertEquals("No test action created yet", 0, TestAction.cnt);
132
        
135
        
133
        assertEquals ("It is DataShadow", org.openide.loaders.DataShadow.class, obj.getClass ());
136
        assertEquals ("It is DataShadow", org.openide.loaders.DataShadow.class, obj.getClass ());
137
        assertEquals("No test action created yet", 0, TestAction.cnt);
134
138
135
        Object a = keymap.getAction (stroke);
139
        final Object a = keymap.getAction (stroke);
136
        assertNotNull ("There is an action", a);
140
        assertNotNull ("There is an action", a);
137
        assertEquals ("It is test action", TestAction.class, a.getClass ());
141
        assertEquals("No test action created yet", 0, TestAction.cnt);
142
        assertTrue("It is action", a instanceof Action);
143
144
        class RR implements Runnable, PropertyChangeListener {
145
            int change;
146
147
            public void run() {
148
                assertTrue("Enabled", ((Action)a).isEnabled());
149
                ((Action)a).actionPerformed(new ActionEvent(this, 0, ""));
150
                assertFalse("Disabled", ((Action)a).isEnabled());
151
                assertEquals("One change", 1, change);
152
            }
153
154
            public void propertyChange(PropertyChangeEvent ev) {
155
                change++;
156
            }
157
        }
158
        RR call = new RR();
159
        ((Action)a).addPropertyChangeListener(call);
160
        EventQueue.invokeAndWait(call);
161
162
        assertEquals("Now the action is created", 1, TestAction.cnt);
163
        assertEquals("Now the action is called", 1, TestAction.invoke);
138
    }
164
    }
139
165
140
    @RandomlyFails
166
    @RandomlyFails
Lines 174-180 Link Here
174
    }
200
    }
175
    
201
    
176
    public static class TestAction extends AbstractAction {
202
    public static class TestAction extends AbstractAction {
177
        public void actionPerformed (ActionEvent ae) {}
203
        static int cnt;
204
        static int invoke;
205
        
206
        public TestAction() {
207
            cnt++;
208
        }
209
210
        public void actionPerformed (ActionEvent ae) {
211
            invoke++;
212
            setEnabled(!isEnabled());
213
        }
178
    }
214
    }
179
    
215
    
180
    public static class ENV extends Object implements org.openide.loaders.Environment.Provider {
216
    public static class ENV extends Object implements org.openide.loaders.Environment.Provider {

Return to bug 133009