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.

category = "Tools",
id = "org.tc.customizer.DesignCustomComponentAction")
displayName = "#CTL_DesignCustomComponentAction",
lazy = false)
@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() {
public DesignAbstractFormAction(Lookup context) {
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) {
//Hide the menu item if it isn't enabled:
putValue(DynamicMenuContent.HIDE_WHEN_DISABLED, true);
public void actionPerformed(ActionEvent ev) {
//Starting point for designer:
TopComponent tc = new TopComponent();
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;
public void run(CompilationController compilationController) throws Exception {
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;
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")) {
} else {
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.