KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > nodes > 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.apache.tools.ant.module.nodes;
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.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import javax.swing.AbstractAction JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JMenu JavaDoc;
35 import javax.swing.JMenuItem JavaDoc;
36 import javax.swing.JPopupMenu JavaDoc;
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 /**
55  * Submenu which permits the user to run various targets from the project.
56  * Distinction made between the main target, other documented targets, and other
57  * undocumented targets.
58  */

59 public final class RunTargetsAction extends SystemAction implements ContextAwareAction {
60
61     @Override JavaDoc
62     public String JavaDoc getName () {
63         return NbBundle.getMessage (RunTargetsAction.class, "LBL_run_targets_action");
64     }
65
66     @Override JavaDoc
67     public HelpCtx getHelpCtx () {
68         return HelpCtx.DEFAULT_HELP;
69     }
70
71     @Override JavaDoc
72     public void actionPerformed(ActionEvent JavaDoc e) {
73         assert false : "Action should never be called without a context";
74     }
75
76     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
77         return new ContextAction(actionContext);
78     }
79     
80     /**
81      * The particular instance of this action for a given project.
82      */

83     private static final class ContextAction extends AbstractAction JavaDoc implements Presenter.Popup {
84         
85         private final AntProjectCookie project;
86         
87         public ContextAction(Lookup lkp) {
88             super(SystemAction.get(RunTargetsAction.class).getName());
89             Collection JavaDoc<? 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 JavaDoc e) {
102             assert false : "Action should not be called directly";
103         }
104
105         public JMenuItem JavaDoc getPopupPresenter() {
106             if (project != null) {
107                 return createMenu(project);
108             } else {
109                 return new Actions.MenuItem(this, false);
110             }
111         }
112
113         @Override JavaDoc
114         public void setEnabled(boolean b) {
115             assert false : "No modifications to enablement status permitted";
116         }
117         
118     }
119
120     /**
121      * Create the submenu.
122      */

123     private static JMenu JavaDoc createMenu(AntProjectCookie project) {
124         return new LazyMenu(project);
125     }
126     
127     private static final class LazyMenu extends JMenu JavaDoc {
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 JavaDoc
138         public JPopupMenu JavaDoc getPopupMenu() {
139             if (!initialized) {
140                 initialized = true;
141                 Set JavaDoc<TargetLister.Target> allTargets;
142                 try {
143                     allTargets = TargetLister.getTargets(project);
144                 } catch (IOException JavaDoc e) {
145                     // XXX how to notify properly?
146
AntModule.err.notify(ErrorManager.INFORMATIONAL, e);
147                     allTargets = Collections.emptySet();
148                 }
149                 String JavaDoc defaultTarget = null;
150                 SortedSet JavaDoc<String JavaDoc> describedTargets = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
151                 SortedSet JavaDoc<String JavaDoc> otherTargets = new TreeSet JavaDoc<String JavaDoc>(Collator.getInstance());
152                 for (TargetLister.Target t : allTargets) {
153                     if (t.isOverridden()) {
154                         // Cannot be called.
155
continue;
156                     }
157                     if (t.isInternal()) {
158                         // Don't present in GUI.
159
continue;
160                     }
161                     String JavaDoc 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 JavaDoc menuitem = new JMenuItem JavaDoc(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 JavaDoc target : describedTargets) {
184                         JMenuItem JavaDoc menuitem = new JMenuItem JavaDoc(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 JavaDoc submenu = new JMenu JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_other_targets"));
196                     for (String JavaDoc target : otherTargets) {
197                         JMenuItem JavaDoc menuitem = new JMenuItem JavaDoc(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     /**
215      * Action handler for a menu item representing one target.
216      */

217     private static final class TargetMenuItemHandler implements ActionListener JavaDoc, Runnable JavaDoc {
218         
219         private final AntProjectCookie project;
220         private final String JavaDoc target;
221         
222         public TargetMenuItemHandler(AntProjectCookie project, String JavaDoc target) {
223             this.project = project;
224             this.target = target;
225         }
226         
227         public void actionPerformed(ActionEvent JavaDoc ev) {
228             // #16720 part 2: don't do this in the event thread...
229
RequestProcessor.getDefault().post(this);
230         }
231         
232         public void run() {
233             try {
234                 TargetExecutor te = new TargetExecutor(project, new String JavaDoc[] {target});
235                 te.execute();
236             } catch (IOException JavaDoc ioe) {
237                 AntModule.err.notify(ioe);
238             }
239         }
240         
241     }
242     
243     /**
244      * Menu item to let the user select a random target(s), and set properties and verbosity.
245      */

246     private static final class AdvancedAction extends AbstractAction JavaDoc {
247         
248         private final AntProjectCookie project;
249         private final Set JavaDoc<TargetLister.Target> allTargets;
250         
251         public AdvancedAction(AntProjectCookie project, Set JavaDoc<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 JavaDoc e) {
258             String JavaDoc 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 JavaDoc run = new JButton JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_run"));
263             run.setDefaultCapable(true);
264             JButton JavaDoc cancel = new JButton JavaDoc(NbBundle.getMessage(RunTargetsAction.class, "LBL_run_advanced_cancel"));
265             dd.setOptions(new Object JavaDoc[] {run, cancel});
266             dd.setModal(true);
267             Object JavaDoc result = DialogDisplayer.getDefault().notify(dd);
268             if (result.equals(run)) {
269                 try {
270                     panel.run();
271                 } catch (IOException JavaDoc x) {
272                     AntModule.err.notify(x);
273                 }
274             }
275         }
276         
277     }
278     
279 }
280
Popular Tags