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 java.util.ArrayList;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IJavaProject;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
21 import net.sourceforge.phpdt.core.JavaModelException;
23 import org.eclipse.core.resources.IContainer;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.Path;
32 * @see IPackageFragment
34 public class PackageFragment extends Openable implements IPackageFragment {
36 * Constant empty list of compilation units
38 protected static ICompilationUnit[] fgEmptyCompilationUnitList= new ICompilationUnit[] {};
40 * Constructs a handle for a package fragment
42 * @see IPackageFragment
44 protected PackageFragment(IPackageFragmentRoot root, String name) {
45 super(PACKAGE_FRAGMENT, root, name);
48 * Compute the children of this package fragment.
50 * <p>Package fragments which are folders recognize files based on the
51 * type of the fragment
52 * <p>Package fragments which are in a jar only recognize .class files (
53 * @see JarPackageFragment).
55 protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
56 ArrayList vChildren = new ArrayList();
57 // int kind = getKind();
59 // if (kind == IPackageFragmentRoot.K_SOURCE) {
60 extType = "php"; //$NON-NLS-1$
62 // extType = "class"; //$NON-NLS-1$
65 char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
66 IResource[] members = ((IContainer) resource).members();
67 for (int i = 0, max = members.length; i < max; i++) {
68 IResource child = members[i];
69 if (child.getType() != IResource.FOLDER
70 && !Util.isExcluded(child, exclusionPatterns)) {
71 String extension = child.getProjectRelativePath().getFileExtension();
72 if (extension != null) {
73 if (extension.equalsIgnoreCase(extType)) {
74 IJavaElement childElement;
75 // if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
76 // childElement = getCompilationUnit(child.getName());
77 // vChildren.add(childElement);
78 // } else if (Util.isValidClassFileName(child.getName())) {
79 // childElement = getClassFile(child.getName());
80 // vChildren.add(childElement);
86 } catch (CoreException e) {
87 throw new JavaModelException(e);
89 IJavaElement[] children = new IJavaElement[vChildren.size()];
90 vChildren.toArray(children);
91 info.setChildren(children);
95 * Returns true if this fragment contains at least one java resource.
96 * Returns false otherwise.
98 //public boolean containsJavaResources() throws JavaModelException {
99 // return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
102 * @see ISourceManipulation
104 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
105 // if (container == null) {
106 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
108 // IJavaElement[] elements= new IJavaElement[] {this};
109 // IJavaElement[] containers= new IJavaElement[] {container};
110 // IJavaElement[] siblings= null;
111 // if (sibling != null) {
112 // siblings= new IJavaElement[] {sibling};
114 // String[] renamings= null;
115 // if (rename != null) {
116 // renamings= new String[] {rename};
118 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
121 * @see IPackageFragment
123 public ICompilationUnit createCompilationUnit(String name, String contents, boolean force, IProgressMonitor monitor) throws JavaModelException {
124 // CreateCompilationUnitOperation op= new CreateCompilationUnitOperation(this, name, contents, force);
125 // runOperation(op, monitor);
126 // return getCompilationUnit(name);
132 protected OpenableElementInfo createElementInfo() {
133 return new PackageFragmentInfo();
136 * @see ISourceManipulation
138 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
139 // IJavaElement[] elements = new IJavaElement[] {this};
140 // getJavaModel().delete(elements, force, monitor);
145 protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
147 return computeChildren(info, underlyingResource);
150 // * @see IPackageFragment#getClassFile(String)
152 //public IClassFile getClassFile(String name) {
153 // return new ClassFile(this, name);
156 * Returns a the collection of class files in this - a folder package fragment which has a root
157 * that has its kind set to <code>IPackageFragmentRoot.K_Source</code> does not
158 * recognize class files.
160 * @see IPackageFragment#getClassFiles()
162 //public IClassFile[] getClassFiles() throws JavaModelException {
163 // if (getKind() == IPackageFragmentRoot.K_SOURCE) {
164 // return fgEmptyClassFileList;
167 // ArrayList list = getChildrenOfType(CLASS_FILE);
168 // IClassFile[] array= new IClassFile[list.size()];
169 // list.toArray(array);
173 * @see IPackageFragment#getCompilationUnit(String)
175 public ICompilationUnit getCompilationUnit(String name) {
176 return new CompilationUnit(this, name);
179 * @see IPackageFragment#getCompilationUnits()
181 public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
182 if (getKind() == IPackageFragmentRoot.K_BINARY) {
183 return fgEmptyCompilationUnitList;
186 ArrayList list = getChildrenOfType(COMPILATION_UNIT);
187 ICompilationUnit[] array= new ICompilationUnit[list.size()];
192 * @see JavaElement#getHandleMementoDelimiter()
194 protected char getHandleMementoDelimiter() {
195 return JavaElement.JEM_PACKAGEFRAGMENT;
198 * @see IPackageFragment#getKind()
200 public int getKind() throws JavaModelException {
201 return ((IPackageFragmentRoot)getParent()).getKind();
204 * Returns an array of non-java resources contained in the receiver.
206 //public Object[] getNonJavaResources() throws JavaModelException {
207 // if (this.isDefaultPackage()) {
208 // // We don't want to show non java resources of the default package (see PR #1G58NB8)
209 // return JavaElementInfo.NO_NON_JAVA_RESOURCES;
211 // return ((PackageFragmentInfo) getElementInfo()).getNonJavaResources(getResource(), (PackageFragmentRoot)getPackageFragmentRoot());
215 * @see IJavaElement#getPath()
217 public IPath getPath() {
218 PackageFragmentRoot root = this.getPackageFragmentRoot();
219 if (root.isArchive()) {
220 return root.getPath();
222 return root.getPath().append(this.getElementName().replace('.', '/'));
226 * @see IJavaElement#getResource()
228 public IResource getResource() {
229 PackageFragmentRoot root = this.getPackageFragmentRoot();
230 if (root.isArchive()) {
231 return root.getResource();
233 String elementName = this.getElementName();
234 if (elementName.length() == 0) {
235 return root.getResource();
237 return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName().replace('.', '/')));
242 * @see IJavaElement#getUnderlyingResource()
244 public IResource getUnderlyingResource() throws JavaModelException {
245 IResource rootResource = fParent.getUnderlyingResource();
246 if (rootResource == null) {
247 //jar package fragment root that has no associated resource
250 // the underlying resource may be a folder or a project (in the case that the project folder
251 // is atually the package fragment root)
252 // if (rootResource.getType() == IResource.FOLDER || rootResource.getType() == IResource.PROJECT) {
253 // IContainer folder = (IContainer) rootResource;
254 // String[] segs = Signature.getSimpleNames(fName);
255 // for (int i = 0; i < segs.length; ++i) {
256 // IResource child = folder.findMember(segs[i]);
257 // if (child == null || child.getType() != IResource.FOLDER) {
258 // throw newNotPresentException();
260 // folder = (IFolder) child;
268 * @see IPackageFragment#hasSubpackages()
270 //public boolean hasSubpackages() throws JavaModelException {
271 // IJavaElement[] packages= ((IPackageFragmentRoot)getParent()).getChildren();
272 // String name = getElementName();
273 // int nameLength = name.length();
274 // String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
275 // for (int i= 0; i < packages.length; i++) {
276 // String otherName = packages[i].getElementName();
277 // if (otherName.length() > nameLength && otherName.startsWith(packageName)) {
284 * @see IPackageFragment#isDefaultPackage()
286 public boolean isDefaultPackage() {
287 return this.getElementName().length() == 0;
290 * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
292 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
293 // if (container == null) {
294 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
296 // IJavaElement[] elements= new IJavaElement[] {this};
297 // IJavaElement[] containers= new IJavaElement[] {container};
298 // IJavaElement[] siblings= null;
299 // if (sibling != null) {
300 // siblings= new IJavaElement[] {sibling};
302 // String[] renamings= null;
303 // if (rename != null) {
304 // renamings= new String[] {rename};
306 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
308 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
309 // if (!this.resourceExists()) throw newNotPresentException();
310 // super.openWhenClosed(pm);
313 * Recomputes the children of this element, based on the current state
316 //public void refreshChildren() {
318 // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
319 // computeChildren(info, getResource());
320 // } catch (JavaModelException e) {
325 * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
327 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
328 // if (name == null) {
329 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
331 // IJavaElement[] elements= new IJavaElement[] {this};
332 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
333 // String[] renamings= new String[] {name};
334 // getJavaModel().rename(elements, dests, renamings, force, monitor);
337 * @see JavaElement#rootedAt(IJavaProject)
339 public IJavaElement rootedAt(IJavaProject project) {
342 (IPackageFragmentRoot)((JavaElement)fParent).rootedAt(project),
348 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
350 super.toStringChildren(tab, buffer, info);
356 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
357 buffer.append(this.tabString(tab));
358 if (getElementName().length() == 0) {
359 buffer.append("[default]"); //$NON-NLS-1$
361 buffer.append(getElementName());
364 buffer.append(" (not open)"); //$NON-NLS-1$
367 buffer.append(" (...)"); //$NON-NLS-1$