/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package net.sourceforge.phpdt.internal.core;

import net.sourceforge.phpdt.core.IImportContainer;
import net.sourceforge.phpdt.core.IImportDeclaration;
import net.sourceforge.phpdt.core.IJavaElement;
import net.sourceforge.phpdt.core.IParent;
import net.sourceforge.phpdt.core.ISourceRange;
import net.sourceforge.phpdt.core.ISourceReference;
import net.sourceforge.phpdt.core.JavaModelException;
import net.sourceforge.phpdt.core.WorkingCopyOwner;
import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;

// import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;

/**
 * @see IImportContainer
 */
public class ImportContainer extends SourceRefElement implements
		IImportContainer {
	protected ImportContainer(CompilationUnit parent) {
		super(parent, ""); //$NON-NLS-1$
	}

	public boolean equals(Object o) {
		if (!(o instanceof ImportContainer))
			return false;
		return super.equals(o);
	}

	/**
	 * @see IJavaElement
	 */
	public int getElementType() {
		return IMPORT_CONTAINER;
	}

	/*
	 * @see JavaElement
	 */
	public IJavaElement getHandleFromMemento(String token,
			MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
		switch (token.charAt(0)) {
		case JEM_COUNT:
			return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
		case JEM_IMPORTDECLARATION:
			if (memento.hasMoreTokens()) {
				String importName = memento.nextToken();
				JavaElement importDecl = (JavaElement) getImport(importName);
				return importDecl.getHandleFromMemento(memento,
						workingCopyOwner);
			} else {
				return this;
			}
		}
		return null;
	}

	/**
	 * @see JavaElement#getHandleMemento()
	 */
	protected char getHandleMementoDelimiter() {
		return JavaElement.JEM_IMPORTDECLARATION;
	}

	/**
	 * @see IImportContainer
	 */
	public IImportDeclaration getImport(String importName) {
		return new ImportDeclaration(this, importName);
	}

	/*
	 * @see JavaElement#getPrimaryElement(boolean)
	 */
	public IJavaElement getPrimaryElement(boolean checkOwner) {
		CompilationUnit cu = (CompilationUnit) this.parent;
		if (checkOwner && cu.isPrimary())
			return this;
		return cu.getImportContainer();
	}

	/**
	 * @see ISourceReference
	 */
	public ISourceRange getSourceRange() throws JavaModelException {
		IJavaElement[] imports = getChildren();
		ISourceRange firstRange = ((ISourceReference) imports[0])
				.getSourceRange();
		ISourceRange lastRange = ((ISourceReference) imports[imports.length - 1])
				.getSourceRange();
		SourceRange range = new SourceRange(firstRange.getOffset(), lastRange
				.getOffset()
				+ lastRange.getLength() - firstRange.getOffset());
		return range;
	}

	/**
	 * Import containers only exist if they have children.
	 * 
	 * @see IParent
	 */
	public boolean hasChildren() {
		return true;
	}

	/**
	 */
	public String readableName() {

		return null;
	}

	/**
	 * @private Debugging purposes
	 */
	protected void toString(int tab, StringBuffer buffer) {
		Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
		if (info == null || !(info instanceof JavaElementInfo))
			return;
		IJavaElement[] children = ((JavaElementInfo) info).getChildren();
		for (int i = 0; i < children.length; i++) {
			if (i > 0)
				buffer.append("\n"); //$NON-NLS-1$
			((JavaElement) children[i]).toString(tab, buffer);
		}
	}

	/**
	 * Debugging purposes
	 */
	protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
		buffer.append(this.tabString(tab));
		buffer.append("<import container>"); //$NON-NLS-1$
		if (info == null) {
			buffer.append(" (not open)"); //$NON-NLS-1$
		}
	}
}