f73c8bdd3745b8d9f0675d848f54a46ecc001d53
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / PackageFragmentRoot.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.ArrayList;
14 import java.util.Map;
15
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IJavaProject;
18 import net.sourceforge.phpdt.core.IPackageFragment;
19 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
20 import net.sourceforge.phpdt.core.JavaModelException;
21 import net.sourceforge.phpdt.core.JavaCore;
22 import net.sourceforge.phpdt.core.compiler.CharOperation;
23 import net.sourceforge.phpdt.internal.core.util.Util;
24
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IFolder;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.resources.ResourcesPlugin;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.IProgressMonitor;
32 import org.eclipse.core.runtime.QualifiedName;
33 import net.sourceforge.phpdt.core.IClasspathEntry;
34 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
35 import net.sourceforge.phpdt.internal.core.Openable;
36 import net.sourceforge.phpdt.internal.core.OpenableElementInfo;
37
38 import net.sourceforge.phpdt.internal.core.JavaProject;
39 import net.sourceforge.phpdt.internal.core.PackageFragmentRootInfo;
40
41
42 /**
43  * @see IPackageFragmentRoot
44  */
45 public class PackageFragmentRoot extends Openable implements IPackageFragmentRoot {
46
47         /**
48          * The delimiter between the source path and root path in the
49          * attachment server property.
50          */
51         protected final static char ATTACHMENT_PROPERTY_DELIMITER= '*';
52         /*
53          * No source attachment property
54          */
55         protected final static String NO_SOURCE_ATTACHMENT = ""; //$NON-NLS-1$
56         /*
57          * No source mapper singleton
58          */
59 //      protected final static SourceMapper NO_SOURCE_MAPPER = new SourceMapper();
60
61         /**
62          * The resource associated with this root.
63          * (an IResource or a java.io.File (for external jar only))
64          */
65         protected Object resource;
66         
67         /**
68          * Constructs a package fragment root which is the root of the java package
69          * directory hierarchy.
70          */
71         protected PackageFragmentRoot(IResource resource, JavaProject project, String name) {
72                 super(project, name);
73                 this.resource = resource;
74         }
75
76         /**
77          * @see Openable
78          */
79         protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
80                 
81                 // check whether this pkg fragment root can be opened
82                 if (!resourceExists() ) { //|| !isOnClasspath()) {
83                         throw newNotPresentException();
84                 }
85
86                 ((PackageFragmentRootInfo) info).setRootKind(determineKind(underlyingResource));
87                 return computeChildren(info, newElements);
88         }
89         /**
90          * Returns the root's kind - K_SOURCE or K_BINARY, defaults
91          * to K_SOURCE if it is not on the classpath.
92          *
93          * @exception NotPresentException if the project and root do
94          *              not exist.
95          */
96         protected int determineKind(IResource underlyingResource) throws JavaModelException {
97 //              IClasspathEntry[] entries= ((JavaProject)getJavaProject()).getExpandedClasspath(true);
98 //              for (int i= 0; i < entries.length; i++) {
99 //                      IClasspathEntry entry= entries[i];
100 //                      if (entry.getPath().equals(underlyingResource.getFullPath())) {
101 //                              return entry.getContentKind();
102 //                      }
103 //              }
104                 return IPackageFragmentRoot.K_SOURCE;
105         }
106         /**
107          * Compute the package fragment children of this package fragment root.
108          * 
109          * @exception JavaModelException  The resource associated with this package fragment root does not exist
110          */
111         protected boolean computeChildren(OpenableElementInfo info, Map newElements) throws JavaModelException {
112                 // Note the children are not opened (so not added to newElements) for a regular package fragment root
113                 // Howver they are opened for a Jar package fragment root (see JarPackageFragmentRoot#computeChildren)
114                 try {
115                         // the underlying resource may be a folder or a project (in the case that the project folder
116                         // is actually the package fragment root)
117                         IResource underlyingResource = getResource();
118                         if (underlyingResource.getType() == IResource.FOLDER || underlyingResource.getType() == IResource.PROJECT) {
119                                 ArrayList vChildren = new ArrayList(5);
120                                 IContainer rootFolder = (IContainer) underlyingResource;
121                         //      char[][] inclusionPatterns = fullInclusionPatternChars();
122                                 char[][] exclusionPatterns = fullExclusionPatternChars();
123                                 computeFolderChildren(rootFolder, !Util.isExcluded(rootFolder, exclusionPatterns), "", vChildren, exclusionPatterns); //$NON-NLS-1$
124                                 
125                                 IJavaElement[] children = new IJavaElement[vChildren.size()];
126                                 vChildren.toArray(children);
127                                 info.setChildren(children);
128                         }
129                 } catch (JavaModelException e) {
130                         //problem resolving children; structure remains unknown
131                         info.setChildren(new IJavaElement[]{});
132                         throw e;
133                 }
134                 return true;
135         }
136         /**
137          * Starting at this folder, create package fragments and add the fragments that are not exclused
138          * to the collection of children.
139          * 
140          * @exception JavaModelException  The resource associated with this package fragment does not exist
141          */
142         protected void computeFolderChildren(IContainer folder, boolean isIncluded, String prefix, ArrayList vChildren, char[][] exclusionPatterns) throws JavaModelException {
143                 //, char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {
144
145                 if (isIncluded) {
146                     IPackageFragment pkg = getPackageFragment(prefix);
147                         vChildren.add(pkg);
148                 }
149                 try {
150                         JavaProject javaProject = (JavaProject)getJavaProject();
151                         IResource[] members = folder.members();
152                         boolean hasIncluded = isIncluded;
153                         for (int i = 0, max = members.length; i < max; i++) {
154                                 IResource member = members[i];
155                                 String memberName = member.getName();
156                                 
157                                 switch(member.getType()) {
158                                     
159                                     case IResource.FOLDER:
160                                                 if (Util.isValidFolderNameForPackage(memberName)) {
161                                                     boolean isMemberIncluded = !Util.isExcluded(member, exclusionPatterns);
162                                                         // keep looking inside as long as included already, or may have child included due to inclusion patterns
163 //                                                  if (isMemberIncluded || inclusionPatterns != null) { 
164 //                                                              // eliminate binary output only if nested inside direct subfolders
165 //                                                              if (javaProject.contains(member)) {
166 //                                                                      String newPrefix;
167 //                                                                      if (prefix.length() == 0) {
168 //                                                                              newPrefix = memberName;
169 //                                                                      } else {
170 //                                                                              newPrefix = prefix + "." + memberName; //$NON-NLS-1$
171 //                                                                      }
172 //                                                                      computeFolderChildren((IFolder) member, isMemberIncluded, newPrefix, vChildren, inclusionPatterns, exclusionPatterns);
173 //                                                              }
174 //                                                      }
175                                                 }
176                                         break;
177                                     case IResource.FILE:
178                                         // inclusion filter may only include files, in which case we still want to include the immediate parent package (lazily)
179                                                 if (!hasIncluded
180                                                                         && Util.isValidCompilationUnitName(memberName)
181                                                                         && !Util.isExcluded(member, exclusionPatterns)) {
182                                                         hasIncluded = true;
183                                                     IPackageFragment pkg = getPackageFragment(prefix);
184                                                     vChildren.add(pkg); 
185                                                 }
186                                         break;
187                                 }
188                         }
189                 } catch(IllegalArgumentException e){
190                         throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); // could be thrown by ElementTree when path is not found
191                 } catch (CoreException e) {
192                         throw new JavaModelException(e);
193                 }
194         }
195 /**
196  * @see IPackageFragmentRoot
197  */
198 //public void attachSource(IPath sourcePath, IPath rootPath, IProgressMonitor monitor) throws JavaModelException {
199 //      try {
200 //              verifyAttachSource(sourcePath);
201 //              if (monitor != null) {
202 //                      monitor.beginTask(Util.bind("element.attachingSource"), 2); //$NON-NLS-1$
203 //              }
204 //              SourceMapper oldMapper= getSourceMapper();
205 //              IWorkspace workspace = ResourcesPlugin.getWorkspace();
206 //              boolean rootNeedsToBeClosed= false;
207 //
208 //              if (sourcePath == null) {
209 //                      //source being detached
210 //                      rootNeedsToBeClosed= true;
211 //                      setSourceMapper(null);
212 //              /* Disable deltas (see 1GDTUSD)
213 //                      // fire a delta to notify the UI about the source detachement.
214 //                      JavaModelManager manager = (JavaModelManager) JavaModelManager.getJavaModelManager();
215 //                      JavaModel model = (JavaModel) getJavaModel();
216 //                      JavaElementDelta attachedSourceDelta = new JavaElementDelta(model);
217 //                      attachedSourceDelta .sourceDetached(this); // this would be a PackageFragmentRoot
218 //                      manager.registerResourceDelta(attachedSourceDelta );
219 //                      manager.fire(); // maybe you want to fire the change later. Let us know about it.
220 //              */
221 //              } else {
222 //              /*
223 //                      // fire a delta to notify the UI about the source attachement.
224 //                      JavaModelManager manager = (JavaModelManager) JavaModelManager.getJavaModelManager();
225 //                      JavaModel model = (JavaModel) getJavaModel();
226 //                      JavaElementDelta attachedSourceDelta = new JavaElementDelta(model);
227 //                      attachedSourceDelta .sourceAttached(this); // this would be a PackageFragmentRoot
228 //                      manager.registerResourceDelta(attachedSourceDelta );
229 //                      manager.fire(); // maybe you want to fire the change later. Let us know about it.
230 //               */
231 //
232 //                      //check if different from the current attachment
233 //                      IPath storedSourcePath= getSourceAttachmentPath();
234 //                      IPath storedRootPath= getSourceAttachmentRootPath();
235 //                      if (monitor != null) {
236 //                              monitor.worked(1);
237 //                      }
238 //                      if (storedSourcePath != null) {
239 //                              if (!(storedSourcePath.equals(sourcePath) && (rootPath != null && rootPath.equals(storedRootPath)) || storedRootPath == null)) {
240 //                                      rootNeedsToBeClosed= true;
241 //                              }
242 //                      }
243 //                      // check if source path is valid
244 //                      Object target = JavaModel.getTarget(workspace.getRoot(), sourcePath, false);
245 //                      if (target == null) {
246 //                              if (monitor != null) {
247 //                                      monitor.done();
248 //                              }
249 //                              throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, sourcePath));
250 //                      }
251 //                      SourceMapper mapper = createSourceMapper(sourcePath, rootPath);
252 //                      if (rootPath == null && mapper.rootPath != null) {
253 //                              // as a side effect of calling the SourceMapper constructor, the root path was computed
254 //                              rootPath = new Path(mapper.rootPath);
255 //                      }
256 //                      setSourceMapper(mapper);
257 //              }
258 //              if (sourcePath == null) {
259 //                      setSourceAttachmentProperty(null); //remove the property
260 //              } else {
261 //                      //set the property to the path of the mapped source
262 //                      setSourceAttachmentProperty(
263 //                              sourcePath.toString() 
264 //                              + (rootPath == null ? "" : (ATTACHMENT_PROPERTY_DELIMITER + rootPath.toString()))); //$NON-NLS-1$
265 //              }
266 //              if (rootNeedsToBeClosed) {
267 //                      if (oldMapper != null) {
268 //                              oldMapper.close();
269 //                      }
270 //                      BufferManager manager= BufferManager.getDefaultBufferManager();
271 //                      Enumeration openBuffers= manager.getOpenBuffers();
272 //                      while (openBuffers.hasMoreElements()) {
273 //                              IBuffer buffer= (IBuffer) openBuffers.nextElement();
274 //                              IOpenable possibleMember= buffer.getOwner();
275 //                              if (isAncestorOf((IJavaElement) possibleMember)) {
276 //                                      buffer.close();
277 //                              }
278 //                      }
279 //                      if (monitor != null) {
280 //                              monitor.worked(1);
281 //                      }
282 //              }
283 //      } catch (JavaModelException e) {
284 //              setSourceAttachmentProperty(null); // loose info - will be recomputed
285 //              throw e;
286 //      } finally {
287 //              if (monitor != null) {
288 //                      monitor.done();
289 //              }
290 //      }
291 //}
292
293 //SourceMapper createSourceMapper(IPath sourcePath, IPath rootPath) {
294 //      SourceMapper mapper = new SourceMapper(
295 //              sourcePath, 
296 //              rootPath == null ? null : rootPath.toOSString(), 
297 //              this.isExternal() ? JavaCore.getOptions() : this.getJavaProject().getOptions(true)); // only project options if associated with resource
298 //      return mapper;
299 //}
300 /*
301  * @see org.eclipse.jdt.core.IPackageFragmentRoot#delete
302  */
303 //public void delete(
304 //      int updateResourceFlags,
305 //      int updateModelFlags,
306 //      IProgressMonitor monitor)
307 //      throws JavaModelException {
308 //
309 //      DeletePackageFragmentRootOperation op = new DeletePackageFragmentRootOperation(this, updateResourceFlags, updateModelFlags);
310 //      runOperation(op, monitor);
311 //}
312
313 /**
314  * This root is being closed.  If this root has an associated source attachment, 
315  * close it too.
316  *
317  * @see JavaElement
318  */
319 //protected void closing(Object info) throws JavaModelException { TODO remove after 2.1
320 //      ((PackageFragmentRootInfo) info).sourceMapper = null;
321 //      super.closing(info);
322 //}
323 /**
324  * Compute the package fragment children of this package fragment root.
325  * 
326  * @exception JavaModelException  The resource associated with this package fragment root does not exist
327  */
328 //protected boolean computeChildren(OpenableElementInfo info) throws JavaModelException {
329 //      try {
330 //              // the underlying resource may be a folder or a project (in the case that the project folder
331 //              // is actually the package fragment root)
332 //              IResource resource = getResource();
333 //              if (resource.getType() == IResource.FOLDER || resource.getType() == IResource.PROJECT) {
334 //                      ArrayList vChildren = new ArrayList(5);
335 //                      char[][] exclusionPatterns = fullExclusionPatternChars();
336 //                      computeFolderChildren((IContainer) resource, "", vChildren, exclusionPatterns); //$NON-NLS-1$
337 //                      IJavaElement[] children = new IJavaElement[vChildren.size()];
338 //                      vChildren.toArray(children);
339 //                      info.setChildren(children);
340 //              }
341 //      } catch (JavaModelException e) {
342 //              //problem resolving children; structure remains unknown
343 //              info.setChildren(new IJavaElement[]{});
344 //              throw e;
345 //      }
346 //      return true;
347 //}
348
349 /**
350  * Starting at this folder, create package fragments and add the fragments that are not exclused
351  * to the collection of children.
352  * 
353  * @exception JavaModelException  The resource associated with this package fragment does not exist
354  */
355 //protected void computeFolderChildren(IContainer folder, String prefix, ArrayList vChildren, char[][] exclusionPatterns) throws JavaModelException {
356 //      IPackageFragment pkg = getPackageFragment(prefix);
357 //      vChildren.add(pkg);
358 //      try {
359 //              JavaProject javaProject = (JavaProject)getJavaProject();
360 //              IResource[] members = folder.members();
361 //              for (int i = 0, max = members.length; i < max; i++) {
362 //                      IResource member = members[i];
363 //                      String memberName = member.getName();
364 //                      if (member.getType() == IResource.FOLDER 
365 //                              && Util.isValidFolderNameForPackage(memberName)
366 //                              && !Util.isExcluded(member, exclusionPatterns)) {
367 //                                      
368 //                              // eliminate binary output only if nested inside direct subfolders
369 //                              if (javaProject.contains(member)) {
370 //                                      String newPrefix;
371 //                                      if (prefix.length() == 0) {
372 //                                              newPrefix = memberName;
373 //                                      } else {
374 //                                              newPrefix = prefix + "." + memberName; //$NON-NLS-1$
375 //                                      }
376 //                                      computeFolderChildren((IFolder) member, newPrefix, vChildren, exclusionPatterns);
377 //                              }
378 //                      }
379 //              }
380 //      } catch(IllegalArgumentException e){
381 //              throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); // could be thrown by ElementTree when path is not found
382 //      } catch (CoreException e) {
383 //              throw new JavaModelException(e);
384 //      }
385 //}
386 /*
387  * Computes and returns the source attachment root path for the given source attachment path.
388  * Returns <code>null</code> if none could be found.
389  * 
390  * @param sourceAttachmentPath the given absolute path to the source archive or folder
391  * @return the computed source attachment root path or <code>null</cde> if none could be found
392  * @throws JavaModelException
393  */
394 //public IPath computeSourceAttachmentRootPath(IPath sourceAttachmentPath) throws JavaModelException {
395 //      IPath sourcePath = this.getSourceAttachmentPath();
396 //      if (sourcePath == null) return null;
397 //      SourceMapper mapper = 
398 //              new SourceMapper(
399 //              sourcePath, 
400 //              null, // detect root path
401 //              this.isExternal() ? JavaCore.getOptions() : this.getJavaProject().getOptions(true) // only project options if associated with resource
402 //      );
403 //      if (mapper.rootPath == null) return null;
404 //      return new Path(mapper.rootPath);
405 //}
406 /*
407  * @see org.eclipse.jdt.core.IPackageFragmentRoot#copy
408  */
409 //public void copy(
410 //      IPath destination,
411 //      int updateResourceFlags,
412 //      int updateModelFlags,
413 //      IClasspathEntry sibling,
414 //      IProgressMonitor monitor)
415 //      throws JavaModelException {
416 //              
417 //      CopyPackageFragmentRootOperation op = 
418 //              new CopyPackageFragmentRootOperation(this, destination, updateResourceFlags, updateModelFlags, sibling);
419 //      runOperation(op, monitor);
420 //}
421
422
423
424         /**
425          * Returns a new element info for this element.
426          */
427         protected Object createElementInfo() {
428                 return new PackageFragmentRootInfo();
429         }
430
431 /**
432  * @see IPackageFragmentRoot
433  */
434 //public IPackageFragment createPackageFragment(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
435 //      CreatePackageFragmentOperation op = new CreatePackageFragmentOperation(this, name, force);
436 //      runOperation(op, monitor);
437 //      return getPackageFragment(name);
438 //}
439
440 /**
441  * Returns the root's kind - K_SOURCE or K_BINARY, defaults
442  * to K_SOURCE if it is not on the classpath.
443  *
444  * @exception NotPresentException if the project and root do
445  *              not exist.
446  */
447 //protected int determineKind(IResource underlyingResource) throws JavaModelException {
448 //      IClasspathEntry[] entries= ((JavaProject)getJavaProject()).getExpandedClasspath(true);
449 //      for (int i= 0; i < entries.length; i++) {
450 //              IClasspathEntry entry= entries[i];
451 //              if (entry.getPath().equals(underlyingResource.getFullPath())) {
452 //                      return entry.getContentKind();
453 //              }
454 //      }
455 //      return IPackageFragmentRoot.K_SOURCE;
456 //}
457
458 /**
459  * Compares two objects for equality;
460  * for <code>PackageFragmentRoot</code>s, equality is having the
461  * same <code>JavaModel</code>, same resources, and occurrence count.
462  *
463  */
464 public boolean equals(Object o) {
465         if (this == o)
466                 return true;
467         if (!(o instanceof PackageFragmentRoot))
468                 return false;
469         PackageFragmentRoot other = (PackageFragmentRoot) o;
470         return getJavaModel().equals(other.getJavaModel()) && 
471                         this.resource.equals(other.resource) &&
472                         occurrenceCount == other.occurrenceCount;
473 }
474
475 /**
476  * @see IJavaElement
477  */
478 //public boolean exists() {
479 //      return super.exists() 
480 //                              && isOnClasspath();
481 //}
482
483 //public IClasspathEntry findSourceAttachmentRecommendation() {
484 //      try {
485 //              IPath rootPath = this.getPath();
486 //              IClasspathEntry entry;
487 //              IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
488 //              
489 //              // try on enclosing project first
490 //              JavaProject parentProject = (JavaProject) getJavaProject();
491 //              try {
492 //                      entry = parentProject.getClasspathEntryFor(rootPath);
493 //                      if (entry != null){
494 //                              Object target = JavaModel.getTarget(workspaceRoot, entry.getSourceAttachmentPath(), true);
495 //                              if (target instanceof IFile){
496 //                                      IFile file = (IFile) target;
497 //                                      if (Util.isArchiveFileName(file.getName())){
498 //                                              return entry;
499 //                                      }
500 //                              } else if (target instanceof IFolder) {
501 //                                      return entry;
502 //                              }
503 //                              if (target instanceof java.io.File){
504 //                                      java.io.File file = (java.io.File) target;
505 //                                      if (file.isFile()) {
506 //                                              if (Util.isArchiveFileName(file.getName())){
507 //                                                      return entry;
508 //                                              }
509 //                                      } else {
510 //                                              // external directory
511 //                                              return entry;
512 //                                      }
513 //                              }
514 //                      }
515 //              } catch(JavaModelException e){
516 //              }
517 //              
518 //              // iterate over all projects
519 //              IJavaModel model = getJavaModel();
520 //              IJavaProject[] jProjects = model.getJavaProjects();
521 //              for (int i = 0, max = jProjects.length; i < max; i++){
522 //                      JavaProject jProject = (JavaProject) jProjects[i];
523 //                      if (jProject == parentProject) continue; // already done
524 //                      try {
525 //                              entry = jProject.getClasspathEntryFor(rootPath);
526 //                              if (entry != null){
527 //                                      Object target = JavaModel.getTarget(workspaceRoot, entry.getSourceAttachmentPath(), true);
528 //                                      if (target instanceof IFile){
529 //                                              IFile file = (IFile) target;
530 //                                              if (Util.isArchiveFileName(file.getName())){
531 //                                                      return entry;
532 //                                              }
533 //                                      } else if (target instanceof IFolder) {
534 //                                              return entry;
535 //                                      }
536 //                                      if (target instanceof java.io.File){
537 //                                              java.io.File file = (java.io.File) target;
538 //                                              if (file.isFile()) {
539 //                                                      if (Util.isArchiveFileName(file.getName())){
540 //                                                              return entry;
541 //                                                      }
542 //                                              } else {
543 //                                                      // external directory
544 //                                                      return entry;
545 //                                              }
546 //                                      }
547 //                              }
548 //                      } catch(JavaModelException e){
549 //                      }
550 //              }
551 //      } catch(JavaModelException e){
552 //      }
553 //
554 //      return null;
555 //}
556
557 /*
558  * Returns the exclusion patterns from the classpath entry associated with this root.
559  */
560 char[][] fullExclusionPatternChars() {
561         return null;
562 //      try {
563                 
564 //              if (this.isOpen() && this.getKind() != IPackageFragmentRoot.K_SOURCE) return null;
565 //              ClasspathEntry entry = (ClasspathEntry)getRawClasspathEntry();
566 //              if (entry == null) {
567 //                      return null;
568 //              } else {
569 //                      return entry.fullExclusionPatternChars();
570 //              }
571 //      } catch (JavaModelException e) { 
572 //              return null;
573 //      }
574 }               
575
576 /**
577  * @see Openable
578  */
579 protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
580         
581 //      ((PackageFragmentRootInfo) info).setRootKind(determineKind(underlyingResource));
582 //      return computeChildren(info);
583   return false;
584 }
585
586 /**
587  * @see JavaElement#getHandleMemento()
588  */
589 protected char getHandleMementoDelimiter() {
590         return JavaElement.JEM_PACKAGEFRAGMENTROOT;
591 }
592 /**
593  * @see IJavaElement
594  */
595 public int getElementType() {
596         return PACKAGE_FRAGMENT_ROOT;
597 }
598 /**
599  * @see JavaElement#getHandleMemento()
600  */
601 public String getHandleMemento(){
602         IPath path;
603         IResource resource = getResource();
604         if (resource != null) {
605                 // internal jar or regular root
606                 if (getResource().getProject().equals(getJavaProject().getProject())) {
607                         path = resource.getProjectRelativePath();
608                 } else {
609                         path = resource.getFullPath();
610                 }
611         } else {
612                 // external jar
613                 path = getPath();
614         }
615         StringBuffer buff= new StringBuffer(((JavaElement)getParent()).getHandleMemento());
616         buff.append(getHandleMementoDelimiter());
617         buff.append(path.toString()); 
618         return buff.toString();
619 }
620 /**
621  * @see IPackageFragmentRoot
622  */
623 public int getKind() throws JavaModelException {
624         return ((PackageFragmentRootInfo)getElementInfo()).getRootKind();
625 }
626
627 /**
628  * Returns an array of non-java resources contained in the receiver.
629  */
630 //public Object[] getNonJavaResources() throws JavaModelException {
631 //      return ((PackageFragmentRootInfo) getElementInfo()).getNonJavaResources(getJavaProject(), getResource(), this);
632 //}
633
634 /**
635  * @see IPackageFragmentRoot
636  */
637 public IPackageFragment getPackageFragment(String packageName) {
638         if (packageName.indexOf(' ') != -1) { // tolerate package names with spaces (e.g. 'x . y') (http://bugs.eclipse.org/bugs/show_bug.cgi?id=21957)
639                 char[][] compoundName = Util.toCompoundChars(packageName);
640                 StringBuffer buffer = new StringBuffer(packageName.length());
641                 for (int i = 0, length = compoundName.length; i < length; i++) {
642                         buffer.append(CharOperation.trim(compoundName[i]));
643                         if (i != length-1) {
644                                 buffer.append('.');
645                         }
646                 }
647                 packageName = buffer.toString();
648         }
649         return new PackageFragment(this, packageName);
650 }
651
652 /**
653  * Returns the package name for the given folder
654  * (which is a decendent of this root).
655  */
656 protected String getPackageName(IFolder folder) throws JavaModelException {
657         IPath myPath= getPath();
658         IPath pkgPath= folder.getFullPath();
659         int mySegmentCount= myPath.segmentCount();
660         int pkgSegmentCount= pkgPath.segmentCount();
661         StringBuffer name = new StringBuffer(IPackageFragment.DEFAULT_PACKAGE_NAME);
662         for (int i= mySegmentCount; i < pkgSegmentCount; i++) {
663                 if (i > mySegmentCount) {
664                         name.append('.');
665                 }
666                 name.append(pkgPath.segment(i));
667         }
668         return name.toString();
669 }
670
671 /**
672  * @see IJavaElement
673  */
674 public IPath getPath() {
675         return getResource().getFullPath();
676 }
677
678 /*
679  * @see IPackageFragmentRoot 
680  */
681 //public IClasspathEntry getRawClasspathEntry() throws JavaModelException {
682 //
683 //      IClasspathEntry rawEntry = null;
684 //      IJavaProject project = this.getJavaProject();
685 //      project.getResolvedClasspath(true); // force the reverse rawEntry cache to be populated
686 //      JavaModelManager.PerProjectInfo perProjectInfo = 
687 //              JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project.getProject());
688 //      if (perProjectInfo != null && perProjectInfo.resolvedPathToRawEntries != null) {
689 //              rawEntry = (IClasspathEntry) perProjectInfo.resolvedPathToRawEntries.get(this.getPath());
690 //      }
691 //      return rawEntry;
692 //}
693
694 /*
695  * @see IJavaElement
696  */
697 public IResource getResource() {
698         return (IResource)this.resource;
699 }
700
701 /**
702  * @see IPackageFragmentRoot
703  */
704 //public IPath getSourceAttachmentPath() throws JavaModelException {
705 //      if (getKind() != K_BINARY) return null;
706 //      
707 //      String serverPathString= getSourceAttachmentProperty();
708 //      if (serverPathString == null) {
709 //              return null;
710 //      }
711 //      int index= serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER);
712 //      if (index < 0) {
713 //              // no root path specified
714 //              return new Path(serverPathString);
715 //      } else {
716 //              String serverSourcePathString= serverPathString.substring(0, index);
717 //              return new Path(serverSourcePathString);
718 //      }
719 //}
720
721 /**
722  * Returns the server property for this package fragment root's
723  * source attachement.
724  */
725 //protected String getSourceAttachmentProperty() throws JavaModelException {
726 //      String propertyString = null;
727 //      QualifiedName qName= getSourceAttachmentPropertyName();
728 //      try {
729 //              propertyString = ResourcesPlugin.getWorkspace().getRoot().getPersistentProperty(qName);
730 //              
731 //              // if no existing source attachment information, then lookup a recommendation from classpath entries
732 //              if (propertyString == null) {
733 //                      IClasspathEntry recommendation = findSourceAttachmentRecommendation();
734 //                      if (recommendation != null) {
735 //                              IPath rootPath = recommendation.getSourceAttachmentRootPath();
736 //                              propertyString = 
737 //                                      recommendation.getSourceAttachmentPath().toString() 
738 //                                              + ((rootPath == null) 
739 //                                                      ? "" : //$NON-NLS-1$
740 //                                                      (ATTACHMENT_PROPERTY_DELIMITER + rootPath.toString())); 
741 //                              setSourceAttachmentProperty(propertyString);
742 //                      } else {
743 //                              // mark as being already looked up
744 //                              setSourceAttachmentProperty(NO_SOURCE_ATTACHMENT);
745 //                      }
746 //              } else if (NO_SOURCE_ATTACHMENT.equals(propertyString)) {
747 //                      // already looked up and no source attachment found
748 //                      return null;
749 //              }
750 //              return propertyString;
751 //      } catch (CoreException ce) {
752 //              throw new JavaModelException(ce);
753 //      }
754 //}
755         
756 /**
757  * Returns the qualified name for the source attachment property
758  * of this root.
759  */
760 protected QualifiedName getSourceAttachmentPropertyName() throws JavaModelException {
761         return new QualifiedName(JavaCore.PLUGIN_ID, "sourceattachment: " + this.getPath().toOSString()); //$NON-NLS-1$
762 }
763
764 public void setSourceAttachmentProperty(String property) {
765         try {
766                 ResourcesPlugin.getWorkspace().getRoot().setPersistentProperty(this.getSourceAttachmentPropertyName(), property);
767         } catch (CoreException ce) {
768         }
769 }
770
771 /**
772  * For use by <code>AttachSourceOperation</code> only.
773  * Sets the source mapper associated with this root.
774  */
775 //public void setSourceMapper(SourceMapper mapper) throws JavaModelException {
776 //      ((PackageFragmentRootInfo) getElementInfo()).setSourceMapper(mapper);
777 //}
778
779
780
781 /**
782  * @see IPackageFragmentRoot
783  */
784 //public IPath getSourceAttachmentRootPath() throws JavaModelException {
785 //      if (getKind() != K_BINARY) return null;
786 //      
787 //      String serverPathString= getSourceAttachmentProperty();
788 //      if (serverPathString == null) {
789 //              return null;
790 //      }
791 //      int index = serverPathString.lastIndexOf(ATTACHMENT_PROPERTY_DELIMITER);
792 //      if (index == -1) return null;
793 //      String serverRootPathString= IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH;
794 //      if (index != serverPathString.length() - 1) {
795 //              serverRootPathString= serverPathString.substring(index + 1);
796 //      }
797 //      return new Path(serverRootPathString);
798 //}
799
800 /**
801  * @see JavaElement
802  */
803 //public SourceMapper getSourceMapper() {
804 //      SourceMapper mapper;
805 //      try {
806 //              PackageFragmentRootInfo rootInfo = (PackageFragmentRootInfo) getElementInfo();
807 //              mapper = rootInfo.getSourceMapper();
808 //              if (mapper == null) {
809 //                      // first call to this method
810 //                      IPath sourcePath= getSourceAttachmentPath();
811 //                      if (sourcePath != null) {
812 //                              IPath rootPath= getSourceAttachmentRootPath();
813 //                              mapper = this.createSourceMapper(sourcePath, rootPath);
814 //                              if (rootPath == null && mapper.rootPath != null) {
815 //                                      // as a side effect of calling the SourceMapper constructor, the root path was computed
816 //                                      rootPath = new Path(mapper.rootPath);
817 //                                      
818 //                                      //set the property to the path of the mapped source
819 //                                      this.setSourceAttachmentProperty(
820 //                                              sourcePath.toString() 
821 //                                              + ATTACHMENT_PROPERTY_DELIMITER 
822 //                                              + rootPath.toString());
823 //                              }
824 //                              rootInfo.setSourceMapper(mapper);
825 //                      } else {
826 //                              // remember that no source is attached
827 //                              rootInfo.setSourceMapper(NO_SOURCE_MAPPER);
828 //                              mapper = null;
829 //                      }
830 //              } else if (mapper == NO_SOURCE_MAPPER) {
831 //                      // a previous call to this method found out that no source was attached
832 //                      mapper = null;
833 //              }
834 //      } catch (JavaModelException e) {
835 //              // no source can be attached
836 //              mapper = null;
837 //      }
838 //      return mapper;
839 //}
840
841 /**
842  * @see IJavaElement
843  */
844 public IResource getUnderlyingResource() throws JavaModelException {
845         if (!exists()) throw newNotPresentException();
846         return getResource();
847 }
848
849 public int hashCode() {
850         return this.resource.hashCode();
851 }
852
853 /**
854  * @see IPackageFragmentRoot
855  */
856 public boolean isArchive() {
857         return false;
858 }
859
860 /**
861  * @see IPackageFragmentRoot
862  */
863 public boolean isExternal() {
864         return false;
865 }
866
867 /*
868  * Returns whether this package fragment root is on the classpath of its project.
869  */
870 //protected boolean isOnClasspath() {
871 //      if (this.getElementType() == IJavaElement.JAVA_PROJECT){
872 //              return true;
873 //      }
874 //      
875 //      IPath path = this.getPath();
876 //      try {
877 //              // check package fragment root on classpath of its project
878 //              IJavaProject project = this.getJavaProject();
879 //              IClasspathEntry[] classpath = project.getResolvedClasspath(true);       
880 //              for (int i = 0, length = classpath.length; i < length; i++) {
881 //                      IClasspathEntry entry = classpath[i];
882 //                      if (entry.getPath().equals(path)) {
883 //                              return true;
884 //                      }
885 //              }
886 //      } catch(JavaModelException e){
887 //              // could not read classpath, then assume it is outside
888 //      }
889 //      return false;
890 //}
891 /*
892  * @see org.eclipse.jdt.core.IPackageFragmentRoot#move
893  */
894 //public void move(
895 //      IPath destination,
896 //      int updateResourceFlags,
897 //      int updateModelFlags,
898 //      IClasspathEntry sibling,
899 //      IProgressMonitor monitor)
900 //      throws JavaModelException {
901 //
902 //      MovePackageFragmentRootOperation op = 
903 //              new MovePackageFragmentRootOperation(this, destination, updateResourceFlags, updateModelFlags, sibling);
904 //      runOperation(op, monitor);
905 //}
906 //
907 //
908 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
909 //      if (!this.resourceExists() 
910 //                      || !this.isOnClasspath()) {
911 //              throw newNotPresentException();
912 //      }
913 //      super.openWhenClosed(pm);
914 //}
915
916 /**
917  * Recomputes the children of this element, based on the current state
918  * of the workbench.
919  */
920 //public void refreshChildren() {
921 //      try {
922 //              OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
923 //              computeChildren(info);
924 //      } catch (JavaModelException e) {
925 //              // do nothing.
926 //      }
927 //}
928
929 ///*
930 // * @see JavaElement#rootedAt(IJavaProject)
931 // */
932 //public IJavaElement rootedAt(IJavaProject project) {
933 //      return
934 //              new PackageFragmentRoot(
935 //                      getResource(),
936 //                      project, 
937 //                      name);
938 //}
939
940 /**
941  * @private Debugging purposes
942  */
943 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
944         buffer.append(this.tabString(tab));
945         if (getElementName().length() == 0) {
946                 buffer.append("[project root]"); //$NON-NLS-1$
947         } else {
948                 IPath path = getPath();
949                 if (getJavaProject().getElementName().equals(path.segment(0))) {
950                         buffer.append(path.removeFirstSegments(1).makeRelative());
951                 } else {
952                         buffer.append(path);
953                 }
954         }
955         if (info == null) {
956                 buffer.append(" (not open)"); //$NON-NLS-1$
957         }
958 }
959
960 /**
961  * Possible failures: <ul>
962  *  <li>ELEMENT_NOT_PRESENT - the root supplied to the operation
963  *      does not exist
964  *  <li>INVALID_ELEMENT_TYPES - the root is not of kind K_BINARY
965  *   <li>RELATIVE_PATH - the path supplied to this operation must be
966  *      an absolute path
967  *  </ul>
968  */
969 //protected void verifyAttachSource(IPath sourcePath) throws JavaModelException {
970 //      if (!exists()) {
971 //              throw newNotPresentException();
972 //      } else if (this.getKind() != K_BINARY) {
973 //              throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this));
974 //      } else if (sourcePath != null && !sourcePath.isAbsolute()) {
975 //              throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.RELATIVE_PATH, sourcePath));
976 //      }
977 //}
978
979 }