1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 import java.util.Map ; 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 41 public class JavaModel extends Openable implements IJavaModel { 42 43 48 public static HashSet existingExternalFiles = new HashSet (); 49 50 55 public static HashSet existingExternalConfirmedFiles = new HashSet (); 56 57 65 protected JavaModel() throws Error { 66 super(null); 67 } 68 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) { 69 70 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 92 public boolean contains(IResource resource) { 93 switch (resource.getType()) { 94 case IResource.ROOT: 95 case IResource.PROJECT: 96 return true; 97 } 98 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 116 public void copy(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String [] 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 126 protected Object createElementInfo() { 127 return new JavaModelInfo(); 128 } 129 130 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 o) { 141 if (!(o instanceof JavaModel)) return false; 142 return super.equals(o); 143 } 144 147 public int getElementType() { 148 return JAVA_MODEL; 149 } 150 153 public static void flushExternalFileCache() { 154 existingExternalFiles = new HashSet (); 155 existingExternalConfirmedFiles = new HashSet (); 156 } 157 158 161 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) { 162 switch (token.charAt(0)) { 163 case JEM_JAVAPROJECT: 164 if (!memento.hasMoreTokens()) return this; 165 String projectName = memento.nextToken(); 166 JavaElement project = (JavaElement)getJavaProject(projectName); 167 return project.getHandleFromMemento(memento, owner); 168 } 169 return null; 170 } 171 174 protected void getHandleMemento(StringBuffer buff) { 175 buff.append(getElementName()); 176 } 177 181 protected char getHandleMementoDelimiter(){ 182 Assert.isTrue(false, "Should not be called"); return 0; 184 } 185 188 public IJavaProject getJavaProject(String projectName) { 189 return new JavaProject(ResourcesPlugin.getWorkspace().getRoot().getProject(projectName), this); 190 } 191 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 (Messages.element_invalidResourceForProject); 209 } 210 } 211 214 public IJavaProject[] getJavaProjects() throws JavaModelException { 215 ArrayList list = getChildrenOfType(JAVA_PROJECT); 216 IJavaProject[] array= new IJavaProject[list.size()]; 217 list.toArray(array); 218 return array; 219 220 } 221 224 public Object [] getNonJavaResources() throws JavaModelException { 225 return ((JavaModelInfo) getElementInfo()).getNonJavaResources(); 226 } 227 228 231 public IPath getPath() { 232 return Path.ROOT; 233 } 234 237 public IResource getResource() { 238 return ResourcesPlugin.getWorkspace().getRoot(); 239 } 240 243 public IResource getUnderlyingResource() { 244 return null; 245 } 246 249 public IWorkspace getWorkspace() { 250 return ResourcesPlugin.getWorkspace(); 251 } 252 253 256 public void move(IJavaElement[] elements, IJavaElement[] containers, IJavaElement[] siblings, String [] 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 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 277 public void rename(IJavaElement[] elements, IJavaElement[] destinations, String [] 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 290 protected void runOperation(MultiOperation op, IJavaElement[] elements, IJavaElement[] siblings, String [] 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 302 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 303 buffer.append(this.tabString(tab)); 304 buffer.append("Java Model"); if (info == null) { 306 buffer.append(" (not open)"); } 308 } 309 310 315 public static Object getTarget(IContainer container, IPath path, boolean checkResourceExistence) { 316 317 if (path == null) return null; 318 319 if (path.getDevice() == null) { 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.isAbsolute()) return null; 333 334 return getTargetAsExternalFile(path, checkResourceExistence); 336 } 337 private synchronized static Object getTargetAsExternalFile(IPath path, boolean checkResourceExistence) { 338 File externalFile = new File (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()); } 347 if (externalFile.exists()) { 348 existingExternalFiles.add(externalFile); 350 return externalFile; 351 } 352 } 353 return null; 354 } 355 356 359 public static boolean isFile(Object target) { 360 return getFile(target) != null; 361 } 362 363 367 public static synchronized File getFile(Object target) { 368 if (existingExternalConfirmedFiles.contains(target)) 369 return (File ) target; 370 if (target instanceof File ) { 371 File f = (File ) target; 372 if (f.isFile()) { 373 existingExternalConfirmedFiles.add(f); 374 return f; 375 } 376 } 377 378 return null; 379 } 380 } 381 | Popular Tags |