KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > RunTargetsAction


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.debugger;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.text.Collator JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.SortedSet JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import javax.swing.AbstractAction JavaDoc;
33 import javax.swing.Action JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JMenu JavaDoc;
36 import javax.swing.JMenuItem JavaDoc;
37 import javax.swing.JPopupMenu JavaDoc;
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 /**
57  * Submenu which permits the user to run various targets from the project.
58  * Distinction made between the main target, other documented targets, and other
59  * undocumented targets.
60  */

61 public final class RunTargetsAction extends SystemAction implements ContextAwareAction {
62
63     public String JavaDoc 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 JavaDoc e) {
72         assert false : "Action should never be called without a context";
73     }
74
75     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
76         return new ContextAction(actionContext);
77     }
78     
79     /**
80      * The particular instance of this action for a given project.
81      */

82     private static final class ContextAction extends AbstractAction JavaDoc implements Presenter.Popup {
83         
84         private final AntProjectCookie project;
85         
86         public ContextAction(Lookup lkp) {
87             super(SystemAction.get(RunTargetsAction.class).getName());
88             Collection JavaDoc/*<AntProjectCookie>*/ 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 JavaDoc e) {
101             assert false : "Action should not be called directly";
102         }
103
104         public JMenuItem JavaDoc 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     /**
119      * Create the submenu.
120      */

121     private static JMenu JavaDoc createMenu(AntProjectCookie project) {
122         return new LazyMenu(project);
123     }
124     
125     private static final class LazyMenu extends JMenu JavaDoc {
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 JavaDoc getPopupMenu() {
136             if (!initialized) {
137                 initialized = true;
138                 Set JavaDoc/*<TargetLister.Target>*/ allTargets;
139                 try {
140                     allTargets = TargetLister.getTargets(project);
141                 } catch (IOException JavaDoc e) {
142                     DialogDisplayer.getDefault().notify(
143                             new NotifyDescriptor.Message(e.getLocalizedMessage()));
144                     allTargets = Collections.EMPTY_SET;
145                 }
146                 String JavaDoc defaultTarget = null;
147                 SortedSet JavaDoc/*<String>*/ describedTargets = new TreeSet JavaDoc(Collator.getInstance());
148                 SortedSet JavaDoc/*<String>*/ otherTargets = new TreeSet JavaDoc(Collator.getInstance());
149                 Iterator JavaDoc it = allTargets.iterator();
150                 while (it.hasNext()) {
151                     TargetLister.Target t = (TargetLister.Target) it.next();
152                     if (t.isOverridden()) {
153                         // Cannot be called.
154
continue;
155                     }
156                     if (t.isInternal()) {
157                         // Don't present in GUI.
158
continue;
159                     }
160                     String JavaDoc 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 JavaDoc menuitem = new JMenuItem JavaDoc(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 JavaDoc target = (String JavaDoc) it.next();
185                         JMenuItem JavaDoc menuitem = new JMenuItem JavaDoc(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 JavaDoc submenu = new JMenu JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_other_targets"));
197                     it = otherTargets.iterator();
198                     while (it.hasNext()) {
199                         String JavaDoc target = (String JavaDoc) it.next();
200                         JMenuItem JavaDoc menuitem = new JMenuItem JavaDoc(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     /**
218      * Action handler for a menu item representing one target.
219      */

220     private static final class TargetMenuItemHandler implements ActionListener JavaDoc, Runnable JavaDoc {
221         
222         private final AntProjectCookie project;
223         private final String JavaDoc target;
224         
225         public TargetMenuItemHandler(AntProjectCookie project, String JavaDoc target) {
226             this.project = project;
227             this.target = target;
228         }
229         
230         public void actionPerformed(ActionEvent JavaDoc ev) {
231             // #16720 part 2: don't do this in the event thread...
232
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 JavaDoc[] {target});
243                 DebuggerAntLogger.getDefault().fileExecutor(project.getFile(), executorTask);
244             } catch (IOException JavaDoc ioe) {
245                 AntModule.err.notify(ioe);
246             }
247         }
248         
249     }
250     
251     /**
252      * Menu item to let the user select a random target(s), and set properties and verbosity.
253      */

254     private static final class AdvancedAction extends AbstractAction JavaDoc {
255         
256         private final AntProjectCookie project;
257         private final Set JavaDoc/*<TargetLister.Target>*/ allTargets;
258         
259         public AdvancedAction(AntProjectCookie project, Set JavaDoc/*<TargetLister.Target>*/ 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 JavaDoc e) {
266             String JavaDoc 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 JavaDoc run = new JButton JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_run"));
271             run.setDefaultCapable(true);
272             JButton JavaDoc cancel = new JButton JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_cancel"));
273             dd.setOptions(new Object JavaDoc[] {run, cancel});
274             dd.setModal(true);
275             Object JavaDoc result = DialogDisplayer.getDefault().notify(dd);
276             if (result.equals(run)) {
277                 try {
278                     panel.run();
279                 } catch (IOException JavaDoc x) {
280                     AntModule.err.notify(x);
281                 }
282             }
283         }
284         
285     }
286     
287 }
288
Popular Tags