1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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.IImportContainer;
14 import net.sourceforge.phpdt.core.IImportDeclaration;
15 import net.sourceforge.phpdt.core.IJavaElement;
16 import net.sourceforge.phpdt.core.IParent;
17 import net.sourceforge.phpdt.core.ISourceRange;
18 import net.sourceforge.phpdt.core.ISourceReference;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.core.WorkingCopyOwner;
21 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
23 // import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
26 * @see IImportContainer
28 public class ImportContainer extends SourceRefElement implements
30 protected ImportContainer(CompilationUnit parent) {
31 super(parent, ""); //$NON-NLS-1$
34 public boolean equals(Object o) {
35 if (!(o instanceof ImportContainer))
37 return super.equals(o);
43 public int getElementType() {
44 return IMPORT_CONTAINER;
50 public IJavaElement getHandleFromMemento(String token,
51 MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
52 switch (token.charAt(0)) {
54 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
55 case JEM_IMPORTDECLARATION:
56 if (memento.hasMoreTokens()) {
57 String importName = memento.nextToken();
58 JavaElement importDecl = (JavaElement) getImport(importName);
59 return importDecl.getHandleFromMemento(memento,
69 * @see JavaElement#getHandleMemento()
71 protected char getHandleMementoDelimiter() {
72 return JavaElement.JEM_IMPORTDECLARATION;
76 * @see IImportContainer
78 public IImportDeclaration getImport(String importName) {
79 return new ImportDeclaration(this, importName);
83 * @see JavaElement#getPrimaryElement(boolean)
85 public IJavaElement getPrimaryElement(boolean checkOwner) {
86 CompilationUnit cu = (CompilationUnit) this.parent;
87 if (checkOwner && cu.isPrimary())
89 return cu.getImportContainer();
93 * @see ISourceReference
95 public ISourceRange getSourceRange() throws JavaModelException {
96 IJavaElement[] imports = getChildren();
97 ISourceRange firstRange = ((ISourceReference) imports[0])
99 ISourceRange lastRange = ((ISourceReference) imports[imports.length - 1])
101 SourceRange range = new SourceRange(firstRange.getOffset(), lastRange
103 + lastRange.getLength() - firstRange.getOffset());
108 * Import containers only exist if they have children.
112 public boolean hasChildren() {
118 public String readableName() {
124 * @private Debugging purposes
126 protected void toString(int tab, StringBuffer buffer) {
127 Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
128 if (info == null || !(info instanceof JavaElementInfo))
130 IJavaElement[] children = ((JavaElementInfo) info).getChildren();
131 for (int i = 0; i < children.length; i++) {
133 buffer.append("\n"); //$NON-NLS-1$
134 ((JavaElement) children[i]).toString(tab, buffer);
141 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
142 buffer.append(this.tabString(tab));
143 buffer.append("<import container>"); //$NON-NLS-1$
145 buffer.append(" (not open)"); //$NON-NLS-1$