KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.openide.util.actions;
20
21
22 /** An action that can be toggled on or off.
23 * The actual "performing" of the action is the toggle itself, so
24 * this action should be used by listening to the {@link #PROP_BOOLEAN_STATE} property.
25 * <p>The default value of the state is <code>true</code> (on).
26 *
27 *
28 * @author Ian Formanek, Petr Hamernik
29 */

30 public abstract class BooleanStateAction extends SystemAction implements Presenter.Menu, Presenter.Popup,
31     Presenter.Toolbar {
32     /** serialVersionUID */
33     static final long serialVersionUID = 6394800019181426199L;
34
35     /** Name of property hold the state of the action. */
36     public static final String JavaDoc PROP_BOOLEAN_STATE = "booleanState"; // NOI18N
37

38     /* Returns a JMenuItem that presents the Action, that implements this
39     * interface, in a MenuBar.
40     * @return the JMenuItem representation for the Action
41     */

42     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
43         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createMenuPresenter(this);
44     }
45
46     /* Returns a JMenuItem that presents the Action, that implements this
47     * interface, in a Popup Menu.
48     * The default implmentation returns the same JMenuItem as the getMenuPresenter.
49     * @return the JMenuItem representation for the Action
50     */

51     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
52         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createPopupPresenter(this);
53     }
54
55     /* Returns a Component that presents the Action, that implements this
56     * interface, in a ToolBar.
57     * @return the Component representation for the Action
58     */

59     public java.awt.Component JavaDoc getToolbarPresenter() {
60         return org.netbeans.modules.openide.util.AWTBridge.getDefault().createToolbarPresenter(this);
61     }
62
63     /** Get the current state.
64     * @return <code>true</code> if on
65     */

66     public boolean getBooleanState() {
67         return getProperty(PROP_BOOLEAN_STATE).equals(Boolean.TRUE);
68     }
69
70     /** Set the current state.
71     * Fires a change event, which should be used to affect other components when
72     * its state is toggled.
73     * @param value <code>true</code> to turn on, <code>false</code> to turn off
74     */

75     public void setBooleanState(boolean value) {
76         Boolean JavaDoc newValue = value ? Boolean.TRUE : Boolean.FALSE;
77         Boolean JavaDoc oldValue = (Boolean JavaDoc) putProperty(PROP_BOOLEAN_STATE, newValue);
78
79         firePropertyChange(PROP_BOOLEAN_STATE, oldValue, newValue);
80     }
81
82     /* Initializes its own properties (and let superclass initialize
83     * too).
84     */

85     protected void initialize() {
86         putProperty(PROP_BOOLEAN_STATE, Boolean.TRUE);
87         super.initialize();
88     }
89
90     /* Implementation of method of javax.swing.Action interface.
91     * Changes the boolean state.
92     *
93     * @param ev ignored
94     */

95     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
96         setBooleanState(!getBooleanState());
97     }
98 }
99
Popular Tags