Apache NetBeans
Apache NetBeans
Latest release

Apache NetBeans 23

Download

How do I add a toggle-able action to the toolbar/main menu?

  • extend from org.openide.util.actions.BooleanStateAction

  • in the constructor add a property-change listener to the action itself and let the action implement `PropertyChangeListener `

  • within PropertyChangeListener#propertyChange check for the propertyName PROP_BOOLEAN_STATE to distinguish the toggle event from other events

Example

package de.markiewb.netbeans.plugins.debuggerutils;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Action;
import org.netbeans.api.annotations.common.StaticResource;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.*;
import org.openide.util.NbBundle.Messages;
import org.openide.util.actions.BooleanStateAction;

@ActionID(
        category = "Debug",
        id = "de.markiewb.netbeans.plugins.debuggerutils.SkipAllBreakpoints"
)
@ActionRegistration(
        lazy = false,
        displayName = "#CTL_SkipAllBreakpoints"
)
@ActionReferences({
    @ActionReference(path = "Toolbars/Debug", position = 1050),
    @ActionReference(path = "Menu/RunProject", position = 2350)
}
)
@Messages("CTL_SkipAllBreakpoints=Skip all breakpoin&ts")

public final class SkipAllBreakpoints extends BooleanStateAction implements PropertyChangeListener {

    @StaticResource
    private static final String iconpath = "de/markiewb/netbeans/plugins/debuggerutils/Breakpoint_stroke.png";

    public SkipAllBreakpoints() {
        addPropertyChangeListener(this);

        setBooleanState(false); //initially unchecked
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(PROP_BOOLEAN_STATE)) {
            /* your action here, get the state by getBooleanState()*/
        }
    }

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

    @Override
    public String getName() {
        return Bundle.CTL_SkipAllBreakpoints();
    }

    @Override
    protected String iconResource() {
        return iconpath;
    }
}