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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.core.IPackageFragment;
17 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
18 import net.sourceforge.phpdt.core.JavaModelException;
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.Path;
27 * @see IPackageFragment
29 public class PackageFragment extends Openable implements IPackageFragment {
31 * Constant empty list of compilation units
33 protected static ICompilationUnit[] fgEmptyCompilationUnitList= new ICompilationUnit[] {};
35 * Constructs a handle for a package fragment
37 * @see IPackageFragment
39 protected PackageFragment(IPackageFragmentRoot root, String name) {
40 super(PACKAGE_FRAGMENT, null, name);
43 * Compute the children of this package fragment.
45 * <p>Package fragments which are folders recognize files based on the
46 * type of the fragment
47 * <p>Package fragments which are in a jar only recognize .class files (
48 * @see JarPackageFragment).
50 //protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
51 // ArrayList vChildren = new ArrayList();
52 // int kind = getKind();
54 // if (kind == IPackageFragmentRoot.K_SOURCE) {
55 // extType = "java"; //$NON-NLS-1$
57 // extType = "class"; //$NON-NLS-1$
60 // char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
61 // IResource[] members = ((IContainer) resource).members();
62 // for (int i = 0, max = members.length; i < max; i++) {
63 // IResource child = members[i];
64 // if (child.getType() != IResource.FOLDER
65 // && !Util.isExcluded(child, exclusionPatterns)) {
66 // String extension = child.getProjectRelativePath().getFileExtension();
67 // if (extension != null) {
68 // if (extension.equalsIgnoreCase(extType)) {
69 // IJavaElement childElement;
70 // if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
71 // childElement = getCompilationUnit(child.getName());
72 // vChildren.add(childElement);
73 // } else if (Util.isValidClassFileName(child.getName())) {
74 // childElement = getClassFile(child.getName());
75 // vChildren.add(childElement);
81 // } catch (CoreException e) {
82 // throw new JavaModelException(e);
84 // IJavaElement[] children = new IJavaElement[vChildren.size()];
85 // vChildren.toArray(children);
86 // info.setChildren(children);
90 * Returns true if this fragment contains at least one java resource.
91 * Returns false otherwise.
93 //public boolean containsJavaResources() throws JavaModelException {
94 // return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
97 * @see ISourceManipulation
99 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
100 // if (container == null) {
101 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
103 // IJavaElement[] elements= new IJavaElement[] {this};
104 // IJavaElement[] containers= new IJavaElement[] {container};
105 // IJavaElement[] siblings= null;
106 // if (sibling != null) {
107 // siblings= new IJavaElement[] {sibling};
109 // String[] renamings= null;
110 // if (rename != null) {
111 // renamings= new String[] {rename};
113 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
116 * @see IPackageFragment
118 public ICompilationUnit createCompilationUnit(String name, String contents, boolean force, IProgressMonitor monitor) throws JavaModelException {
119 // CreateCompilationUnitOperation op= new CreateCompilationUnitOperation(this, name, contents, force);
120 // runOperation(op, monitor);
121 // return getCompilationUnit(name);
127 //protected OpenableElementInfo createElementInfo() {
128 // return new PackageFragmentInfo();
131 * @see ISourceManipulation
133 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
134 // IJavaElement[] elements = new IJavaElement[] {this};
135 // getJavaModel().delete(elements, force, monitor);
140 //protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
142 // return computeChildren(info, underlyingResource);
145 // * @see IPackageFragment#getClassFile(String)
147 //public IClassFile getClassFile(String name) {
148 // return new ClassFile(this, name);
151 * Returns a the collection of class files in this - a folder package fragment which has a root
152 * that has its kind set to <code>IPackageFragmentRoot.K_Source</code> does not
153 * recognize class files.
155 * @see IPackageFragment#getClassFiles()
157 //public IClassFile[] getClassFiles() throws JavaModelException {
158 // if (getKind() == IPackageFragmentRoot.K_SOURCE) {
159 // return fgEmptyClassFileList;
162 // ArrayList list = getChildrenOfType(CLASS_FILE);
163 // IClassFile[] array= new IClassFile[list.size()];
164 // list.toArray(array);
168 * @see IPackageFragment#getCompilationUnit(String)
170 public ICompilationUnit getCompilationUnit(String name) {
171 return new CompilationUnit(this, name);
174 * @see IPackageFragment#getCompilationUnits()
176 //public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
177 // if (getKind() == IPackageFragmentRoot.K_BINARY) {
178 // return fgEmptyCompilationUnitList;
181 // ArrayList list = getChildrenOfType(COMPILATION_UNIT);
182 // ICompilationUnit[] array= new ICompilationUnit[list.size()];
183 // list.toArray(array);
187 * @see JavaElement#getHandleMementoDelimiter()
189 protected char getHandleMementoDelimiter() {
190 return JavaElement.JEM_PACKAGEFRAGMENT;
193 * @see IPackageFragment#getKind()
195 //public int getKind() throws JavaModelException {
196 // return ((IPackageFragmentRoot)getParent()).getKind();
199 * Returns an array of non-java resources contained in the receiver.
201 //public Object[] getNonJavaResources() throws JavaModelException {
202 // if (this.isDefaultPackage()) {
203 // // We don't want to show non java resources of the default package (see PR #1G58NB8)
204 // return JavaElementInfo.NO_NON_JAVA_RESOURCES;
206 // return ((PackageFragmentInfo) getElementInfo()).getNonJavaResources(getResource(), (PackageFragmentRoot)getPackageFragmentRoot());
210 * @see IJavaElement#getPath()
212 //public IPath getPath() {
213 // PackageFragmentRoot root = this.getPackageFragmentRoot();
214 // if (root.isArchive()) {
215 // return root.getPath();
217 // return root.getPath().append(this.getElementName().replace('.', '/'));
221 * @see IJavaElement#getResource()
223 public IResource getResource() {
224 PackageFragmentRoot root = this.getPackageFragmentRoot();
225 if (root.isArchive()) {
226 return root.getResource();
228 String elementName = this.getElementName();
229 if (elementName.length() == 0) {
230 return root.getResource();
232 return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName().replace('.', '/')));
237 * @see IJavaElement#getUnderlyingResource()
239 public IResource getUnderlyingResource() throws JavaModelException {
240 IResource rootResource = fParent.getUnderlyingResource();
241 if (rootResource == null) {
242 //jar package fragment root that has no associated resource
245 // the underlying resource may be a folder or a project (in the case that the project folder
246 // is atually the package fragment root)
247 // if (rootResource.getType() == IResource.FOLDER || rootResource.getType() == IResource.PROJECT) {
248 // IContainer folder = (IContainer) rootResource;
249 // String[] segs = Signature.getSimpleNames(fName);
250 // for (int i = 0; i < segs.length; ++i) {
251 // IResource child = folder.findMember(segs[i]);
252 // if (child == null || child.getType() != IResource.FOLDER) {
253 // throw newNotPresentException();
255 // folder = (IFolder) child;
263 * @see IPackageFragment#hasSubpackages()
265 //public boolean hasSubpackages() throws JavaModelException {
266 // IJavaElement[] packages= ((IPackageFragmentRoot)getParent()).getChildren();
267 // String name = getElementName();
268 // int nameLength = name.length();
269 // String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
270 // for (int i= 0; i < packages.length; i++) {
271 // String otherName = packages[i].getElementName();
272 // if (otherName.length() > nameLength && otherName.startsWith(packageName)) {
279 * @see IPackageFragment#isDefaultPackage()
281 public boolean isDefaultPackage() {
282 return this.getElementName().length() == 0;
285 * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
287 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
288 // if (container == null) {
289 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
291 // IJavaElement[] elements= new IJavaElement[] {this};
292 // IJavaElement[] containers= new IJavaElement[] {container};
293 // IJavaElement[] siblings= null;
294 // if (sibling != null) {
295 // siblings= new IJavaElement[] {sibling};
297 // String[] renamings= null;
298 // if (rename != null) {
299 // renamings= new String[] {rename};
301 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
303 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
304 // if (!this.resourceExists()) throw newNotPresentException();
305 // super.openWhenClosed(pm);
308 * Recomputes the children of this element, based on the current state
311 //public void refreshChildren() {
313 // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
314 // computeChildren(info, getResource());
315 // } catch (JavaModelException e) {
320 * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
322 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
323 // if (name == null) {
324 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
326 // IJavaElement[] elements= new IJavaElement[] {this};
327 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
328 // String[] renamings= new String[] {name};
329 // getJavaModel().rename(elements, dests, renamings, force, monitor);
332 * @see JavaElement#rootedAt(IJavaProject)
334 public IJavaElement rootedAt(IJavaProject project) {
337 (IPackageFragmentRoot)((JavaElement)fParent).rootedAt(project),
343 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
345 super.toStringChildren(tab, buffer, info);
351 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
352 buffer.append(this.tabString(tab));
353 if (getElementName().length() == 0) {
354 buffer.append("[default]"); //$NON-NLS-1$
356 buffer.append(getElementName());
359 buffer.append(" (not open)"); //$NON-NLS-1$
362 buffer.append(" (...)"); //$NON-NLS-1$