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.builds.MakeExecSupport;
050: import org.netbeans.modules.cnd.loaders.MakefileDataObject;
051: import org.netbeans.modules.cnd.settings.CppSettings;
052: import org.netbeans.modules.cnd.settings.MakeSettings;
053: import org.openide.LifecycleManager;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.loaders.DataObject;
057: import org.openide.nodes.Node;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.NbBundle;
060: import org.openide.util.actions.NodeAction;
061:
062: /**
063: * Base class for Make Actions ...
064: */
065: public abstract class MakeBaseAction 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 = (DataObject) activatedNodes[0]
075: .getCookie(DataObject.class);
076: if (dataObject instanceof MakefileDataObject)
077: enabled = true;
078: else
079: enabled = false;
080: }
081: return enabled;
082: }
083:
084: protected void performAction(Node[] activatedNodes) {
085: for (int i = 0; i < activatedNodes.length; i++)
086: performAction(activatedNodes[i], "");
087: }
088:
089: public HelpCtx getHelpCtx() {
090: return HelpCtx.DEFAULT_HELP; // FIXUP ???
091: }
092:
093: // public void actionPerformed(ActionEvent evt) {
094: // Node[] activeNodes = WindowManager.getDefault().getRegistry ().getActivatedNodes();
095: // performAction(activeNodes);
096: // }
097:
098: protected void performAction(Node node, String target) {
099: MakeExecSupport mes = (MakeExecSupport) node
100: .getCookie(MakeExecSupport.class);
101: DataObject dataObject = (DataObject) node
102: .getCookie(DataObject.class);
103: FileObject fileObject = dataObject.getPrimaryFile();
104:
105: if (MakeSettings.getDefault().getSaveAll()) {
106: LifecycleManager.getDefault().saveAll();
107: }
108:
109: File makefile = FileUtil.toFile(fileObject);
110: // Build directory
111: String bdir = mes.getBuildDirectory();
112: File buildDir;
113: if (bdir.length() == 0 || bdir.equals(".")) { // NOI18N
114: buildDir = makefile.getParentFile();
115: } else if (IpeUtils.isPathAbsolute(bdir)) {
116: buildDir = new File(bdir);
117: } else {
118: buildDir = new File(makefile.getParentFile(), bdir);
119: }
120: try {
121: buildDir = buildDir.getCanonicalFile();
122: } catch (IOException ioe) {
123: ;
124: ; // FIXUP
125: }
126: // Executable
127: String executable = mes.getMakeCommand();
128: // Arguments
129: String arguments = "-f " + makefile.getName() + " " + target; // NOI18N
130: // Tab Name
131: String tabName = getString("MAKE_LABEL", node.getName());
132: if (target != null && target.length() > 0)
133: tabName += " " + target; // NOI18N
134:
135: // Execute the makefile
136: String[] envp = { Path.getPathName() + '='
137: + Path.getPathAsString() };
138: try {
139: new NativeExecutor(buildDir.getPath(), executable,
140: arguments, envp, tabName, "make", // NOI18N
141: true).execute();
142: } catch (IOException ioe) {
143: }
144: }
145:
146: protected final static String getString(String key) {
147: return NbBundle.getBundle(MakeBaseAction.class).getString(key);
148: }
149:
150: protected final static String getString(String key, String a1) {
151: return NbBundle.getMessage(MakeBaseAction.class, key, a1);
152: }
153: }
|