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