// @SuppressWarnings("restriction")
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
EditorHelper editor = EditorHelper.getCurrent(event);
IProject project = editor.getProject();
IJavaProject jProject = JavaCore.create(project);
if(editor != null) {
String relativePath = ((IFileEditorInput) editor.textEditor.getEditorInput()).getFile().getProjectRelativePath().toString();
if (relativePath.startsWith("app/japidviews/") && relativePath.endsWith(".java")) {
// bran: if we are in the japid derived Java template code, let's switch back to the html view
String jFile = DirUtil.mapJavaToSrc(relativePath);
IFile f = project.getFile(jFile);
try {
FilesAccess.openFile(f);
} catch (CoreException e) {
PlayPlugin.showError(e);
}
return null;
}
}
boolean useJapid = true;
String line;
String viewName = null;
String title = editor.getTitle();
String controllerName = title.replace(".java", "");
String packageName = "";
IEditorInput editorInput = editor.textEditor.getEditorInput();
ITextSelection selection = (ITextSelection) editor.textEditor.getSelectionProvider().getSelection();
IJavaElement elem = JavaUI.getEditorInputJavaElement(editorInput);
ICompilationUnit unit = null;
if (elem instanceof ICompilationUnit) {
unit = (ICompilationUnit) elem;
try {
IPackageDeclaration[] packs = unit.getPackageDeclarations();
if (packs.length < 1) {
info("This action can only apply to controllers.");
return null;
} else {
packageName = packs[0].getElementName();
if (!packageName.startsWith("controllers")) {
info("This action can only apply to controllers.");
return null;
}
}
// get the class declaration line
IType type = unit.getType(controllerName);
ITypeHierarchy superTypes = type.newSupertypeHierarchy(null);
// String name = JapidController.class.getName(); // this will require play.jar
String name = "cn.bran.play.JapidController";
IType japidController = jProject.findType(name);
if (superTypes.contains(japidController)) {
useJapid = true;
}
else {
useJapid = false;
}
// String superclassName = type.getSuperclassName();
// if (superclassName.toLowerCase().contains("japid")) {
// useJapid = true;
// }
// current selected elem
IJavaElement[] elements= unit.codeSelect(selection.getOffset(), selection.getLength());
if (elements.length > 0) {
// TODO extract the current selection to tell if the cursor in on renderJapidXXX line
System.out.println(elements);
}
} catch (JavaModelException e) {
e.printStackTrace();
}
} else {
System.out.println(elem.getElementType() + ":" + elem.getElementName());
}
viewName = getEnclosingActionName(selection, unit);
int lineNo = editor.getCurrentLineNo();
line = editor.getLine(lineNo);
if (line.contains("render")) {
if (!line.contains("renderJapid")) {
Pattern pt = Pattern.compile("\"(.*)\"");
Matcher m = pt.matcher(line);
if (m.find()) {