Home > Action, Filter, Quick Search > Recipe 15: How to add a toggle action

Recipe 15: How to add a toggle action

Problem: How can I add a toggle button as an action? I tried to add a JToggleButton but the NetBeans toolbar doesn’t render it correctly when it is pressed.

Solution: For context-unaware actions use BooleanStateAction. Here are the steps to follow:

  1. Create a new Action, e.g.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(iconBase = "todo/controller/options/find16.png",
displayName = "#CTL_QuickFilterAction")
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        // TODO implement action body
    }
}

2.  Extend BooleanStateAction and since it implements ActionListener this can be deleted from our action:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "Options",
id = "todo.controller.options.QuickFilterAction")
@ActionRegistration(
    iconBase = "todo/controller/options/find16.png",
    displayName = "#CTL_QuickFilterAction"),
    lazy = true
)
@ActionReferences({
    @ActionReference(path = "Menu/Options", position = 50),
    @ActionReference(path = "Toolbars/Options", position = 50),
    @ActionReference(path = "Shortcuts", name = "F3")
})
@Messages("CTL_QuickFilterAction=Quick Filter")
public final class QuickFilterAction extends BooleanStateAction {

 @Override
 protected void initialize() {
   super.initialize();
   setBooleanState(Settings.isSearchVisible());
 }

 @Override
 public void actionPerformed(ActionEvent e) {
   super.actionPerformed(e);
   TasksTopComponent tasksTopComponent = (TasksTopComponent) WindowManager.getDefault().findTopComponent("TasksTopComponent");
   tasksTopComponent.setSearchVisible(getBooleanState());
 }

 @Override
 public String getName() {
   return "Quick Filter";
 }

 @Override
 public HelpCtx getHelpCtx() {
   return HelpCtx.DEFAULT_HELP;
 }
}

@ActionRegistration(lazy=true) is important and it denotes whether a factory should be used or not. Because, by default, it is selected, we need to set it to unselected in the initialize() method.

For context-aware toggle actions, there are a number of options.

Actions class and @ActionState annotation are replacements to the deprecated BooleanActionState class.

@ActionState specifies that the action’s behaviour is conditional and how the action should obtain the state for its presentation. It is defined by @ActionRegistration(enableOn) and @ActionRegistration(checkedOn) to control action’s enabled or checked state. The annotation can be only applied on context actions, which have a single parameter constructor, which accept the model object.

When used as ActionRegistration.checkedOn value, the annotated action will change to toggle on/off action, represented by a checkbox (menu) or a toggle button (toolbar). The action state will track the model property specified by this annotation. Toggle actions become enabled when the model object is found in the Lookup, and checked (or toggled on) when the model property is set to a defined value (usually true).

The type specifies type which is searched for in the Lookup and if an instance is found, it is used as the model object.

The property specifies bean property whose value should be used to determine checked state.

E.g.:

@ActionRegistration(
   displayName = "Save modified", 
   enabledOn = @ActionState(property = "modified"),
   checkedOn = @ActionState(
     property = "selectionMode", checkedValue = "true", 
     listenOn = EditorStateListener.class)
)

 

Advertisements
Categories: Action, Filter, Quick Search
  1. No comments yet.
  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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: