Adding a Visual Tab to the NetBeans Java Editor

With the upcoming NetBeans Platform 7.1, because of the new ability to add tabs to existing file editors, you'll be able to do this, i.e., you'll be able to add tabs to a file editor "from the outside", via a new plugin that you've created yourself:

What you see above is a new tab in the Java editor, containing a LabelWidget from the Visual Library for each method found in the Java class.

Here's the code and, again, note that this will work in NetBeans Platform 7.1 (i.e., a download of a recent trunk build) only:

@MultiViewElement.Registration(displayName = "#LBL_JAVA_VISUAL",
iconBase = "SET/PATH/TO/ICON/HERE",
mimeType = "text/x-java",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "JavaVisual",
position = 2000)
@Messages({
    "LBL_JAVA_VISUAL=Visual"
})
public class JavaVisualDesigner extends JPanel implements MultiViewElement {

    private DataObject obj;
    private static LayerWidget layerWidget;
    private JToolBar toolbar = new JToolBar();
    private transient MultiViewElementCallback callback;
    private static Scene scene;

    public JavaVisualDesigner(Lookup lkp) {
        obj = lkp.lookup(DataObject.class);
        assert obj != null;
    }

    @Override
    public String getName() {
        return "#LBL_JAVA_VISUAL";
    }

    @Override
    public JComponent getVisualRepresentation() {
        scene = new Scene();
        layerWidget = new LayerWidget(scene);
        FileObject fo = obj.getPrimaryFile();
        JavaSource js = JavaSource.forFileObject(fo);
        try {
            js.runUserActionTask(new Task<CompilationController>() {
                @Override
                public void run(CompilationController compilationController) throws Exception {
                    compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
                    Document document = compilationController.getDocument();
                    if (document != null) {
                        new MemberVisitor(compilationController).scan(compilationController.getCompilationUnit(), null);
                    }
                }
            }, true);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        scene.addChild(layerWidget);
        return scene.createView();
    }

    private static class MemberVisitor extends TreePathScanner<Void, Void> {
        private CompilationInfo info;
        public MemberVisitor(CompilationInfo info) {
            this.info = info;
        }
        @Override
        public Void visitClass(ClassTree t, Void v) {
            Element el = info.getTrees().getElement(getCurrentPath());
            if (el != null) {
                TypeElement te = (TypeElement) el;
                List enclosedElements = te.getEnclosedElements();
                for (int i = 0; i < enclosedElements.size(); i++) {
                    Element enclosedElement = (Element) enclosedElements.get(i);
                    String name = enclosedElement.getSimpleName().toString();
                    LabelWidget lw = new LabelWidget(scene, name);
                    lw.setPreferredLocation(new Point(i * 20, i * 40));
                    lw.getActions().addAction(ActionFactory.createMoveAction());
                    layerWidget.addChild(lw);
                }
            }
            return null;
        }
    }

    @Override
    public JComponent getToolbarRepresentation() {
        return toolbar;
    }

    @Override
    public Action[] getActions() {
        return new Action[0];
    }

    @Override
    public Lookup getLookup() {
        return obj.getLookup();
    }

    @Override
    public void componentOpened() {
    }

    @Override
    public void componentClosed() {
    }

    @Override
    public void componentShowing() {
    }

    @Override
    public void componentHidden() {
    }

    @Override
    public void componentActivated() {
    }

    @Override
    public void componentDeactivated() {
    }

    @Override
    public UndoRedo getUndoRedo() {
        return UndoRedo.NONE;
    }

    @Override
    public void setMultiViewCallback(MultiViewElementCallback callback) {
        this.callback = callback;
    }

    @Override
    public CloseOperationState canCloseElement() {
        return CloseOperationState.STATE_OK;
    }

}

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 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today