KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > CallableSystemAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.actions;
21
22 import java.awt.Toolkit JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import org.openide.util.WeakSet;
27
28 /** An action which may be called programmatically.
29 * Typically a presenter will call its {@link #performAction} method,
30 * which must be implemented.
31 * <p>Provides default presenters using the <a HREF="@org-openide-awt@/org/openide/awt/Actions.html">Actions</a> utility class.
32 *
33 * @author Ian Formanek, Jaroslav Tulach, Jan Jancura, Petr Hamernik
34 */

35 public abstract class CallableSystemAction extends SystemAction implements Presenter.Menu, Presenter.Popup,
36     Presenter.Toolbar {
37     /** serialVersionUID */
38     static final long serialVersionUID = 2339794599168944156L;
39
40     // ASYNCHRONICITY
41
// Adapted from org.netbeans.core.ModuleActions by jglick
42

43     /**
44      * Set of action classes for which we have already issued a warning that
45      * {@link #asynchronous} was not overridden to return false.
46      */

47     private static final Set JavaDoc<Class JavaDoc> warnedAsynchronousActions = new WeakSet<Class JavaDoc>();
48     private static final boolean DEFAULT_ASYNCH = !Boolean.getBoolean(
49             "org.openide.util.actions.CallableSystemAction.synchronousByDefault"
50         );
51
52     /* Returns a JMenuItem that presents the Action, that implements this
53     * interface, in a MenuBar.
54     * @return the JMenuItem representation for the Action
55     */

56     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
57         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createMenuPresenter(this);
58     }
59
60     /* Returns a JMenuItem that presents the Action, that implements this
61     * interface, in a Popup Menu.
62     * @return the JMenuItem representation for the Action
63     */

64     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
65         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createPopupPresenter(this);
66     }
67
68     /* Returns a Component that presents the Action, that implements this
69     * interface, in a ToolBar.
70     * @return the Component representation for the Action
71     */

72     public java.awt.Component JavaDoc getToolbarPresenter() {
73         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createToolbarPresenter(this);
74     }
75
76     /** Actually perform the action.
77     * This is the method which should be called programmatically.
78     * Presenters in <a HREF="@org-openide-awt@/org/openide/awt/Actions.html">Actions</a> use this.
79     * <p>See {@link SystemAction#actionPerformed} for a note on
80     * threading usage: in particular, do not access GUI components
81     * without explicitly asking for the AWT event thread!
82     */

83     public abstract void performAction();
84
85     /* Implementation of method of javax.swing.Action interface.
86     * Delegates the execution to performAction method.
87     *
88     * @param ev the action event
89     */

90     public void actionPerformed(ActionEvent JavaDoc ev) {
91         if (isEnabled()) {
92             org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(
93                 this,
94                 new org.netbeans.modules.openide.util.ActionsBridge.ActionRunnable(ev, this, asynchronous()) {
95                     public void run() {
96                         performAction();
97                     }
98                 }
99             );
100         } else {
101             // Should not normally happen.
102
Toolkit.getDefaultToolkit().beep();
103         }
104     }
105
106     /**
107      * If true, this action should be performed asynchronously in a private thread.
108      * If false, it will be performed synchronously as called in the event thread.
109      * <p>The default value is true for compatibility reasons; subclasses are strongly
110      * encouraged to override it to be false, and to either do their work promptly
111      * in the event thread and return, or to somehow do work asynchronously (for example
112      * using {@link org.openide.util.RequestProcessor#getDefault}).
113      * <p class="nonnormative">You may currently set the global default to false
114      * by setting the system property
115      * <code>org.openide.util.actions.CallableSystemAction.synchronousByDefault</code>
116      * to <code>true</code>.</p>
117      * <p class="nonnormative">When true, the current implementation also provides for a wait cursor during
118      * the execution of the action. Subclasses which override to return false should
119      * consider directly providing a wait or busy cursor if the nature of the action
120      * merits it.</p>
121      * @return true if this action should automatically be performed asynchronously
122      * @since 4.11
123      */

124     protected boolean asynchronous() {
125         if (warnedAsynchronousActions.add(getClass())) {
126             Logger.getAnonymousLogger().warning(
127                 "Warning - " + getClass().getName() +
128                 " should override CallableSystemAction.asynchronous() to return false"
129             );
130         }
131
132         return DEFAULT_ASYNCH;
133     }
134 }
135
Popular Tags