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

(-)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 () {
(-)src/org/openide/util/actions/CallableSystemAction.java (-3 / +3 lines)
Lines 106-114 Link Here
106
    final void doPerformAction(final Runnable r) {
106
    final void doPerformAction(final Runnable 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()) {
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
                    try {
Lines 155-160 Link Here
155
     * @since 4.11
152
     * @since 4.11
156
     */
153
     */
157
    protected boolean asynchronous() {
154
    protected boolean asynchronous() {
155
        if (warnedAsynchronousActions.add(getClass())) {
156
            ErrorManager.getDefault().log(ErrorManager.WARNING, "Warning - " + getClass().getName() + " should override CallableSystemAction.asynchronous() to return false");
157
        }
158
        return DEFAULT_ASYNCH;
158
        return DEFAULT_ASYNCH;
159
    }
159
    }
160
    private static final boolean DEFAULT_ASYNCH = !Boolean.getBoolean("org.openide.util.actions.CallableSystemAction.synchronousByDefault");
160
    private static final boolean DEFAULT_ASYNCH = !Boolean.getBoolean("org.openide.util.actions.CallableSystemAction.synchronousByDefault");
(-)test/unit/src/org/openide/util/actions/AsynchronousTest.java (+164 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", Lkp.class.getName ());
41
        TestRunner.run(new NbTestSuite(AsynchronousTest.class));
42
    }
43
    
44
    protected void setUp () {
45
        ErrManager.messages.delete (0, ErrManager.messages.length ());
46
    }
47
    
48
    public void testExecutionOfActionsThatDoesNotOverrideAsynchronousIsAsynchronousButWarningIsPrinted () throws Exception {
49
        DoesNotOverride action = (DoesNotOverride)DoesNotOverride.get (DoesNotOverride.class);
50
        
51
        synchronized (action) {
52
            action.actionPerformed (new ActionEvent(this, 0, ""));
53
            Thread.sleep (500);
54
            assertFalse ("Not yet finished", action.finished);
55
            action.wait ();
56
            assertTrue ("The asynchronous action is finished", action.finished);
57
        }
58
        
59
        if (ErrManager.messages.toString ().indexOf (DoesNotOverride.class.getName () + " should override") < 0) {
60
            fail ("There should be warning about not overriding asynchronous: " + ErrManager.messages);
61
        }
62
    }
63
    
64
    public void testExecutionCanBeAsynchronous () throws Exception {
65
        DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get (DoesOverrideAndReturnsTrue.class);
66
        
67
        synchronized (action) {
68
            action.actionPerformed (new ActionEvent(this, 0, ""));
69
            Thread.sleep (500);
70
            assertFalse ("Not yet finished", action.finished);
71
            action.wait ();
72
            assertTrue ("The asynchronous action is finished", action.finished);
73
        }
74
        
75
        if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) {
76
            fail ("No warning about the class: " + ErrManager.messages);
77
        }
78
    }
79
    
80
    public void testExecutionCanBeSynchronous () throws Exception {
81
        DoesOverrideAndReturnsFalse action = (DoesOverrideAndReturnsFalse)DoesOverrideAndReturnsFalse.get (DoesOverrideAndReturnsFalse.class);
82
        
83
        synchronized (action) {
84
            action.actionPerformed (new ActionEvent(this, 0, ""));
85
            assertTrue ("The synchronous action is finished immediatelly", action.finished);
86
        }
87
        
88
        if (ErrManager.messages.toString ().indexOf (DoesOverrideAndReturnsTrue.class.getName ()) >= 0) {
89
            fail ("No warning about the class: " + ErrManager.messages);
90
        }
91
    }
92
93
94
    public static class DoesNotOverride extends CallableSystemAction {
95
        boolean finished;
96
        
97
        public HelpCtx getHelpCtx () {
98
            return HelpCtx.DEFAULT_HELP;
99
        }
100
        
101
        public String getName () {
102
            return "Should warn action";
103
        }
104
        
105
        public synchronized void performAction () {
106
            notifyAll ();
107
            finished = true;
108
        }
109
        
110
    }
111
    
112
    public static class DoesOverrideAndReturnsTrue extends DoesNotOverride {
113
        public boolean asynchronous () {
114
            return true;
115
        }
116
    }
117
    
118
    public static final class DoesOverrideAndReturnsFalse extends DoesOverrideAndReturnsTrue {
119
        public boolean asynchronous () {
120
            return false;
121
        }
122
    }
123
    
124
    
125
    public static final class Lkp extends AbstractLookup {
126
        public Lkp () {
127
            this (new org.openide.util.lookup.InstanceContent ());
128
        }
129
        
130
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
131
            super (ic);
132
            ic.add (new ErrManager ());
133
        }
134
    }
135
    
136
    private static final class ErrManager extends org.openide.ErrorManager {
137
        public static final StringBuffer messages = new StringBuffer ();
138
        
139
        public Throwable annotate (Throwable t, int severity, String message, String localizedMessage, Throwable stackTrace, java.util.Date date) {
140
            return t;
141
        }
142
        
143
        public Throwable attachAnnotations (Throwable t, org.openide.ErrorManager.Annotation[] arr) {
144
            return t;
145
        }
146
        
147
        public org.openide.ErrorManager.Annotation[] findAnnotations (Throwable t) {
148
            return null;
149
        }
150
        
151
        public org.openide.ErrorManager getInstance (String name) {
152
            return this;
153
        }
154
        
155
        public void log (int severity, String s) {
156
            messages.append (s);
157
            messages.append ('\n');
158
        }
159
        
160
        public void notify (int severity, Throwable t) {
161
        }
162
        
163
    } 
164
}

Return to bug 39640