KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > PropertiesAction


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.actions;
21
22
23 import java.awt.Component JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.util.Collection JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JMenuItem JavaDoc;
29 import org.openide.awt.Actions;
30 import org.openide.awt.JInlineMenu;
31 import org.openide.nodes.Node;
32 import org.openide.nodes.NodeOperation;
33 import org.openide.util.ContextAwareAction;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.Lookup;
36 import org.openide.util.NbBundle;
37 import org.openide.util.actions.NodeAction;
38 import org.openide.util.actions.Presenter;
39 import org.openide.util.actions.SystemAction;
40
41 /** Get properties of a node.
42 *
43 * @see NodeOperation#showProperties(Node[])
44 * @author Ian Formanek, Jan Jancura
45 */

46 public class PropertiesAction extends NodeAction {
47     protected void performAction(Node[] activatedNodes) {
48         if (activatedNodes.length == 1) {
49             NodeOperation.getDefault().showProperties(activatedNodes[0]);
50         } else {
51             NodeOperation.getDefault().showProperties(activatedNodes);
52         }
53     }
54
55     protected boolean asynchronous() {
56         return false;
57     }
58
59     protected boolean enable(Node[] activatedNodes) {
60         return activatedNodes != null;
61     }
62
63     public JMenuItem JavaDoc getPopupPresenter() {
64         JMenuItem JavaDoc prop = new Actions.MenuItem(this, false);
65
66         CustomizeAction customizeAction = (CustomizeAction) SystemAction.get(CustomizeAction.class);
67
68         if (customizeAction.isEnabled()) {
69             JInlineMenu mi = new JInlineMenu();
70             mi.setMenuItems(new JMenuItem JavaDoc[] { new Actions.MenuItem(customizeAction, false), prop });
71
72             return mi;
73         } else {
74             return prop;
75         }
76     }
77
78     public String JavaDoc getName() {
79         return NbBundle.getMessage(PropertiesAction.class, "Properties");
80     }
81
82     public HelpCtx getHelpCtx() {
83         return new HelpCtx(PropertiesAction.class);
84     }
85
86     protected String JavaDoc iconResource() {
87         return "org/openide/resources/actions/properties.gif"; // NOI18N
88
}
89
90     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
91         return new DelegateAction(this, actionContext);
92     }
93
94     /** Delegate action for clonned context. Used to provide a special
95      * support for getPopupPresenter.
96      */

97     private static final class DelegateAction implements Action JavaDoc, Presenter.Menu, Presenter.Toolbar, Presenter.Popup {
98         /** action to delegate to */
99         private PropertiesAction delegate;
100
101         /** lookup we try to work in */
102         private Lookup lookup;
103
104         public DelegateAction(PropertiesAction a, Lookup actionContext) {
105             this.delegate = a;
106             this.lookup = actionContext;
107         }
108
109         private Node[] nodes() {
110             Collection JavaDoc c = lookup.lookupAll(Node.class);
111
112             return (Node[]) c.toArray(new Node[c.size()]);
113         }
114
115         /** Overrides superclass method, adds delegate description. */
116         public String JavaDoc toString() {
117             return super.toString() + "[delegate=" + delegate + "]"; // NOI18N
118
}
119
120         /** Invoked when an action occurs.
121          */

122         public void actionPerformed(ActionEvent JavaDoc e) {
123             delegate.performAction(nodes());
124         }
125
126         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
127             // ignore
128
}
129
130         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
131             // ignore
132
}
133
134         public void putValue(String JavaDoc key, Object JavaDoc o) {
135         }
136
137         public Object JavaDoc getValue(String JavaDoc key) {
138             return delegate.getValue(key);
139         }
140
141         public boolean isEnabled() {
142             return delegate.enable(nodes());
143         }
144
145         public void setEnabled(boolean b) {
146             assert false;
147         }
148
149         public JMenuItem JavaDoc getMenuPresenter() {
150             return new Actions.MenuItem(this, true);
151         }
152
153         public JMenuItem JavaDoc getPopupPresenter() {
154             JMenuItem JavaDoc prop = new Actions.MenuItem(this, false);
155
156             Action JavaDoc customizeAction = (CustomizeAction) SystemAction.get(CustomizeAction.class);
157
158             // Retrieve context sensitive action instance if possible.
159
if (lookup != null) {
160                 customizeAction = ((ContextAwareAction) customizeAction).createContextAwareInstance(lookup);
161             }
162
163             if (customizeAction.isEnabled()) {
164                 JInlineMenu mi = new JInlineMenu();
165                 mi.setMenuItems(new JMenuItem JavaDoc[] { new Actions.MenuItem(customizeAction, false), prop });
166
167                 return mi;
168             } else {
169                 return prop;
170             }
171         }
172
173         public Component JavaDoc getToolbarPresenter() {
174             return new Actions.ToolbarButton(this);
175         }
176     }
177      // end of DelegateAction
178
}
179
Popular Tags