001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.actions;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import org.netbeans.modules.cnd.api.execution.NativeExecutor;
047: import org.netbeans.modules.cnd.api.utils.IpeUtils;
048: import org.netbeans.modules.cnd.api.utils.Path;
049: import org.netbeans.modules.cnd.execution.ShellExecSupport;
050: import org.netbeans.modules.cnd.loaders.ShellDataObject;
051: import org.netbeans.modules.cnd.settings.CppSettings;
052: import org.openide.LifecycleManager;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.loaders.DataObject;
056: import org.openide.nodes.Node;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059: import org.openide.util.Utilities;
060: import org.openide.util.actions.NodeAction;
061:
062: /**
063: * Base class for Make Actions ...
064: */
065: public class ShellRunAction extends NodeAction {
066:
067: protected boolean enable(Node[] activatedNodes) {
068: boolean enabled = false;
069:
070: if (activatedNodes == null || activatedNodes.length == 0
071: || activatedNodes.length > 1) {
072: enabled = false;
073: } else {
074: DataObject dataObject = activatedNodes[0]
075: .getCookie(DataObject.class);
076: if (dataObject instanceof ShellDataObject)
077: enabled = true;
078: else
079: enabled = false;
080: }
081: return enabled;
082: }
083:
084: public String getName() {
085: return getString("BTN_Run");
086: }
087:
088: protected void performAction(Node[] activatedNodes) {
089: // Save everything first
090: LifecycleManager.getDefault().saveAll();
091:
092: for (int i = 0; i < activatedNodes.length; i++)
093: performAction(activatedNodes[i]);
094: }
095:
096: public HelpCtx getHelpCtx() {
097: return HelpCtx.DEFAULT_HELP; // FIXUP ???
098: }
099:
100: public void performAction(Node node) {
101: ShellExecSupport bes = node.getCookie(ShellExecSupport.class);
102: DataObject dataObject = node.getCookie(DataObject.class);
103: FileObject fileObject = dataObject.getPrimaryFile();
104:
105: File shellFile = FileUtil.toFile(fileObject);
106: // Build directory
107: String bdir = bes.getRunDirectory();
108: File buildDir;
109: if (bdir.length() == 0 || bdir.equals(".")) { // NOI18N
110: buildDir = shellFile.getParentFile();
111: } else if (IpeUtils.isPathAbsolute(bdir)) {
112: buildDir = new File(bdir);
113: } else {
114: buildDir = new File(shellFile.getParentFile(), bdir);
115: }
116: try {
117: buildDir = buildDir.getCanonicalFile();
118: } catch (IOException ioe) {
119: // FIXUP
120: }
121: // Tab Name
122: String tabName = getString("RUN_LABEL", node.getName());
123:
124: String[] shellCommandAndArgs = bes
125: .getShellCommandAndArgs(fileObject); // from inside shell file or properties
126: String shellCommand = shellCommandAndArgs[0];
127: String shellFilePath = IpeUtils.toRelativePath(buildDir
128: .getPath(), shellFile.getPath()); // Absolute path to shell file
129: String[] args = bes.getArguments(); // from properties
130:
131: // Windows: The command is usually of the from "/bin/sh", but this
132: // doesn't work here, so extract the 'sh' part and use that instead.
133: // FIXUP: This is not entirely correct though.
134: if (Utilities.isWindows() && shellCommand.length() > 0) {
135: int i = shellCommand.lastIndexOf("/"); // UNIX PATH // NOI18N
136: if (i >= 0) {
137: shellCommand = shellCommand.substring(i + 1);
138: }
139: }
140:
141: StringBuilder argsFlat = new StringBuilder();
142: if (shellCommandAndArgs[0].length() > 0) {
143: for (int i = 1; i < shellCommandAndArgs.length; i++) {
144: argsFlat.append(" "); // NOI18N
145: argsFlat.append(shellCommandAndArgs[i]);
146: }
147: }
148: argsFlat.append(shellFilePath);
149: for (int i = 0; i < args.length; i++) {
150: argsFlat.append(" "); // NOI18N
151: argsFlat.append(args[i]);
152: }
153:
154: // Execute the makefile
155: String[] envp = { Path.getPathName() + '='
156: + Path.getPathAsString() };
157: try {
158: new NativeExecutor(buildDir.getPath(), shellCommand,
159: argsFlat.toString(), envp, tabName, "Run", // NOI18N
160: true).execute();
161: } catch (IOException ioe) {
162: }
163: }
164:
165: protected final static String getString(String key) {
166: return NbBundle.getBundle(ShellRunAction.class).getString(key);
167: }
168:
169: protected final static String getString(String key, String a1) {
170: return NbBundle.getMessage(ShellRunAction.class, key, a1);
171: }
172:
173: @Override
174: protected boolean asynchronous() {
175: return false;
176: }
177: }
|