KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > datatransfer > ProjectCreator


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.datatransfer;
12
13 import java.io.File JavaDoc;
14 import com.ibm.icu.text.MessageFormat;
15
16 import org.apache.tools.ant.BuildException;
17 import org.apache.tools.ant.taskdefs.Javac;
18 import org.eclipse.ant.internal.ui.AntUIPlugin;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IProjectDescription;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.jdt.core.IClasspathEntry;
33 import org.eclipse.jdt.core.IJavaProject;
34 import org.eclipse.jdt.core.IPackageFragmentRoot;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jdt.launching.JavaRuntime;
38
39 public class ProjectCreator {
40         
41     public IJavaProject createJavaProjectFromJavacNode(String JavaDoc projectName, Javac javacTask, IProgressMonitor monitor) throws CoreException {
42         try {
43             IJavaProject javaProject = createJavaProject(projectName, monitor);
44             
45             File JavaDoc destDir= javacTask.getDestdir();
46             String JavaDoc destDirName= destDir == null ? null : destDir.getName();
47             org.apache.tools.ant.types.Path sourceDirs= javacTask.getSrcdir();
48             createSourceDirectories(destDir, destDirName, sourceDirs, javaProject, monitor);
49             
50             // add rt.jar
51
addVariableEntry(javaProject, new Path(JavaRuntime.JRELIB_VARIABLE), new Path(JavaRuntime.JRESRC_VARIABLE), new Path(JavaRuntime.JRESRCROOT_VARIABLE), monitor);
52             
53             setClasspath(javacTask, javaProject, monitor);
54             
55             javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
56             return javaProject;
57         } catch (BuildException be) {
58             IStatus status= new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, IStatus.OK, be.getLocalizedMessage(), be);
59             throw new CoreException(status);
60         }
61     }
62     
63     private void setClasspath(Javac javacTask, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException {
64         try {
65             org.apache.tools.ant.types.Path classpath= javacTask.getClasspath();
66             if (classpath == null) {
67                 return;
68             }
69             String JavaDoc[] classpaths= classpath.list();
70             for (int i = 0; i < classpaths.length; i++) {
71                 String JavaDoc cp = classpaths[i];
72                 File JavaDoc classpathEntry= new File JavaDoc(cp);
73                 if (classpathEntry.isFile()) {
74                     addLibrary(javaProject, new Path(classpathEntry.getAbsolutePath()), monitor);
75                 } else {
76                     addContainer(javaProject, new Path(classpathEntry.getAbsolutePath()), monitor);
77                 }
78             }
79         } catch (BuildException be) {
80             IStatus status= new Status(IStatus.ERROR, AntUIPlugin.PI_ANTUI, IStatus.OK, MessageFormat.format(DataTransferMessages.ProjectCreator_0, new String JavaDoc[]{be.getLocalizedMessage()}), null);
81             throw new CoreException(status);
82         }
83     }
84
85     private void createSourceDirectories(File JavaDoc destDir, String JavaDoc destDirName, org.apache.tools.ant.types.Path sourceDirs, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException {
86         String JavaDoc[] sourceDirectories= sourceDirs.list();
87         for (int i = 0; i < sourceDirectories.length; i++) {
88             String JavaDoc srcDir = sourceDirectories[i];
89             File JavaDoc srcDirectory= new File JavaDoc(srcDir);
90             String JavaDoc srcDirectoryName= srcDirectory.getName();
91             String JavaDoc destDirPath= destDir == null ? srcDir : destDir.getAbsolutePath();
92             if (destDirName == null) {
93                 destDirName= srcDirectoryName;
94             }
95             addSourceContainer(javaProject, srcDirectoryName, srcDir, destDirName, destDirPath, monitor);
96         }
97     }
98
99     private IJavaProject createJavaProject(String JavaDoc projectName, IProgressMonitor monitor) throws CoreException {
100         IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
101         IProject project= root.getProject(projectName);
102         if (!project.exists()) {
103             project.create(monitor);
104         } else {
105             project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
106         }
107         
108         if (!project.isOpen()) {
109             project.open(monitor);
110         }
111         
112         if (!project.hasNature(JavaCore.NATURE_ID)) {
113             addNatureToProject(project, JavaCore.NATURE_ID, monitor);
114         }
115         
116         IJavaProject jproject= JavaCore.create(project);
117         
118         jproject.setRawClasspath(new IClasspathEntry[0], monitor);
119         
120         return jproject;
121     }
122     
123     private void addNatureToProject(IProject proj, String JavaDoc natureId, IProgressMonitor monitor) throws CoreException {
124         IProjectDescription description = proj.getDescription();
125         String JavaDoc[] prevNatures= description.getNatureIds();
126         String JavaDoc[] newNatures= new String JavaDoc[prevNatures.length + 1];
127         System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
128         newNatures[prevNatures.length]= natureId;
129         description.setNatureIds(newNatures);
130         proj.setDescription(description, monitor);
131     }
132     
133     /**
134      * Adds a source container to a IJavaProject.
135      */

136     private void addSourceContainer(IJavaProject jproject, String JavaDoc srcName, String JavaDoc srcPath, String JavaDoc outputName, String JavaDoc outputPath, IProgressMonitor monitor) throws CoreException {
137         IProject project= jproject.getProject();
138         IContainer container= null;
139         if (srcName == null || srcName.length() == 0) {
140             container= project;
141         } else {
142             IFolder folder= project.getFolder(srcName);
143             if (!folder.exists()) {
144                 folder.createLink(new Path(srcPath), IResource.ALLOW_MISSING_LOCAL, monitor);
145             }
146             container= folder;
147         }
148         IPackageFragmentRoot root= jproject.getPackageFragmentRoot(container);
149
150         IPath output= null;
151         if (outputName!= null) {
152             IFolder outputFolder = project.getFolder(outputName);
153             if (!outputFolder.exists()) {
154                 outputFolder.createLink(new Path(outputPath), IResource.ALLOW_MISSING_LOCAL, monitor);
155             }
156             output= outputFolder.getFullPath();
157         }
158                 
159         IClasspathEntry cpe= JavaCore.newSourceEntry(root.getPath(), new IPath[0], output);
160         
161         addToClasspath(jproject, cpe, monitor);
162     }
163     
164     private void addToClasspath(IJavaProject jproject, IClasspathEntry cpe, IProgressMonitor monitor) throws JavaModelException {
165         IClasspathEntry[] oldEntries= jproject.getRawClasspath();
166         for (int i= 0; i < oldEntries.length; i++) {
167             if (oldEntries[i].equals(cpe)) {
168                 return;
169             }
170         }
171         int nEntries= oldEntries.length;
172         IClasspathEntry[] newEntries= new IClasspathEntry[nEntries + 1];
173         System.arraycopy(oldEntries, 0, newEntries, 0, nEntries);
174         newEntries[nEntries]= cpe;
175         jproject.setRawClasspath(newEntries, monitor);
176     }
177     
178     /**
179      * Adds a variable entry with source attachment to a IJavaProject if the path can be resolved.
180      */

181     private void addVariableEntry(IJavaProject jproject, IPath path, IPath sourceAttachPath, IPath sourceAttachRoot, IProgressMonitor monitor) throws JavaModelException {
182         IClasspathEntry cpe= JavaCore.newVariableEntry(path, sourceAttachPath, sourceAttachRoot);
183         addToClasspath(jproject, cpe, monitor);
184     }
185     
186     /**
187      * Adds a library entry to an IJavaProject.
188      */

189     private void addLibrary(IJavaProject jproject, IPath path, IProgressMonitor monitor) throws JavaModelException {
190         IClasspathEntry cpe= JavaCore.newLibraryEntry(path, null, null);
191         addToClasspath(jproject, cpe, monitor);
192     }
193     
194     /**
195      * Adds a container entry to an IJavaProject.
196      */

197     private void addContainer(IJavaProject jproject, IPath path, IProgressMonitor monitor) throws CoreException {
198         IClasspathEntry cpe= JavaCore.newContainerEntry(path);
199         addToClasspath(jproject, cpe, monitor);
200         IProject project= jproject.getProject();
201         IFolder folder= project.getFolder(path.lastSegment());
202         if (!folder.exists()) {
203             folder.createLink(path, IResource.ALLOW_MISSING_LOCAL, monitor);
204         }
205     }
206 }
207
Popular Tags