NetBeans Platform Tip: BooleanStateActions

If you want to have an action that changes flag state using toobar button or menu check, you can use BooleanStateActions.

Create ChangeStateAction using wizard dialog with menu and toolbar buttons.

Here is the generated code:

public final class ChangeStateAction extends CallableSystemAction
{

public void performAction()
{
// TODO implement action body
}

public String getName()
{
return NbBundle.getMessage(ChangeStateAction.class, "CTL_ChangeStateAction");
}

@Override
protected String iconResource()
{
return "com/antilogics/binary.png";
}

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

@Override
protected boolean asynchronous()
{
return false;
}
}

Replace CallableSystemAction class with BooleanStateAction, remove "performAction" and "asynchronous" methods. Add following code:

    private JToggleButton toggleButton;
private JCheckBoxMenuItem menuButton;


public ChangeStateAction()
{
// initial state value
setBooleanState(true);

toggleButton = new JToggleButton();
Actions.connect(toggleButton, this);
menuButton = new JCheckBoxMenuItem(getName());
Actions.connect(menuButton, this);
}


@Override
public void actionPerformed(ActionEvent event)
{
super.actionPerformed(event);
boolean state = getBooleanState();
// do something with new state...
}


// replace toolbar button with yours
@Override
public Component getToolbarPresenter()
{
return toggleButton;
}

// replace menu item with yours
@Override
public JMenuItem getMenuPresenter()
{
return menuButton;
}


Here is the result:

That's all! Enjoy

Article Type: 
How-to
0

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Varun Nischal replied on Sun, 2008/07/06 - 2:46pm

What are the contents of XML Layer?

I don't see that file getting modified as shown in the Wizard Modified Files, though it should get modified...

Thanks,
Varun

Konstantin Chikarev replied on Tue, 2008/07/15 - 4:47am

layer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE filesystem PUBLIC "-//NetBeans//DTD Filesystem 1.1//EN" "http://www.netbeans.org/dtds/filesystem-1_1.dtd">
<filesystem>
<folder name="Actions">
<folder name="Tools">
<file name="com-antilogics-testmodule-ChangeStateAction.instance"/>
</folder>
</folder>
<folder name="Menu">
<folder name="Tools">
<attr name="com-antilogics-testmodule-ChangeStateAction.shadow/PaletteManager" boolvalue="true"/>
<file name="com-antilogics-testmodule-ChangeStateAction.shadow">
<attr name="originalFile" stringvalue="Actions/Tools/com-antilogics-testmodule-ChangeStateAction.instance"/>
</file>
</folder>
</folder>
<folder name="Toolbars">
<folder name="File">
<file name="com-antilogics-testmodule-ChangeStateAction.shadow">
<attr name="originalFile" stringvalue="Actions/Tools/com-antilogics-testmodule-ChangeStateAction.instance"/>
<attr name="position" intvalue="0"/>
</file>
</folder>
</folder>
</filesystem>

File should be in the list,instead of project.xml
Looks like a bug :)

Varun Nischal replied on Tue, 2008/07/15 - 5:10am

Yeah, maybe its a bug!

<attr name="com-antilogics-testmodule-ChangeStateAction.shadow/PaletteManager" boolvalue="true"/>

Check out the bold text, what exactly does this do?

 

 

michaljohn replied on Thu, 2009/06/11 - 5:30am

Information is truly extraordinary, I am very interested to read this post its useful. International Accreditation

jiji530 replied on Fri, 2009/06/26 - 8:23pm

thanks for your post.perhaps you will like abercrombie,ed hardy,mortgage rates,tiffanysanded hardyIs not it?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.