KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > SetClasspathOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core;
12
13 import org.eclipse.core.runtime.IPath;
14 import org.eclipse.jdt.core.IClasspathEntry;
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jdt.core.IJavaModelStatus;
17 import org.eclipse.jdt.core.IJavaProject;
18 import org.eclipse.jdt.core.JavaModelException;
19
20 /**
21  * This operation sets an <code>IJavaProject</code>'s classpath.
22  *
23  * @see IJavaProject
24  */

25 public class SetClasspathOperation extends ChangeClasspathOperation {
26
27     IClasspathEntry[] newRawClasspath;
28     IPath newOutputLocation;
29     JavaProject project;
30             
31     /**
32      * When executed, this operation sets the raw classpath and output location of the given project.
33      */

34     public SetClasspathOperation(
35         JavaProject project,
36         IClasspathEntry[] newRawClasspath,
37         IPath newOutputLocation,
38         boolean canChangeResource) {
39
40         super(new IJavaElement[] { project }, canChangeResource);
41         this.project = project;
42         this.newRawClasspath = newRawClasspath;
43         this.newOutputLocation = newOutputLocation;
44     }
45
46     /**
47      * Sets the classpath of the pre-specified project.
48      */

49     protected void executeOperation() throws JavaModelException {
50         checkCanceled();
51         try {
52             // set raw classpath and null out resolved info
53
this.project.getPerProjectInfo().setClasspath(this.newRawClasspath, this.newOutputLocation, JavaModelStatus.VERIFIED_OK/*format is ok*/, null, null, null, null);
54             
55             // if needed, generate delta, update project ref, create markers, ...
56
classpathChanged(this.project);
57             
58             // write .classpath file
59
if (this.canChangeResources && this.project.saveClasspath(this.newRawClasspath, this.newOutputLocation))
60                 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
61         } finally {
62             done();
63         }
64     }
65
66     public String JavaDoc toString(){
67         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(20);
68         buffer.append("SetClasspathOperation\n"); //$NON-NLS-1$
69
buffer.append(" - classpath : "); //$NON-NLS-1$
70
buffer.append("{"); //$NON-NLS-1$
71
for (int i = 0; i < this.newRawClasspath.length; i++) {
72             if (i > 0) buffer.append(","); //$NON-NLS-1$
73
IClasspathEntry element = this.newRawClasspath[i];
74             buffer.append(" ").append(element.toString()); //$NON-NLS-1$
75
}
76         buffer.append("\n - output location : "); //$NON-NLS-1$
77
buffer.append(this.newOutputLocation.toString());
78         return buffer.toString();
79     }
80
81     public IJavaModelStatus verify() {
82         IJavaModelStatus status = super.verify();
83         if (!status.isOK())
84             return status;
85         return ClasspathEntry.validateClasspath( this.project, this.newRawClasspath, this.newOutputLocation);
86     }
87
88 }
89
Popular Tags