KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > customcommands > NewCommandAction


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.netbeans.modules.ant.freeform.customcommands;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.spi.project.ActionProvider;
37 import org.openide.DialogDisplayer;
38 import org.openide.ErrorManager;
39 import org.openide.WizardDescriptor;
40 import org.openide.awt.DynamicMenuContent;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataFolder;
44 import org.openide.util.ContextAwareAction;
45 import org.openide.util.Lookup;
46 import org.openide.util.NbBundle;
47 import org.openide.util.actions.Presenter;
48 import org.openide.xml.XMLUtil;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Element JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54
55 public final class NewCommandAction extends AbstractAction JavaDoc implements ContextAwareAction {
56     
57     public NewCommandAction() {
58         // Label not likely displayed in GUI, but used also from ContextAction.
59
super(NbBundle.getMessage(NewCommandAction.class, "LBL_action"));
60     }
61     
62     public void actionPerformed(ActionEvent JavaDoc e) {
63         // Cannot be invoked without any context.
64
assert false;
65     }
66     
67     public Action JavaDoc createContextAwareInstance(Lookup context) {
68         return new ContextAction(context);
69     }
70     
71     private WizardDescriptor.Panel[] panels;
72     
73     private void run(String JavaDoc[] likelyCommandNames) {
74         WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(likelyCommandNames));
75         wizardDescriptor.setTitleFormat(new MessageFormat JavaDoc("{0}"));
76         wizardDescriptor.setTitle("Create Custom Project Command"); // XXX I18N
77
Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
78         dialog.setVisible(true);
79         dialog.toFront();
80         boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
81         if (cancelled) {
82             return;
83         }
84         String JavaDoc command = (String JavaDoc) wizardDescriptor.getProperty("command"); // NOI18N
85
String JavaDoc displayName = (String JavaDoc) wizardDescriptor.getProperty("displayName"); // NOI18N
86
String JavaDoc menu = (String JavaDoc) wizardDescriptor.getProperty("menu"); // NOI18N
87
int position = ((Integer JavaDoc) wizardDescriptor.getProperty("position")).intValue();
88         DataFolder menuFolder = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + menu)); // NOI18N
89
try {
90             new Command(command, displayName, null).create(menuFolder, position);
91         } catch (IOException JavaDoc e) {
92             ErrorManager.getDefault().notify(e);
93         }
94     }
95     
96     private WizardDescriptor.Panel[] getPanels(String JavaDoc[] likelyCommandNames) {
97         if (panels == null) {
98             panels = new WizardDescriptor.Panel[] {
99                 new NewCommandWizardPanel(likelyCommandNames),
100             };
101             String JavaDoc[] steps = new String JavaDoc[panels.length];
102             for (int i = 0; i < panels.length; i++) {
103                 Component JavaDoc c = panels[i].getComponent();
104                 steps[i] = c.getName();
105                 if (c instanceof JComponent JavaDoc) {
106                     JComponent JavaDoc jc = (JComponent JavaDoc) c;
107                     jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i));
108                     jc.putClientProperty("WizardPanel_contentData", steps);
109                     jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
110                     jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
111                     jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
112                 }
113             }
114         }
115         return panels;
116     }
117     
118     private static final Set JavaDoc/*<String>*/ STANDARD_COMMANDS = new HashSet JavaDoc();
119     static {
120         STANDARD_COMMANDS.add(ActionProvider.COMMAND_COMPILE_SINGLE);
121         STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_SINGLE);
122         STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_STEP_INTO);
123         STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_TEST_SINGLE);
124         STANDARD_COMMANDS.add(ActionProvider.COMMAND_RUN_SINGLE);
125         STANDARD_COMMANDS.add(ActionProvider.COMMAND_TEST_SINGLE);
126         STANDARD_COMMANDS.add("debug.fix"); // XXX no def?
127
}
128
129     private static String JavaDoc[] findLikelyCommandNames(Project p) throws IOException JavaDoc, SAXException JavaDoc {
130         if (p == null) {
131             return null; // #72266
132
}
133         FileObject projectXml = p.getProjectDirectory().getFileObject("nbproject/project.xml"); // NOI18N
134
if (projectXml == null) {
135             return null;
136         }
137         Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(projectXml.getURL().toExternalForm()), false, false, null, null);
138         NodeList JavaDoc actions = doc.getElementsByTagName("action"); // NOI18N
139
SortedSet JavaDoc/*<String>*/ commands = new TreeSet JavaDoc();
140         for (int i = 0; i < actions.getLength(); i++) {
141             Element JavaDoc action = (Element JavaDoc) actions.item(i);
142             String JavaDoc command = action.getAttribute("name"); // NOI18N
143
if (command == null) {
144                 // Might be in <context-menu> rather than <ide-actions>; ignore.
145
continue;
146             }
147             if (STANDARD_COMMANDS.contains(command)) {
148                 continue;
149             }
150             if (action.getElementsByTagName("context").getLength() == 0) { // NOI18N
151
// Not context-sensitive; ignore.
152
continue;
153             }
154             commands.add(command);
155         }
156         return commands.isEmpty() ? null : (String JavaDoc[]) commands.toArray(new String JavaDoc[commands.size()]);
157     }
158         
159     private final class ContextAction extends AbstractAction JavaDoc implements Presenter.Popup {
160         
161         private String JavaDoc[] likelyCommandNames;
162         
163         public ContextAction(Lookup context) {
164             super((String JavaDoc) NewCommandAction.this.getValue(Action.NAME));
165             Project p = (Project) context.lookup(Project.class);
166             try {
167                 likelyCommandNames = findLikelyCommandNames(p);
168             } catch (IOException JavaDoc e) {
169                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
170                 likelyCommandNames = null;
171             } catch (SAXException JavaDoc e) {
172                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
173                 likelyCommandNames = null;
174             }
175         }
176         
177         public void actionPerformed(ActionEvent JavaDoc e) {
178             assert likelyCommandNames != null;
179             run(likelyCommandNames);
180         }
181         
182         public JMenuItem JavaDoc getPopupPresenter() {
183             class Presenter extends JMenuItem JavaDoc implements DynamicMenuContent {
184                 public Presenter() {
185                     super(ContextAction.this);
186                 }
187                 public JComponent JavaDoc[] getMenuPresenters() {
188                     if (likelyCommandNames != null) {
189                         return new JComponent JavaDoc[] {this};
190                     } else {
191                         // Disabled, so do not display at all.
192
return new JComponent JavaDoc[0];
193                     }
194                 }
195                 public JComponent JavaDoc[] synchMenuPresenters(JComponent JavaDoc[] items) {
196                     return getMenuPresenters();
197                 }
198             }
199             return new Presenter();
200         }
201
202     }
203     
204 }
205
Popular Tags