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