KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > SuiteActions


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.apisupport.project.ui;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JSeparator JavaDoc;
28 import org.apache.tools.ant.module.api.support.ActionUtils;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.modules.apisupport.project.NbModuleProject;
31 import org.netbeans.modules.apisupport.project.Util;
32 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
33 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteCustomizer;
34 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
35 import org.netbeans.spi.project.ActionProvider;
36 import org.netbeans.spi.project.SubprojectProvider;
37 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
38 import org.netbeans.spi.project.ui.support.CommonProjectActions;
39 import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
40 import org.netbeans.spi.project.ui.support.ProjectSensitiveActions;
41 import org.openide.NotifyDescriptor;
42 import org.openide.actions.FindAction;
43 import org.openide.actions.ToolsAction;
44 import org.openide.execution.ExecutorTask;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.Repository;
47 import org.openide.loaders.DataFolder;
48 import org.openide.loaders.FolderLookup;
49 import org.openide.util.Lookup;
50 import org.openide.util.NbBundle;
51 import org.openide.util.actions.SystemAction;
52
53 /**
54  * Defines actions available on a suite.
55  * @author Jesse Glick
56  */

57 public final class SuiteActions implements ActionProvider {
58     
59     static Action JavaDoc[] getProjectActions(SuiteProject project) {
60         List JavaDoc<Action JavaDoc> actions = new ArrayList JavaDoc<Action JavaDoc>();
61         actions.add(CommonProjectActions.newFileAction());
62         actions.add(null);
63         actions.add(ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_BUILD, NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_build"), null));
64         actions.add(ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_REBUILD, NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_rebuild"), null));
65         actions.add(ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_CLEAN, NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_clean"), null));
66         actions.add(null);
67         actions.add(ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_RUN, NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_run"), null));
68         actions.add(ProjectSensitiveActions.projectCommandAction(ActionProvider.COMMAND_DEBUG, NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_debug"), null));
69         actions.add(null);
70         actions.add(ProjectSensitiveActions.projectCommandAction("build-zip", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_zip"), null));
71         actions.add(null);
72         actions.add(ProjectSensitiveActions.projectCommandAction("build-jnlp", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_build_jnlp"), null));
73         actions.add(ProjectSensitiveActions.projectCommandAction("run-jnlp", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_run_jnlp"), null));
74         actions.add(ProjectSensitiveActions.projectCommandAction("debug-jnlp", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_debug_jnlp"), null));
75         actions.add(null);
76         NbPlatform platform = project.getPlatform(true); //true -> #96095
77
if (platform != null && platform.getHarnessVersion() >= NbPlatform.HARNESS_VERSION_55u1) {
78             actions.add(ProjectSensitiveActions.projectCommandAction("build-mac", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_mac"), null));
79             actions.add(null);
80         }
81         if (platform != null && platform.getHarnessVersion() >= NbPlatform.HARNESS_VERSION_50u1) { // #71631
82
actions.add(ProjectSensitiveActions.projectCommandAction("nbms", NbBundle.getMessage(SuiteActions.class, "SUITE_ACTION_nbms"), null)); // #64426
83
actions.add(null);
84         }
85         actions.add(CommonProjectActions.setAsMainProjectAction());
86         actions.add(CommonProjectActions.openSubprojectsAction());
87         actions.add(CommonProjectActions.closeProjectAction());
88         actions.add(null);
89         actions.add(SystemAction.get(FindAction.class));
90         actions.add(null);
91         actions.add(CommonProjectActions.renameProjectAction());
92         actions.add(CommonProjectActions.moveProjectAction());
93         actions.add(CommonProjectActions.deleteProjectAction());
94         FileObject fo = Repository.getDefault().getDefaultFileSystem().findResource("Projects/Actions"); // NOI18N
95
if (fo != null && fo.isFolder()) {
96             actions.add(null);
97             for (Object JavaDoc next : new FolderLookup(DataFolder.findFolder(fo)).getLookup().lookupAll(Object JavaDoc.class)) {
98                 if (next instanceof Action JavaDoc) {
99                     actions.add((Action JavaDoc) next);
100                 } else if (next instanceof JSeparator JavaDoc) {
101                     actions.add(null);
102                 }
103             }
104         }
105         actions.add(null);
106         actions.add(SystemAction.get(ToolsAction.class));
107         actions.add(null);
108         actions.add(CommonProjectActions.customizeProjectAction());
109         return actions.toArray(new Action JavaDoc[actions.size()]);
110     }
111     
112     private final SuiteProject project;
113     
114     public SuiteActions(SuiteProject project) {
115         this.project = project;
116     }
117     
118     public String JavaDoc[] getSupportedActions() {
119         return new String JavaDoc[] {
120             ActionProvider.COMMAND_BUILD,
121             ActionProvider.COMMAND_CLEAN,
122             ActionProvider.COMMAND_REBUILD,
123             ActionProvider.COMMAND_RUN,
124             ActionProvider.COMMAND_DEBUG,
125             "build-zip", // NOI18N
126
"build-jnlp", // NOI18N
127
"run-jnlp", // NOI18N
128
"debug-jnlp", // NOI18N
129
"build-mac", // NOI18N
130
"nbms", // NOI18N
131
"profile", // NOI18N
132
ActionProvider.COMMAND_RENAME,
133             ActionProvider.COMMAND_MOVE,
134             ActionProvider.COMMAND_DELETE
135         };
136     }
137     
138     public boolean isActionEnabled(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
139         if (ActionProvider.COMMAND_DELETE.equals(command) ||
140                 ActionProvider.COMMAND_RENAME.equals(command) ||
141                 ActionProvider.COMMAND_MOVE.equals(command)) {
142             return true;
143         } else if (Arrays.asList(getSupportedActions()).contains(command)) {
144             return findBuildXml(project) != null;
145         } else {
146             throw new IllegalArgumentException JavaDoc(command);
147         }
148     }
149     
150     public void invokeAction(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc {
151         if (ActionProvider.COMMAND_DELETE.equals(command)) {
152             DefaultProjectOperations.performDefaultDeleteOperation(project);
153         } else if (ActionProvider.COMMAND_RENAME.equals(command)) {
154             DefaultProjectOperations.performDefaultRenameOperation(project, null);
155         } else if (ActionProvider.COMMAND_MOVE.equals(command)) {
156             DefaultProjectOperations.performDefaultMoveOperation(project);
157         } else {
158             NbPlatform plaf = project.getPlatform(false);
159             if (plaf != null) {
160                 int v = plaf.getHarnessVersion();
161                 if (v != NbPlatform.HARNESS_VERSION_UNKNOWN) {
162                     for (Project p : project.getLookup().lookup(SubprojectProvider.class).getSubprojects()) {
163                         if (v < ((NbModuleProject) p).getMinimumHarnessVersion()) {
164                             ModuleActions.promptForNewerHarness();
165                             return;
166                         }
167                     }
168                 }
169             }
170             try {
171                 invokeActionImpl(command, context);
172             } catch (IOException JavaDoc e) {
173                 Util.err.notify(e);
174             }
175         }
176     }
177     
178     /** Used from tests to start the build script and get task that allows to track its progress.
179      * @return null or task that was started
180      */

181     public ExecutorTask invokeActionImpl(String JavaDoc command, Lookup context) throws IllegalArgumentException JavaDoc, IOException JavaDoc {
182         String JavaDoc[] targetNames;
183         if (command.equals(ActionProvider.COMMAND_BUILD)) {
184             targetNames = new String JavaDoc[] {"build"}; // NOI18N
185
} else if (command.equals(ActionProvider.COMMAND_CLEAN)) {
186             targetNames = new String JavaDoc[] {"clean"}; // NOI18N
187
} else if (command.equals(ActionProvider.COMMAND_REBUILD)) {
188             targetNames = new String JavaDoc[] {"clean", "build"}; // NOI18N
189
} else if (command.equals(ActionProvider.COMMAND_RUN)) {
190             targetNames = new String JavaDoc[] {"run"}; // NOI18N
191
} else if (command.equals(ActionProvider.COMMAND_DEBUG)) {
192             targetNames = new String JavaDoc[] {"debug"}; // NOI18N
193
} else if (command.equals("build-zip")) { // NOI18N
194
if (promptForAppName(PROMPT_FOR_APP_NAME_MODE_ZIP)) { // #65006
195
return null;
196             }
197             targetNames = new String JavaDoc[] {"build-zip"}; // NOI18N
198
} else if (command.equals("build-jnlp")) { // NOI18N
199
if (promptForAppName(PROMPT_FOR_APP_NAME_MODE_JNLP)) {
200                 return null;
201             }
202             targetNames = new String JavaDoc[] {"build-jnlp"}; // NOI18N
203
} else if (command.equals("run-jnlp")) { // NOI18N
204
if (promptForAppName(PROMPT_FOR_APP_NAME_MODE_JNLP)) {
205                 return null;
206             }
207             targetNames = new String JavaDoc[] {"run-jnlp"}; // NOI18N
208
} else if (command.equals("debug-jnlp")) { // NOI18N
209
if (promptForAppName(PROMPT_FOR_APP_NAME_MODE_JNLP)) {
210                 return null;
211             }
212             targetNames = new String JavaDoc[] {"debug-jnlp"}; // NOI18N
213
} else if (command.equals("build-mac")) { // NOI18N
214
targetNames = new String JavaDoc[] {"build-mac"}; // NOI18N
215
} else if (command.equals("nbms")) { // NOI18N
216
targetNames = new String JavaDoc[] {"nbms"}; // NOI18N
217
} else if (command.equals("profile")) { // NOI18N
218
targetNames = new String JavaDoc[] {"profile"}; // NOI18N
219
} else {
220             throw new IllegalArgumentException JavaDoc(command);
221         }
222         
223         return ActionUtils.runTarget(findBuildXml(project), targetNames, null);
224     }
225     
226     private static FileObject findBuildXml(SuiteProject project) {
227         return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
228     }
229     
230     private static final int PROMPT_FOR_APP_NAME_MODE_JNLP = 0;
231     private static final int PROMPT_FOR_APP_NAME_MODE_ZIP = 1;
232     /** @return true if the dialog is shown */
233     private boolean promptForAppName(int mode) {
234         String JavaDoc name = project.getEvaluator().getProperty("app.name"); // NOI18N
235
if (name != null) {
236             return false;
237         }
238         
239         // #61372: warn the user, rather than disabling the action.
240
String JavaDoc msg;
241         switch (mode) {
242             case PROMPT_FOR_APP_NAME_MODE_JNLP:
243                 msg = NbBundle.getMessage(ModuleActions.class, "ERR_app_name_jnlp");
244                 break;
245             case PROMPT_FOR_APP_NAME_MODE_ZIP:
246                 msg = NbBundle.getMessage(ModuleActions.class, "ERR_app_name_zip");
247                 break;
248             default:
249                 throw new AssertionError JavaDoc(mode);
250         }
251         if (UIUtil.showAcceptCancelDialog(
252                 NbBundle.getMessage(ModuleActions.class, "TITLE_app_name"),
253                 msg,
254                 NbBundle.getMessage(ModuleActions.class, "LBL_configure_app_name"),
255                 null,
256                 NotifyDescriptor.WARNING_MESSAGE)) {
257             SuiteCustomizer cpi = project.getLookup().lookup(SuiteCustomizer.class);
258             cpi.showCustomizer(SuiteCustomizer.APPLICATION, SuiteCustomizer.APPLICATION_CREATE_STANDALONE_APPLICATION);
259         }
260         return true;
261     }
262     
263 }
264
Popular Tags