001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.project;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Properties;
029: import org.apache.tools.ant.module.api.support.ActionUtils;
030: import org.netbeans.api.project.ant.AntArtifact;
031: import org.netbeans.modules.compapp.projects.base.ui.customizer.IcanproProjectProperties;
032: import org.netbeans.modules.compapp.projects.base.ui.customizer.VisualClassPathItem;
033: import org.netbeans.spi.project.ActionProvider;
034: import org.netbeans.spi.project.support.ant.AntProjectHelper;
035: import org.netbeans.spi.project.support.ant.ReferenceHelper;
036: import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
037: import org.openide.ErrorManager;
038: import org.openide.execution.ExecutorTask;
039: import org.openide.filesystems.FileObject;
040: import org.openide.util.Lookup;
041: import org.openide.util.Task;
042: import org.openide.util.TaskListener;
043: import org.openide.windows.IOProvider;
044: import org.openide.windows.OutputWriter;
045:
046: /**
047: * Action provider of the Web project. This is the place where to do
048: * strange things to Web actions. E.g. compile-single.
049: *
050: * @author Vitaly Bychkov
051: * @version 1.0
052: */
053: public class XsltproActionProvider implements ActionProvider {
054:
055: // Commands available from Web project
056: private static final String[] supportedActions = { COMMAND_BUILD,
057: COMMAND_CLEAN, COMMAND_REBUILD, COMMAND_DELETE,
058: XsltproConstants.POPULATE_CATALOG, COMMAND_DELETE,
059: COMMAND_COPY, COMMAND_MOVE, COMMAND_RENAME };
060:
061: XsltproProject project;
062:
063: // Ant project helper of the project
064: private AntProjectHelper antProjectHelper;
065: private ReferenceHelper refHelper;
066:
067: /** Map from commands to ant targets */
068: Map<String, String[]> commands;
069:
070: public XsltproActionProvider(XsltproProject project,
071: AntProjectHelper antProjectHelper, ReferenceHelper refHelper) {
072: commands = new HashMap<String, String[]>();
073: commands.put(COMMAND_BUILD, new String[] { "dist" }); // NOI18N
074: commands.put(COMMAND_CLEAN, new String[] { "clean" }); // NOI18N
075: commands.put(COMMAND_REBUILD, new String[] { "clean", "dist" }); // NOI18N
076: commands.put(XsltproConstants.POPULATE_CATALOG,
077: new String[] { "populate" });
078: //commands.put(XsltproConstants.COMMAND_REDEPLOY, new String[] {"run"}); // NOI18N
079: //commands.put(XsltproConstants.COMMAND_DEPLOY, new String[] {"run"}); // NOI18N
080:
081: this .antProjectHelper = antProjectHelper;
082: this .project = project;
083: this .refHelper = refHelper;
084: }
085:
086: public String[] getSupportedActions() {
087: return supportedActions;
088: }
089:
090: public void invokeAction(String command, Lookup context)
091: throws IllegalArgumentException {
092: if (COMMAND_COPY.equals(command)) {
093: DefaultProjectOperations
094: .performDefaultCopyOperation(project);
095: return;
096: }
097:
098: if (COMMAND_MOVE.equals(command)) {
099: DefaultProjectOperations
100: .performDefaultMoveOperation(project);
101: return;
102: }
103:
104: if (COMMAND_RENAME.equals(command)) {
105: DefaultProjectOperations.performDefaultRenameOperation(
106: project, null);
107: return;
108: }
109: if (COMMAND_DELETE.equals(command)) {
110: DefaultProjectOperations
111: .performDefaultDeleteOperation(project);
112: return;
113: }
114: if (command.equals(XsltproConstants.POPULATE_CATALOG)) {
115: XsltProjectRetriever bpRetriever = new XsltProjectRetriever(
116: project.getProjectDirectory());
117: bpRetriever.execute();
118: return;
119: }
120: Properties p = null;
121: String[] targetNames = commands.get(command);
122: //// //EXECUTION PART
123: //// if (command.equals (XsltproConstants.COMMAND_DEPLOY) || command.equals (XsltproConstants.COMMAND_REDEPLOY)) {
124: //// if (!isSelectedServer ()) {
125: //// return;
126: //// }
127: //// if (isDebugged()) {
128: //// NotifyDescriptor nd;
129: //// ProjectInformation pi = (ProjectInformation)project.getLookup().lookup(ProjectInformation.class);
130: //// String text = pi.getDisplayName();
131: //// nd = new NotifyDescriptor.Confirmation(
132: //// NbBundle.getMessage(XsltproActionProvider.class, "MSG_SessionRunning", text),
133: //// NotifyDescriptor.OK_CANCEL_OPTION);
134: //// Object o = DialogDisplayer.getDefault().notify(nd);
135: //// if (o.equals(NotifyDescriptor.OK_OPTION)) {
136: //// DebuggerManager.getDebuggerManager().getCurrentSession().kill();
137: //// } else {
138: //// return;
139: //// }
140: //// }
141: //// } else {
142: //// p = null;
143: //// if (targetNames == null) {
144: //// throw new IllegalArgumentException(command);
145: //// }
146: //// }
147:
148: // if build command then build any depedent project
149: if (command.equals(COMMAND_BUILD)) {
150: try {
151: buildDependentProjectsAndRunTask(targetNames, p);
152: } catch (IOException e) {
153: ErrorManager.getDefault().notify(e);
154: }
155: } else {
156: runTask(targetNames, p);
157: }
158: }
159:
160: public boolean isActionEnabled(String command, Lookup context)
161: throws IllegalArgumentException {
162: if (findBuildXml() == null) {
163: return false;
164: }
165: return true;
166: }
167:
168: // private methods
169:
170: /**
171: * @return array of targets or null to stop execution; can return empty array
172: */
173: String[] getTargetNames(String command, Lookup context, Properties p)
174: throws IllegalArgumentException {
175: String[] targetNames = commands.get(command);
176: return targetNames;
177: }
178:
179: private FileObject findBuildXml() {
180: return project.getProjectDirectory().getFileObject(
181: project.getBuildXmlName());
182: }
183:
184: private void runTask(String[] targetNames, Properties p) {
185: try {
186: ActionUtils.runTarget(findBuildXml(), targetNames, p);
187: } catch (IOException e) {
188: ErrorManager.getDefault().notify(e);
189: }
190: }
191:
192: private void buildDependentProjectsAndRunTask(String[] targetNames,
193: Properties p) throws IOException {
194: IcanproProjectProperties app = this .project
195: .getProjectProperties();
196: List items = (List) app
197: .get(IcanproProjectProperties.JAVAC_CLASSPATH);
198: ArrayList artifacts = new ArrayList();
199:
200: if (items != null) {
201: for (int i = 0, size = items.size(); i < size; i++) {
202: VisualClassPathItem vi = (VisualClassPathItem) items
203: .get(i);
204: AntArtifact aa = (AntArtifact) vi.getObject();
205: String loc = aa.getProject().getProjectDirectory()
206: .getPath()
207: + "/" + aa.getArtifactLocation().getPath();
208: File asa = new File(loc);
209: log(" Dependent Project artifact jar: " + loc + ", ["
210: + (asa.exists() ? "exist" : "missing") + "]");
211: if (!asa.exists()) {
212: artifacts.add(aa);
213: }
214: }
215: }
216:
217: if (artifacts.size() != 0) {
218: //use AntTaskListener which invokes the target on
219: //current project build script after all the depedent projects
220: //are build
221: AntTaskListener antTaskListener = new AntTaskListener(
222: targetNames, p);
223: antTaskListener.setTotalTasks(artifacts.size());
224: Iterator it = artifacts.iterator();
225: while (it.hasNext()) {
226: AntArtifact aa = (AntArtifact) it.next();
227: String loc = aa.getProject().getProjectDirectory()
228: .getPath()
229: + "/" + aa.getArtifactLocation().getPath();
230: log(" Building dependent project " + loc + "...");
231: ExecutorTask task = ActionUtils.runTarget(aa
232: .getScriptFile(), new String[] { aa
233: .getTargetName() }, null);
234: task.addTaskListener(antTaskListener);
235: }
236: } else {
237: //no need to build depedent projects
238: //directly invoke target on current project build script;
239: runTask(targetNames, p);
240: }
241: }
242:
243: private boolean isDebugged() {
244: return false;
245: }
246:
247: // private boolean isSelectedServer () {
248: // String instance = antProjectHelper.getStandardPropertyEvaluator ().getProperty (XsltproConstants.J2EE_SERVER_INSTANCE);
249: // boolean selected;
250: // if (instance != null) {
251: // selected = true;
252: // } else {
253: // // no selected server => warning
254: // String server = antProjectHelper.getStandardPropertyEvaluator ().getProperty (XsltproConstants.J2EE_SERVER_TYPE);
255: // NoSelectedServerWarning panel = new NoSelectedServerWarning (server);
256: //
257: // Object[] options = new Object[] {
258: // DialogDescriptor.OK_OPTION,
259: // DialogDescriptor.CANCEL_OPTION
260: // };
261: // DialogDescriptor desc = new DialogDescriptor (panel,
262: // NbBundle.getMessage (NoSelectedServerWarning.class, "CTL_NoSelectedServerWarning_Title"), // NOI18N
263: // true, options, options[0], DialogDescriptor.DEFAULT_ALIGN, null, null);
264: // Dialog dlg = DialogDisplayer.getDefault().createDialog (desc);
265: // dlg.setVisible (true);
266: // if (desc.getValue() != options[0]) {
267: // selected = false;
268: // } else {
269: // instance = panel.getSelectedInstance ();
270: // selected = instance != null;
271: // if (selected) {
272: // XsltproProjectProperties wpp = new XsltproProjectProperties (project, antProjectHelper, refHelper);
273: // wpp.put (XsltproConstants.J2EE_SERVER_INSTANCE, instance);
274: // wpp.store ();
275: // }
276: // }
277: // dlg.dispose();
278: // }
279: // return selected;
280: // }
281:
282: private void log(String str) {
283: OutputWriter out = IOProvider.getDefault().getStdOut();
284: out.println(str);
285: out.flush();
286: }
287:
288: private class AntTaskListener implements TaskListener {
289: int totalTaskCount;
290: int finishedTaskCount = 0;
291: private String[] mTargetNames;
292: private Properties mProperties;
293:
294: public AntTaskListener(String[] targetNames, Properties p) {
295: this .mTargetNames = targetNames;
296: this .mProperties = p;
297: }
298:
299: public void setTotalTasks(int total) {
300: this .totalTaskCount = total;
301: }
302:
303: public void taskFinished(Task task) {
304: finishedTaskCount++;
305: if (finishedTaskCount == totalTaskCount) {
306: runTask(this.mTargetNames, this.mProperties);
307: }
308: }
309: }
310:
311: }
|