KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > ImportContainer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import org.eclipse.jdt.core.*;
14 import org.eclipse.jdt.core.IImportContainer;
15 import org.eclipse.jdt.core.IImportDeclaration;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.ISourceRange;
18 import org.eclipse.jdt.core.ISourceReference;
19 import org.eclipse.jdt.core.JavaModelException;
20 import org.eclipse.jdt.internal.core.util.MementoTokenizer;
21
22 /**
23  * @see IImportContainer
24  */

25 public class ImportContainer extends SourceRefElement implements IImportContainer {
26 protected ImportContainer(CompilationUnit parent) {
27     super(parent);
28 }
29 public boolean equals(Object JavaDoc o) {
30     if (!(o instanceof ImportContainer)) return false;
31     return super.equals(o);
32 }
33 /**
34  * @see IJavaElement
35  */

36 public int getElementType() {
37     return IMPORT_CONTAINER;
38 }
39 /*
40  * @see JavaElement
41  */

42 public IJavaElement getHandleFromMemento(String JavaDoc token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
43     switch (token.charAt(0)) {
44         case JEM_COUNT:
45             return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
46         case JEM_IMPORTDECLARATION:
47             if (memento.hasMoreTokens()) {
48                 String JavaDoc importName = memento.nextToken();
49                 JavaElement importDecl = (JavaElement)getImport(importName);
50                 return importDecl.getHandleFromMemento(memento, workingCopyOwner);
51             } else {
52                 return this;
53             }
54     }
55     return null;
56 }
57 /**
58  * @see JavaElement#getHandleMemento()
59  */

60 protected char getHandleMementoDelimiter() {
61     return JavaElement.JEM_IMPORTDECLARATION;
62 }
63 /**
64  * @see IImportContainer
65  */

66 public IImportDeclaration getImport(String JavaDoc importName) {
67     int index = importName.indexOf(".*"); ///$NON-NLS-1$
68
boolean isOnDemand = index != -1;
69     if (isOnDemand)
70         // make sure to copy the string (so that it doesn't hold on the underlying char[] that might be much bigger than necessary)
71
importName = new String JavaDoc(importName.substring(0, index));
72     return new ImportDeclaration(this, importName, isOnDemand);
73 }
74 /*
75  * @see JavaElement#getPrimaryElement(boolean)
76  */

77 public IJavaElement getPrimaryElement(boolean checkOwner) {
78     CompilationUnit cu = (CompilationUnit)this.parent;
79     if (checkOwner && cu.isPrimary()) return this;
80     return cu.getImportContainer();
81 }
82 /**
83  * @see ISourceReference
84  */

85 public ISourceRange getSourceRange() throws JavaModelException {
86     IJavaElement[] imports= getChildren();
87     ISourceRange firstRange= ((ISourceReference)imports[0]).getSourceRange();
88     ISourceRange lastRange= ((ISourceReference)imports[imports.length - 1]).getSourceRange();
89     SourceRange range= new SourceRange(firstRange.getOffset(), lastRange.getOffset() + lastRange.getLength() - firstRange.getOffset());
90     return range;
91 }
92 /**
93  */

94 public String JavaDoc readableName() {
95
96     return null;
97 }
98 /**
99  * @private Debugging purposes
100  */

101 protected void toString(int tab, StringBuffer JavaDoc buffer) {
102     Object JavaDoc info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
103     if (info == null || !(info instanceof JavaElementInfo)) return;
104     IJavaElement[] children = ((JavaElementInfo)info).getChildren();
105     for (int i = 0; i < children.length; i++) {
106         if (i > 0) buffer.append("\n"); //$NON-NLS-1$
107
((JavaElement)children[i]).toString(tab, buffer);
108     }
109 }
110 /**
111  * Debugging purposes
112  */

113 protected void toStringInfo(int tab, StringBuffer JavaDoc buffer, Object JavaDoc info, boolean showResolvedInfo) {
114     buffer.append(this.tabString(tab));
115     buffer.append("<import container>"); //$NON-NLS-1$
116
if (info == null) {
117         buffer.append(" (not open)"); //$NON-NLS-1$
118
}
119 }
120 }
121
Popular Tags