Quicktip: Maven-based NBM development and the user-dir

When you develop a NetBeans module using the Maven-approach, every time you “Clean & build” your module the userdir, which is placed in the target-directory by default, will be deleted. “Clean & Build” is necessary if you’re altering the layer.xml directly or indirectly by using annotations like @ActionReferences.  So after that you have to reconfigure your target platform again, f.e. by opening the same projects and files to restore the previous state. That is annoying, but easy to fix.

Add a profile to your settings.xml

<profile>
    <id>userdir</id>
    <properties>
        <netbeans.userdir>../userdir</netbeans.userdir>
    </properties>
</profile>

2016-01-23_13h14_03

After that you can choose the profile from the profile-dropdown or in the context-menu of the project node.

2016-01-23_13h18_46

This way the configured userdir is used for running/debugging your NetBeans module. It won’t get deleted automatically.

Advanced tips: Of course you can also configure an absolute path or even make the profile default by applying activeByDefault

<profile>
    <id>userdir</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <netbeans.userdir>/home/user/myuserdir</netbeans.userdir>
    </properties>
</profile>

 

Quicktip: How to display your own Java code-templates in the code completion of NetBeans IDE

After blogged about How to display your own PHP code-templates in the code completion of NetBeans IDE a NetBeans user asked me how it works for Java code.

Yes, it is possible and it is even simpler. Since NetBeans 7.4 (issue #160771) you can define the context, where code-template has to be shown. Don’t be overwhelmed by the available contexts. For completions within a block/method choose BLOCK.

2016-01-09_13h39_35

Quicktip: How to display your own PHP code-templates in the code completion of NetBeans IDE

Here a small tip for all the PHP developers, which want to see their code-templates in the code-completion list of NetBeans IDE.

  1. First create your own code template in code-template options at “Tools|Options|Editor|Code Templates” and save the options.
  2. Then open the code-templates file from your NetBeans user dir. For example: c:\Users\johndoe\AppData\Roaming\NetBeans\8.0.2\config\Editors\text\x-php5\CodeTemplates\org-netbeans-modules-editor-settings-CustomCodeTemplates.xml This XML file contains all your custom template settings for the given mime-type.
  3. Search for your entry and add contexts="php-code" to the <codetemplate>-tag and save it.

This way the template will be displayed in the code-completion list too.

When it looks like this

<codetemplate abbreviation="ech" contexts="php-code">
    <code><![CDATA[echo "${cursor} it works"]]></code>
</codetemplate>

it will work like that.

2016-01-07_23h05_10

If you like to have a option for that in the UI the NetBeans IDE itself, then please file an issue at https://netbeans.org/bugzilla -> Not needed anymore. See the update below.

UPDATE:  In the next version of NetBeans IDE (>8.1) no fiddling in files is needed anymore. Thanks to junichi11, who provided a patch to solve this via issue #246664 . The context can be set in the new context tab within the code-templates options.

2016-01-09_13h25_12

Code outline plugin 1.3.0 now supports dark themes

As you might already read in the NetBeans weekly newsletter, I also updated the Code outline plugin this week. This plugin shows the document content in miniature at the right gutter of the editor. Some of you know that feature from “Sublime Text” editor. This implementation is a bit simpler.

screenshot1-3

A highly requested feature was the support of dark themes. And now it’s supported. The basic text color and the background color of the editor is used. You can also check the “Darkening” option, which lowers the brightness of the code outside of the viewport. Try it!

Other improvements: You can define the font-size, but I propose to use single-digit font-sizes. The display quality has also been improved by applying anti-aliasing.

2016-01-07_22h12_55

The plugin is already available from the plugin dialog within your NetBeans IDE 8+. Or it can by downloaded manually from the plugin page at [1] and github [2].

Please file issues/enhancement at [2]. I also like to merge your pull-requests for further improvements! Happy coding with NetBeans!

[1] http://plugins.netbeans.org/plugin/57219/?show=true
[2] https://github.com/markiewb/nb-codeoutline

New version of Eclipse Code Formatter for Java provides support for Eclipse 4.5.1 Mars.1 and @format:off

Within the last days I had time to update the formatter plugin, which allows you to format java source code using the formatter engine of Eclipse JDT. In a few days it will be available as an update from within your NetBeans IDE. If you are curious and you don’t want to wait for it, then you can install it manually from [1] or [2].

The major change is that the jars for the formatter engine are now taken from Eclipse 4.5.1 Mars.1.

As you might know, the plugin already provided support for @format:off before – if it was enabled in the formatter file. But now it provides some code templates, so that it easier to use. See the documentation at [3]

63957236-af2c-11e5-8095-c1ee38727d49

66a04320-af2c-11e5-8d8b-35e68c25df1d

Feel free to file issues and provide pull-requests to see your enhancement in the next release! Happy coding and a happy new year!

[1] http://plugins.netbeans.org/plugin/50877
[2] https://github.com/markiewb/eclipsecodeformatter_for_netbeans
[3] https://github.com/markiewb/eclipsecodeformatter_for_netbeans/wiki/Support-of-@formatter:off

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.

@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 most important part of the popup action is to emulate a context-aware action using a lookup listener.

2015-09-13_21h36_02

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.

@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 result:
SubmenuWithContextSenstiveAction

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/