Added cycleToProperty() which configures a button to cycle a config property

to the next of a set of ints each time it is pressed.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1318 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2003-11-18 01:57:02 +00:00
parent 5202d2a61e
commit 992f3191e2
@@ -1,5 +1,5 @@
//
// $Id: ButtonUtil.java,v 1.2 2002/12/14 02:11:24 mdb Exp $
// $Id: ButtonUtil.java,v 1.3 2003/11/18 01:57:02 ray Exp $
package com.samskivert.swing.util;
@@ -17,6 +17,7 @@ import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import com.samskivert.util.Config;
import com.samskivert.util.IntListUtil;
/**
* Utilities for buttons.
@@ -58,6 +59,27 @@ public class ButtonUtil
new ButtonConfigBinding(property, config, button, defval);
}
/**
* Configure the specified button to cause the specified
* property to cycle through the specified values whenever the button is
* pressed.
*/
public static void cycleToProperty (
final String property, final Config config, AbstractButton button,
final int[] values)
{
button.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent event)
{
// get the current value and find out where it is in the list
int oldval = config.getValue(property, values[0]);
int newidx = (1 + IntListUtil.indexOf(values, oldval))
% values.length;
config.setValue(property, values[newidx]);
}
});
}
/** Used for {@link #bindToProperty}. */
protected static class ButtonConfigBinding
implements AncestorListener, PropertyChangeListener, ItemListener