001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.ide.util;
025:
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.Collection;
029:
030: import org.eclipse.core.resources.ICommand;
031: import org.eclipse.core.resources.IContainer;
032: import org.eclipse.core.resources.IFolder;
033: import org.eclipse.core.resources.IProject;
034: import org.eclipse.core.resources.IProjectDescription;
035: import org.eclipse.core.resources.IResource;
036: import org.eclipse.core.resources.ResourcesPlugin;
037: import org.eclipse.core.runtime.CoreException;
038: import org.eclipse.core.runtime.IAdaptable;
039: import org.eclipse.core.runtime.IPath;
040: import org.eclipse.core.runtime.NullProgressMonitor;
041: import org.eclipse.core.runtime.Path;
042:
043: public class ProjectUtil {
044: /** Avoid instantiation */
045: private ProjectUtil() {
046: super ();
047: }
048:
049: public static boolean projectHasBuilder(IProject project,
050: String builderId) {
051: try {
052: IProjectDescription desc = project.getDescription();
053: ICommand[] commands = desc.getBuildSpec();
054:
055: for (int i = 0; i < commands.length; i++) {
056: if (commands[i].getBuilderName().equals(builderId))
057: return true;
058: }
059:
060: return false;
061: } catch (CoreException e) {
062: e.printStackTrace();
063: return false;
064: }
065: }
066:
067: /**
068: * Adds a specific builder to the end of a given project's builder list.
069: * @param project
070: * @param builderId
071: * @return Whether or not the builder was successfully added
072: */
073: public static boolean addProjectBuilder(IProject project,
074: String builderId) {
075: try {
076: IProjectDescription desc = project.getDescription();
077: ICommand[] commands = desc.getBuildSpec();
078:
079: //add builders to project
080: ICommand builderCommand = desc.newCommand();
081: builderCommand.setBuilderName(builderId);
082:
083: ICommand[] newCommands = new ICommand[commands.length + 1];
084: System.arraycopy(commands, 0, newCommands, 0,
085: commands.length);
086: newCommands[newCommands.length - 1] = builderCommand;
087:
088: desc.setBuildSpec(newCommands);
089:
090: project.setDescription(desc, new NullProgressMonitor());
091: } catch (CoreException e) {
092: e.printStackTrace();
093: return false;
094: }
095:
096: return true;
097: }
098:
099: /**
100: * Removes a specific builder from the end of a given project's builder list.
101: * @param project
102: * @param builderId
103: * @return Whether or not the builder was successfully removed
104: */
105: public static boolean removeProjectBuilder(IProject project,
106: String builderId) {
107: try {
108: IProjectDescription desc = project.getDescription();
109: ICommand[] commands = desc.getBuildSpec();
110: ArrayList newCommands = new ArrayList();
111:
112: for (int i = 0; i < commands.length; i++) {
113: if (!commands[i].getBuilderName().equals(builderId)) {
114: newCommands.add(commands[i]);
115: }
116: }
117:
118: desc.setBuildSpec((ICommand[]) newCommands
119: .toArray(new ICommand[newCommands.size()]));
120:
121: project.setDescription(desc, new NullProgressMonitor());
122: } catch (CoreException e) {
123: e.printStackTrace();
124: return false;
125: }
126:
127: return true;
128: }
129:
130: /**
131: * Adds the specified project nature to a project
132: * @param project
133: * @param natureId
134: * @return boolean true add project
135: */
136: public static boolean addProjectNature(IProject project,
137: String natureId) {
138: boolean added = false;
139: try {
140: if (project != null && natureId != null) {
141: IProjectDescription desc = project.getDescription();
142:
143: if (!project.hasNature(natureId)) {
144: String natureIds[] = desc.getNatureIds();
145: String newNatureIds[] = new String[natureIds.length + 1];
146:
147: System.arraycopy(natureIds, 0, newNatureIds, 1,
148: natureIds.length);
149: newNatureIds[0] = natureId;
150: desc.setNatureIds(newNatureIds);
151:
152: project.getProject().setDescription(desc,
153: new NullProgressMonitor());
154: added = true;
155: }
156: }
157: } catch (CoreException e) {
158: e.printStackTrace();
159: }
160: return added;
161: }
162:
163: /**
164: * Removes the specified project nature from the project
165: * @param project
166: * @param natureId
167: * @return true removeProject
168: */
169: public static boolean removeProjectNature(IProject project,
170: String natureId) {
171: boolean removed = false;
172: try {
173: if (project != null && natureId != null) {
174: IProjectDescription desc = project.getDescription();
175:
176: if (project.hasNature(natureId)) {
177: String natureIds[] = desc.getNatureIds();
178: String newNatureIds[] = new String[natureIds.length - 1];
179: int n = 0;
180:
181: for (int i = 0; i < natureIds.length; i++) {
182: if (!natureIds[i].equals(natureId)) {
183: newNatureIds[n++] = natureIds[i];
184: }
185: }
186:
187: desc.setNatureIds(newNatureIds);
188:
189: project.getProject().setDescription(desc,
190: new NullProgressMonitor());
191: removed = true;
192: }
193: }
194: } catch (CoreException e) {
195: e.printStackTrace();
196: }
197: return removed;
198: }
199:
200: /**
201: * Creates a folder in a project, with the intermediate folders if needed.
202: *
203: * @param project
204: * @param folderName
205: */
206: public static void createFolder(IProject project, String folderName) {
207: IFolder newFolder = project.getFolder(folderName);
208: IPath folderPath = newFolder.getProjectRelativePath();
209:
210: if (!newFolder.exists()) {
211: try {
212: for (int i = 0; i < folderPath.segmentCount(); i++) {
213: IPath path = folderPath.uptoSegment(i);
214: IFolder folder = project.getFolder(path);
215: folder.create(true, true, null);
216: }
217: } catch (CoreException e) {//TODO
218: //AbstractPlugin.log(e);
219: }
220: }
221: }
222:
223: /**
224: * Gets the all the opened projects in the worksapce
225: *
226: * @return A collection of opened projects
227: */
228: public static Collection getAllOpenedProjects() {
229: return Arrays.asList(ResourcesPlugin.getWorkspace().getRoot()
230: .getProjects());
231: }
232:
233: /**
234: * Gets a file from a workspace relative path. The file may not exists.
235: *
236: * @param pathRelativeToWorkspace A workspace relative path
237: * @return The file handle
238: */
239: public static IResource getFile(String pathRelativeToWorkspace) {
240: IPath path = new Path(null, pathRelativeToWorkspace);
241:
242: // Try to map to an existing resource
243: IResource resource = getResource(pathRelativeToWorkspace);
244: if (resource == null) {
245: // If not, build a handle on an non-existing resource
246: resource = ResourcesPlugin.getWorkspace().getRoot()
247: .getFile(path);
248: }
249: return resource;
250: }
251:
252: /**
253: * Gets a folder from a workspace relative path. The folder may not exists.
254: *
255: * @param pathRelativeToWorkspace A workspace relative path
256: * @return The folder handle
257: */
258: public static IResource getFolder(String pathRelativeToWorkspace) {
259: IPath path = new Path(null, pathRelativeToWorkspace);
260:
261: // Try to map to an existing resource
262: IResource resource = getResource(pathRelativeToWorkspace);
263: if (resource == null) {
264: // If not, build a handle on an non-existing resource
265: resource = ResourcesPlugin.getWorkspace().getRoot()
266: .getFolder(path);
267: }
268: return resource;
269: }
270:
271: /**
272: * Gets a resource from a workspace relative path. The resoruce must exists.
273: *
274: * @param pathRelativeToWorkspace A workspace relative path
275: * @return The resoruce handle
276: */
277: public static IResource getResource(String pathRelativeToWorkspace) {
278: IPath path = new Path(null, pathRelativeToWorkspace);
279: IResource resource = ResourcesPlugin.getWorkspace().getRoot()
280: .findMember(path);
281: return resource;
282: }
283:
284: /**
285: * This method will try a number of known methods for determining the project
286: * that corresponds with the passed in object.
287: * @param element
288: * @return an IProject or null if one could not be found
289: */
290: public static IProject getProject(Object element) {
291: if (element instanceof IProject) {
292: return (IProject) element;
293: }
294:
295: if (element instanceof IAdaptable) {
296: IAdaptable adaptable = (IAdaptable) element;
297: IProject project = (IProject) adaptable
298: .getAdapter(IProject.class);
299: if (project != null) {
300: return project;
301: }
302: }
303:
304: if (element instanceof IResource) {
305: IResource resource = (IResource) element;
306: return resource.getProject();
307: }
308:
309: if (element instanceof IContainer) {
310: IContainer container = (IContainer) element;
311: return container.getProject();
312: }
313:
314: return null;
315: }
316:
317: public static IPath getProjectLocation(IProject project) {
318: if (project.getRawLocation() == null) {
319: return project.getLocation();
320: } else
321: return project.getRawLocation();
322: }
323: }
|