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

(-)openide/src/org/openide/actions/DeleteAction.java (-1 / +1 lines)
Lines 46-52 Link Here
46
    }
46
    }
47
    
47
    
48
    protected boolean asynchronous() {
48
    protected boolean asynchronous() {
49
        return false;
49
        return true;
50
    }
50
    }
51
    
51
    
52
}
52
}
(-)openide/src/org/openide/actions/PasteAction.java (-39 / +70 lines)
Lines 139-145 Link Here
139
        }
139
        }
140
140
141
        if(t != null) {
141
        if(t != null) {
142
            executePasteType (t);
142
            // posts the action in RP thread
143
            new ActionPT (t, ev.getActionCommand ());
143
        } else {
144
        } else {
144
            ErrorManager.getDefault().notify(
145
            ErrorManager.getDefault().notify(
145
                ErrorManager.INFORMATIONAL,
146
                ErrorManager.INFORMATIONAL,
Lines 147-189 Link Here
147
                    "No paste types available when performing paste action")); // NOI18N
148
                    "No paste types available when performing paste action")); // NOI18N
148
        }
149
        }
149
    }
150
    }
150
    
151
151
    protected boolean asynchronous() {
152
    protected boolean asynchronous() {
152
        return false;
153
        return false;
153
    }
154
    }
154
155
155
    /** Does the execution of a paste type with all handling around
156
     */
157
    private static void executePasteType (PasteType t) {
158
        NodeSelector sel = null;
159
        try {
160
            ExplorerManager em = findExplorerManager ();
161
            if (em != null) {
162
                sel = new NodeSelector (em, null);
163
            }
164
            
165
            Transferable trans = t.paste();
166
            Clipboard clipboard = getClipboard();
167
168
169
            if (trans != null) {
170
                ClipboardOwner owner = trans instanceof ClipboardOwner ?
171
                                       (ClipboardOwner)trans
172
                                       :
173
                                       new StringSelection (""); // NOI18N
174
                clipboard.setContents(trans, owner);
175
            }
176
        } catch (UserCancelException exc) {
177
            // ignore - user just pressed cancel in some dialog....
178
        } catch (java.io.IOException e) {
179
            ErrorManager.getDefault().notify(e);
180
        } finally {
181
            if (sel != null) {
182
                sel.select ();
183
            }
184
        }
185
    }
186
187
    /** Set possible paste types.
156
    /** Set possible paste types.
188
    * Automatically enables or disables the paste action according to whether there are any.
157
    * Automatically enables or disables the paste action according to whether there are any.
189
    * @param types the new types to allow, or <code>null</code>
158
    * @param types the new types to allow, or <code>null</code>
Lines 392-397 Link Here
392
        }
361
        }
393
362
394
        public void performActionAt(int index) {
363
        public void performActionAt(int index) {
364
            performActionAt (index, null);
365
        }
366
        
367
        public void performActionAt(int index, ActionEvent ev) {
395
            Action[] action = new Action[1];
368
            Action[] action = new Action[1];
396
            
369
            
397
            Object[] arr = getPasteTypesOrActions (action);
370
            Object[] arr = getPasteTypesOrActions (action);
Lines 401-407 Link Here
401
374
402
            if (arr[index] instanceof PasteType) {
375
            if (arr[index] instanceof PasteType) {
403
                PasteType t = (PasteType)arr[index];
376
                PasteType t = (PasteType)arr[index];
404
                new ActionPT(t).actionPerformed(new ActionEvent(t, ActionEvent.ACTION_PERFORMED, javax.swing.Action.NAME));
377
                // posts the action is RP thread
378
                new ActionPT(t, ev == null ? null : ev.getActionCommand ());
405
                return;
379
                return;
406
            } else {
380
            } else {
407
                // is action
381
                // is action
Lines 676-682 Link Here
676
         */
650
         */
677
        public void actionPerformed(java.awt.event.ActionEvent e) {
651
        public void actionPerformed(java.awt.event.ActionEvent e) {
678
            if (model != null) {
652
            if (model != null) {
679
                model.performActionAt(0);
653
                model.performActionAt(0, e);
680
            }
654
            }
681
        }
655
        }
682
        
656
        
Lines 710-724 Link Here
710
    
684
    
711
    /** Action that wraps paste type.
685
    /** Action that wraps paste type.
712
     */
686
     */
713
    private static final class ActionPT extends javax.swing.AbstractAction {
687
    private static final class ActionPT extends javax.swing.AbstractAction 
688
    implements Runnable {
714
        private PasteType t;
689
        private PasteType t;
690
        private NodeSelector sel;
691
        private boolean secondInvocation;
715
        
692
        
716
        public ActionPT (PasteType t) {
693
        public ActionPT (PasteType t, String command) {
717
            this.t = t;
694
            this.t = t;
695
            
696
            ExplorerManager em = findExplorerManager ();
697
            if (em != null) {
698
                this.sel = new NodeSelector (em, null);
699
            }
700
701
            if ("synchronous".equals (command)) {
702
                run ();
703
            } else {
704
                org.openide.util.RequestProcessor.getDefault ().post (this);
705
            }
718
        }
706
        }
719
        
707
        
708
        
720
        public void actionPerformed (java.awt.event.ActionEvent ev) {
709
        public void actionPerformed (java.awt.event.ActionEvent ev) {
721
            executePasteType (t);
710
            try {
711
                Transferable trans = t.paste();
712
                Clipboard clipboard = getClipboard();
713
714
715
                if (trans != null) {
716
                    ClipboardOwner owner = trans instanceof ClipboardOwner ?
717
                                           (ClipboardOwner)trans
718
                                           :
719
                                           new StringSelection (""); // NOI18N
720
                    clipboard.setContents(trans, owner);
721
                }
722
            } catch (UserCancelException exc) {
723
                // ignore - user just pressed cancel in some dialog....
724
            } catch (java.io.IOException e) {
725
                ErrorManager.getDefault().notify(e);
726
            } finally {
727
                javax.swing.SwingUtilities.invokeLater (this);
728
            }
722
        }
729
        }
730
        
731
        public void run () {
732
            if (secondInvocation) {
733
                if (sel != null) {
734
                    sel.select ();
735
                }
736
            } else {
737
                secondInvocation = true;
738
                ActionManager.getDefault ().invokeAction (
739
                    this, 
740
                    new ActionEvent (t, ActionEvent.ACTION_PERFORMED, javax.swing.Action.NAME)
741
                );
742
            }
743
        }
744
        
745
        public boolean isEnabled () {
746
            return ((PasteAction)PasteAction.get (PasteAction.class)).isEnabled ();
747
        }
748
        
749
        public Object getValue (String key) {
750
            return ((PasteAction)PasteAction.get (PasteAction.class)).getValue (key);
751
        }
752
        
723
    }
753
    }
754
    
724
}
755
}
(-)openide/src/org/openide/actions/PrintAction.java (-11 / +7 lines)
Lines 32-51 Link Here
32
    }
32
    }
33
33
34
    protected void performAction(final Node[] activatedNodes) {
34
    protected void performAction(final Node[] activatedNodes) {
35
        RequestProcessor.getDefault().post(new Runnable() {
35
        for (int i = 0; i < activatedNodes.length; i++) {
36
            public void run() {
36
            PrintCookie pc = (PrintCookie)activatedNodes[i].getCookie (PrintCookie.class);
37
                for (int i = 0; i < activatedNodes.length; i++) {
37
            if (pc != null) {
38
                    PrintCookie pc = (PrintCookie)activatedNodes[i].getCookie (PrintCookie.class);
38
                pc.print();
39
                    if (pc != null) {
39
            }
40
                        pc.print();
40
        }
41
                    }
42
                }
43
	    }
44
	});
45
    }
41
    }
46
    
42
    
47
    protected boolean asynchronous() {
43
    protected boolean asynchronous() {
48
        return false;
44
        return true;
49
    }
45
    }
50
46
51
    protected int mode () {
47
    protected int mode () {
(-)openide/src/org/openide/explorer/ExplorerActions.java (-5 / +10 lines)
Lines 617-628 Link Here
617
                }
617
                }
618
                
618
                
619
                doDestroy(sel);
619
                doDestroy(sel);
620
                
620
        
621
                if (attachPerformers) {
621
                class Run implements Runnable {
622
                    delete.setActionPerformer (null); // fixes bug #673
622
                    public void run () {
623
                } else {
623
                        if (attachPerformers) {
624
                    setEnabled (false);
624
                            delete.setActionPerformer (null); // fixes bug #673
625
                        } else {
626
                            setEnabled (false);
627
                        }
628
                    }
625
                }
629
                }
630
                org.openide.util.Mutex.EVENT.readAccess (new Run ());
626
            }
631
            }
627
        }
632
        }
628
        
633
        
(-)openide/src/org/openide/util/actions/CallableSystemAction.java (-23 / +100 lines)
Lines 73-79 Link Here
73
    */
73
    */
74
    public void actionPerformed(ActionEvent ev) {
74
    public void actionPerformed(ActionEvent ev) {
75
        if (isEnabled()) {
75
        if (isEnabled()) {
76
            doPerformAction(new Runnable() {
76
            doPerformAction(new ActionRunnable(ev) {
77
                public void run() {
77
                public void run() {
78
                    performAction();
78
                    performAction();
79
                }
79
                }
Lines 103-133 Link Here
103
     * @param r a block to run
103
     * @param r a block to run
104
     * @see #asynchronous
104
     * @see #asynchronous
105
     */
105
     */
106
    final void doPerformAction(final Runnable r) {
106
    final void doPerformAction(final ActionRunnable r) {
107
        assert EventQueue.isDispatchThread() : "Action " + getClass().getName() + " may not be invoked from the thread " + Thread.currentThread().getName() + ", only the event queue: http://www.netbeans.org/download/dev/javadoc/OpenAPIs/apichanges.html#actions-event-thread";
107
        assert EventQueue.isDispatchThread() : "Action " + getClass().getName() + " may not be invoked from the thread " + Thread.currentThread().getName() + ", only the event queue: http://www.netbeans.org/download/dev/javadoc/OpenAPIs/apichanges.html#actions-event-thread";
108
        if (asynchronous()) {
108
        if (asynchronous() && !r.needsToBeSynchronous ()) {
109
            if (warnedAsynchronousActions.add(getClass())) {
110
                ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning - " + getClass().getName() + " should override CallableSystemAction.asynchronous() to return false");
111
            }
112
            Runnable r2 = new Runnable() {
109
            Runnable r2 = new Runnable() {
113
                public void run() {
110
                public void run() {
114
                    try {
111
                    r.doRun();
115
                        EventQueue.invokeLater(new Runnable() {
116
                            public void run() {
117
                                MouseCursorUtils.showWaitCursor(r);
118
                            }
119
                        });
120
                        //addRunningAction(r);
121
                        r.run();
122
                    } finally {
123
                        //removeRunningAction(r);
124
                        EventQueue.invokeLater(new Runnable() {
125
                            public void run() {
126
                                MouseCursorUtils.hideWaitCursor(r);
127
                            }
128
                        });
129
                        //fireRunningActionsChange();
130
                    }
131
                }
112
                }
132
            };
113
            };
133
            RP.post(r2);
114
            RP.post(r2);
Lines 155-164 Link Here
155
     * @since 4.11
136
     * @since 4.11
156
     */
137
     */
157
    protected boolean asynchronous() {
138
    protected boolean asynchronous() {
139
        if (warnedAsynchronousActions.add(getClass())) {
140
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning - " + getClass().getName() + " should override CallableSystemAction.asynchronous() to return false");
141
        }
158
        return DEFAULT_ASYNCH;
142
        return DEFAULT_ASYNCH;
159
    }
143
    }
160
    private static final boolean DEFAULT_ASYNCH = !Boolean.getBoolean("org.openide.util.actions.CallableSystemAction.synchronousByDefault");
144
    private static final boolean DEFAULT_ASYNCH = !Boolean.getBoolean("org.openide.util.actions.CallableSystemAction.synchronousByDefault");
161
    
145
    
146
    /** variables for invokeAction methods */
147
    private static Object invokeInstance;
148
    private static Object invokeAction;
149
    /** Call ActionManager.invokeAction method.
150
     */
151
    public static void invokeAction (javax.swing.Action action, java.awt.event.ActionEvent ev) {
152
        if (invokeAction == null) {
153
            ClassLoader loader = (ClassLoader)org.openide.util.Lookup.getDefault ().lookup (ClassLoader.class);
154
            if (loader != null) {
155
                loader = CallableSystemAction.class.getClassLoader ();
156
            }
157
            try {
158
                Class clazz = Class.forName ("org.openide.actions.ActionManager", true, loader);
159
                invokeInstance = org.openide.util.Lookup.getDefault ().lookup (clazz);
160
                if (invokeInstance != null) {
161
                    invokeAction = clazz.getMethod ("invokeAction", new Class[] { 
162
                        javax.swing.Action.class,
163
                        java.awt.event.ActionEvent.class 
164
                    });
165
                } else {
166
                    // dummy value
167
                    invokeAction = new Object ();
168
                }
169
            } catch (Exception ex) {
170
                ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ex);
171
                // some empty value
172
                invokeAction = new Object ();
173
            }
174
        }
175
        
176
        if (invokeAction instanceof java.lang.reflect.Method) {
177
            java.lang.reflect.Method m = (java.lang.reflect.Method)invokeAction;
178
            try {
179
                m.invoke (invokeInstance, new Object[] { action, ev });
180
                return;
181
            } catch (Exception ex) {
182
                ErrorManager.getDefault ().notify (ex);
183
            }
184
        }
185
        action.actionPerformed (ev);
186
    }
187
    
162
    /**
188
    /**
163
     * Adds action to <code>runningActions</code> map using runnable as a key.
189
     * Adds action to <code>runningActions</code> map using runnable as a key.
164
     * @param r the block being run
190
     * @param r the block being run
Lines 196-200 Link Here
196
        // whatever
222
        // whatever
197
    }
223
    }
198
     */
224
     */
225
226
    /** Special class that can be passed to invokeAction and delegates 
227
     * to correct values
228
     */
229
    abstract class ActionRunnable implements javax.swing.Action {
230
        private ActionEvent ev;
231
        
232
        public ActionRunnable (ActionEvent ev) {
233
            this.ev = ev;
234
        }
235
        
236
        public final boolean needsToBeSynchronous () {
237
            return "synchronous".equals (ev.getActionCommand ()); // NOI18N
238
        }
239
        
240
        public final void doRun () {
241
            invokeAction (this, ev);
242
        }
243
        
244
        protected abstract void run ();
245
        
246
        public final void actionPerformed (ActionEvent e) {
247
            run ();
248
        }
249
        
250
        public final void addPropertyChangeListener (java.beans.PropertyChangeListener listener) {
251
            throw new java.lang.UnsupportedOperationException ();
252
        }
253
        
254
        public final Object getValue (String key) {
255
            return CallableSystemAction.this.getValue (key);
256
        }
257
        
258
        public final boolean isEnabled () {
259
            return CallableSystemAction.this.isEnabled ();
260
        }
261
        
262
        public final void putValue (String key, Object value) {
263
            throw new java.lang.UnsupportedOperationException ();
264
        }
265
        
266
        public final void removePropertyChangeListener (java.beans.PropertyChangeListener listener) {
267
            throw new java.lang.UnsupportedOperationException ();
268
        }
269
        
270
        public final void setEnabled (boolean b) {
271
            throw new java.lang.UnsupportedOperationException ();
272
        }
273
        
274
    } // end of ActionRunnable
275
    
199
    
276
    
200
}
277
}
(-)openide/src/org/openide/util/actions/CallbackSystemAction.java (-4 / +9 lines)
Lines 175-181 Link Here
175
175
176
        final ActionPerformer ap = getActionPerformer ();
176
        final ActionPerformer ap = getActionPerformer ();
177
        if (ap != null) {
177
        if (ap != null) {
178
            doPerformAction(new Runnable() {
178
            doPerformAction(new ActionRunnable (ev) {
179
                public void run() {
179
                public void run() {
180
                    ap.performAction(CallbackSystemAction.this);
180
                    ap.performAction(CallbackSystemAction.this);
181
                }
181
                }
Lines 430-439 Link Here
430
        
430
        
431
        /** Invoked when an action occurs.
431
        /** Invoked when an action occurs.
432
         */
432
         */
433
        public void actionPerformed(java.awt.event.ActionEvent e) {
433
        public void actionPerformed(final java.awt.event.ActionEvent e) {
434
            javax.swing.Action a = findAction ();
434
            final javax.swing.Action a = findAction ();
435
            if (a != null) {
435
            if (a != null) {
436
                a.actionPerformed(e);
436
                ActionRunnable run = delegate.new ActionRunnable (e) {
437
                    public void run () {
438
                        a.actionPerformed(e);
439
                    }
440
                };
441
                delegate.doPerformAction (run);
437
            } else {
442
            } else {
438
                // XXX #30303 if the action falls back to the old behaviour
443
                // XXX #30303 if the action falls back to the old behaviour
439
                // it may not be performed in case it is in dialog and
444
                // it may not be performed in case it is in dialog and
(-)openide/src/org/openide/util/actions/MouseCursorUtils.java (-127 lines)
Removed 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.util.actions;
15
16
import java.awt.Component;
17
import java.awt.Cursor;
18
import java.awt.EventQueue;
19
import java.awt.KeyboardFocusManager;
20
import java.awt.Window;
21
import java.util.HashMap;
22
import java.util.Map;
23
import javax.swing.RootPaneContainer;
24
import org.openide.ErrorManager;
25
import org.openide.util.Utilities;
26
27
/**
28
 * Static utility class for setting/resetting mouse cursor on surface of
29
 * whatever window (e.g. main window) is associated with events.
30
 * Event thread only.
31
 * @author David Simonek, Jesse Glick
32
 * @see "#27780"
33
 */
34
final class MouseCursorUtils {
35
    
36
    private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.openide.util.actions.MouseCursorUtils"); // NOI18N
37
    
38
    private MouseCursorUtils() {}
39
    
40
    /**
41
     * Running show/hide count for glass panes in use.
42
     * Maps arbitrary keys to glass panes.
43
     * Several keys may map to the same glass pane - the wait cursor is shown
44
     * so long as there are any.
45
     */
46
    private static final Map glassPaneUses = new HashMap(); // Map<Object,Component>
47
    
48
    /**
49
     * Try to find the active window's glass pane.
50
     * @return a glass pane, or null
51
     */
52
    private static Component activeGlassPane() {
53
        Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
54
        if (w instanceof RootPaneContainer) {
55
            return ((RootPaneContainer)w).getGlassPane();
56
        } else {
57
            return null;
58
        }
59
    }
60
    
61
    /**
62
     * Sets wait cursor visible on the window associated with an event, if any.
63
     * @param key something to pass to {@link #hideWaitCursor} to turn it off
64
     */
65
    public static void showWaitCursor(Object key) {
66
        assert EventQueue.isDispatchThread();
67
        assert !glassPaneUses.containsKey(key);
68
        Component c = activeGlassPane();
69
        if (c == null) {
70
            if (err.isLoggable(ErrorManager.WARNING)) {
71
                err.log(ErrorManager.WARNING, "showWaitCursor could not find a suitable glass pane; key=" + key);
72
            }
73
            return;
74
        }
75
        if (glassPaneUses.values().contains(c)) {
76
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
77
                err.log("wait cursor already displayed on " + c);
78
            }
79
        } else {
80
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
81
                err.log("wait cursor will be displayed on " + c);
82
            }
83
            c.setCursor(Utilities.createProgressCursor(c));
84
            c.setVisible(true);
85
        }
86
        glassPaneUses.put(key, c);
87
    }
88
    
89
    /**
90
     * Resets cursor to default.
91
     * @param key the same key passed to {@link #showWaitCursor}
92
     */
93
    public static void hideWaitCursor(Object key) {
94
        assert EventQueue.isDispatchThread();
95
        Component c = (Component)glassPaneUses.get(key);
96
        if (c == null) {
97
            return;
98
        }
99
        glassPaneUses.remove(key);
100
        if (glassPaneUses.values().contains(c)) {
101
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
102
                err.log("wait cursor still displayed on " + c);
103
            }
104
        } else {
105
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
106
                err.log("wait cursor will be hidden on " + c);
107
            }
108
            c.setVisible(false);
109
            c.setCursor(null);
110
        }
111
    }
112
    
113
    // XXX is this really necessary?
114
    /**
115
     * Defensive cursor reseting, resets cursor whenever some window is opened
116
     * during the time action is performed.
117
     * Please treat as this method as private, don't use outside this class.
118
     * @param event incoming event
119
     * /
120
    public void eventDispatched(AWTEvent event) {
121
        if (event.getID() == WindowEvent.WINDOW_OPENED) {
122
            hideWaitCursor();
123
        }
124
    }
125
     */
126
    
127
}
(-)openide/src/org/openide/util/actions/NodeAction.java (-4 / +4 lines)
Lines 166-181 Link Here
166
     * a lookup containing all desired <code>Node</code> instances.
166
     * a lookup containing all desired <code>Node</code> instances.
167
     * @param ev action event
167
     * @param ev action event
168
     */
168
     */
169
    public void actionPerformed(ActionEvent ev) {
169
    public void actionPerformed(final ActionEvent ev) {
170
        final Object s = ev == null ? null : ev.getSource();
170
        final Object s = ev == null ? null : ev.getSource();
171
        if (s instanceof Node) {
171
        if (s instanceof Node) {
172
            doPerformAction(new Runnable() {
172
            doPerformAction(new ActionRunnable (ev) {
173
                public void run() {
173
                public void run() {
174
                    performAction(new Node[] {(Node)s});
174
                    performAction(new Node[] {(Node)s});
175
                }
175
                }
176
            });
176
            });
177
        } else if (s instanceof Node[]) {
177
        } else if (s instanceof Node[]) {
178
            doPerformAction(new Runnable() {
178
            doPerformAction(new ActionRunnable(ev) {
179
                public void run() {
179
                public void run() {
180
                    performAction((Node[])s);
180
                    performAction((Node[])s);
181
                }
181
                }
Lines 435-441 Link Here
435
        /** Invoked when an action occurs.
435
        /** Invoked when an action occurs.
436
         */
436
         */
437
        public void actionPerformed(ActionEvent e) {
437
        public void actionPerformed(ActionEvent e) {
438
            delegate.doPerformAction(new Runnable() {
438
            delegate.doPerformAction(delegate.new ActionRunnable(e) {
439
                public void run() {
439
                public void run() {
440
                    delegate.performAction(nodes());
440
                    delegate.performAction(nodes());
441
                }
441
                }
(-)openide/test/unit/src/org/openide/actions/AbstractCallbackActionTestHidden.java (-1 / +6 lines)
Lines 59-66 Link Here
59
    /** The key that is used in the action map
59
    /** The key that is used in the action map
60
     */
60
     */
61
    protected abstract String actionKey ();
61
    protected abstract String actionKey ();
62
62
    
63
    
63
    
64
    
65
    protected boolean runInEQ () {
66
        return true;
67
    }
68
    
64
    protected void setUp() throws Exception {
69
    protected void setUp() throws Exception {
65
        global = (CallbackSystemAction)CallbackSystemAction.get (actionClass ());
70
        global = (CallbackSystemAction)CallbackSystemAction.get (actionClass ());
66
        map = new ActionMap ();
71
        map = new ActionMap ();
Lines 74-80 Link Here
74
    }
79
    }
75
    
80
    
76
    public void testThatDefaultEditorKitPasteActionIsTheCorrectKeyOfPasteAction () {
81
    public void testThatDefaultEditorKitPasteActionIsTheCorrectKeyOfPasteAction () {
77
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
82
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
78
        action.assertCnt ("Clone correctly delegates to OurAction", 1);
83
        action.assertCnt ("Clone correctly delegates to OurAction", 1);
79
    }
84
    }
80
    
85
    
(-)openide/test/unit/src/org/openide/actions/PasteActionTest.java (-3 / +3 lines)
Lines 116-122 Link Here
116
        assertTrue ("Enabled again", clone.isEnabled ());
116
        assertTrue ("Enabled again", clone.isEnabled ());
117
        
117
        
118
        
118
        
119
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
119
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
120
        arr[0].assertCnt ("First delegate invoked", 1);
120
        arr[0].assertCnt ("First delegate invoked", 1);
121
    }
121
    }
122
    
122
    
Lines 141-147 Link Here
141
        action.putValue ("delegates", arr);
141
        action.putValue ("delegates", arr);
142
        assertTrue ("Enabled again", clone.isEnabled ());
142
        assertTrue ("Enabled again", clone.isEnabled ());
143
        
143
        
144
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
144
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
145
        arr[0].assertCnt ("First delegate invoked", 1);
145
        arr[0].assertCnt ("First delegate invoked", 1);
146
146
147
        arr = new OurPasteType[] { new OurPasteType () };
147
        arr = new OurPasteType[] { new OurPasteType () };
Lines 149-155 Link Here
149
        action.putValue ("delegates", arr);
149
        action.putValue ("delegates", arr);
150
        assertTrue ("Enabled still", clone.isEnabled ());
150
        assertTrue ("Enabled still", clone.isEnabled ());
151
        
151
        
152
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
152
        clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
153
        arr[0].assertCnt ("First delegate invoked", 1);
153
        arr[0].assertCnt ("First delegate invoked", 1);
154
    }
154
    }
155
    
155
    
(-)openide/test/unit/src/org/openide/explorer/ExplorerPanelTest.java (-4 / +4 lines)
Lines 135-144 Link Here
135
        assertTrue("Copy action has to be enabled", copy.isEnabled());
135
        assertTrue("Copy action has to be enabled", copy.isEnabled());
136
        assertTrue("Cut action has to be enabled", cut.isEnabled());
136
        assertTrue("Cut action has to be enabled", cut.isEnabled());
137
        
137
        
138
        copy.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
138
        copy.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
139
        assertEquals ("clipboardCopy invoked", 1, enabledNode.countCopy);
139
        assertEquals ("clipboardCopy invoked", 1, enabledNode.countCopy);
140
        
140
        
141
        cut.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
141
        cut.actionPerformed (new java.awt.event.ActionEvent (this, 0, "synchronous"));
142
        assertEquals ("clipboardCut invoked", 1, enabledNode.countCut);
142
        assertEquals ("clipboardCut invoked", 1, enabledNode.countCut);
143
        
143
        
144
144
Lines 175-181 Link Here
175
        manager.setSelectedNodes (new Node[] { enabledNode, enabledNode2 });
175
        manager.setSelectedNodes (new Node[] { enabledNode, enabledNode2 });
176
        assertTrue ("It gets enabled", delete.isEnabled ());
176
        assertTrue ("It gets enabled", delete.isEnabled ());
177
        
177
        
178
        delete.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
178
        delete.actionPerformed(new java.awt.event.ActionEvent (this, 0, "synchronous"));
179
        
179
        
180
        assertEquals ("Destoy was called", 1, enabledNode.countDelete);
180
        assertEquals ("Destoy was called", 1, enabledNode.countDelete);
181
        assertEquals ("Destoy was called", 1, enabledNode2.countDelete);
181
        assertEquals ("Destoy was called", 1, enabledNode2.countDelete);
Lines 209-215 Link Here
209
        
209
        
210
        assertTrue ("Paste is enabled", paste.isEnabled ());
210
        assertTrue ("Paste is enabled", paste.isEnabled ());
211
        
211
        
212
        paste.actionPerformed(new java.awt.event.ActionEvent (this, 0, ""));
212
        paste.actionPerformed(new java.awt.event.ActionEvent (this, 0, "synchronous"));
213
        assertEquals ("Paste invoked", 1, arr[0].count);
213
        assertEquals ("Paste invoked", 1, arr[0].count);
214
        
214
        
215
        manager.setSelectedNodes (new Node[] { disabledNode });
215
        manager.setSelectedNodes (new Node[] { disabledNode });
(-)openide/test/unit/src/org/openide/util/actions/AsynchronousTest.java (+180 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-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.util.actions;
15
16
import java.awt.Image;
17
import java.awt.Toolkit;
18
import java.awt.event.ActionEvent;
19
import java.awt.image.BufferedImage;
20
import java.awt.image.ImageObserver;
21
import java.awt.image.PixelGrabber;
22
import javax.swing.Icon;
23
import javax.swing.JButton;
24
import org.netbeans.junit.*;
25
import junit.textui.TestRunner;
26
import org.openide.util.HelpCtx;
27
import org.openide.util.lookup.AbstractLookup;
28
29
/** Test general aspects of system actions.
30
 * Currently, just the icon.
31
 * @author Jesse Glick
32
 */
33
public class AsynchronousTest extends NbTestCase {
34
    
35
    public AsynchronousTest (String name) {
36
        super(name);
37
    }
38
    
39
    public static void main(String[] args) {
40
        System.setProperty("org.openide.util.Lookup", "org.openide.util.actions.AsynchronousTest$Lkp");
41
        TestRunner.run(new NbTestSuite(AsynchronousTest.class));
42
    }
43
    
44
    protected void setUp () {
45
        ErrManager.messages.delete (0, ErrManager.messages.length ());
46
    }
47
    
48
    protected boolean runInEQ () {
49
        return true;
50
    }
51
    
52
    public void testExecutionOfActionsThatDoesNotOverrideAsynchronousIsAsynchronousButWarningIsPrinted () throws Exception {
53
        DoesNotOverride action = (DoesNotOverride)DoesNotOverride.get (DoesNotOverride.class);
54
        
55
        synchronized (action) {
56
            action.actionPerformed (new ActionEvent(this, 0, ""));
57
            Thread.sleep (500);
58
            assertFalse ("Not yet finished", action.finished);
59
            action.wait ();
60
            assertTrue ("The asynchronous action is finished", action.finished);
61
        }
62
        
63
        if (ErrManager.messages.toString ().indexOf (DoesNotOverride.class.getName () + " should override") < 0) {
64
            fail ("There should be warning about not overriding asynchronous: " + ErrManager.messages);
65
        }
66
    }
67
    
68
    public void testExecutionCanBeAsynchronous () throws Exception {
69
        DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get (DoesOverrideAndReturnsTrue.class);
70
        
71
        synchronized (action) {
72
            action.actionPerformed (new ActionEvent(this, 0, ""));
73
            Thread.sleep (500);
74
            assertFalse ("Not yet finished", action.finished);
75
            action.wait ();
76
            assertTrue ("The asynchronous action is finished", action.finished);
77
        }
78
        
79
        if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) {
80
            fail ("No warning about the class: " + ErrManager.messages);
81
        }
82
    }
83
    
84
    public void testExecutionCanBeSynchronous () throws Exception {
85
        DoesOverrideAndReturnsFalse action = (DoesOverrideAndReturnsFalse)DoesOverrideAndReturnsFalse.get (DoesOverrideAndReturnsFalse.class);
86
        
87
        synchronized (action) {
88
            action.actionPerformed (new ActionEvent(this, 0, ""));
89
            assertTrue ("The synchronous action is finished immediatelly", action.finished);
90
        }
91
        
92
        if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) {
93
            fail ("No warning about the class: " + ErrManager.messages);
94
        }
95
    }
96
97
    public void testExecutionCanBeForcedToBeSynchronous () throws Exception {
98
        DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get (DoesOverrideAndReturnsTrue.class);
99
        
100
        synchronized (action) {
101
            action.actionPerformed (new ActionEvent(this, 0, "synchronous"));
102
            assertTrue ("When asked for synchronous the action is finished immediatelly", action.finished);
103
        }
104
        
105
        if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) {
106
            fail ("No warning about the class: " + ErrManager.messages);
107
        }
108
    }
109
110
    public static class DoesNotOverride extends CallableSystemAction {
111
        boolean finished;
112
        
113
        public HelpCtx getHelpCtx () {
114
            return HelpCtx.DEFAULT_HELP;
115
        }
116
        
117
        public String getName () {
118
            return "Should warn action";
119
        }
120
        
121
        public synchronized void performAction () {
122
            notifyAll ();
123
            finished = true;
124
        }
125
        
126
    }
127
    
128
    public static class DoesOverrideAndReturnsTrue extends DoesNotOverride {
129
        public boolean asynchronous () {
130
            return true;
131
        }
132
    }
133
    
134
    public static final class DoesOverrideAndReturnsFalse extends DoesOverrideAndReturnsTrue {
135
        public boolean asynchronous () {
136
            return false;
137
        }
138
    }
139
    
140
    
141
    public static final class Lkp extends AbstractLookup {
142
        public Lkp () {
143
            this (new org.openide.util.lookup.InstanceContent ());
144
        }
145
        
146
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
147
            super (ic);
148
            ic.add (new ErrManager ());
149
        }
150
    }
151
    
152
    private static final class ErrManager extends org.openide.ErrorManager {
153
        public static final StringBuffer messages = new StringBuffer ();
154
        
155
        public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, java.util.Date date) {
156
            return t;
157
        }
158
        
159
        public Throwable attachAnnotations (Throwable t, org.openide.ErrorManager.Annotation[] arr) {
160
            return t;
161
        }
162
        
163
        public org.openide.ErrorManager.Annotation[] findAnnotations (Throwable t) {
164
            return null;
165
        }
166
        
167
        public org.openide.ErrorManager getInstance (String name) {
168
            return this;
169
        }
170
        
171
        public void log (int severity, String s) {
172
            messages.append (s);
173
            messages.append ('\n');
174
        }
175
        
176
        public void notify (int severity, Throwable t) {
177
        }
178
        
179
    } 
180
}
(-)openide/test/unit/src/org/openide/util/actions/CallbackSystemActionTest.java (+4 lines)
Lines 39-44 Link Here
39
        // May have used AWT thread.
39
        // May have used AWT thread.
40
        System.exit(0);
40
        System.exit(0);
41
    }
41
    }
42
43
    protected boolean runInEQ () {
44
        return true;
45
    }
42
    
46
    
43
    /** Make sure that the performer system works and controls enablement.
47
    /** Make sure that the performer system works and controls enablement.
44
     */
48
     */
(-)core/execution/src/org/netbeans/core/execution/Install.java (+1 lines)
Lines 287-292 Link Here
287
        ArrayList pendingTasks = new ArrayList( 10 );
287
        ArrayList pendingTasks = new ArrayList( 10 );
288
        // XXX no access to running actions at the moment
288
        // XXX no access to running actions at the moment
289
        //pendingTasks.addAll(CallableSystemAction.getRunningActions());
289
        //pendingTasks.addAll(CallableSystemAction.getRunningActions());
290
        pendingTasks.addAll(org.netbeans.core.ModuleActions.getDefaultInstance().getRunningActions());
290
        
291
        
291
        if ( !Boolean.getBoolean( "netbeans.full.hack" ) ) { // NOI18N
292
        if ( !Boolean.getBoolean( "netbeans.full.hack" ) ) { // NOI18N
292
            // Avoid showing the tasks in the dialog when running internal tests
293
            // Avoid showing the tasks in the dialog when running internal tests
(-)core/src/org/netbeans/core/ModuleActions.java (+129 lines)
Lines 13-19 Link Here
13
13
14
package org.netbeans.core;
14
package org.netbeans.core;
15
15
16
import java.awt.event.ActionEvent;
16
import java.util.*;
17
import java.util.*;
18
import javax.swing.Action;
17
19
18
import org.openide.actions.ActionManager;
20
import org.openide.actions.ActionManager;
19
import org.openide.util.actions.SystemAction;
21
import org.openide.util.actions.SystemAction;
Lines 35-40 Link Here
35
    private static Map map = new HashMap (7);
37
    private static Map map = new HashMap (7);
36
    /** current module */
38
    /** current module */
37
    private static Object module = null;
39
    private static Object module = null;
40
    /** Map of currently running actions, (maps action event to action) */
41
    private Map runningActions = new HashMap();
38
42
39
    public static ModuleActions getDefaultInstance() {
43
    public static ModuleActions getDefaultInstance() {
40
        return (ModuleActions)ActionManager.getDefault();
44
        return (ModuleActions)ActionManager.getDefault();
Lines 52-64 Link Here
52
        return a;
56
        return a;
53
    }
57
    }
54
    
58
    
59
    /** Invokes action in a RequestPrecessor dedicated to performing
60
     * actions.
61
     */
62
    public void invokeAction(final Action a, final ActionEvent e) {
63
        try {
64
            java.awt.EventQueue.invokeLater(new Runnable() {
65
                public void run() {
66
                    showWaitCursor(e);
67
                }
68
            });
69
            addRunningAction(a, e);
70
            
71
            a.actionPerformed (e);
72
        } finally {
73
            removeRunningAction(e);
74
            java.awt.EventQueue.invokeLater(new Runnable() {
75
                public void run() {
76
                    hideWaitCursor(e);
77
                }
78
            });
79
        }
80
    }
81
    
55
    /** Listens on change of modules and if changed,
82
    /** Listens on change of modules and if changed,
56
    * fires change to all listeners.
83
    * fires change to all listeners.
57
    */
84
    */
58
    private void fireChange () {
85
    private void fireChange () {
59
        firePropertyChange(PROP_CONTEXT_ACTIONS, null, null);
86
        firePropertyChange(PROP_CONTEXT_ACTIONS, null, null);
60
    }
87
    }
88
    
89
    /** Adds action to <code>runningAction</code> map using event as a key.
90
     * @param rp <code>RequestProcessor</code> which runs the actio task
91
     * @param action action to put in map 
92
     * @param evt action event used as key in the map */
93
    private void addRunningAction(Action action, ActionEvent evt) {
94
        synchronized(runningActions) {
95
            runningActions.put(evt, action);
96
        }
97
    }
98
    
99
    /** Removes action from <code>runningAction</code> map for key.
100
     * @param evt action event used as a key in map */
101
    private void removeRunningAction(ActionEvent evt) {
102
        synchronized(runningActions) {
103
            runningActions.remove(evt);
104
        }
105
    }
61
106
107
    /** Gets collection of currently running actions. */
108
    public Collection getRunningActions() {
109
        synchronized(runningActions) {
110
            return new ArrayList(runningActions.values());
111
        }
112
    }
113
     
62
    /** Change enabled property of an action
114
    /** Change enabled property of an action
63
    *
115
    *
64
    public void propertyChange (PropertyChangeEvent ev) {
116
    public void propertyChange (PropertyChangeEvent ev) {
Lines 142-147 Link Here
142
194
143
        return (SystemAction[])arr.toArray (new SystemAction[arr.size ()]);
195
        return (SystemAction[])arr.toArray (new SystemAction[arr.size ()]);
144
    }
196
    }
197
198
    
199
    private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.openide.util.actions.MouseCursorUtils"); // NOI18N
200
    
201
    /**
202
     * Running show/hide count for glass panes in use.
203
     * Maps arbitrary keys to glass panes.
204
     * Several keys may map to the same glass pane - the wait cursor is shown
205
     * so long as there are any.
206
     */
207
    private static final Map glassPaneUses = new HashMap(); // Map<Object,Component>
208
    
209
    /**
210
     * Try to find the active window's glass pane.
211
     * @return a glass pane, or null
212
     */
213
    private static java.awt.Component activeGlassPane() {
214
        java.awt.Window w = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
215
        if (w instanceof javax.swing.RootPaneContainer) {
216
            return ((javax.swing.RootPaneContainer)w).getGlassPane();
217
        } else {
218
            return null;
219
        }
220
    }
221
    
222
    /**
223
     * Sets wait cursor visible on the window associated with an event, if any.
224
     * @param key something to pass to {@link #hideWaitCursor} to turn it off
225
     */
226
    public static void showWaitCursor(Object key) {
227
        assert java.awt.EventQueue.isDispatchThread();
228
        assert !glassPaneUses.containsKey(key);
229
        java.awt.Component c = activeGlassPane();
230
        if (c == null) {
231
            if (err.isLoggable(ErrorManager.WARNING)) {
232
                err.log(ErrorManager.WARNING, "showWaitCursor could not find a suitable glass pane; key=" + key);
233
            }
234
            return;
235
        }
236
        if (glassPaneUses.values().contains(c)) {
237
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
238
                err.log("wait cursor already displayed on " + c);
239
            }
240
        } else {
241
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
242
                err.log("wait cursor will be displayed on " + c);
243
            }
244
            c.setCursor(org.openide.util.Utilities.createProgressCursor(c));
245
            c.setVisible(true);
246
        }
247
        glassPaneUses.put(key, c);
248
    }
249
    
250
    /**
251
     * Resets cursor to default.
252
     * @param key the same key passed to {@link #showWaitCursor}
253
     */
254
    public static void hideWaitCursor(Object key) {
255
        assert java.awt.EventQueue.isDispatchThread();
256
        java.awt.Component c = (java.awt.Component)glassPaneUses.get(key);
257
        if (c == null) {
258
            return;
259
        }
260
        glassPaneUses.remove(key);
261
        if (glassPaneUses.values().contains(c)) {
262
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
263
                err.log("wait cursor still displayed on " + c);
264
            }
265
        } else {
266
            if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
267
                err.log("wait cursor will be hidden on " + c);
268
            }
269
            c.setVisible(false);
270
            c.setCursor(null);
271
        }
272
    }
273
    
145
    
274
    
146
}
275
}
147
276
(-)core/test/unit/src/org/netbeans/core/ModuleActionsTest.java (+79 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.netbeans.core;
15
16
import org.netbeans.junit.*;
17
import junit.textui.TestRunner;
18
19
/** Tests behaviour of asynchronous actions and exit dialog.
20
 */
21
public class ModuleActionsTest extends NbTestCase {
22
    
23
    public ModuleActionsTest (String name) {
24
        super(name);
25
    }
26
    
27
    public static void main(String[] args) {
28
        TestRunner.run(new NbTestSuite(ModuleActionsTest.class));
29
    }
30
    
31
    protected boolean runInEQ () {
32
        return true;
33
    }    
34
    
35
    public void testActionIsListedAsRunning () throws Exception {
36
        Act act = (Act)Act.get (Act.class);
37
        
38
        synchronized (act) {
39
            act.actionPerformed (new java.awt.event.ActionEvent (this, 0, ""));
40
            act.wait ();
41
        }
42
        
43
        java.util.Collection col = ModuleActions.getDefaultInstance ().getRunningActions ();
44
        java.util.Iterator it = col.iterator ();
45
        while (it.hasNext ()) {
46
            javax.swing.Action a = (javax.swing.Action)it.next ();
47
            if (a.getValue (javax.swing.Action.NAME) == act.getName ()) {
48
                return;
49
            }
50
        }
51
        fail ("Act should be running: " + col);
52
    }
53
54
    public static class Act extends org.openide.util.actions.CallableSystemAction {
55
        
56
        public org.openide.util.HelpCtx getHelpCtx () {
57
            return org.openide.util.HelpCtx.DEFAULT_HELP;
58
        }
59
        
60
        public String getName () {
61
            return getClass().getName ();
62
        }
63
        
64
        public synchronized void performAction () {
65
            notifyAll ();
66
            try {
67
                wait ();
68
            } catch (InterruptedException ex) {
69
                fail ("Shall not be interupted");
70
            }
71
        }
72
        
73
        protected boolean asynchronous () {
74
            return true;
75
        }
76
        
77
    }
78
    
79
}

Return to bug 39640