Roles / Profiles / Perspectives in NetBeans IDE 7.1

With a check out of main-silver from yesterday, I'm able to use the brand new "role" attribute in @TopComponent.Registration, as you can see below, in the bit in bold:

@ConvertAsProperties(dtd = "-//org.role.demo.ui//Admin//EN",
autostore = false)
@TopComponent.Description(preferredID = "AdminTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true, role="admin")
public final class AdminTopComponent extends TopComponent {

And here's a window for general users of the application, with the "role" attribute set to "user":

@ConvertAsProperties(dtd = "-//org.role.demo.ui//User//EN",
autostore = false)
@TopComponent.Description(preferredID = "UserTopComponent",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true, role="user")
public final class UserTopComponent extends TopComponent {

So, I have two windows. One is assigned to the "admin" role, the other to the "user" role. In the "ModuleInstall" class, I add a "WindowSystemListener" and set "user" as the application's role:

public class Installer extends ModuleInstall implements WindowSystemListener {

    public void restored() {

    public void beforeLoad(WindowSystemEvent event) {

    public void afterLoad(WindowSystemEvent event) {

    public void beforeSave(WindowSystemEvent event) {

    public void afterSave(WindowSystemEvent event) {

So, when the application starts, the "UserTopComponent" is shown, not the "AdminTopComponent".

Next, I have two Actions, for switching between the two roles, as shown below:

@ActionID(category = "Window",
id = "org.role.demo.ui.SwitchToAdminAction")
@ActionRegistration(displayName = "#CTL_SwitchToAdminAction")
    @ActionReference(path = "Menu/Window", position = 250)
@Messages("CTL_SwitchToAdminAction=Switch To Admin")
public final class SwitchToAdminAction extends AbstractAction {

    public void actionPerformed(ActionEvent e) {

    public boolean isEnabled() {
        return !WindowManager.getDefault().getRole().equals("admin");
@ActionID(category = "Window",
id = "org.role.demo.ui.SwitchToUserAction")
@ActionRegistration(displayName = "#CTL_SwitchToUserAction")
    @ActionReference(path = "Menu/Window", position = 250)
@Messages("CTL_SwitchToUserAction=Switch To User")
public final class SwitchToUserAction extends AbstractAction {

    public void actionPerformed(ActionEvent e) {

    public boolean isEnabled() {
        return !WindowManager.getDefault().getRole().equals("user");

When I select one of the above actions, the role changes, and the other window is shown. I could, of course, add a Login dialog to the "SwitchToAdminAction", so that authentication is required in order to switch to the "admin" role.

Now, let's say I am now in the "user" role. So, the "UserTopComponent" shown above is now opened. I decide to also open another window, the Properties window, as below...

...and, when I am in the "admin" role, when the "AdminTopComponent" is open, I decide to also open the Output window, as below...

Now, when I switch from one role to the other, the additional window/s I opened will also be opened, together with the explicit members of the currently selected role. And, the main window position and size are also persisted across roles.

When I look in the "build" folder of my project in development, I see two different Windows2Local folders, one per role, automatically created by the fact that there is something to be persisted for a particular role, e.g., when a switch to a different role is done:

And, with that, we now clearly have roles/profiles/perspectives in NetBeans Platform applications from NetBeans Platform 7.1 onwards.


Does it support multiple role login i.e. user has several roles? My requirement is that user's manager can assign several roles to a rolegroup.

Posted by Thomas Edwin on June 23, 2011 at 02:36 AM PDT #

Yes. In that case, you would create a role for each rolegroup. Then when the user logs in, they would log in to a particular rolegroup.

Posted by Geertjan on June 23, 2011 at 04:32 AM PDT #

Gr8 ! a step by step Netbeans tutorial of multi-user login, accessing databases, would be a very helpful, appreciated addition.

Posted by guest on June 29, 2011 at 11:02 PM PDT #

thanks for the explanation.

since roles in a component is static, while a user's role is dynamic, there seem to be a need to say something like
setRoles (component-a: role-x, component-b, role-y)

also, i tried the above approach, and noticed (might not be doing it right) that the windows can be opened from windows menu regardless the current set role (shouldn't they be dynamically removed when role is changed?)

And I concur that a login tutorial with multiple role user could be very helpful.

Posted by guest on September 17, 2011 at 01:19 AM PDT #

Hi Geertjan!
I'm using NB 6.9.1 and can't import WindowSystemListener

Posted by Cường on October 16, 2011 at 04:36 PM PDT #

Cường, are you able to read the title of this blog entry? If so, now you know the answer to your question.

Posted by Geertjan on October 16, 2011 at 04:53 PM PDT #

Hey Geertjan,

is it also possible to enable, disable or even hide certain actions in the menus when changing the perspective? This would be wonderful as not every role should be able to access every part of the application in the admin/user example.


Posted by guest on January 26, 2012 at 12:15 AM PST #

Hi Lukas, that would be great, could you file an issue for it. Right now that isn't supported since the NetBeans Platform client that needed this feature, i.e., JDeveloper, didn't need the extension that you're describing here. I.e., right now roles only applies to the window system, not to the action system. However, there are other techniques you can use for what you describe. I.e., elsewhere in this blog you'll find instructions for dynamically injecting a layer file into an application. That's the approach you would use for what you're trying to do.

Posted by Geertjan on January 26, 2012 at 02:08 AM PST #

Hi Geertjan,

When application starts I want to present user with login perspective. This perspective should contain only 1 uncloseable TopComponent that spans the whole window area, this means the toolbar, menubar, statusbar etc will be hidden except the title bar, is this possible in Netbeans RCP ?

Thanks & Regards,


Posted by guest on January 26, 2012 at 11:19 PM PST #

Hi Setya, yes, that's possible.

Posted by Geertjan on January 27, 2012 at 01:34 AM PST #

Hi Geertjan,

I've followed to remove Toolbar, but I can not find ToolbarPool class in UI Utilities API. How can I set the Toolbar configuration then ?



Posted by guest on February 05, 2012 at 07:13 PM PST #

It's clearly there:

Posted by Geertjan on February 05, 2012 at 11:52 PM PST #

Hi Geertjan,

I mean I could not find ToolbarPool in the UI Utilities API, after some searching it's somehow moved to Datasystems API.



Posted by guest on February 06, 2012 at 03:01 AM PST #

Why should there have been any searching? Right-click the module's Libraries node in the Projects window. Then choose Add Dependency. Then type the class you need and you will see the module that provides it.

Posted by Geertjan on February 06, 2012 at 03:10 PM PST #

Hi Geertjan,

I'm converting my application to use roles but I notice something odd when switching roles. The whole window disappear for one second and it appears again with the new role. Is this a bug? Do you have the same issue?


Posted by Mathieu on February 26, 2012 at 01:15 AM PST #

The javadoc is very clear in how to solve that problem:

Posted by Geertjan on February 26, 2012 at 01:35 AM PST #

Is there a typo on the line?

@TopComponent.Registration(mode = "explorer", openAtStartup = true, role="user"

should it be roles, like the following:

@TopComponent.Registration(mode = "explorer", openAtStartup = true, roles="user"

If I use role I get the following error, but if I use roles then I am ok.

symbol : method role()
location: @interface
@TopComponent.Registration(mode = "explorer", openAtStartup = true, role="admin")


Posted by Brad Gibson on April 09, 2012 at 06:52 AM PDT #

Yes, it is "roles". But that's much better than "role" isn't it? Here's the notes on what's new in 7.1:

Posted by Geertjan on April 09, 2012 at 03:17 PM PDT #

Thanks for the quick response Geertjan. Yes it is better. Is there a way to set the rolls dynamically through the annotations? Right now I have two basic roles, user and advanced. Lets say the user wants to add a third role called standard. Is there a way to accomplish that through the annotations? For example point it to a function that returns the list of roles?


Posted by Brad Gibson on April 10, 2012 at 06:22 AM PDT #

Geertjan I have everything working for roles except the initial disabling of the selected role. Even though I set the role programmatically the isEnabled is never called until I actually change the role through the Windows/Switch View/ menu actions. Any ideas on this? I've even tried to call actionPerformed method directly but still the selected role's menu item does not get disabled.

@ActionID(category = "Window", id = "org.role.demo.ui.SwitchToUserRole")
@ActionRegistration(displayName = "#CTL_SwitchToUserRole")
@ActionReference(path = "Menu/Window/Switch View", position = 250)


Posted by Brad Gibson on April 12, 2012 at 05:06 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

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.


« May 2016