Monday Jan 07, 2013

Savable in Node Based MultiViewElement

Some time ago, Timon Veenstra from Ordina wrote about how to create MultiViewElements for Nodes. A follow up question is... how to enable the Save Action for MultiViewElements that have been created for Nodes?

The constructor of a MultiViewElement always receives a Lookup. But how to add a Saveable object to that Lookup so that the Save Action can become enabled?

My solution is weird, but the only thing that I can think of: put the InstanceContent into the Node's Lookup, i.e., add InstanceContent to InstanceContent:

public class CustomerNode extends BeanNode<Customer> implements Serializable {
    
    public CustomerNode(Customer bean) throws IntrospectionException {
        this(bean, new InstanceContent());
    }
    
    private CustomerNode(Customer bean, InstanceContent ic) throws IntrospectionException {
        super(bean, Children.LEAF, new AbstractLookup(ic));
        ic.add(ic);
        setDisplayName(bean.getName());
    }

    @Override
    public Action getPreferredAction() {
        return new AbstractAction("Edit") {
            @Override
            public void actionPerformed(ActionEvent e) {
                TopComponent tc = MultiViews.createMultiView("application/x-customernode", CustomerNode.this);
                tc.open();
                tc.requestActive();
            }
        };
    }

}

Now, in your MultiViewElement, you can get the InstanceContent from the Lookup that you receive in the constructor. That Lookup is defined by the Lookup in the Node. Therefore, because there's an InstanceContent in the Node Lookup (as you can see above), the InstanceContent is now in the MultiViewElement:

@MultiViewElement.Registration(
        displayName = "General",
        mimeType = "application/x-customernode",
        persistenceType = TopComponent.PERSISTENCE_NEVER,
        preferredID = "CustomerNodeGeneralElement",
        position = 100)
public class GeneralPanel extends JPanel implements MultiViewElement {

    private final Lookup lookup;
    private final InstanceContent ic;
    
    public GeneralPanel(Lookup lookup) {
        this.lookup = lookup;
        this.ic = lookup.lookup(InstanceContent.class);
        ...
        ...
        ...

And now, anywhere in the MultiViewElement, you can add/remove the Savable to/from the InstanceContent whenever needed. That will trigger the Save Action to enable/disable itself.

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
« January 2013 »
SunMonTueWedThuFriSat
  
25
26
27
30
  
       
Today