1 19 20 package org.netbeans.modules.ant.freeform.customcommands; 21 22 import java.awt.Component ; 23 import java.awt.Dialog ; 24 import java.awt.event.ActionEvent ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.HashSet ; 28 import java.util.Set ; 29 import java.util.SortedSet ; 30 import java.util.TreeSet ; 31 import javax.swing.AbstractAction ; 32 import javax.swing.Action ; 33 import javax.swing.JComponent ; 34 import javax.swing.JMenuItem ; 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 ; 50 import org.w3c.dom.Element ; 51 import org.w3c.dom.NodeList ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 55 public final class NewCommandAction extends AbstractAction implements ContextAwareAction { 56 57 public NewCommandAction() { 58 super(NbBundle.getMessage(NewCommandAction.class, "LBL_action")); 60 } 61 62 public void actionPerformed(ActionEvent e) { 63 assert false; 65 } 66 67 public Action createContextAwareInstance(Lookup context) { 68 return new ContextAction(context); 69 } 70 71 private WizardDescriptor.Panel[] panels; 72 73 private void run(String [] likelyCommandNames) { 74 WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(likelyCommandNames)); 75 wizardDescriptor.setTitleFormat(new MessageFormat ("{0}")); 76 wizardDescriptor.setTitle("Create Custom Project Command"); Dialog 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 command = (String ) wizardDescriptor.getProperty("command"); String displayName = (String ) wizardDescriptor.getProperty("displayName"); String menu = (String ) wizardDescriptor.getProperty("menu"); int position = ((Integer ) wizardDescriptor.getProperty("position")).intValue(); 88 DataFolder menuFolder = DataFolder.findFolder(Repository.getDefault().getDefaultFileSystem().findResource("Menu/" + menu)); try { 90 new Command(command, displayName, null).create(menuFolder, position); 91 } catch (IOException e) { 92 ErrorManager.getDefault().notify(e); 93 } 94 } 95 96 private WizardDescriptor.Panel[] getPanels(String [] likelyCommandNames) { 97 if (panels == null) { 98 panels = new WizardDescriptor.Panel[] { 99 new NewCommandWizardPanel(likelyCommandNames), 100 }; 101 String [] steps = new String [panels.length]; 102 for (int i = 0; i < panels.length; i++) { 103 Component c = panels[i].getComponent(); 104 steps[i] = c.getName(); 105 if (c instanceof JComponent ) { 106 JComponent jc = (JComponent ) c; 107 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (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 STANDARD_COMMANDS = new HashSet (); 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"); } 128 129 private static String [] findLikelyCommandNames(Project p) throws IOException , SAXException { 130 if (p == null) { 131 return null; } 133 FileObject projectXml = p.getProjectDirectory().getFileObject("nbproject/project.xml"); if (projectXml == null) { 135 return null; 136 } 137 Document doc = XMLUtil.parse(new InputSource (projectXml.getURL().toExternalForm()), false, false, null, null); 138 NodeList actions = doc.getElementsByTagName("action"); SortedSet commands = new TreeSet (); 140 for (int i = 0; i < actions.getLength(); i++) { 141 Element action = (Element ) actions.item(i); 142 String command = action.getAttribute("name"); if (command == null) { 144 continue; 146 } 147 if (STANDARD_COMMANDS.contains(command)) { 148 continue; 149 } 150 if (action.getElementsByTagName("context").getLength() == 0) { continue; 153 } 154 commands.add(command); 155 } 156 return commands.isEmpty() ? null : (String []) commands.toArray(new String [commands.size()]); 157 } 158 159 private final class ContextAction extends AbstractAction implements Presenter.Popup { 160 161 private String [] likelyCommandNames; 162 163 public ContextAction(Lookup context) { 164 super((String ) NewCommandAction.this.getValue(Action.NAME)); 165 Project p = (Project) context.lookup(Project.class); 166 try { 167 likelyCommandNames = findLikelyCommandNames(p); 168 } catch (IOException e) { 169 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 170 likelyCommandNames = null; 171 } catch (SAXException e) { 172 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 173 likelyCommandNames = null; 174 } 175 } 176 177 public void actionPerformed(ActionEvent e) { 178 assert likelyCommandNames != null; 179 run(likelyCommandNames); 180 } 181 182 public JMenuItem getPopupPresenter() { 183 class Presenter extends JMenuItem implements DynamicMenuContent { 184 public Presenter() { 185 super(ContextAction.this); 186 } 187 public JComponent [] getMenuPresenters() { 188 if (likelyCommandNames != null) { 189 return new JComponent [] {this}; 190 } else { 191 return new JComponent [0]; 193 } 194 } 195 public JComponent [] synchMenuPresenters(JComponent [] items) { 196 return getMenuPresenters(); 197 } 198 } 199 return new Presenter(); 200 } 201 202 } 203 204 } 205 | Popular Tags |