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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.php.project;
029:
030: import java.io.File;
031: import java.text.MessageFormat;
032: import org.netbeans.api.project.Project;
033: import org.netbeans.modules.php.rt.spi.providers.Command;
034: import org.netbeans.modules.php.rt.utils.PhpCommandUtils;
035: import org.netbeans.modules.php.rt.utils.PhpProjectUtils;
036: import org.netbeans.spi.project.support.ant.AntProjectHelper;
037: import org.openide.awt.StatusDisplayer;
038: import org.openide.filesystems.FileObject;
039: import org.openide.filesystems.FileUtil;
040: import org.openide.util.NbBundle;
041: import org.openide.windows.IOProvider;
042: import org.openide.windows.InputOutput;
043: import org.openide.windows.OutputWriter;
044:
045: /**
046: *
047: * @author avk
048: */
049: public abstract class AbstractCommand implements Command {
050:
051: private static final String LBL_OUT_TAB_TITLE = "LBL_OutputTabTitle";
052:
053: public AbstractCommand(Project project) {
054: myProject = project;
055: initActionFiles();
056: }
057:
058: /**
059: * If true, this action should be performed asynchronously in a private thread.
060: * If false, it will be performed synchronously as called in the event thread.
061: * @return true if this action should automatically be performed asynchronously
062: */
063: public boolean asynchronous() {
064: return PhpCommandUtils.defaultAsynchronous();
065: }
066:
067: public boolean isEnabled() {
068: return true;
069: }
070:
071: public void setActionFiles(FileObject[] files) {
072: myFiles = files;
073: }
074:
075: @Override
076: public boolean equals(Object obj) {
077: if (!(obj instanceof AbstractCommand)) {
078: return false;
079: }
080: AbstractCommand command = (AbstractCommand) obj;
081: return command.getId().equals(this .getId())
082: && command.getLabel().equals(this .getLabel());
083: }
084:
085: @Override
086: public int hashCode() {
087: return this .getId().hashCode() * 37
088: + this .getLabel().hashCode();
089: }
090:
091: protected FileObject[] getFileObjects() {
092: return myFiles;
093: }
094:
095: protected Project getProject() {
096: return myProject;
097: }
098:
099: /**
100: * should be called in the beginning of run() method
101: */
102: protected void refresh() {
103: initOutputTabTitle();
104: }
105:
106: protected String getRelativeSrcPath(FileObject fileObject) {
107: return PhpProjectUtils.getRelativeSrcPath(getProject(),
108: fileObject);
109: }
110:
111: protected AntProjectHelper getAntProjectHelper() {
112: return getProject().getLookup().lookup(AntProjectHelper.class);
113: }
114:
115: protected FileObject[] getSourceObjects(Project phpProject) {
116: return Utils.getSourceObjects(phpProject);
117: }
118:
119: protected FileObject getSourceObject() {
120: FileObject[] sources = getSourceObjects(getProject());
121: if (sources == null || sources.length == 0) {
122: return null;
123: }
124: /*
125: * I choose only first source root.
126: * TODO: change if we decide to support multiple src roots
127: */
128: return sources[0];
129: }
130:
131: protected String getOutputTabTitle() {
132: return myOutputTabTitle;
133: }
134:
135: protected void notifyMsg(String bundleKey, Object... args) {
136: notifyMsg(bundleKey, getClass(), args);
137: }
138:
139: protected void notifyMsg(String bundleKey, Class clazz,
140: Object... args) {
141: String msg = loadFormattedMsg(bundleKey, clazz, args);
142: logToOutput(getOutputTabTitle(), msg);
143: }
144:
145: protected void statusMsg(String bundleKey, Object... args) {
146: statusMsg(bundleKey, getClass(), args);
147: }
148:
149: protected void statusMsg(String bundleKey, Class clazz,
150: Object... args) {
151: String msg = loadFormattedMsg(bundleKey, clazz, args);
152: StatusDisplayer.getDefault().setStatusText(msg);
153: }
154:
155: protected static String loadFormattedMsg(String bundleKey,
156: Class clazz, Object... args) {
157: String msg = NbBundle.getMessage(clazz, bundleKey);
158: if (args.length > 0) {
159: msg = MessageFormat.format(msg, args);
160: }
161: return msg;
162: }
163:
164: protected boolean isNbProject(File file) {
165: // moved away from constructor. to prevent NPE on the first init.
166: if (PROJECT_XML == null) {
167: PROJECT_XML = getProject().getLookup().lookup(
168: AntProjectHelper.class).resolveFile(
169: AntProjectHelper.PROJECT_XML_PATH);
170: }
171: File nbProjectFile = PROJECT_XML.getParentFile();
172: return file.equals(nbProjectFile);
173: }
174:
175: private void initActionFiles() {
176: /*
177: * This method should be called in constructor of class because
178: * <code>nodes</code> array could be changed while action
179: * execution. So one need to initialize fileObjects array once
180: * and use it for access to action files.
181: */
182: myFiles = PhpCommandUtils.getActionFiles();
183: }
184:
185: private static void logToOutput(String outTabTitle, String msg) {
186: InputOutput io = IOProvider.getDefault().getIO(outTabTitle,
187: false);
188: io.select();
189: OutputWriter writer = io.getOut();
190: writer.println(msg); //write tag to output window
191: writer.flush();
192: writer.close();
193: }
194:
195: /** sets default output tab title.
196: * Can't be added to constructor because it causes problems
197: * with standard project actions like copy etc.
198: * <br>
199: * is now added into refresh() command
200: */
201: private void initOutputTabTitle() {
202: myOutputTabTitle = NbBundle.getMessage(AbstractCommand.class,
203: LBL_OUT_TAB_TITLE, getLabel());
204: }
205:
206: private final Project myProject;
207: private FileObject[] myFiles;
208: private String myOutputTabTitle;
209: private File PROJECT_XML;
210:
211: }
|