KickJava   Java API By Example, From Geeks To Geeks.

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


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.core;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IWorkspace;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.Assert;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.jdt.core.*;
30 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
31 import org.eclipse.jdt.internal.core.util.Messages;
32
33 /**
34  * Implementation of <code>IJavaModel<code>. The Java Model maintains a cache of
35  * active <code>IJavaProject</code>s in a workspace. A Java Model is specific to a
36  * workspace. To retrieve a workspace's model, use the
37  * <code>#getJavaModel(IWorkspace)</code> method.
38  *
39  * @see IJavaModel
40  */

41 public class JavaModel extends Openable implements IJavaModel {
42
43     /**
44      * A set of java.io.Files used as a cache of external jars that
45      * are known to be existing.
46      * Note this cache is kept for the whole session.
47      */

48     public static HashSet JavaDoc existingExternalFiles = new HashSet JavaDoc();
49
50     /**
51      * A set of external files ({@link #existingExternalFiles}) which have
52      * been confirmed as file (ie. which returns true to {@link java.io.File#isFile()}.
53      * Note this cache is kept for the whole session.
54      */

55     public static HashSet JavaDoc existingExternalConfirmedFiles = new HashSet JavaDoc();
56
57 /**
58  * Constructs a new Java Model on the given workspace.
59  * Note that only one instance of JavaModel handle should ever be created.
60  * One should only indirect through JavaModelManager#getJavaModel() to get
61  * access to it.
62  *
63  * @exception Error if called more than once
64  */

65 protected JavaModel() throws Error JavaDoc {
66     super(null);
67 }
68 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map JavaDoc newElements, IResource underlyingResource) /*throws JavaModelException*/ {
69
70     // determine my children
71
IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
72     int length = projects.length;
73     IJavaElement[] children = new IJavaElement[length];
74     int index = 0;
75     for (int i = 0; i < length; i++) {
76         IProject project = projects[i];
77         if (JavaProject.hasJavaNature(project)) {
78             children[index++] = getJavaProject(project);
79         }
80     }
81     if (index < length)
82         System.arraycopy(children, 0, children = new IJavaElement[index], 0, index);
83     info.setChildren(children);
84
85     newElements.put(this, info);
86     
87     return true;
88 }
89 /*
90  * @see IJavaModel
91  */

92 public boolean contains(IResource resource) {
93     switch (resource.getType()) {
94         case IResource.ROOT:
95         case IResource.PROJECT:
96             return true;
97     }
98     // file or folder
99
IJavaProject[] projects;
100     try {
101         projects = this.getJavaProjects();
102     } catch (JavaModelException e) {
103         return false;
104     }
105     for (int i = 0, length = projects.length; i < length; i++) {
106         JavaProject project = (JavaProject)projects[i];
107         if (!project.contains(resource)) {
108             return false;
109         }
110     }
111     return true;
112 }
113 /**
114  * @see IJavaModel
115  */

116 public void copy(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String JavaDoc[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException {
117     if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < IJavaElement.TYPE) {
118         runOperation(new CopyResourceElementsOperation(elements, containers, force), elements, siblings, renamings, monitor);
119     } else {
120         runOperation(new CopyElementsOperation(elements, containers, force), elements, siblings, renamings, monitor);
121     }
122 }
123 /**
124  * Returns a new element info for this element.
125  */

126 protected Object JavaDoc createElementInfo() {
127     return new JavaModelInfo();
128 }
129
130 /**
131  * @see IJavaModel
132  */

133 public void delete(IJavaElement[] elements, boolean force, IProgressMonitor monitor) throws JavaModelException {
134     if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < IJavaElement.TYPE) {
135         new DeleteResourceElementsOperation(elements, force).runOperation(monitor);
136     } else {
137         new DeleteElementsOperation(elements, force).runOperation(monitor);
138     }
139 }
140 public boolean equals(Object JavaDoc o) {
141     if (!(o instanceof JavaModel)) return false;
142     return super.equals(o);
143 }
144 /**
145  * @see IJavaElement
146  */

147 public int getElementType() {
148     return JAVA_MODEL;
149 }
150 /**
151  * Flushes the cache of external files known to be existing.
152  */

153 public static void flushExternalFileCache() {
154     existingExternalFiles = new HashSet JavaDoc();
155     existingExternalConfirmedFiles = new HashSet JavaDoc();
156 }
157
158 /*
159  * @see JavaElement
160  */

161 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner owner) {
162     switch (token.charAt(0)) {
163         case JEM_JAVAPROJECT:
164             if (!memento.hasMoreTokens()) return this;
165             String JavaDoc projectName = memento.nextToken();
166             JavaElement project = (JavaElement)getJavaProject(projectName);
167             return project.getHandleFromMemento(memento, owner);
168     }
169     return null;
170 }
171 /**
172  * @see JavaElement#getHandleMemento(StringBuffer)
173  */

174 protected void getHandleMemento(StringBuffer JavaDoc buff) {
175     buff.append(getElementName());
176 }
177 /**
178  * Returns the <code>char</code> that marks the start of this handles
179  * contribution to a memento.
180  */

181 protected char getHandleMementoDelimiter(){
182     Assert.isTrue(false, "Should not be called"); //$NON-NLS-1$
183
return 0;
184 }
185 /**
186  * @see IJavaModel
187  */

188 public IJavaProject getJavaProject(String JavaDoc projectName) {
189     return new JavaProject(ResourcesPlugin.getWorkspace().getRoot().getProject(projectName), this);
190 }
191 /**
192  * Returns the active Java project associated with the specified
193  * resource, or <code>null</code> if no Java project yet exists
194  * for the resource.
195  *
196  * @exception IllegalArgumentException if the given resource
197  * is not one of an IProject, IFolder, or IFile.
198  */

199 public IJavaProject getJavaProject(IResource resource) {
200     switch(resource.getType()){
201         case IResource.FOLDER:
202             return new JavaProject(((IFolder)resource).getProject(), this);
203         case IResource.FILE:
204             return new JavaProject(((IFile)resource).getProject(), this);
205         case IResource.PROJECT:
206             return new JavaProject((IProject)resource, this);
207         default:
208             throw new IllegalArgumentException JavaDoc(Messages.element_invalidResourceForProject);
209     }
210 }
211 /**
212  * @see IJavaModel
213  */

214 public IJavaProject[] getJavaProjects() throws JavaModelException {
215     ArrayList JavaDoc list = getChildrenOfType(JAVA_PROJECT);
216     IJavaProject[] array= new IJavaProject[list.size()];
217     list.toArray(array);
218     return array;
219
220 }
221 /**
222  * @see IJavaModel
223  */

224 public Object JavaDoc[] getNonJavaResources() throws JavaModelException {
225         return ((JavaModelInfo) getElementInfo()).getNonJavaResources();
226 }
227
228 /*
229  * @see IJavaElement
230  */

231 public IPath getPath() {
232     return Path.ROOT;
233 }
234 /*
235  * @see IJavaElement
236  */

237 public IResource getResource() {
238     return ResourcesPlugin.getWorkspace().getRoot();
239 }
240 /**
241  * @see IOpenable
242  */

243 public IResource getUnderlyingResource() {
244     return null;
245 }
246 /**
247  * Returns the workbench associated with this object.
248  */

249 public IWorkspace getWorkspace() {
250     return ResourcesPlugin.getWorkspace();
251 }
252
253 /**
254  * @see IJavaModel
255  */

256 public void move(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String JavaDoc[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException {
257     if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < IJavaElement.TYPE) {
258         runOperation(new MoveResourceElementsOperation(elements, containers, force), elements, siblings, renamings, monitor);
259     } else {
260         runOperation(new MoveElementsOperation(elements, containers, force), elements, siblings, renamings, monitor);
261     }
262 }
263
264 /**
265  * @see IJavaModel#refreshExternalArchives(IJavaElement[], IProgressMonitor)
266  */

267 public void refreshExternalArchives(IJavaElement[] elementsScope, IProgressMonitor monitor) throws JavaModelException {
268     if (elementsScope == null){
269         elementsScope = new IJavaElement[] { this };
270     }
271     JavaModelManager.getJavaModelManager().getDeltaProcessor().checkExternalArchiveChanges(elementsScope, monitor);
272 }
273
274 /**
275  * @see IJavaModel
276  */

277 public void rename(IJavaElement[] elements, IJavaElement[] destinations, String JavaDoc[] renamings, boolean force, IProgressMonitor monitor) throws JavaModelException {
278     MultiOperation op;
279     if (elements != null && elements.length > 0 && elements[0] != null && elements[0].getElementType() < IJavaElement.TYPE) {
280         op = new RenameResourceElementsOperation(elements, destinations, renamings, force);
281     } else {
282         op = new RenameElementsOperation(elements, destinations, renamings, force);
283     }
284     
285     op.runOperation(monitor);
286 }
287 /**
288  * Configures and runs the <code>MultiOperation</code>.
289  */

290 protected void runOperation(MultiOperation op, IJavaElement[] elements, IJavaElement[] siblings, String JavaDoc[] renamings, IProgressMonitor monitor) throws JavaModelException {
291     op.setRenamings(renamings);
292     if (siblings != null) {
293         for (int i = 0; i < elements.length; i++) {
294             op.setInsertBefore(elements[i], siblings[i]);
295         }
296     }
297     op.runOperation(monitor);
298 }
299 /**
300  * @private Debugging purposes
301  */

302 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
303     buffer.append(this.tabString(tab));
304     buffer.append("Java Model"); //$NON-NLS-1$
305
if (info == null) {
306         buffer.append(" (not open)"); //$NON-NLS-1$
307
}
308 }
309
310 /**
311  * Helper method - returns the targeted item (IResource if internal or java.io.File if external),
312  * or null if unbound
313  * Internal items must be referred to using container relative paths.
314  */

315 public static Object JavaDoc getTarget(IContainer container, IPath path, boolean checkResourceExistence) {
316
317     if (path == null) return null;
318     
319     // lookup - inside the container
320
if (path.getDevice() == null) { // container relative paths should not contain a device
321
// (see http://dev.eclipse.org/bugs/show_bug.cgi?id=18684)
322
// (case of a workspace rooted at d:\ )
323
IResource resource = container.findMember(path);
324         if (resource != null){
325             if (!checkResourceExistence ||resource.exists()) return resource;
326             return null;
327         }
328     }
329     
330     // if path is relative, it cannot be an external path
331
// (see http://dev.eclipse.org/bugs/show_bug.cgi?id=22517)
332
if (!path.isAbsolute()) return null;
333
334     // lookup - outside the container
335
return getTargetAsExternalFile(path, checkResourceExistence);
336 }
337 private synchronized static Object JavaDoc getTargetAsExternalFile(IPath path, boolean checkResourceExistence) {
338     File JavaDoc externalFile = new File JavaDoc(path.toOSString());
339     if (!checkResourceExistence) {
340         return externalFile;
341     } else if (existingExternalFiles.contains(externalFile)) {
342         return externalFile;
343     } else {
344         if (JavaModelManager.ZIP_ACCESS_VERBOSE) {
345             System.out.println("(" + Thread.currentThread() + ") [JavaModel.getTarget(...)] Checking existence of " + path.toString()); //$NON-NLS-1$ //$NON-NLS-2$
346
}
347         if (externalFile.exists()) {
348             // cache external file
349
existingExternalFiles.add(externalFile);
350             return externalFile;
351         }
352     }
353     return null;
354 }
355
356 /**
357  * Helper method - returns whether an object is afile (ie. which returns true to {@link java.io.File#isFile()}.
358  */

359 public static boolean isFile(Object JavaDoc target) {
360     return getFile(target) != null;
361 }
362
363 /**
364  * Helper method - returns the file item (ie. which returns true to {@link java.io.File#isFile()},
365  * or null if unbound
366  */

367 public static synchronized File JavaDoc getFile(Object JavaDoc target) {
368     if (existingExternalConfirmedFiles.contains(target))
369         return (File JavaDoc) target;
370     if (target instanceof File JavaDoc) {
371         File JavaDoc f = (File JavaDoc) target;
372         if (f.isFile()) {
373             existingExternalConfirmedFiles.add(f);
374             return f;
375         }
376     }
377     
378     return null;
379 }
380 }
381
Popular Tags