Loosely Coupled Tabs in Java Editor

One of the NetBeans Platform 7.1 API enhancements is the @MultiViewElement.Registration annotation.

That lets you add a new tab to any existing NetBeans editor. Really powerful since I didn't need to change the sources (or even look at the sources) of the Java editor to add the "Visualizer" tab to it, as shown below:

Right now, the tab doesn't show anything, that will come in the next blog entry. The point here is to show how to set things up so that you have a new tab in the Java editor, without needing to touch any of the NetBeans IDE sources:

And here's the code, take note of the annotation, which registers the JPanel for the "text/x-java" MIME type:

import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import org.netbeans.core.spi.multiview.CloseOperationState;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.MultiViewElementCallback;
import org.openide.awt.UndoRedo;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;

@MultiViewElement.Registration(displayName = "#LBL_Visualizer",
iconBase = "org/java/vis/icon.gif",
mimeType = "text/x-java",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "JavaVisualizer",
position = 3000)
@NbBundle.Messages({
    "LBL_Visualizer=Visualizer"
})
public class JavaVisualizer extends JPanel implements MultiViewElement {

    private JToolBar toolbar = new JToolBar();
    private DataObject obj;
    private MultiViewElementCallback mvec;

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

    @Override
    public JComponent getVisualRepresentation() {
        return this;
    }

    @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 mvec) {
        this.mvec = mvec;
    }

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

It's a fair amount of code, but mostly pretty self-explanatory. The loosely coupled tabs are applicable to all NetBeans editors, not just the Java editor, which is why the "History" tab is now available to all editors throughout NetBeans IDE. In the next blog entry, you'll see the integration of the Visual Library into the panel I embedded in the Java editor.

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
19
21
22
23
24
25
26
27
28
29
30
   
       
Today