Roles / Profiles / Perspectives in NetBeans IDE 7.1

Posted by Geertjan on Oracle Blogs See other posts from Oracle Blogs or by Geertjan
Published on Thu, 23 Jun 2011 00:31:20 -0700 Indexed on 2011/06/23 8:26 UTC
Read the original article Hit count: 289

Filed under:

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.

© Oracle Blogs or respective owner

Related posts about /NetBeans IDE