2.8.10

How to Make the NetBeans Platform Sensitive to Customers

In your NetBeans Platform application, you may often find yourself in the situation where you need to create actions that are sensitive to their context. A simple case in point is shown below. We have a customer application with an action invoked from the toolbar, as well as from a menu item in the main menubar and from a node in an explorer view. In the screenshot below, the action is invoked from the button with the angry face (a typical customer expression) in the toolbar, from the "Customer Details" menu item on the node in the view window, as well as from a menu item in the File menu (which you can't see below). In this case, there is a Customer object on which the action can be invoked...

...while in this situation, the UI for invoking the action is disabled (as you can see, by looking at the button in the toolbar, which is disabled, and notice that other actions are enabled in this context, since those actions relate to the editor, which is the window that is currently selected) because here we no longer have a Customer object in the context of the application, since the cursor is currently in the editor window, instead of in the view window that exposes the Customer object from the currently selected node:

So the question in this article is how to create an action such as the above. The good news is that it's really easy to do so, but you do need to be aware of the steps you need to take, which is why I am writing this article.

Take the following steps:

  1. In the New Action wizard, in the module where you want to create your action, specify that you want to create a contextually aware action that should be sensitive to Customer objects, which is part of the model in my application:

    Note: We (i.e., the NetBeans team) need to change the strings in the dialog above (i.e., in the NetBeans IDE source code), since in both cases an ActionListener will be created. I.e., when you select the "Conditionally Enabled" radiobutton, you will notget a CookieAction. Instead, you will get an ActionListener that is registered in the layer such that it is injected with context-sensitivity to Customer objects, as will be seen below.
  2. Click Next and then specify that you want to create a menu item and a toolbar button:
  3. Click Next again and choose an icon on disk, as well as specifying a class name prefix and a display name:

    Tip: Read this cool tip about icons and NetBeans Platform applications!
  4. When you complete the above wizard, you will see you have a plain old ActionListener class, which is great news since this means you can port your own ActionListeners from your own application to the NetBeans Platform without needing to rewrite them in any way. In other words, the NetBeans Platform handles ActionListeners natively and does not require you to use some special NetBeans API for creating actions. Here's the ActionListener created from the above, which has access to the current Customer object (pretty handy!):

    view source

    print?

    01.package org.shop.ui;

    02.

    03.import demo.Customer;

    04.import java.awt.event.ActionListener;

    05.import java.awt.event.ActionEvent;

    06.

    07.public final class CustomerDetailsAction implements ActionListener {

    08.

    09.private final Customer context;

    10.

    11.public CustomerDetailsAction(Customer context) {

    12.this.context = context;

    13.}

    14.

    15.public void actionPerformed(ActionEvent ev) {

    16.// TODO use context

    17.}

    18.

    19.}

    Meanwhile, your layer.xml file has the following entries, created by the above wizard, turning your humble ActionListener into a context-sensitive action that is sensitive to Customer objects:

    view source

    print?

    01.<folder name="Actions">

    02.<folder name="Build">

    03.<file name="org-shop-ui-CustomerDetailsAction.instance">

    04.<attr name="delegate" methodvalue="org.openide.awt.Actions.inject"/>

    05.<attr name="displayName"bundlevalue="org.shop.ui.Bundle#CTL_CustomerDetailsAction"/>

    06.<attr name="iconBase" stringvalue="org/shop/ui/customer.png"/>

    07.<attr name="injectable" stringvalue="org.shop.ui.CustomerDetailsAction"/>

    08.<attr name="instanceCreate" methodvalue="org.openide.awt.Actions.context"/>

    09.<attr name="noIconInMenu" boolvalue="false"/>

    10.<attr name="selectionType" stringvalue="EXACTLY_ONE"/>

    11.<attr name="type" stringvalue="demo.Customer"/>

    12.</file>

    13.</folder>

    14.</folder>

    15.<folder name="Menu">

    16.<folder name="File">

    17.<file name="org-shop-ui-CustomerDetailsAction.shadow">

    18.<attr name="originalFile" stringvalue="Actions/Build/org-shop-ui-CustomerDetailsAction.instance"/>

    19.<attr name="position" intvalue="1300"/>

    20.</file>

    21.</folder>

    22.</folder>

    23.<folder name="Toolbars">

    24.<folder name="File">

    25.<file name="org-shop-ui-CustomerDetailsAction.shadow">

    26.<attr name="originalFile" stringvalue="Actions/Build/org-shop-ui-CustomerDetailsAction.instance"/>

    27.</file>

    28.</folder>

    29.</folder>

    Note: I tweaked line 11 above. By default, the type is set to "Customer", while it actually needs to be the fully-qualified name of the Customer class, which is "demo.Customer", since the Customer object is found in a package called "demo". Maybe in the New Action wizard, one should type the fully-qualified name, rather than just the name of the domain class. Need to check that. The "type" element in the layer determines the context available to the ActionListener, i.e., the currently available Customer object.
    By the way, above, in line 10, we are ensuring that the action will be disabled if more than one node is selected (thanks to choosing "User Selects One Node" in the first page of the New Action wizard), as can be seen here:

  5. Finally, let's add the action as a contextual menu item on our node. In this case, I know I have two actions in the "Actions/Build" folder, one from the current module, and the other from another one. You could also get all the actions within a particular folder, rather than specific ones, or you could get actions from different folders. Up to you.

    view source

    print?

    01.private class CustomerNode extends AbstractNode {

    02.

    03.public CustomerNode(Customer c) {

    04.super(Children.LEAF, Lookups.singleton(c));

    05.setDisplayName(c.getName());

    06.setShortDescription(c.getCity());

    07.}

    08.

    09.@Override

    10.public Action[] getActions(boolean context) {

    11.return new Action[]{

    12.Utilities.actionsForPath("Actions/Build/").get(0),

    13.Utilities.actionsForPath("Actions/Build/").get(1),

    14.};

    15.}

    16.

    17.}

And that's really all. You can use your plain old ActionListener class. The downside is you have a bunch of tags in the layer file to deal with, though (aside from the small tweak) it was all generated for you. Looking forward to an annotation for actions, so that my ActionListener can simply be decorated with an annotation that will create the necessary layer entries when the module is compiled.

Nevertheless, you now have a context-sensitive action for customer objects.

http://netbeans.dzone.com/how-to-make-context-sensitive-actions

No comments: