On my application, I need to display a "Create project" button if I have the role "admin", otherwise if just a simple user, the action should be disabled and the button should not be displayed at all.
Here is my code:
@ActionID(id = "com.demos.core.action.project.ProjectCreateAction", category = "Actions")
@ActionRegistration(displayName = "com.demos.core.Bundle#action.project.projectcreate", iconBase = "com/demos/core/action/create_project.png")
@ActionReference(path = "Actions/Ribbon/TaskPanes/group-project/set-project",position = 10)
public final class ProjectCreateAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
...
}
}
In the actionPerformed() method, I'm able to get the user role, but it is too late, I don't want to display the action button at all.
How can I hide this action button if my user is not allowed to use it?
One of possible ways is to implement Presenter.Toolbar from package org.openide.util.actions like this:
// Some Annotations here
public final class SomeAction extends AbstractAction implements Presenter.Toolbar {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Some action
}
@Override
public Component getToolbarPresenter() {
ImageIcon icon = ImageUtilities.loadImageIcon("path/to/image.png", true);
JButton button = new JButton(icon);
button.addActionListener(this);
button.setToolTipText(NbBundle.getMessage(SomeAction.class, "TextID"));
button.setVisible(SomeUtilityClass.isAdmin());
return button;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.