KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.beans.PropertyChangeListener JavaDoc;
23 import javax.swing.event.ChangeEvent JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import javax.swing.event.EventListenerList JavaDoc;
26 import org.openide.awt.Actions;
27 import org.openide.explorer.ExplorerManager;
28 import org.openide.nodes.Node;
29 import org.openide.util.Exceptions;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.Lookup;
32 import org.openide.util.NbBundle;
33 import org.openide.util.actions.NodeAction;
34 import org.openide.util.actions.Presenter;
35 import org.openide.util.datatransfer.NewType;
36 import org.openide.windows.WindowManager;
37
38 /** Creates a new child of the activated node, if appropriate.
39 * @see Node#getNewTypes
40 *
41 * @author Petr Hamernik, Ian Formanek
42 */

43 public final class NewAction extends NodeAction {
44     /** Imlementation of ActSubMenuInt */
45     private static ActSubMenuModel model = new ActSubMenuModel(null);
46
47     protected void performAction(Node[] activatedNodes) {
48         performAction(activatedNodes, 0);
49     }
50
51     protected boolean asynchronous() {
52         return false;
53     }
54
55     /** Performs action on index and nodes.
56     */

57     private static void performAction(Node[] activatedNodes, int indx) {
58         NewType[] types = getNewTypes(activatedNodes);
59
60         if (types.length <= indx) {
61             return;
62         }
63
64         performAction(activatedNodes, types[indx]);
65     }
66
67     /** Performs action on given type
68      */

69     private static void performAction(Node[] activatedNodes, NewType type) {
70         PasteAction.NodeSelector sel = null;
71
72         try {
73             ExplorerManager em = PasteAction.findExplorerManager();
74
75             if (em != null) {
76                 sel = new PasteAction.NodeSelector(em, activatedNodes);
77             }
78
79             type.create();
80         } catch (java.io.IOException JavaDoc e) {
81             Exceptions.printStackTrace(e);
82         } finally {
83             if (sel != null) {
84                 sel.select();
85             }
86         }
87     }
88
89     /** Getter for array of activated new types.
90     */

91     private static NewType[] getNewTypes() {
92         return getNewTypes(WindowManager.getDefault().getRegistry().getCurrentNodes());
93     }
94
95     /** Getter for array of activated new types.
96     */

97     private static NewType[] getNewTypes(Node[] activatedNodes) {
98         if ((activatedNodes == null) || (activatedNodes.length != 1)) {
99             return new NewType[0];
100         } else {
101             return activatedNodes[0].getNewTypes();
102         }
103     }
104
105     protected boolean enable(Node[] activatedNodes) {
106         NewType[] types = getNewTypes();
107
108         // notify listeners
109
Object JavaDoc[] listeners = model.getListenerList();
110
111         if (listeners.length > 0) {
112             ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(model);
113
114             for (int i = listeners.length - 1; i >= 0; i -= 2) {
115                 ((ChangeListener JavaDoc) listeners[i]).stateChanged(ev);
116             }
117         }
118
119         return (types.length > 0);
120     }
121
122     public String JavaDoc getName() {
123         return createName(getNewTypes());
124     }
125
126     public HelpCtx getHelpCtx() {
127         return new HelpCtx(NewAction.class);
128     }
129
130     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
131         return new Actions.SubMenu(this, model, false);
132     }
133     
134     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
135         return new Actions.SubMenu(this, model, true);
136     }
137
138     public javax.swing.Action JavaDoc createContextAwareInstance(Lookup actionContext) {
139         return new DelegateAction(this, actionContext);
140     }
141
142     /** Utility method, creates name for action depending on specified new types. */
143     private static String JavaDoc createName(NewType[] newTypes) {
144         if ((newTypes != null) && (newTypes.length == 1)) {
145             return NbBundle.getMessage(NewAction.class, "NewArg", newTypes[0].getName());
146         } else {
147             return NbBundle.getMessage(NewAction.class, "New");
148         }
149     }
150
151     /** Implementation of ActSubMenuInt */
152     private static class ActSubMenuModel extends EventListenerList JavaDoc implements Actions.SubMenuModel {
153         static final long serialVersionUID = -4273674308662494596L;
154
155         /** lookup to read the new types from or null if they whould be taken
156          * directly from top component's selected nodes
157          */

158         private Lookup lookup;
159         
160         private Node prevNode;
161         private NewType[] prevTypes;
162
163         ActSubMenuModel(Lookup lookup) {
164             this.lookup = lookup;
165         }
166
167         private NewType[] newTypes() {
168             if (lookup != null) {
169                 java.util.Collection JavaDoc c = lookup.lookupResult(Node.class).allItems();
170
171                 if (c.size() == 1) {
172                     java.util.Iterator JavaDoc it = c.iterator();
173
174                     while (it.hasNext()) {
175                         Lookup.Item item = (Lookup.Item) it.next();
176                         Node n = (Node) item.getInstance();
177
178                         if (n != null) {
179                             if (n == prevNode && prevTypes != null) {
180                                 return prevTypes;
181                             }
182                             prevNode = n;
183                             prevTypes = n.getNewTypes();
184                             return prevTypes;
185                         }
186                     }
187                 }
188             }
189
190             return getNewTypes();
191         }
192
193         public int getCount() {
194             return newTypes().length;
195         }
196
197         public String JavaDoc getLabel(int index) {
198             NewType[] newTypes = newTypes();
199
200             if (newTypes.length <= index) {
201                 return null;
202             } else {
203                 return newTypes[index].getName();
204             }
205         }
206
207         public HelpCtx getHelpCtx(int index) {
208             NewType[] newTypes = newTypes();
209
210             if (newTypes.length <= index) {
211                 return null;
212             } else {
213                 return newTypes[index].getHelpCtx();
214             }
215         }
216
217         public void performActionAt(int index) {
218             NewType[] nt = newTypes();
219
220             // bugfix #41587, AIOBE if array is empty
221
if (nt.length <= index) {
222                 return;
223             }
224
225             Node[] arr;
226
227             if (lookup != null) {
228                 arr = (Node[]) lookup.lookupAll(Node.class).toArray(new Node[0]);
229             } else {
230                 arr = WindowManager.getDefault().getRegistry().getCurrentNodes();
231             }
232
233             performAction(arr, nt[index]);
234         }
235
236         /** Adds change listener for changes of the model.
237         */

238         public void addChangeListener(ChangeListener JavaDoc l) {
239             add(ChangeListener JavaDoc.class, l);
240         }
241
242         /** Removes change listener for changes of the model.
243         */

244         public void removeChangeListener(ChangeListener JavaDoc l) {
245             remove(ChangeListener JavaDoc.class, l);
246         }
247     }
248      // end of ActSubMenuModel
249

250     /** A delegate action that is usually associated with a specific lookup and
251      * extract the nodes it operates on from it. Otherwise it delegates to the
252      * regular NodeAction.
253      */

254     private static final class DelegateAction extends Object JavaDoc implements javax.swing.Action JavaDoc, Presenter.Menu,
255         Presenter.Popup {
256         /** Action to delegate to. */
257         private final NodeAction delegate;
258
259         /** Associated model to use. */
260         private final ActSubMenuModel model;
261
262         public DelegateAction(NodeAction a, Lookup actionContext) {
263             this.delegate = a;
264             this.model = new ActSubMenuModel(actionContext);
265         }
266
267         /** Overrides superclass method, adds delegate description. */
268         public String JavaDoc toString() {
269             return super.toString() + "[delegate=" + delegate + "]"; // NOI18N
270
}
271
272         /** Invoked when an action occurs.
273          */

274         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
275             model.performActionAt(0);
276         }
277
278         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
279         }
280
281         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
282         }
283
284         public void putValue(String JavaDoc key, Object JavaDoc o) {
285         }
286
287         public Object JavaDoc getValue(String JavaDoc key) {
288             if (javax.swing.Action.NAME.equals(key)) {
289                 // #30266 Name of action depends on new types.
290
return createName(model.newTypes());
291             } else {
292                 return delegate.getValue(key);
293             }
294         }
295
296         public boolean isEnabled() {
297             return model.getCount() > 0;
298         }
299
300         public void setEnabled(boolean b) {
301         }
302
303         public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
304             return new Actions.SubMenu(this, model, false);
305         }
306
307         public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
308             return new Actions.SubMenu(this, model, true);
309         }
310     }
311      // end of DelegateAction
312
}
313
Popular Tags