/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* 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:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.editors.tmlscript.parsing;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import de.innovationgate.eclipse.editors.tmlscript.TMLScriptCompletionProposal;
import de.innovationgate.wga.model.VersionCompliance;
public class TMLScriptPackage {
private String _name;
Set<String> _classnames = new HashSet<String>();
public Set<String> getClassnames() {
return _classnames;
}
public TMLScriptPackage(String name) {
_name = name;
}
public String getName() {
return _name;
}
public List<TMLScriptCompletionProposal> createProposals(String packageName, String prefix, int cursorOffset, VersionCompliance versionCompliance, boolean newOperant) {
List<TMLScriptCompletionProposal> proposals = new ArrayList<TMLScriptCompletionProposal>();
String name = getName();
String effectivePrefix = packageName;
if (prefix != null && !prefix.equals("")) {
if (!effectivePrefix.equals("")) {
effectivePrefix += "." + prefix;
} else {
effectivePrefix = prefix;
}
}
if (name.toLowerCase().startsWith(effectivePrefix.toLowerCase()) && !name.equals(packageName)) {
//build completion proposal with package name
int cursorAfterProposal = -1;
StringBuffer display = new StringBuffer();
StringBuffer replacement = new StringBuffer();
display.append(name);
replacement.append(name);
if (cursorAfterProposal == -1) {
cursorAfterProposal = replacement.length();
}
//display.append(" : " + ReflectionManager.buildSimpleClassname(getType()));
int replacementOffset = 0;
int replacementLength = 0;
if (effectivePrefix != null) {
replacementOffset = cursorOffset - effectivePrefix.length();
replacementLength = effectivePrefix.length();
if (prefix != null && prefix.equals("") && !effectivePrefix.equals("")) {
// prefix empty so we have a dot at the end ... adjust offset and length
replacementOffset--;
replacementLength++;
}
}
TMLScriptCompletionProposal proposal = new TMLScriptCompletionProposal(replacement.toString(), replacementOffset, replacementLength, cursorAfterProposal, display.toString());
proposals.add(proposal);
} else if (name.equals(packageName)) {
// create completion proposals for constructors or static access
for (String classname : _classnames) {
String classOnlyName = classname.substring(name.length() + 1);
if (prefix == null || classOnlyName.toLowerCase().startsWith(prefix.toLowerCase())) {
if (newOperant) {
Iterator<TMLScriptMethod> constructors = ReflectionManager.getInstance(versionCompliance).getPublicConstructors(classname).iterator();
while (constructors.hasNext()) {
TMLScriptMethod constructor = constructors.next();
proposals.addAll(constructor.createProposals(prefix, cursorOffset));
}
} else {
if (ReflectionManager.getInstance(versionCompliance).getPublicStaticMethods(classname).size() > 0 || ReflectionManager.getInstance(versionCompliance).getPublicStaticProperties(classname).size() > 0) {
//build completion proposal with package name
int cursorAfterProposal = -1;
StringBuffer display = new StringBuffer();
StringBuffer replacement = new StringBuffer();
display.append(classOnlyName);
replacement.append(classOnlyName);
if (cursorAfterProposal == -1) {
cursorAfterProposal = replacement.length();
}
int replacementOffset = 0;
int replacementLength = 0;
if (prefix != null) {
replacementOffset = cursorOffset - prefix.length();
replacementLength = prefix.length();
}
TMLScriptCompletionProposal proposal = new TMLScriptCompletionProposal(replacement.toString(), replacementOffset, replacementLength, cursorAfterProposal, display.toString());
proposals.add(proposal);
}
}
}
}
}
return proposals;
}
}