Home > Uncategorized > NetBeans RCP: How to add an action to Java package/any folder node

NetBeans RCP: How to add an action to Java package/any folder node

In the dev@platform.netbeans.org mailing list recently there was a question: “How can I to add actions to the Java package node?”
So I gave it a try and the solution is straight forward.

Question: “How can I to add actions to the Java package node?” 

Answer: Register your action to Projects/package/Actions

</span>
<pre>@ActionID(
        category = "Build",
        id = "com.foo.bar.OnJavaPackageAction")
@ActionRegistration(
        displayName = "#CTL_OnJavaPackageAction")
@ActionReferences({
    @ActionReference(path = "Projects/package/Actions", position = 0)
})
@Messages("CTL_OnJavaPackageAction=OnJavaPackageAction")
public final class OnJavaPackageAction implements ActionListener {

    private final DataFolder context;

    public OnJavaPackageAction(DataFolder context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        JOptionPane.showMessageDialog(null, context.toString());
    }
}

Example in action:
Javapackage

As a bonus for today:

Question: “How can I add actions to all folder nodes?

Answer: Register to Loaders/folder/any/Actions

@ActionID(
        category = "Build",
        id = "com.foo.bar.OnAnyFolderAction")
@ActionRegistration(
        displayName = "#CTL_OnAnyFolderAction")
@ActionReferences({
    @ActionReference(path = "Loaders/folder/any/Actions", position = 0)
})
@Messages("CTL_OnAnyFolderAction=ActionOnAnyFolder")
public final class OnAnyFolderAction implements ActionListener {

    private final DataFolder context;

    public OnAnyFolderAction(DataFolder context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        JOptionPane.showMessageDialog(null, context.toString());
    }
}

Example in action: actiononfolder

Question: “How can I add actions to Java packages AND Java files at the same time?

Answer: Register to Projects/package/Actions and Loaders/text/x-java/Actions

@ActionID(
        category = "Build",
        id = "com.foo.bar.OnJavaFilesAndPackageAction")
@ActionRegistration(
        displayName = "#CTL_OnJavaFilesAndPackageAction")
@ActionReferences({
    @ActionReference(path = "Projects/package/Actions", position = 500),
    @ActionReference(path = "Loaders/text/x-java/Actions", position = 500)
})
@Messages("CTL_OnJavaFilesAndPackageAction=OnJavaFilesAndPackageAction")
public final class OnJavaFilesAndPackageAction implements ActionListener {

    private final List<DataObject> context;

    public OnJavaFilesAndPackageAction(List<DataObject> context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        StringBuilder sb=new StringBuilder();
        sb.append("<html>");
        for (DataObject dataObject : context) {
            sb.append(dataObject.getPrimaryFile().isFolder()?"Package: ":"File: ");
            sb.append(dataObject.getPrimaryFile().getPath());
            sb.append("<br/>");
        }
        JOptionPane.showMessageDialog(null, sb.toString());
    }
}


Example in action:

actionForPackageAndFile

Please note that this example only works using >=NB7.4. It doesn’t work for previous versions. I don’t know why…

Resources

The full sourcecode of the examples are available at github [1][2] and the tips were added to the NB Developer FAQ [3].

[1] https://github.com/markiewb/nb-api-samples/tree/master/AddActionToPackageOrFolder
[2] https://github.com/markiewb/nb-api-samples/tree/master/AddActionToPackageAndJavaFile
[3] http://wiki.netbeans.org/NetBeansDeveloperFAQ

About these ads
  1. 12/11/2013 at 03:35 | #1

    Benno

    Thanks for the post. How do I add an action for both file and a package. Is there any documentation for context sensitive actions that you can point me to?

    Thank you
    Venkat

  2. 12/11/2013 at 12:25 | #2

    @venkat: “How do I add an action for both file and a package.” I don’t understand. There are different interpretations possible. What is your concrete use-case?

    http://wiki.netbeans.org/DevFaqActionContextSensitive
    http://netbeans.dzone.com/news/which-netbeans-platform-action

  3. 19/11/2013 at 04:57 | #3

    Thanks for the reply Benno. If a user selects a java file and a package, how can I get both the package and the file inside the action.

    In case you are interested, what I want to do is pass the java files and the packages user selects into PlantUML dependency so that it generates a plantuml file. But, turns out plantuml dependency only accepts one directory at a time. No multiple directories or files, so for now I have no need for this. Thanks for the answer though.

    http://plantuml-depend.sourceforge.net/index.html

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: