2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.ant.freeform.customcommands;
44
import java.awt.Component;
45
import java.awt.Dialog;
46
import java.awt.event.ActionEvent;
47
import java.io.IOException;
48
import java.text.MessageFormat;
49
import java.util.HashSet;
51
import java.util.SortedSet;
52
import java.util.TreeSet;
53
import javax.swing.AbstractAction;
54
import javax.swing.Action;
55
import javax.swing.JComponent;
56
import javax.swing.JMenuItem;
57
import org.netbeans.api.project.Project;
58
import org.netbeans.spi.project.ActionProvider;
59
import org.openide.DialogDisplayer;
60
import org.openide.ErrorManager;
61
import org.openide.WizardDescriptor;
62
import org.openide.awt.DynamicMenuContent;
63
import org.openide.filesystems.FileObject;
64
import org.openide.filesystems.Repository;
65
import org.openide.loaders.DataFolder;
66
import org.openide.util.ContextAwareAction;
67
import org.openide.util.Lookup;
68
import org.openide.util.NbBundle;
69
import org.openide.util.actions.Presenter;
70
import org.openide.xml.XMLUtil;
71
import org.w3c.dom.Document;
72
import org.w3c.dom.Element;
73
import org.w3c.dom.NodeList;
74
import org.xml.sax.InputSource;
75
import org.xml.sax.SAXException;
77
public final class NewCommandAction extends AbstractAction implements ContextAwareAction {
79
public NewCommandAction() {
80
// Label not likely displayed in GUI, but used also from ContextAction.
81
super(NbBundle.getMessage(NewCommandAction.class, "LBL_action"));
84
public void actionPerformed(ActionEvent e) {
85
// Cannot be invoked without any context.
89
public Action createContextAwareInstance(Lookup context) {
90
return new ContextAction(context);
93
private WizardDescriptor.Panel[] panels;
95
private void run(String[] likelyCommandNames) {
96
WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(likelyCommandNames));
97
wizardDescriptor.setTitleFormat(new MessageFormat("{0}"));
98
wizardDescriptor.setTitle("Create Custom Project Command"); // XXX I18N
99
Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor);
100
dialog.setVisible(true);
102
boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION;
106
String command = (String) wizardDescriptor.getProperty("command"); // NOI18N
107
String displayName = (String) wizardDescriptor.getProperty("displayName"); // NOI18N
108
String menu = (String) wizardDescriptor.getProperty("menu"); // NOI18N
109
int position = ((Integer) wizardDescriptor.getProperty("position")).intValue();
110
DataFolder menuFolder = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + menu)); // NOI18N
112
new Command(command, displayName, null).create(menuFolder, position);
113
} catch (IOException e) {
114
ErrorManager.getDefault().notify(e);
118
private WizardDescriptor.Panel[] getPanels(String[] likelyCommandNames) {
119
if (panels == null) {
120
panels = new WizardDescriptor.Panel[] {
121
new NewCommandWizardPanel(likelyCommandNames),
123
String[] steps = new String[panels.length];
124
for (int i = 0; i < panels.length; i++) {
125
Component c = panels[i].getComponent();
126
steps[i] = c.getName();
127
if (c instanceof JComponent) {
128
JComponent jc = (JComponent) c;
129
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer(i));
130
jc.putClientProperty("WizardPanel_contentData", steps);
131
jc.putClientProperty("WizardPanel_autoWizardStyle", Boolean.TRUE);
132
jc.putClientProperty("WizardPanel_contentDisplayed", Boolean.TRUE);
133
jc.putClientProperty("WizardPanel_contentNumbered", Boolean.TRUE);
140
private static final Set/*<String>*/ STANDARD_COMMANDS = new HashSet();
142
STANDARD_COMMANDS.add(ActionProvider.COMMAND_COMPILE_SINGLE);
143
STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_SINGLE);
144
STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_STEP_INTO);
145
STANDARD_COMMANDS.add(ActionProvider.COMMAND_DEBUG_TEST_SINGLE);
146
STANDARD_COMMANDS.add(ActionProvider.COMMAND_RUN_SINGLE);
147
STANDARD_COMMANDS.add(ActionProvider.COMMAND_TEST_SINGLE);
148
STANDARD_COMMANDS.add("debug.fix"); // XXX no def?
151
private static String[] findLikelyCommandNames(Project p) throws IOException, SAXException {
153
return null; // #72266
155
FileObject projectXml = p.getProjectDirectory().getFileObject("nbproject/project.xml"); // NOI18N
156
if (projectXml == null) {
159
Document doc = XMLUtil.parse(new InputSource(projectXml.getURL().toExternalForm()), false, false, null, null);
160
NodeList actions = doc.getElementsByTagName("action"); // NOI18N
161
SortedSet/*<String>*/ commands = new TreeSet();
162
for (int i = 0; i < actions.getLength(); i++) {
163
Element action = (Element) actions.item(i);
164
String command = action.getAttribute("name"); // NOI18N
165
if (command == null) {
166
// Might be in <context-menu> rather than <ide-actions>; ignore.
169
if (STANDARD_COMMANDS.contains(command)) {
172
if (action.getElementsByTagName("context").getLength() == 0) { // NOI18N
173
// Not context-sensitive; ignore.
176
commands.add(command);
178
return commands.isEmpty() ? null : (String[]) commands.toArray(new String[commands.size()]);
181
private final class ContextAction extends AbstractAction implements Presenter.Popup {
183
private String[] likelyCommandNames;
185
public ContextAction(Lookup context) {
186
super((String) NewCommandAction.this.getValue(Action.NAME));
187
Project p = (Project) context.lookup(Project.class);
189
likelyCommandNames = findLikelyCommandNames(p);
190
} catch (IOException e) {
191
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
192
likelyCommandNames = null;
193
} catch (SAXException e) {
194
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
195
likelyCommandNames = null;
199
public void actionPerformed(ActionEvent e) {
200
assert likelyCommandNames != null;
201
run(likelyCommandNames);
204
public JMenuItem getPopupPresenter() {
205
class Presenter extends JMenuItem implements DynamicMenuContent {
207
super(ContextAction.this);
209
public JComponent[] getMenuPresenters() {
210
if (likelyCommandNames != null) {
211
return new JComponent[] {this};
213
// Disabled, so do not display at all.
214
return new JComponent[0];
217
public JComponent[] synchMenuPresenters(JComponent[] items) {
218
return getMenuPresenters();
221
return new Presenter();