Opening a TopComponent per Node

In the Node, displayed in a viewer TopComponent, add an OpenCookie to the Lookup, and define it such that a new editor TopComponent is created only if no currently open editor TopComponent contains the Object in its Lookup:
public class PersonNode extends BeanNode {

    public PersonNode(Person person) throws IntrospectionException {
        this(person, new InstanceContent());
    }

    private PersonNode(final Person person, InstanceContent ic) throws IntrospectionException {
        super(person, Children.LEAF, new AbstractLookup(ic));
        ic.add(new OpenCookie() {
            @Override
            public void open() {
                TopComponent tc = findTopComponent(person);
                if (tc == null) {
                    tc = new PersonEditorTopComponent(person);
                    tc.open()
                }
                tc.requestActive();
            }
        });
        setDisplayName(person.getType());
    }

    private TopComponent findTopComponent(Person person) {
        Set openTopComponents = WindowManager.getDefault().getRegistry().getOpened();
        for (TopComponent tc : openTopComponents) {
            if (tc.getLookup().lookup(Person.class) == person) {
                return tc;
            }
        }
        return null;
    }

    @Override
    public Action[] getActions(boolean context) {
        return new Action[]{SystemAction.get(OpenAction.class)};
    }
    
}

Note that we also have an OpenAction from the Actions API in the context menu of the Node. When you right-click the Node, the Open action will be enabled if there's an OpenCookie in the Lookup. As you can see in the constructor above, there is an OpenCookie in the Lookup, with the definition of what should happen when the action is invoked.

When a new TopComponent is created, immediately put the Object into the TopComponent Lookup so that the same TopComponent will be found next time Open is clicked on the Node:

public final class PersonEditorTopComponent extends TopComponent {
    
    public PersonEditorTopComponent(Person person) {
        initComponents();
        setName(person.getType());
        setToolTipText(person.getType());
        associateLookup(Lookups.singleton(person));
    }

    ...
    ...
    ...

Note: In the above TopComponent, I have removed all the annotations that are generated into the top of the TopComponent by the New Window wizard.

Complete source code for the above: http://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.3/misc/SimpleApp

PS: I tried using Openable instead of OpenCookie, but the OpenAction doesn't respond to it. 

Comments:

Is there a way to change the display name of the action shown in the context menu for each node?

In short, if you right-click an "X" node, is there a way to make the context menu display "Open X?"

Posted by Michael Bishop on March 02, 2013 at 08:18 PM PST #

Hi Michael. Good question and here's the answer:

https://blogs.oracle.com/geertjan/entry/creating_a_customized_openaction

Posted by Geertjan on March 03, 2013 at 12:25 AM PST #

Hi the simpleApp doesn't compile this is the error I get:-
D:\Program Files\NetBeans 7.3\harness\suite.xml:130: Cannot open E:\Repos\7.3\misc\SimpleApp\${project.org.person.actions}\nbproject\project.xml
BUILD FAILED (total time: 0 seconds)

I think the package project.org.person.actions is missing (correct me if am wrong)

Posted by guest on July 15, 2013 at 02:21 AM PDT #

I'm new to the application platform and my work has asked that I start creating my swing applications in the module application style for now on. Do you have any examples on how to open a topcomponent based on a custom menu click event. Example: lets say you have two radio buttons as a custom menu kind of like the workflow predicition system oracle provides. If radio button 1 is pressed a topcomponent based on that theme will open, if button 2 a different topcomponent will open.

Posted by guest on January 08, 2014 at 09:24 AM PST #

This should help: https://platform.netbeans.org/tutorials/nbm-workflow.html Also, drop me an e-mail at geertjan.wielenga@oracle.com and we'll chat further.

Posted by Geertjan on January 08, 2014 at 09:27 AM PST #

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