Popup Action Based on Java Subclass

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;
        }
    }
}
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2015
SunMonTueWedThuFriSat
   
4
5
11
12
19
20
21
22
23
24
25
26
27
28
29
30
  
       
Today