protected IStatus run(IProgressMonitor monitor) {
try {
final IPackageFragment pkg = (IPackageFragment) target;
String uri = new String(source);
Search search = CodeConjurer.getInstance().getActiveEditorSearch();
final BodyDeclaration selectedElement = search.getSearchResult()
.find(uri);
// Insert class
if (selectedElement.getNodeType() == BodyDeclaration.TYPE_DECLARATION) {
monitor.beginTask("Insert class into package", 2);
TypeDeclaration typeDec = (TypeDeclaration) selectedElement;
String adapterCode = null;
// If this is a result from test-driven search and an adapter is
// present, use it.
if (Integer.parseInt((String) typeDec
.getProperty(ResultProperty.SEARCH_KIND.name())) == Search.TEST_DRIVEN_SEARCH) {
adapterCode = (String) typeDec
.getProperty(ResultProperty.TEST_RESULT.name());
if (adapterCode.contains("<adapter>false</adapter>")) {
adapterCode = null; // Only if an adapter is necessary,
// we leave this value set
}
}
String name = typeDec.getName().toString();
String sourceCode = (String) selectedElement
.getProperty(ResultProperty.RAW_SOURCE.name());
/** Indicates if an adapter was created */
boolean adapted = false;
/* Insert the adapter if the user wishes */
if (Activator.getDefault().getPreferenceStore()
.getBoolean(PreferenceConstants.P_SHOW_ADAPTER)) {
if (adapterCode != null) {
adapterCode = adapterCode
.replace(
"merobase_auto_generated_package_for_adaptation",
pkg.getElementName());
monitor.subTask("Insert adapter class");
// Find out the name of the adapter
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(adapterCode.toCharArray());
CompilationUnit astRoot = (CompilationUnit) parser
.createAST(null);
List<TypeDeclaration> typeList = astRoot.types();
if (typeList != null && typeList.size() > 0) {
TypeDeclaration adapterType = (TypeDeclaration) typeList
.get(0);
insertType(pkg, adapterType.getName().toString(),
adapterCode, true, monitor);
}
monitor.worked(1);
monitor.subTask("Check for adaptee package");
IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) pkg
.getParent();
String pkgname = pkg.getElementName();
// pkgRoot.open(monitor);
IPackageFragment adapteePkg = pkgRoot
.createPackageFragment(pkgname + "."
+ "adaptee", true, monitor);
monitor.subTask("Insert adaptee with functionality");
/* Insert the adaptee class */
insertType(adapteePkg, name, sourceCode, true, monitor);
monitor.worked(1);
logger.debug("Check for interfaces");
for (Object typeDecO : typeDec.superInterfaceTypes()) {
Type tDec = (Type) typeDecO;
logger.debug("Implements " + tDec);
StringBuilder iface = new StringBuilder("package "
+ adapteePkg.getElementName() + ";"
+ System.getProperty("line.separator"));
iface.append(System.getProperty("line.separator")
+ "/** Automatically generated interface dependency */"
+ System.getProperty("line.separator"));
iface.append("public interface " + tDec + "{}");
// The interfaces should not be opened
insertType(adapteePkg, tDec.toString(),
iface.toString(), false, monitor);
}
adapted = true;
}
}
// Insert the result without adapter
if (!adapted) {
insertType(pkg, name, sourceCode, true, monitor);
monitor.worked(1);
}
}
if (selectedElement.getNodeType() == BodyDeclaration.METHOD_DECLARATION) {
MethodDeclaration methodDec = (MethodDeclaration) selectedElement;
IWorkbenchWindow window = PluginUI.getWindow();
if (window == null)
return Status.CANCEL_STATUS;
Shell shell = window.getShell();