KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17
18 import org.eclipse.jdt.core.IClasspathEntry;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaCore;
22
23 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
24 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27
28 public class DeleteFromClasspathChange extends JDTChange {
29
30     private final String JavaDoc fProjectHandle;
31     private final IPath fPathToDelete;
32     
33     private IPath fPath;
34     private IPath fSourceAttachmentPath;
35     private IPath fSourceAttachmentRootPath;
36     private int fEntryKind;
37     
38     public DeleteFromClasspathChange(IPackageFragmentRoot root) {
39         this(root.getPath(), root.getJavaProject());
40     }
41     
42     DeleteFromClasspathChange(IPath pathToDelete, IJavaProject project){
43         Assert.isNotNull(pathToDelete);
44         fPathToDelete= pathToDelete;
45         fProjectHandle= project.getHandleIdentifier();
46     }
47     
48     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
49         // we have checked the .classpath file in the delete change.
50
return super.isValid(pm, READ_ONLY | DIRTY);
51     }
52     
53     public Change perform(IProgressMonitor pm) throws CoreException {
54         pm.beginTask(getName(), 1);
55         try{
56             IJavaProject project= getJavaProject();
57             IClasspathEntry[] cp= project.getRawClasspath();
58             IClasspathEntry[] newCp= new IClasspathEntry[cp.length-1];
59             int i= 0;
60             int j= 0;
61             while (j < newCp.length) {
62                 IClasspathEntry current= JavaCore.getResolvedClasspathEntry(cp[i]);
63                 if (current != null && toBeDeleted(current)) {
64                     i++;
65                     setDeletedEntryProperties(current);
66                 }
67
68                 newCp[j]= cp[i];
69                 i++;
70                 j++;
71             }
72             
73             IClasspathEntry last= JavaCore.getResolvedClasspathEntry(cp[cp.length - 1]);
74             if (last != null && toBeDeleted(last))
75                 setDeletedEntryProperties(last);
76                 
77             project.setRawClasspath(newCp, pm);
78             
79             return new AddToClasspathChange(getJavaProject(), fEntryKind, fPath,
80                 fSourceAttachmentPath, fSourceAttachmentRootPath);
81         } finally {
82             pm.done();
83         }
84     }
85     
86     private boolean toBeDeleted(IClasspathEntry entry){
87         if (entry == null) //safety net
88
return false;
89         return fPathToDelete.equals(entry.getPath());
90     }
91     
92     private void setDeletedEntryProperties(IClasspathEntry entry){
93         fEntryKind= entry.getEntryKind();
94         fPath= entry.getPath();
95         fSourceAttachmentPath= entry.getSourceAttachmentPath();
96         fSourceAttachmentRootPath= entry.getSourceAttachmentRootPath();
97     }
98     
99     private IJavaProject getJavaProject(){
100         return (IJavaProject)JavaCore.create(fProjectHandle);
101     }
102     
103     public String JavaDoc getName() {
104         return RefactoringCoreMessages.DeleteFromClassPathChange_remove + getJavaProject().getElementName();
105     }
106
107     public Object JavaDoc getModifiedElement() {
108         return getJavaProject();
109     }
110 }
111
Popular Tags