Konstantin has posted 11 posts at DZone. View Full User Profile

NetBeans Platform Tip: BooleanStateActions

07.06.2008
| 8182 views |
  • submit to reddit

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

Published at DZone with permission of its author, Konstantin Chikarev.

(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?

 

 

Matt Coleman replied on Fri, 2012/10/19 - 1:25am in response to: Konstantin Chikarev

Thanks for this codes...it helped a lot present for graphic designer

Cata Nic replied on Fri, 2013/09/06 - 2:31am

 Hos these actions are running? The script looks very modular. I will test it on my server and will be back with an answer.

Comment viewing options

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