Filter the Palette!

In Listen to the Palette!, I wrote about the palette's property change listener. Something else that the palette has is... a filter. Thanks to the NetBeans API PaletteFilter class, you can cause a category or palette item to be invalid. If a category or item is invalid, it is not shown. For example, here only one category (called "Basic") is valid, initially:

However, when the user selects the "Expert" checkbox, the palette displays an additional category, with additional items:

In this way, you can let the user (or the user's actions) trigger the content of the palette, either on category-level or on item-level. That could be useful in a variety of situations. And it is not very hard to set up. First, when the checkbox is selected, a Boolean value is sent to the class where all my palette activities are handled:

private void expertCheckBoxItemStateChanged(java.awt.event.ItemEvent evt) {
  PaletteSupport.MyPaletteFilter.setCategoryExpertVisible(expertCheckBox.isSelected());
}

Then, in that class, the filter is defined:

public static class MyPaletteFilter extends PaletteFilter {

    static boolean isCategoryExpertVisible = false;

    public static void setCategoryExpertVisible(boolean isCheckedOrNot){
        if( isCategoryExpertVisible != isCheckedOrNot ) {
            isCategoryExpertVisible = isCheckedOrNot;
            controller.refresh();
        }
    }

    public boolean isValidCategory(Lookup lookup) {
        Node categoryNode = (Node)lookup.lookup( Node.class );
        String categoryDisplayName = categoryNode.getDisplayName();
        if (categoryDisplayName.equals("Expert") && !isCategoryExpertVisible) {
            return false;
        }
        return true;
    }

    public boolean isValidItem(Lookup lookup) {
        return true;
    }

}

Note the line that is highlighted above—we need to call controller.refresh to wake up the controller, which will refresh the palette for us, by calling isValidCategory and isValidItem. (An alternative, less user friendly way, is to make the user do it manually in the palette, by right-clicking it and choosing "Refresh Palette".) The refresh is also done when the palette is created, so at that point the Boolean is set to its default value, which is false, so that the "Expert" category is not shown initially.

The two methods required by the PaletteFilter are isValidCategory and isValidItem. If they return true, they show something; if they return false, they hide something. What that "something" is, is up to you. Here in isValidCategory, we get the display name (set in the XML layer file) from the node and then check the Boolean isCategoryExpertVisible, which is set/unset by the checkbox. And isValidItem is only there because it is a required method; here, it doesn't filter anything, it is just a dummy method.

Note that you need to attach the filter to the palette controller. You do this when you define the palette controller:

controller = PaletteFactory.createPalette("abcde", new MyActions(), new MyPaletteFilter(), null );

And that's all that's needed to show or hide the "Expert" category.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
25
26
27
28
29
30
   
       
Today