1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.changes;
13 import java.util.ArrayList;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.core.resources.IResource;
23 import org.eclipse.ltk.core.refactoring.Change;
24 import org.eclipse.ltk.core.refactoring.NullChange;
25 import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
27 import org.eclipse.jdt.core.IClasspathEntry;
28 import org.eclipse.jdt.core.IJavaProject;
29 import org.eclipse.jdt.core.JavaConventions;
30 import org.eclipse.jdt.core.JavaModelException;
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
34 public class ClasspathChange extends ResourceChange {
36 public static ClasspathChange addEntryChange(IJavaProject project, IClasspathEntry entryToAdd) throws JavaModelException {
37 IClasspathEntry[] rawClasspath= project.getRawClasspath();
38 IClasspathEntry[] newClasspath= new IClasspathEntry[rawClasspath.length + 1];
39 System.arraycopy(rawClasspath, 0, newClasspath, 0, rawClasspath.length);
40 newClasspath[rawClasspath.length]= entryToAdd;
42 IPath outputLocation= project.getOutputLocation();
44 return newChange(project, newClasspath, outputLocation);
47 public static ClasspathChange removeEntryChange(IJavaProject project, IClasspathEntry entryToRemove) throws JavaModelException {
48 IClasspathEntry[] rawClasspath= project.getRawClasspath();
49 ArrayList<IClasspathEntry> newClasspath= new ArrayList<IClasspathEntry>();
50 for (int i= 0; i < rawClasspath.length; i++) {
51 IClasspathEntry curr= rawClasspath[i];
52 if (curr.getEntryKind() != entryToRemove.getEntryKind() || !curr.getPath().equals(entryToRemove.getPath())) {
53 newClasspath.add(curr);
56 IClasspathEntry[] entries= newClasspath.toArray(new IClasspathEntry[newClasspath.size()]);
57 IPath outputLocation= project.getOutputLocation();
59 return newChange(project, entries, outputLocation);
62 public static ClasspathChange newChange(IJavaProject project, IClasspathEntry[] newClasspath, IPath outputLocation) {
63 if (!JavaConventions.validateClasspath(project, newClasspath, outputLocation).matches(IStatus.ERROR)) {
64 return new ClasspathChange(project, newClasspath, outputLocation);
69 private IJavaProject fProject;
70 private IClasspathEntry[] fNewClasspath;
71 private final IPath fOutputLocation;
73 public ClasspathChange(IJavaProject project, IClasspathEntry[] newClasspath, IPath outputLocation) {
75 fNewClasspath= newClasspath;
76 fOutputLocation= outputLocation;
78 setValidationMethod(VALIDATE_NOT_DIRTY | VALIDATE_NOT_READ_ONLY);
82 public Change perform(IProgressMonitor pm) throws CoreException {
83 pm.beginTask(RefactoringCoreMessages.ClasspathChange_progress_message, 1);
85 if (!JavaConventions.validateClasspath(fProject, fNewClasspath, fOutputLocation).matches(IStatus.ERROR)) {
86 IClasspathEntry[] oldClasspath= fProject.getRawClasspath();
87 IPath oldOutputLocation= fProject.getOutputLocation();
89 fProject.setRawClasspath(fNewClasspath, fOutputLocation, new SubProgressMonitor(pm, 1));
91 return new ClasspathChange(fProject, oldClasspath, oldOutputLocation);
93 return new NullChange();
101 public String getName() {
102 return RefactoringCoreMessages.ClasspathChange_change_name;
106 protected IResource getModifiedResource() {
107 return fProject.getResource();
111 public Object getModifiedElement() {