/*******************************************************************************
* Copyright (c) 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* Zend Technologies
*******************************************************************************/
package org.eclipse.php.internal.core.codeassist.strategies;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.dltk.core.ISourceRange;
import org.eclipse.dltk.core.IType;
import org.eclipse.dltk.core.ModelException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.php.core.codeassist.ICompletionContext;
import org.eclipse.php.core.codeassist.ICompletionReporter;
import org.eclipse.php.core.codeassist.IElementFilter;
import org.eclipse.php.internal.core.PHPCorePlugin;
import org.eclipse.php.internal.core.codeassist.ProposalExtraInfo;
import org.eclipse.php.internal.core.codeassist.contexts.AbstractCompletionContext;
import org.eclipse.php.internal.core.codeassist.contexts.NamespaceMemberContext;
/**
* This strategy completes namespace classes and interfaces
*
* @author michael
*/
public class NamespaceTypesStrategy extends NamespaceMembersStrategy {
public NamespaceTypesStrategy(ICompletionContext context, IElementFilter elementFilter) {
super(context, elementFilter);
}
public NamespaceTypesStrategy(ICompletionContext context) {
super(context);
}
public void apply(ICompletionReporter reporter) throws BadLocationException {
ICompletionContext context = getContext();
if (!(context instanceof NamespaceMemberContext)) {
return;
}
NamespaceMemberContext concreteContext = (NamespaceMemberContext) context;
// now we compute type suffix in PHPCompletionProposalCollector
String suffix = "";//$NON-NLS-1$
ISourceRange replaceRange = getReplacementRange(concreteContext);
for (IType type : getTypes(concreteContext)) {
reporter.reportType(type, suffix, replaceRange, getExtraInfo());
}
}
public IType[] getTypes(NamespaceMemberContext context) throws BadLocationException {
String prefix = context.getPrefix();
List<IType> result = new LinkedList<IType>();
for (IType ns : context.getNamespaces()) {
try {
for (IType type : ns.getTypes()) {
if (StringUtils.startsWithIgnoreCase(type.getElementName(), prefix)) {
result.add(type);
}
}
} catch (ModelException e) {
PHPCorePlugin.log(e);
}
}
return (IType[]) result.toArray(new IType[result.size()]);
}
public String getSuffix(AbstractCompletionContext abstractContext) {
String nextWord = null;
try {
nextWord = abstractContext.getNextWord();
} catch (BadLocationException e) {
PHPCorePlugin.log(e);
}
return "::".equals(nextWord) ? "" : "::"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
protected int getExtraInfo() {
return ProposalExtraInfo.TYPE_ONLY;
}
}