NB: How to create a context-aware submenu

Last time I blogged about “How create a context sensitive action with a submenu in the project-view?” [1].  Now there was an additional requirement to be fulfilled: 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.

Thanks to the help of Jean-Marc Borer a solution can be provided too.

        category = "MyActions",
        id = "de.markiewb.netbeans.sample.ContextAwarePopupAction"
        displayName = "#CTL_ContextAwarePopupAction", lazy = false
    @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
        //create an action, which is only enabled when exactly 2 projects are selected
        result = Utilities.actionsGlobalContext().lookupResult(Project.class);
        this.lookupListener = new LookupListener() {

            public void resultChanged(LookupEvent ev) {
                final Runnable runnable = new Runnable() {

                    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()) {
                } else {
        result.addLookupListener(WeakListeners.create(LookupListener.class, this.lookupListener, result));

    public void actionPerformed(ActionEvent e) {

    public JMenuItem getPopupPresenter() {
        JMenu main = new JMenu(this);
        List<? extends Action> actionsForPath = Utilities.actionsForPath("Actions/MyActions/SubActions");
        for (Action action : actionsForPath) {
        return main;

The most important part of the popup action is to emulate a context-aware action using a lookup listener.


The updated sample code can be found at https://github.com/markiewb/nb-api-samples/tree/master/SubmenuWithContextSensitiveAction. The entry of the offical developer FAQ at http://wiki.netbeans.org/DevFaqActionNodePopupSubmenu has also been updated.

[1] https://benkiew.wordpress.com/2015/09/01/nb-how-to-create-a-context-sensitive-action-within-a-submenu/


NB: How to create a context sensitive action within a submenu

Today somebody asked “how create a context sensitive action with a submenu in the project-view?” in the NetBeans mailing list. So I figured it out and I like to document it using this post.

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.

        category = "MyActions",
        id = "de.markiewb.netbeans.sample.PopupAction"
        displayName = "#CTL_PopupAction", lazy = false
    @ActionReference(path = "Projects/Actions")
@Messages("CTL_PopupAction=I am a submenu")
public final class PopupAction extends AbstractAction implements ActionListener, Presenter.Popup {

    public void actionPerformed(ActionEvent e) {

    public JMenuItem getPopupPresenter() {
        JMenu main = new JMenu(Bundle.CTL_PopupAction());
        List<? extends Action> actionsForPath = Utilities.actionsForPath("Actions/MyActions/SubActions");
        for (Action action : actionsForPath) {
        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.

        category = "MyActions/SubActions",
        id = "de.markiewb.netbeans.sample.HelloProjectsAction"
        displayName = "#CTL_HelloProjectsAction"
public final class HelloProjectsAction implements ActionListener {

    private final List context;

    public HelloProjectsAction(List context) {
        this.context = context;

    public void actionPerformed(ActionEvent ev) {
        JOptionPane.showMessageDialog(null, context.size() + " projects selected: " + context);

The result:

The complete sample code can be found at https://github.com/markiewb/nb-api-samples/tree/master/SubmenuWithContextSensitiveAction. I will also add this documentation to the offical developer FAQ at http://wiki.netbeans.org/DevFaqActionNodePopupSubmenu

Happy coding!

Update: The second part of this post can be found at https://benkiew.wordpress.com/2015/09/13/nb-how-to-create-a-context-aware-submenu/

Github issues and Color Preview plugins

Today I like to blog about some new plugins from another very productive plugin author. All of them were created by junichi11, who created more than 15 plugins, which all are freely available at the NetBeans plugin portal. Many of them are PHP-related, so if you are a PHP developer you are certainly using one of his plugins already.

a) Github issues plugin


This plugin integrates issues from your github repositories into the tasks view of NetBeans. From within NB you can view, edit and create issues. And much more…


BTW if you are using Backlog for tracking your issues, then you can also use https://github.com/junichi11/netbeans-backlog-plugin

b) Color preview plugin


This plugin shows color for hexadecimal encoded colors (f.e. #FF0045) in the left sidebar of the editor. This feature is not new to IDEs, but it is now finally available for NetBeans. Enable this feature via “View->Show Colors” and the colors in CSS files will be visualized. It is configurable and thus not limited to CSS.


The plugins are signed and this way they can be installed directly from your IDE (Tools->Plugins->Available Plugins). You can also download them from their github pages or from http://plugins.netbeans.org and install them manually

Do not hesitate to post feedback or file issues at the respective github pages of the plugins. junichi11 is a friendly one and very responsive! Thank you for the good work junichi11!

New version of JRebel for NetBeans Plugin 6.1.0

I am sure you already noticed that there was an update to the JRebel for NetBeans plugin.

Regarding to http://plugins.netbeans.org/plugin/22254/jrebel-netbeans-plugin version 6.1.0 contains the following changes:

  • Added support for launching Grails applications with JRebel.
  • Added JRebel startup tab to JRebel Configuration. Added setup instructions for three startup scenarios (IDE, CLI and remoting).
  • Added support for enabling/disabling JRebel agent per server and standalone project via the JRebel Configuration. Removed the global on/off JRebel toolbar button.
  • Fixed an issue with setting VPN IPs for remoting/cloud.

One highly visible and very useful new feature is the configuration wizard in the options. This way you can configure your app server/application to use JRebel without any external documenation. Good Job ZT!


NB platform: Catching missing resources at compile-time using @StaticResource

When you develop a NetBeans platform application sometimes you like to refer to existing resource files like images.

Therefor you have to hard-code the path into your sources in a string literal. Normally you would notice a wrong path at runtime because of a FileNotFoundException – not very professional.

But wait! Use the org.netbeans.api.annotations.common.StaticResource-annotation from the Common Annotations module (/org.netbeans.api:org-netbeans-api-annotations-common) instead. Backed by an annotation processor NetBeans will check whether the path is correct at compile-time. This is pretty cool.

What do you get? NetBeans shows missing resources in the editor, in the “Action Items”-view and even the build will fail.


[1] http://bits.netbeans.org/8.0/javadoc/org-netbeans-api-annotations-common/org/netbeans/api/annotations/common/StaticResource.html

New version of Open File At Cursor plugin – 1.3.1

Today I released the version 1.3.1 of the “Open File At Cursor” plugin. There are some new minor features, which proofed to be useful for myself and may be also useful for other Java developers.


Updates in 1.3.1:

  • [Issue 18]: NPE when pressing CTRL in the diff dialog

Updates in 1.3.0:

  • [Feature 12]: Support fully qualified classnames
  • [Feature 14]: Search for classname in dependencies too (only works for dependencies with sources)
  • [Feature 10]: Find files in same package but different source root
  • [Issue 16]: Make the hyperlinking faster / use less IO

You can download it from http://plugins.netbeans.org/plugin/52349/open-file-at-cursor. The plugin is currently scheduled for verification for NB 8.0 and NB 7.4 and will be available from the plugin dialog within your NetBeans IDE within the next days (after verification).

As always please file issues and enhancements at https://github.com/markiewb/nb-resource-hyperlink-at-cursor/issues . Pull request are also appreciated. Thank you

Maven: Force the exclusion of dependencies

In large Maven-based projects consisting of several high-level frameworks sooner or later there will come the time, when there are two versions of the same dependency in the classpath. For example: two versions of the same logging framework.

One approach to solve such ambiguity is to choose one of the versions (which is hopefully compatible) and to use it as an explicit dependency. Nevertheless other dependencies may still introduce other version as transitive dependencies. This may be caused by different groupIds, which will result in two similar named jar.

Once you got a candidate you can start finding all the possible sources of the dependency.

mvn dependency:tree -Dverbose -Dincludes=log4j:log4j

will show you the dependency-tree, but only the relevant excerpt. Using this information you can now add your exclusions to the affected pom.xml files.
Exclusions are configured via the exclusion-tag [1], which excludes specific transitive dependencies. For example:


By the way: Java IDEs can help you doing this.

After that you can make sure the faulty dependency versions will never ever be included again. This can be done using the maven-enforcer-plugin [2]

									<!-- exclude all versions lower than 1.2.17-->									

[1] http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html
[2] http://maven.apache.org/enforcer/maven-enforcer-plugin/