-
Notifications
You must be signed in to change notification settings - Fork 1
DevFaqActionNodePopupSubmenu
-
Override
getActions()
of your Node subclass. -
Create a custom subclass of
javax.swing.Action
which implements the interfacePresenter.Popup
. -
Return a
JMenu
with whatever you want on it fromgetPopupPresenter()
. -
Return that action in the array of actions from
getActions()
.
First here is the action for the submenu. The action is registered to the project context menu. It uses the Presenter.Popup
interface to register itself as a submenu. In the getPopupPresenter()
method the submenu is assembled via
Utilities.actionsForPath
.
@ActionID(
category = "MyActions",
id = "de.markiewb.netbeans.sample.PopupAction"
)
@ActionRegistration(
displayName = "#CTL_PopupAction", lazy = false
)
@ActionReferences({
@ActionReference(path = "Projects/Actions")
})
@Messages("CTL_PopupAction=I am a submenu")
public final class PopupAction extends AbstractAction implements ActionListener, Presenter.Popup {
@Override
public void actionPerformed(ActionEvent e) {
//NOP
}
@Override
public JMenuItem getPopupPresenter() {
JMenu main = new JMenu(Bundle.CTL_PopupAction());
List<? extends Action> actionsForPath = Utilities.actionsForPath("Actions/MyActions/SubActions");
for (Action action : actionsForPath) {
main.add(action);
}
return main;
}
}
In the previous code snippet Utilities.actionsForPath
has been used to resolve action(s) at Actions/MyActions/SubActions
. Here is a context sensitive action, which is registered at this location.
@ActionID(
category = "MyActions/SubActions",
id = "de.markiewb.netbeans.sample.HelloProjectsAction"
)
@ActionRegistration(
displayName = "#CTL_HelloProjectsAction"
)
@Messages("CTL_HelloProjectsAction=HelloProjects...")
public final class HelloProjectsAction implements ActionListener {
private final List context;
public HelloProjectsAction(List context) {
this.context = context;
}
@Override
public void actionPerformed(ActionEvent ev) {
JOptionPane.showMessageDialog(null, context.size() + " projects selected: " + context);
}
}
The requirement: "The submenu should be only enabled, when exactly two project nodes are selected. Only if this condition is true, the submenu items should be displayed."
See the following sample code. The most important part of the popup action is to emulate a context-aware action using a lookup listener.
@ActionID(
category = "MyActions",
id = "de.markiewb.netbeans.sample.ContextAwarePopupAction"
)
@ActionRegistration(
displayName = "#CTL_ContextAwarePopupAction", lazy = false
)
@ActionReferences({
@ActionReference(path = "Projects/Actions")
})
@Messages("CTL_ContextAwarePopupAction=I am a context-aware submenu")
public final class ContextAwarePopupAction extends AbstractAction implements ActionListener, Presenter.Popup {
private final Lookup.Result<Project> result;
private final transient LookupListener lookupListener;
public ContextAwarePopupAction() {
putValue(NAME, Bundle.CTL_ContextAwarePopupAction());
//disabled by default - at loading time
setEnabled(false);
//create an action, which is only enabled when exactly 2 projects are selected
result = Utilities.actionsGlobalContext().lookupResult(Project.class);
this.lookupListener = new LookupListener() {
@Override
public void resultChanged(LookupEvent ev) {
final Runnable runnable = new Runnable() {
@Override
public void run() {
int s = result.allInstances().size();
ContextAwarePopupAction.this.setEnabled(s == 2);
}
};
// to make sure that it will be executed on EDT
if (EventQueue.isDispatchThread()) {
runnable.run();
} else {
SwingUtilities.invokeLater(runnable);
}
}
};
result.addLookupListener(WeakListeners.create(LookupListener.class, this.lookupListener, result));
}
@Override
public void actionPerformed(ActionEvent e) {
//NOP
}
@Override
public JMenuItem getPopupPresenter() {
JMenu main = new JMenu(this);
List<? extends Action> actionsForPath = Utilities.actionsForPath("Actions/MyActions/SubActions");
for (Action action : actionsForPath) {
main.add(action);
}
return main;
}
}
The content in this page was kindly donated by Oracle Corp. to the Apache Software Foundation.
This page was exported from http://wiki.netbeans.org/DevFaqActionNodePopupSubmenu , that was last modified by NetBeans user Markiewb on 2015-09-13T19:53:44Z.
NOTE: This document was automatically converted to the AsciiDoc format on 2018-01-26, and needs to be reviewed.
Apache NetBeans is an effort undergoing incubation at The Apache Software Foundation (ASF).
Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.
While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
This wiki is an experiment pending Apache NetBeans Community approval.