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-2006 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: package org.netbeans.modules.php.rt.utils;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import org.netbeans.api.project.FileOwnerQuery;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.api.project.ProjectUtils;
052: import org.openide.LifecycleManager;
053: import org.openide.cookies.SaveCookie;
054: import org.openide.filesystems.FileObject;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.nodes.Node;
058: import org.openide.windows.TopComponent;
059:
060: /**
061: *
062: * @author avk
063: */
064: public class PhpCommandUtils {
065:
066: // ASYNCHRONICITY adapted from org.openide.util.actions.CallableSystemAction
067: private static final boolean DEFAULT_ASYNCH = !Boolean
068: .getBoolean("org.openide.util.actions.CallableSystemAction.synchronousByDefault"); // NOI18N
069:
070: public static boolean defaultAsynchronous() {
071: return DEFAULT_ASYNCH;
072: }
073:
074: public static Node[] getActionNodes() {
075: return TopComponent.getRegistry().getCurrentNodes();
076: }
077:
078: public static boolean isInvokedForProject() {
079: Node[] nodes = getActionNodes();
080: if (nodes == null) {
081: return false;
082: }
083:
084: for (Node node : nodes) {
085: if (isProjectNode(node)) {
086: return true;
087: }
088: }
089: return false;
090: }
091:
092: public static FileObject[] getActionFiles() {
093: Node[] nodes = getActionNodes();
094: if (nodes == null) {
095: return new FileObject[0];
096: }
097: List<FileObject> list = new ArrayList<FileObject>(nodes.length);
098: for (Node node : nodes) {
099: FileObject fileObject = node.getLookup().lookup(
100: FileObject.class);
101:
102: if (fileObject == null) {
103: fileObject = getFileObjectByDataObject(node);
104: }
105:
106: if (fileObject == null) {
107: fileObject = getFileObjectForProject(node);
108: }
109:
110: if (fileObject != null) {
111: list.add(fileObject);
112: }
113: }
114: return list.toArray(new FileObject[list.size()]);
115: }
116:
117: /**
118: * returns true if at one of selected nodes is src root node.
119: */
120: public static boolean isInvokedForSrcRoot() {
121: Map<Project, FileObject[]> projectToSrc = new HashMap<Project, FileObject[]>();
122:
123: FileObject[] files = getActionFiles();
124: for (FileObject fileObject : files) {
125: Project ownerProject = FileOwnerQuery.getOwner(fileObject);
126:
127: // alresdy retrieved sources arecached
128: FileObject[] sources = getAndCacheSources(projectToSrc,
129: ownerProject);
130: for (FileObject source : sources) {
131: if (source.equals(fileObject)) {
132: return true;
133: }
134: }
135: }
136: return false;
137: }
138:
139: public static void saveAll() {
140: // suggested by nb team as general approach
141: // it seems that this will save files even in other projects.
142: // It's not correct but temporarily use it as a general behaviour.
143: LifecycleManager.getDefault().saveAll();
144: }
145:
146: /*
147: public static void saveProject(Project project) throws IOException{
148: ProjectManager.getDefault().saveProject(project);
149: }
150: */
151:
152: /**
153: * saves specified fileObject
154: */
155: public static void saveFile(FileObject fileObject)
156: throws IOException {
157: try {
158: DataObject dataObject = DataObject.find(fileObject);
159: if (dataObject != null) {
160: SaveCookie saveCookie = dataObject
161: .getCookie(SaveCookie.class);
162: if (saveCookie != null) {
163: saveCookie.save();
164: }
165: }
166: } catch (DataObjectNotFoundException e) {
167: // ignore saving for this file
168: }
169: }
170:
171: private static FileObject[] getAndCacheSources(
172: Map<Project, FileObject[]> projectToSrcCache,
173: Project project) {
174: if (!projectToSrcCache.containsKey(project)) {
175: FileObject[] sources = PhpProjectUtils
176: .getSourceObjects(project);
177: projectToSrcCache.put(project, sources);
178: }
179: return projectToSrcCache.get(project);
180: }
181:
182: private static FileObject getFileObjectForProject(Node node) {
183: // project node case
184: Project project = node.getLookup().lookup(Project.class);
185: if (project != null) {
186: return project.getProjectDirectory();
187: }
188: return null;
189: }
190:
191: private static FileObject getFileObjectByDataObject(Node node) {
192: DataObject dobj = node.getLookup().lookup(DataObject.class);
193:
194: if (dobj != null) {
195: return dobj.getPrimaryFile();
196: }
197: return null;
198: }
199:
200: private static boolean isProjectNode(Node node) {
201: Project project = node.getLookup().lookup(Project.class);
202: return project != null;
203: }
204: }
|