X

Geertjan's Blog

  • April 4, 2014

Popup Action Based on Java Subclass

Geertjan Wielenga
Product Manager

Climb up the hierarchy (thanks to this) to determine whether a class is a subclass of com.vaadin.ui.CustomComponent and, if so, enable and display a popup menu on it so that it can be opened in a visual designer of some kind.

@ActionID(
category = "Tools",
id = "org.tc.customizer.DesignCustomComponentAction")
@ActionRegistration(
displayName = "#CTL_DesignCustomComponentAction",
lazy = false)
@ActionReferences({
@ActionReference(path = "Loaders/text/x-java/Actions", position = 150)
})
@Messages("CTL_DesignCustomComponentAction=Design Custom Component")
public final class DesignAbstractFormAction extends AbstractAction implements ContextAwareAction {
private final DataObject dobj;
private static Map args = new HashMap();
public DesignAbstractFormAction() {
this(Utilities.actionsGlobalContext());
}
public DesignAbstractFormAction(Lookup context) {
super(Bundle.CTL_DesignCustomComponentAction());
this.dobj = context.lookup(DataObject.class);
JavaSource javaSource = JavaSource.forFileObject(dobj.getPrimaryFile());
if (javaSource != null) {
try {
javaSource.runUserActionTask(new ScanForObjectTask(this), true);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
//Hide the menu item if it isn't enabled:
putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
}
@Override
public void actionPerformed(ActionEvent ev) {
//Starting point for designer:
TopComponent tc = new TopComponent();
tc.setDisplayName(dobj.getName());
tc.open();
tc.requestActive();
}
@Override
public Action createContextAwareInstance(Lookup actionContext) {
return new DesignAbstractFormAction(actionContext);
}
private static class ScanForObjectTask implements Task<CompilationController&g; {
private final DesignAbstractFormAction action;
private ScanForObjectTask(DesignAbstractFormAction action) {
this.action = action;
}
@Override
public void run(CompilationController compilationController) throws Exception {
compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
new MemberVisitor(compilationController, action).scan(
compilationController.getCompilationUnit(), null);
}
}
private static class MemberVisitor extends TreePathScanner<Void, Void&g; {
private CompilationInfo info;
private final AbstractAction action;
public MemberVisitor(CompilationInfo info, AbstractAction action) {
this.info = info;
this.action = action;
}
@Override
public Void visitClass(ClassTree t, Void v) {
Element el = info.getTrees().getElement(getCurrentPath());
if (el != null) {
TypeElement element = (TypeElement) el;
Types typeUtils = info.getTypes();
while (element!=null&&!element.toString().equals("com.vaadin.ui.CustomComponent")) {
element = (TypeElement) typeUtils.asElement(element.getSuperclass());
if (element!=null&&element.toString().equals("com.vaadin.ui.CustomComponent")) {
action.setEnabled(true);
} else {
action.setEnabled(false);
}
}
}
return null;
}
}
}

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.