KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > AddToClasspathChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.corext.refactoring.changes;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22
23 import org.eclipse.jdt.core.IClasspathEntry;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.JavaConventions;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28
29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
30 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
31 import org.eclipse.ltk.core.refactoring.Change;
32 import org.eclipse.ltk.core.refactoring.NullChange;
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34
35 public class AddToClasspathChange extends JDTChange {
36     
37     private IJavaProject fProjectHandle;
38     private IClasspathEntry fEntryToAdd;
39     
40     public AddToClasspathChange(IJavaProject project, IClasspathEntry entryToAdd) {
41         fProjectHandle= project;
42         fEntryToAdd= entryToAdd;
43     }
44     
45     public AddToClasspathChange(IJavaProject project, String JavaDoc sourceFolderName){
46         this(project, JavaCore.newSourceEntry(project.getPath().append(sourceFolderName)));
47     }
48     
49     /**
50      * Adds a new project class path entry to the project.
51      * @param project
52      * @param newProjectEntry (must be absolute <code>IPath</code>)
53      */

54     public AddToClasspathChange(IJavaProject project, IPath newProjectEntry){
55         this(project, JavaCore.newProjectEntry(newProjectEntry));
56     }
57     
58     public AddToClasspathChange(IJavaProject project, int entryKind, IPath path, IPath sourceAttachmentPath, IPath sourceAttachmentRootPath){
59         this(project, createNewClasspathEntry(entryKind, path, sourceAttachmentPath, sourceAttachmentRootPath));
60     }
61
62     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
63         // .classpath file will be handled by JDT/Core.
64
return super.isValid(pm, READ_ONLY | DIRTY);
65     }
66     
67     public Change perform(IProgressMonitor pm) throws CoreException {
68         pm.beginTask(getName(), 1);
69         try {
70             if (validateClasspath()) {
71                 getJavaProject().setRawClasspath(getNewClasspathEntries(), new SubProgressMonitor(pm, 1));
72                 IPath classpathEntryPath= JavaCore.getResolvedClasspathEntry(fEntryToAdd).getPath();
73                 return new DeleteFromClasspathChange(classpathEntryPath, getJavaProject());
74             } else {
75                 return new NullChange();
76             }
77         } finally {
78             pm.done();
79         }
80     }
81     
82     public boolean validateClasspath() throws JavaModelException {
83         IJavaProject javaProject= getJavaProject();
84         IPath outputLocation= javaProject.getOutputLocation();
85         IClasspathEntry[] newClasspathEntries= getNewClasspathEntries();
86         return JavaConventions.validateClasspath(javaProject, newClasspathEntries, outputLocation).isOK();
87     }
88     
89     private IClasspathEntry[] getNewClasspathEntries() throws JavaModelException{
90         IClasspathEntry[] entries= getJavaProject().getRawClasspath();
91         List JavaDoc cp= new ArrayList JavaDoc(entries.length + 1);
92         cp.addAll(Arrays.asList(entries));
93         cp.add(fEntryToAdd);
94         return (IClasspathEntry[])cp.toArray(new IClasspathEntry[cp.size()]);
95     }
96     
97     private static IClasspathEntry createNewClasspathEntry(int kind, IPath path, IPath sourceAttach, IPath sourceAttachRoot){
98         switch(kind){
99             case IClasspathEntry.CPE_LIBRARY:
100                 return JavaCore.newLibraryEntry(path, sourceAttach, sourceAttachRoot);
101             case IClasspathEntry.CPE_PROJECT:
102                 return JavaCore.newProjectEntry(path);
103             case IClasspathEntry.CPE_SOURCE:
104                 return JavaCore.newSourceEntry(path);
105             case IClasspathEntry.CPE_VARIABLE:
106                 return JavaCore.newVariableEntry(path, sourceAttach, sourceAttachRoot);
107             case IClasspathEntry.CPE_CONTAINER:
108                 return JavaCore.newContainerEntry(path);
109             default:
110                 Assert.isTrue(false);
111                 return null;
112         }
113     }
114     
115     private IJavaProject getJavaProject(){
116         return fProjectHandle;
117     }
118
119     public String JavaDoc getName() {
120         return RefactoringCoreMessages.AddToClasspathChange_add + getJavaProject().getElementName();
121  
122     }
123
124     public Object JavaDoc getModifiedElement() {
125         return getJavaProject();
126     }
127     
128     public IClasspathEntry getClasspathEntry() {
129         return fEntryToAdd;
130     }
131 }
132
Popular Tags