KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IProjectDescription;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jdt.core.IClasspathEntry;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.core.compiler.CharOperation;
23 import org.eclipse.jdt.internal.core.util.Util;
24
25 public class ProjectReferenceChange {
26     
27     private JavaProject project;
28     private IClasspathEntry[] oldResolvedClasspath;
29     
30     public ProjectReferenceChange(JavaProject project, IClasspathEntry[] oldResolvedClasspath) {
31         this.project = project;
32         this.oldResolvedClasspath = oldResolvedClasspath;
33     }
34
35     /*
36      * Update projects references so that the build order is consistent with the classpath
37      */

38     public void updateProjectReferencesIfNecessary() throws JavaModelException {
39         
40         String JavaDoc[] oldRequired = this.oldResolvedClasspath == null ? CharOperation.NO_STRINGS : this.project.projectPrerequisites(this.oldResolvedClasspath);
41         IClasspathEntry[] newResolvedClasspath = this.project.getResolvedClasspath();
42         String JavaDoc[] newRequired = this.project.projectPrerequisites(newResolvedClasspath);
43         try {
44             IProject projectResource = this.project.getProject();
45             IProjectDescription description = projectResource.getDescription();
46              
47             IProject[] projectReferences = description.getDynamicReferences();
48             
49             HashSet JavaDoc oldReferences = new HashSet JavaDoc(projectReferences.length);
50             for (int i = 0; i < projectReferences.length; i++){
51                 String JavaDoc projectName = projectReferences[i].getName();
52                 oldReferences.add(projectName);
53             }
54             HashSet JavaDoc newReferences = (HashSet JavaDoc)oldReferences.clone();
55     
56             for (int i = 0; i < oldRequired.length; i++){
57                 String JavaDoc projectName = oldRequired[i];
58                 newReferences.remove(projectName);
59             }
60             for (int i = 0; i < newRequired.length; i++){
61                 String JavaDoc projectName = newRequired[i];
62                 newReferences.add(projectName);
63             }
64     
65             Iterator JavaDoc iter;
66             int newSize = newReferences.size();
67             
68             checkIdentity: {
69                 if (oldReferences.size() == newSize){
70                     iter = newReferences.iterator();
71                     while (iter.hasNext()){
72                         if (!oldReferences.contains(iter.next())){
73                             break checkIdentity;
74                         }
75                     }
76                     return;
77                 }
78             }
79             String JavaDoc[] requiredProjectNames = new String JavaDoc[newSize];
80             int index = 0;
81             iter = newReferences.iterator();
82             while (iter.hasNext()){
83                 requiredProjectNames[index++] = (String JavaDoc)iter.next();
84             }
85             Util.sort(requiredProjectNames); // ensure that if changed, the order is consistent
86

87             IProject[] requiredProjectArray = new IProject[newSize];
88             IWorkspaceRoot wksRoot = projectResource.getWorkspace().getRoot();
89             for (int i = 0; i < newSize; i++){
90                 requiredProjectArray[i] = wksRoot.getProject(requiredProjectNames[i]);
91             }
92             description.setDynamicReferences(requiredProjectArray);
93             projectResource.setDescription(description, null);
94     
95         } catch(CoreException e){
96             if (!ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(this.project.getElementName()))
97                 throw new JavaModelException(e);
98         }
99     }
100     public String JavaDoc toString() {
101         return "ProjectRefenceChange: " + this.project.getElementName(); //$NON-NLS-1$
102
}
103 }
104
Popular Tags