Thursday, June 23, 2011

Roles / Profiles / Perspectives in NetBeans IDE 7.1

By: Geertjan Wielenga | Product Manager

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",
//iconBase="SET/PATH/TO/ICON/HERE",
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",
//iconBase="SET/PATH/TO/ICON/HERE",
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 {
@Override
public void restored() {
WindowManager.getDefault().addWindowSystemListener(this);
}
@Override
public void beforeLoad(WindowSystemEvent event) {
WindowManager.getDefault().setRole("user");
WindowManager.getDefault().removeWindowSystemListener(this);
}
@Override
public void afterLoad(WindowSystemEvent event) {
}
@Override
public void beforeSave(WindowSystemEvent event) {
}
@Override
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")
@ActionReferences({
@ActionReference(path = "Menu/Window", position = 250)
})
@Messages("CTL_SwitchToAdminAction=Switch To Admin")
public final class SwitchToAdminAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
WindowManager.getDefault().setRole("admin");
}
@Override
public boolean isEnabled() {
return !WindowManager.getDefault().getRole().equals("admin");
}
}
@ActionID(category = "Window",
id = "org.role.demo.ui.SwitchToUserAction")
@ActionRegistration(displayName = "#CTL_SwitchToUserAction")
@ActionReferences({
@ActionReference(path = "Menu/Window", position = 250)
})
@Messages("CTL_SwitchToUserAction=Switch To User")
public final class SwitchToUserAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
WindowManager.getDefault().setRole("user");
}
@Override
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.

Join the discussion

Comments ( 20 )
  • Thomas Edwin Thursday, June 23, 2011

    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.


  • Geertjan Thursday, June 23, 2011

    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.


  • guest Thursday, June 30, 2011

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


  • guest Saturday, September 17, 2011

    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.


  • Cường Sunday, October 16, 2011

    Hi Geertjan!

    I'm using NB 6.9.1 and can't import WindowSystemListener


  • Geertjan Sunday, October 16, 2011

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


  • guest Thursday, January 26, 2012

    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.

    Greeting,

    Lukas


  • Geertjan Thursday, January 26, 2012

    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.


  • guest Friday, January 27, 2012

    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,

    Setya


  • Geertjan Friday, January 27, 2012

    Hi Setya, yes, that's possible.


  • guest Monday, February 6, 2012

    Hi Geertjan,

    I've followed http://wiki.netbeans.org/DevFaqHideShowToolbar to remove Toolbar, but I can not find ToolbarPool class in UI Utilities API. How can I set the Toolbar configuration then ?

    Regards,

    Setya


  • Geertjan Monday, February 6, 2012
  • guest Monday, February 6, 2012

    Hi Geertjan,

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

    Regards,

    Setya


  • Geertjan Monday, February 6, 2012

    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.


  • Mathieu Sunday, February 26, 2012

    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?

    Thanks


  • Geertjan Sunday, February 26, 2012
  • Brad Gibson Monday, April 9, 2012

    Geertjan,

    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 org.openide.windows.TopComponent.Registration

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

    Thanks,

    Brad


  • Geertjan Monday, April 9, 2012

    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:

    http://platform.netbeans.org/whatsnew/71.html


  • Brad Gibson Tuesday, April 10, 2012

    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?

    Thanks,

    Brad


  • Brad Gibson Thursday, April 12, 2012

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

    @ActionReferences({

    @ActionReference(path = "Menu/Window/Switch View", position = 250)

    })

    Thanks,

    Brad


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

Visit the Oracle Blog

 

Contact Us

Oracle

Integrated Cloud Applications & Platform Services