Over a million developers have joined DZone.
Platinum Partner

How to Create MultiView Editors for Nodes

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

The NetBeans MultiView Windows component is great for providing multiple views of the data in a file. This could be very useful for data that isn't in a file as well. Fortunately, it is quite easy to use the MultiView component for your own non-file data as well.

Let's say you have a Farm object created from another source than a file. You have a node representing your farm in an explorer and now you want to create an editor-like component which would be able to give you different views on your farm. For example, one with the properties of your farm and one with the most urgent tasks to execute.

Lets start with the FarmNode: 

final class FarmNode extends AbstractNode implements Serializable, Lookup.Provider {

    public FarmNode(Farm farm) {
          super(Children.LEAF, Lookups.singleton(farm));

The FarmNode is a leaf node and contains a Farm in its lookup.

Now we create a properties editor tab for our first multi view tab.

@MultiViewElement.Registration(displayName = "#LBL_Farm_PROPERTIES", 
iconBase = "nl/cloudfarming/client/farm/model/house.png",
mimeType = "application/x-agrosense-farm",
persistenceType = TopComponent.PERSISTENCE_NEVER,
preferredID = "FarmVisual",
position = 100)
public final class FarmVisualElement extends JPanel implements MultiViewElement, PropertyChangeListener {

    private JToolBar toolbar = new JToolBar();
    private transient MultiViewElementCallback callback;
    private Lookup lkp;

    public FarmVisualElement(Lookup lkp) {
        this.lkp = lkp;
        setLayout(new BorderLayout());
        add(new FarmDetailsPanel(lkp.lookup(Farm.class)));

We have created a nice FarmDetailsPanel which takes a Farm as input and added it to a MultiViewElement implementation class. Important part is the mimeType section containing "application/x-agrosense-farm". We will see the same string appear again later. The FarmVisualElement expects a Farm object in the Lookup that is passed on in its constructor. 

Now we would like to add an action to the FarmNode to open a multiview TopComponent with our FarmVisualElement as "Properties" tab. We add the following to our FarmNode class:

    public Action[] getActions(boolean arg0) {
        Action[] nodeActions = new Action[1];
        nodeActions[0] = new OpenFarmDetailsAction();
        return nodeActions;

    @NbBundle.Messages(value="Farm open details action name=Open")
    private final class OpenFarmDetailsAction extends AbstractAction{

        public OpenFarmDetailsAction() {
        public void actionPerformed(ActionEvent e) {
            TopComponent tc = MultiViews.createMultiView("application/x-agrosense-farm", FarmNode.this);

The OpenFarmDetailsAction is the action we will use to open the MultiView TopComponent.

TopComponent tc = MultiViews.createMultiView("application/x-agrosense-farm", FarmNode.this); Contains a reference to the mimeType.

The MultiView will take a look in the MimeType registry and will discover we have registered our MulitViewElement there for the same MimeType. The lookup from our FarmNode will be passed on to the FarmVisualElement and provides the context for the tabs to base their content on.

This also shows how easy it would be for another module developer to add an extra tab to our farm MultiView component without even a dependency on our Farm module.


Full source code is available in the AgroSense source code repository (1) in the module farm/project.

 1- http://java.net/projects/agrosense/sources/core/show  

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}