Java developer and architect, specialized in rich clients on the netbeans rcp. Believes in fast, effective computing for everyone. Ubuntu, humanity to others. Timon has posted 11 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create MultiView Editors for Nodes

03.07.2012
| 6407 views |
  • submit to reddit

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)
@Messages("LBL_Farm_PROPERTIES=Properties")
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)));
        getLookup().lookup(Farm.class).addPropertyChangeListener(this);
    }
... 

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:

    @Override
    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() {
            super(Bundle.Farm_open_details_action_name());
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            TopComponent tc = MultiViews.createMultiView("application/x-agrosense-farm", FarmNode.this);
            tc.open();
            tc.requestActive();
        }

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.

Result:


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  

Published at DZone with permission of its author, Timon Veenstra.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)