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;
14 import java.util.HashSet;
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.core.util.Util;
23 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
25 import org.eclipse.core.resources.IContainer;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.Path;
34 * @see IPackageFragment
36 public class PackageFragment extends Openable implements IPackageFragment {
38 * Constant empty list of compilation units
40 protected static ICompilationUnit[] fgEmptyCompilationUnitList= new ICompilationUnit[] {};
42 * Constructs a handle for a package fragment
44 * @see IPackageFragment
46 protected PackageFragment(PackageFragmentRoot root, String name) {
52 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
54 // check whether this pkg can be opened
55 if (!underlyingResource.isAccessible()) throw newNotPresentException();
59 // if (kind == IPackageFragmentRoot.K_SOURCE) {
60 extType = "php"; //EXTENSION_java;
62 // extType = EXTENSION_class;
65 // add compilation units/class files from resources
66 HashSet vChildren = new HashSet();
68 PackageFragmentRoot root = getPackageFragmentRoot();
69 // char[][] inclusionPatterns = root.fullInclusionPatternChars();
70 char[][] exclusionPatterns = root.fullExclusionPatternChars();
71 IResource[] members = ((IContainer) underlyingResource).members();
72 for (int i = 0, max = members.length; i < max; i++) {
73 IResource child = members[i];
74 if (child.getType() != IResource.FOLDER
75 && !Util.isExcluded(child, exclusionPatterns)) {
76 String extension = child.getProjectRelativePath().getFileExtension();
77 if (extension != null) {
78 if (extension.equalsIgnoreCase(extType)) {
79 IJavaElement childElement;
80 // if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
81 // childElement = new CompilationUnit(this, child.getName(), DefaultWorkingCopyOwner.PRIMARY);
82 // vChildren.add(childElement);
83 // } else if (Util.isValidClassFileName(child.getName())) {
84 // childElement = getClassFile(child.getName());
85 // vChildren.add(childElement);
91 } catch (CoreException e) {
92 throw new JavaModelException(e);
95 // if (kind == IPackageFragmentRoot.K_SOURCE) {
96 // // add primary compilation units
97 // ICompilationUnit[] primaryCompilationUnits = getCompilationUnits(DefaultWorkingCopyOwner.PRIMARY);
98 // for (int i = 0, length = primaryCompilationUnits.length; i < length; i++) {
99 // ICompilationUnit primary = primaryCompilationUnits[i];
100 // vChildren.add(primary);
104 IJavaElement[] children = new IJavaElement[vChildren.size()];
105 vChildren.toArray(children);
106 info.setChildren(children);
110 // * Compute the children of this package fragment.
112 // * <p>Package fragments which are folders recognize files based on the
113 // * type of the fragment
114 // * <p>Package fragments which are in a jar only recognize .class files (
115 // * @see JarPackageFragment).
117 //protected boolean computeChildren(OpenableElementInfo info, IResource resource) throws JavaModelException {
118 // ArrayList vChildren = new ArrayList();
119 //// int kind = getKind();
121 //// if (kind == IPackageFragmentRoot.K_SOURCE) {
122 // extType = "php"; //$NON-NLS-1$
124 //// extType = "class"; //$NON-NLS-1$
127 // char[][] exclusionPatterns = ((PackageFragmentRoot)getPackageFragmentRoot()).fullExclusionPatternChars();
128 // IResource[] members = ((IContainer) resource).members();
129 // for (int i = 0, max = members.length; i < max; i++) {
130 // IResource child = members[i];
131 // if (child.getType() != IResource.FOLDER
132 // && !Util.isExcluded(child, exclusionPatterns)) {
133 // String extension = child.getProjectRelativePath().getFileExtension();
134 // if (extension != null) {
135 // if (extension.equalsIgnoreCase(extType)) {
136 // IJavaElement childElement;
137 //// if (kind == IPackageFragmentRoot.K_SOURCE && Util.isValidCompilationUnitName(child.getName())) {
138 //// childElement = getCompilationUnit(child.getName());
139 //// vChildren.add(childElement);
140 //// } else if (Util.isValidClassFileName(child.getName())) {
141 //// childElement = getClassFile(child.getName());
142 //// vChildren.add(childElement);
148 // } catch (CoreException e) {
149 // throw new JavaModelException(e);
151 // IJavaElement[] children = new IJavaElement[vChildren.size()];
152 // vChildren.toArray(children);
153 // info.setChildren(children);
157 * Returns true if this fragment contains at least one java resource.
158 * Returns false otherwise.
160 //public boolean containsJavaResources() throws JavaModelException {
161 // return ((PackageFragmentInfo) getElementInfo()).containsJavaResources();
164 * @see ISourceManipulation
166 //public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
167 // if (container == null) {
168 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
170 // IJavaElement[] elements= new IJavaElement[] {this};
171 // IJavaElement[] containers= new IJavaElement[] {container};
172 // IJavaElement[] siblings= null;
173 // if (sibling != null) {
174 // siblings= new IJavaElement[] {sibling};
176 // String[] renamings= null;
177 // if (rename != null) {
178 // renamings= new String[] {rename};
180 // getJavaModel().copy(elements, containers, siblings, renamings, force, monitor);
183 * @see IPackageFragment
185 public ICompilationUnit createCompilationUnit(String name, String contents, boolean force, IProgressMonitor monitor) throws JavaModelException {
186 // CreateCompilationUnitOperation op= new CreateCompilationUnitOperation(this, name, contents, force);
187 // runOperation(op, monitor);
188 // return getCompilationUnit(name);
194 protected Object createElementInfo() {
195 return new PackageFragmentInfo();
198 * @see ISourceManipulation
200 //public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
201 // IJavaElement[] elements = new IJavaElement[] {this};
202 // getJavaModel().delete(elements, force, monitor);
207 //protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
209 // return computeChildren(info, underlyingResource);
212 // * @see IPackageFragment#getClassFile(String)
214 //public IClassFile getClassFile(String name) {
215 // return new ClassFile(this, name);
218 * Returns a the collection of class files in this - a folder package fragment which has a root
219 * that has its kind set to <code>IPackageFragmentRoot.K_Source</code> does not
220 * recognize class files.
222 * @see IPackageFragment#getClassFiles()
224 //public IClassFile[] getClassFiles() throws JavaModelException {
225 // if (getKind() == IPackageFragmentRoot.K_SOURCE) {
226 // return fgEmptyClassFileList;
229 // ArrayList list = getChildrenOfType(CLASS_FILE);
230 // IClassFile[] array= new IClassFile[list.size()];
231 // list.toArray(array);
235 * @see IPackageFragment#getCompilationUnit(String)
236 * @exception IllegalArgumentExcpetion if the name does not end with ".java"
238 public ICompilationUnit getCompilationUnit(String cuName) {
239 if (! PHPFileUtil.isValidPHPUnitName(cuName)) {
240 throw new IllegalArgumentException(Util.bind("convention.unit.notJavaName")); //$NON-NLS-1$
242 return new CompilationUnit(this, cuName, DefaultWorkingCopyOwner.PRIMARY);
245 * @see IPackageFragment#getCompilationUnits()
247 public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
248 if (getKind() == IPackageFragmentRoot.K_BINARY) {
249 return fgEmptyCompilationUnitList;
252 ArrayList list = getChildrenOfType(COMPILATION_UNIT);
253 ICompilationUnit[] array= new ICompilationUnit[list.size()];
260 public int getElementType() {
261 return PACKAGE_FRAGMENT;
264 * @see JavaElement#getHandleMementoDelimiter()
266 protected char getHandleMementoDelimiter() {
267 return JavaElement.JEM_PACKAGEFRAGMENT;
270 * @see IPackageFragment#getKind()
272 public int getKind() throws JavaModelException {
273 return ((IPackageFragmentRoot)getParent()).getKind();
276 * Returns an array of non-java resources contained in the receiver.
278 //public Object[] getNonJavaResources() throws JavaModelException {
279 // if (this.isDefaultPackage()) {
280 // // We don't want to show non java resources of the default package (see PR #1G58NB8)
281 // return JavaElementInfo.NO_NON_JAVA_RESOURCES;
283 // return ((PackageFragmentInfo) getElementInfo()).getNonJavaResources(getResource(), (PackageFragmentRoot)getPackageFragmentRoot());
287 * @see IJavaElement#getPath()
289 public IPath getPath() {
290 PackageFragmentRoot root = this.getPackageFragmentRoot();
291 if (root.isArchive()) {
292 return root.getPath();
294 return root.getPath().append(this.getElementName().replace('.', '/'));
298 * @see IJavaElement#getResource()
300 public IResource getResource() {
301 PackageFragmentRoot root = this.getPackageFragmentRoot();
302 if (root.isArchive()) {
303 return root.getResource();
305 String elementName = this.getElementName();
306 if (elementName.length() == 0) {
307 return root.getResource();
309 return ((IContainer)root.getResource()).getFolder(new Path(this.getElementName().replace('.', '/')));
314 * @see IJavaElement#getUnderlyingResource()
316 public IResource getUnderlyingResource() throws JavaModelException {
317 IResource rootResource = parent.getUnderlyingResource();
318 if (rootResource == null) {
319 //jar package fragment root that has no associated resource
322 // the underlying resource may be a folder or a project (in the case that the project folder
323 // is atually the package fragment root)
324 // if (rootResource.getType() == IResource.FOLDER || rootResource.getType() == IResource.PROJECT) {
325 // IContainer folder = (IContainer) rootResource;
326 // String[] segs = Signature.getSimpleNames(fName);
327 // for (int i = 0; i < segs.length; ++i) {
328 // IResource child = folder.findMember(segs[i]);
329 // if (child == null || child.getType() != IResource.FOLDER) {
330 // throw newNotPresentException();
332 // folder = (IFolder) child;
340 * @see IPackageFragment#hasSubpackages()
342 //public boolean hasSubpackages() throws JavaModelException {
343 // IJavaElement[] packages= ((IPackageFragmentRoot)getParent()).getChildren();
344 // String name = getElementName();
345 // int nameLength = name.length();
346 // String packageName = isDefaultPackage() ? name : name+"."; //$NON-NLS-1$
347 // for (int i= 0; i < packages.length; i++) {
348 // String otherName = packages[i].getElementName();
349 // if (otherName.length() > nameLength && otherName.startsWith(packageName)) {
356 * @see IPackageFragment#isDefaultPackage()
358 public boolean isDefaultPackage() {
359 return this.getElementName().length() == 0;
362 * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
364 //public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException {
365 // if (container == null) {
366 // throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
368 // IJavaElement[] elements= new IJavaElement[] {this};
369 // IJavaElement[] containers= new IJavaElement[] {container};
370 // IJavaElement[] siblings= null;
371 // if (sibling != null) {
372 // siblings= new IJavaElement[] {sibling};
374 // String[] renamings= null;
375 // if (rename != null) {
376 // renamings= new String[] {rename};
378 // getJavaModel().move(elements, containers, siblings, renamings, force, monitor);
380 //protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
381 // if (!this.resourceExists()) throw newNotPresentException();
382 // super.openWhenClosed(pm);
385 * Recomputes the children of this element, based on the current state
388 //public void refreshChildren() {
390 // OpenableElementInfo info= (OpenableElementInfo)getElementInfo();
391 // computeChildren(info, getResource());
392 // } catch (JavaModelException e) {
397 * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
399 //public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
400 // if (name == null) {
401 // throw new IllegalArgumentException(Util.bind("element.nullName")); //$NON-NLS-1$
403 // IJavaElement[] elements= new IJavaElement[] {this};
404 // IJavaElement[] dests= new IJavaElement[] {this.getParent()};
405 // String[] renamings= new String[] {name};
406 // getJavaModel().rename(elements, dests, renamings, force, monitor);
409 // * @see JavaElement#rootedAt(IJavaProject)
411 //public IJavaElement rootedAt(IJavaProject project) {
413 // new PackageFragment(
414 // (IPackageFragmentRoot)((JavaElement)parent).rootedAt(project),
420 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
422 super.toStringChildren(tab, buffer, info);
428 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
429 buffer.append(this.tabString(tab));
430 if (getElementName().length() == 0) {
431 buffer.append("[default]"); //$NON-NLS-1$
433 buffer.append(getElementName());
436 buffer.append(" (not open)"); //$NON-NLS-1$
439 buffer.append(" (...)"); //$NON-NLS-1$