X

Geertjan's Blog

  • March 2, 2013

Opening a TopComponent per Node

Geertjan Wielenga
Product Manager
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. 

Join the discussion

Comments ( 9 )
  • Michael Bishop Sunday, March 3, 2013

    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?"


  • Geertjan Sunday, March 3, 2013

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

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


  • guest Monday, July 15, 2013

    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)


  • guest Wednesday, January 8, 2014

    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.


  • Geertjan Wednesday, January 8, 2014

    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.


  • Alex Saturday, May 17, 2014

    Hello,

    I'm unable to compile the provided sources that I checked out from here:

    https://svn.java.net/svn/nb-api-samples~api-samples/versions/7.3/misc/SimpleApp

    It says it does not find the referenced PersonOpen module.

    In fact I was trying to compile the example exactly to understand how to define an action and it seems missing.

    I'm using netbeans 8 but I suppose this should not be a problem


  • Geertjan Sunday, May 18, 2014

    You were trying to compile the example to understand how to define an action? In that case, get the book and learn about actions -- https://leanpub.com/nbp4beginners. And why should using NetBeans 8 not be a problem? Clearly the example wasn't made for NetBeans 8, was it?


  • Alex Sunday, May 18, 2014

    I was able to make it work without defining a custom action, I just used the standard open action.

    Anyway, the PersonOpen module is missing from the source, isn't?

    It's referenced from the makefile but it's not in the checkout.


  • Shardul Vaidya Friday, November 20, 2015

    I am migrating my eclipse RCP application to netbeans RCP.

    In my case there are 3 different child nodes under one parent node. Now i want to open a wizard window when i right click one of the child node and select some option on the pop up which my right click has generated.

    can you please help me about this?


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.