1 19 20 package org.apache.tools.ant.module.nodes; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.io.IOException ; 25 import java.text.Collator ; 26 import java.util.Collection ; 27 import java.util.Collections ; 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.JButton ; 34 import javax.swing.JMenu ; 35 import javax.swing.JMenuItem ; 36 import javax.swing.JPopupMenu ; 37 import org.apache.tools.ant.module.AntModule; 38 import org.apache.tools.ant.module.api.AntProjectCookie; 39 import org.apache.tools.ant.module.api.support.TargetLister; 40 import org.apache.tools.ant.module.run.TargetExecutor; 41 import org.openide.DialogDescriptor; 42 import org.openide.DialogDisplayer; 43 import org.openide.ErrorManager; 44 import org.openide.NotifyDescriptor; 45 import org.openide.awt.Actions; 46 import org.openide.util.ContextAwareAction; 47 import org.openide.util.HelpCtx; 48 import org.openide.util.Lookup; 49 import org.openide.util.NbBundle; 50 import org.openide.util.RequestProcessor; 51 import org.openide.util.actions.Presenter; 52 import org.openide.util.actions.SystemAction; 53 54 59 public final class RunTargetsAction extends SystemAction implements ContextAwareAction { 60 61 @Override 62 public String getName () { 63 return NbBundle.getMessage (RunTargetsAction.class, "LBL_run_targets_action"); 64 } 65 66 @Override 67 public HelpCtx getHelpCtx () { 68 return HelpCtx.DEFAULT_HELP; 69 } 70 71 @Override 72 public void actionPerformed(ActionEvent e) { 73 assert false : "Action should never be called without a context"; 74 } 75 76 public Action createContextAwareInstance(Lookup actionContext) { 77 return new ContextAction(actionContext); 78 } 79 80 83 private static final class ContextAction extends AbstractAction implements Presenter.Popup { 84 85 private final AntProjectCookie project; 86 87 public ContextAction(Lookup lkp) { 88 super(SystemAction.get(RunTargetsAction.class).getName()); 89 Collection <? extends AntProjectCookie> apcs = lkp.lookupAll(AntProjectCookie.class); 90 AntProjectCookie _project = null; 91 if (apcs.size() == 1) { 92 _project = apcs.iterator().next(); 93 if (_project.getParseException() != null) { 94 _project = null; 95 } 96 } 97 project = _project; 98 super.setEnabled(project != null); 99 } 100 101 public void actionPerformed(ActionEvent e) { 102 assert false : "Action should not be called directly"; 103 } 104 105 public JMenuItem getPopupPresenter() { 106 if (project != null) { 107 return createMenu(project); 108 } else { 109 return new Actions.MenuItem(this, false); 110 } 111 } 112 113 @Override 114 public void setEnabled(boolean b) { 115 assert false : "No modifications to enablement status permitted"; 116 } 117 118 } 119 120 123 private static JMenu createMenu(AntProjectCookie project) { 124 return new LazyMenu(project); 125 } 126 127 private static final class LazyMenu extends JMenu { 128 129 private final AntProjectCookie project; 130 private boolean initialized = false; 131 132 public LazyMenu(AntProjectCookie project) { 133 super(SystemAction.get(RunTargetsAction.class).getName()); 134 this.project = project; 135 } 136 137 @Override 138 public JPopupMenu getPopupMenu() { 139 if (!initialized) { 140 initialized = true; 141 Set <TargetLister.Target> allTargets; 142 try { 143 allTargets = TargetLister.getTargets(project); 144 } catch (IOException e) { 145 AntModule.err.notify(ErrorManager.INFORMATIONAL, e); 147 allTargets = Collections.emptySet(); 148 } 149 String defaultTarget = null; 150 SortedSet <String > describedTargets = new TreeSet <String >(Collator.getInstance()); 151 SortedSet <String > otherTargets = new TreeSet <String >(Collator.getInstance()); 152 for (TargetLister.Target t : allTargets) { 153 if (t.isOverridden()) { 154 continue; 156 } 157 if (t.isInternal()) { 158 continue; 160 } 161 String name = t.getName(); 162 if (t.isDefault()) { 163 defaultTarget = name; 164 } else if (t.isDescribed()) { 165 describedTargets.add(name); 166 } else { 167 otherTargets.add(name); 168 } 169 } 170 boolean needsep = false; 171 if (defaultTarget != null) { 172 needsep = true; 173 JMenuItem menuitem = new JMenuItem (defaultTarget); 174 menuitem.addActionListener(new TargetMenuItemHandler(project, defaultTarget)); 175 add(menuitem); 176 } 177 if (needsep) { 178 needsep = false; 179 addSeparator(); 180 } 181 if (!describedTargets.isEmpty()) { 182 needsep = true; 183 for (String target : describedTargets) { 184 JMenuItem menuitem = new JMenuItem (target); 185 menuitem.addActionListener(new TargetMenuItemHandler(project, target)); 186 add(menuitem); 187 } 188 } 189 if (needsep) { 190 needsep = false; 191 addSeparator(); 192 } 193 if (!otherTargets.isEmpty()) { 194 needsep = true; 195 JMenu submenu = new JMenu (NbBundle.getMessage(RunTargetsAction.class, "LBL_run_other_targets")); 196 for (String target : otherTargets) { 197 JMenuItem menuitem = new JMenuItem (target); 198 menuitem.addActionListener(new TargetMenuItemHandler(project, target)); 199 submenu.add(menuitem); 200 } 201 add(submenu); 202 } 203 if (needsep) { 204 needsep = false; 205 addSeparator(); 206 } 207 add(new AdvancedAction(project, allTargets)); 208 } 209 return super.getPopupMenu(); 210 } 211 212 } 213 214 217 private static final class TargetMenuItemHandler implements ActionListener , Runnable { 218 219 private final AntProjectCookie project; 220 private final String target; 221 222 public TargetMenuItemHandler(AntProjectCookie project, String target) { 223 this.project = project; 224 this.target = target; 225 } 226 227 public void actionPerformed(ActionEvent ev) { 228 RequestProcessor.getDefault().post(this); 230 } 231 232 public void run() { 233 try { 234 TargetExecutor te = new TargetExecutor(project, new String [] {target}); 235 te.execute(); 236 } catch (IOException ioe) { 237 AntModule.err.notify(ioe); 238 } 239 } 240 241 } 242 243 246 private static final class AdvancedAction extends AbstractAction { 247 248 private final AntProjectCookie project; 249 private final Set <TargetLister.Target> allTargets; 250 251 public AdvancedAction(AntProjectCookie project, Set <TargetLister.Target> allTargets) { 252 super(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced")); 253 this.project = project; 254 this.allTargets = allTargets; 255 } 256 257 public void actionPerformed(ActionEvent e) { 258 String title = NbBundle.getMessage(RunTargetsAction.class, "TITLE_run_advanced"); 259 AdvancedActionPanel panel = new AdvancedActionPanel(project, allTargets); 260 DialogDescriptor dd = new DialogDescriptor(panel, title); 261 dd.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION); 262 JButton run = new JButton (NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_run")); 263 run.setDefaultCapable(true); 264 JButton cancel = new JButton (NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_cancel")); 265 dd.setOptions(new Object [] {run, cancel}); 266 dd.setModal(true); 267 Object result = DialogDisplayer.getDefault().notify(dd); 268 if (result.equals(run)) { 269 try { 270 panel.run(); 271 } catch (IOException x) { 272 AntModule.err.notify(x); 273 } 274 } 275 } 276 277 } 278 279 } 280 | Popular Tags |