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.Iterator;
028: import java.util.List;
029:
030: import org.eclipse.core.internal.resources.ProjectDescription;
031: import org.eclipse.core.resources.IFile;
032: import org.eclipse.core.resources.IProject;
033: import org.eclipse.core.resources.IProjectDescription;
034: import org.eclipse.core.runtime.CoreException;
035: import org.eclipse.core.runtime.IPath;
036: import org.eclipse.core.runtime.Path;
037: import org.eclipse.jdt.core.IClasspathEntry;
038: import org.eclipse.jdt.core.IJavaElement;
039: import org.eclipse.jdt.core.JavaCore;
040: import org.eclipse.jdt.core.JavaModelException;
041: import org.eclipse.jface.viewers.IStructuredSelection;
042:
043: import com.bostechcorp.cbesb.ui.util.MsgUtil;
044:
045: public class JavaProjectUtil {
046: /** Avoid instantiation */
047: private JavaProjectUtil() {
048: }
049:
050: /**
051: * Description of the Method
052: *
053: * @param project
054: * Description of the Parameter
055: * @param srcFolderName
056: * Description of the Parameter
057: * @return Description of the Return Value
058: * @exception CoreException
059: * Description of the Exception
060: */
061: public static IClasspathEntry createSourceClasspathEntry(
062: IProject project, String srcFolderName)
063: throws CoreException {
064: return createSourceClasspathEntry(project, srcFolderName, null);
065: }
066:
067: /**
068: * Description of the Method
069: *
070: * @param project
071: * Description of the Parameter
072: * @param srcFolderName
073: * Description of the Parameter
074: * @param outputFolderName
075: * Description of the Parameter
076: * @return Description of the Return Value
077: * @exception CoreException
078: * Description of the Exception
079: */
080: public static IClasspathEntry createSourceClasspathEntry(
081: IProject project, String srcFolderName,
082: String outputFolderName) throws CoreException {
083: ProjectUtil.createFolder(project, srcFolderName);
084: if (outputFolderName != null) {
085: ProjectUtil.createFolder(project, outputFolderName);
086: }
087:
088: // Add the source entry with a specific output location
089: IPath outputFolderPath = null;
090: if (outputFolderName != null) {
091: outputFolderPath = new Path(
092: "/" + project.getName() + "/" + outputFolderName);//$NON-NLS-1$ //$NON-NLS-2$
093: }
094:
095: IClasspathEntry entry = JavaCore
096: .newSourceEntry(
097: new Path(
098: "/" + project.getName() + "/" + srcFolderName), new Path[] {}, outputFolderPath);//$NON-NLS-1$ //$NON-NLS-2$
099:
100: return entry;
101: }
102:
103: /**
104: * Description of the Method
105: *
106: * @return Description of the Return Value
107: * @exception CoreException
108: * Description of the Exception
109: */
110: public static IClasspathEntry[] mergeClasspathEntry(
111: IClasspathEntry[] entries, IClasspathEntry cpEntry)
112: throws CoreException {
113: if (cpEntry == null) {
114: return entries;
115: }
116:
117: List list = new ArrayList();
118: if (entries != null) {
119: String containerId = cpEntry.getPath().toString();
120: for (int i = 0; i < entries.length; i++) {
121: if (!entries[i].getPath().toString()
122: .equals(containerId)) {
123: list.add(entries[i]);
124: }
125: }
126: }
127: list.add(cpEntry);
128:
129: return (IClasspathEntry[]) list
130: .toArray(new IClasspathEntry[list.size()]);
131: }
132:
133: /**
134: * Description of the Method
135: *
136: * @param entries
137: * Description of the Parameter
138: * @param cpEntry
139: * Description of the Parameter
140: * @return Description of the Return Value
141: * @exception CoreException
142: * Description of the Exception
143: */
144: public static IClasspathEntry[] removeClasspathEntry(
145: IClasspathEntry[] entries, IClasspathEntry cpEntry)
146: throws CoreException {
147: List list = new ArrayList();
148: if (entries != null) {
149: String containerId = cpEntry.getPath().toString();
150: for (int i = 0; i < entries.length; i++) {
151: if (!entries[i].getPath().toString()
152: .equals(containerId)) {
153: list.add(entries[i]);
154: }
155: }
156: }
157:
158: return (IClasspathEntry[]) list
159: .toArray(new IClasspathEntry[list.size()]);
160: }
161:
162: public static void addNature(IProject project, String nature) {
163: IProjectDescription description;
164: try {
165: description = project.getProject().getDescription();
166: if (description == null) {
167: description = new ProjectDescription();
168: description.setNatureIds(new String[] { nature });
169: } else {
170: int length = description.getNatureIds().length;
171: String[] newNature = new String[length + 1];
172: for (int i = 0; i < length; i++) {
173: newNature[i] = description.getNatureIds()[i];
174: }
175: newNature[length] = nature;
176: description.setNatureIds(newNature);
177: }
178: project.setDescription(description, null);
179: } catch (CoreException e) {
180: e.printStackTrace();
181: MsgUtil.warningMsg("Can not convert project:"
182: + e.toString());
183: }
184: }
185:
186: public static IProject getProject(IStructuredSelection selection) {
187: if (!(selection instanceof IStructuredSelection))
188: return null;
189: Iterator iter = ((IStructuredSelection) selection).iterator();
190: while (iter.hasNext()) {
191: Object item = (Object) iter.next();
192: if (item instanceof IJavaElement) {
193: IJavaElement javaElem = (IJavaElement) item;
194: try {
195: item = javaElem.getUnderlyingResource();
196: return javaElem.getJavaProject().getProject();
197: } catch (JavaModelException e) {
198: e.printStackTrace();
199: continue;
200: }
201: }
202: if (item instanceof IFile) {
203: IFile file = (IFile) item;
204: return file.getProject();
205: }
206: if (item instanceof IProject) {
207: return (IProject) item;
208: }
209: }
210: return null;
211:
212: }
213: }
|