*
* @return The controllers of this component
*/
- public List<Controller> controllers();
+ public List<Controller<?>> controllers();
}
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public interface Controller {
+public interface Controller<V extends Comparable<V>> {
/**
* Returns the minimum value of this controller.
*
* @return The minimum value of this controller
*/
- int minimum();
+ V minimum();
/**
* Returns the maximum value of this controller.
*
* @return The maximum value of this controller
*/
- int maximum();
+ V maximum();
/**
* Returns whether this control has a “center” position.
*
* @return The current value of this controller
*/
- int value();
+ V value();
/**
* Sets the current value of this controller.
* @param value
* The current value of this controller
*/
- void value(int value);
+ void value(V value);
}
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public abstract class AbstractController implements Controller {
+public abstract class AbstractController<V extends Comparable<V>> implements Controller<V> {
/** The minimum value of this controller. */
- private final int minimum;
+ private final V minimum;
/** The maximum value of this controller. */
- private final int maximum;
+ private final V maximum;
/** Whether this controller has a “center” position. */
private final boolean centered;
/** The current value of this controller. */
- private int value;
+ private V value;
/**
* Creates a new abstract controller.
* @param currentValue
* The current value of this controller
*/
- public AbstractController(int minimum, int maximum, boolean centered, int currentValue) {
+ public AbstractController(V minimum, V maximum, boolean centered, V currentValue) {
this.minimum = minimum;
this.maximum = maximum;
this.centered = centered;
//
@Override
- public int minimum() {
+ public V minimum() {
return minimum;
}
@Override
- public int maximum() {
+ public V maximum() {
return maximum;
}
}
@Override
- public int value() {
+ public V value() {
return value;
}
@Override
- public void value(int value) {
- int newValue = Math.min(maximum, Math.max(minimum, value));
- if (newValue != this.value) {
+ public void value(V value) {
+ V newValue = (value.compareTo(minimum) < 0) ? minimum : ((value.compareTo(maximum) > 0) ? maximum : value);
+ if (newValue.compareTo(this.value) != 0) {
this.value = newValue;
valueSet(newValue);
}
//
/**
- * Adjusts the controller. This method is called from {@link #value(int)} if
- * the new value is different from the current value. Also, the value is
- * clamped to fit within the range of this controller.
+ * Adjusts the controller. This method is called from {@link
+ * #value(Comparable)} if the new value is different from the current value.
+ * Also, the value is clamped to fit within the range of this controller.
*
* @param value
* The new value
*/
- protected abstract void valueSet(int value);
+ protected abstract void valueSet(V value);
}
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public abstract class Fader extends AbstractController {
+public abstract class Fader extends AbstractController<Double> {
/** Creates a new fader that is at maximum position. */
public Fader() {
* @param currentValue
* The current value of the fader (from {@code 0.0} to {@code 1.0})
*/
- public Fader(double currentValue) {
- super(0, Integer.MAX_VALUE, false, (int) (Math.max(0, Math.min(1, currentValue)) * Integer.MAX_VALUE));
+ public Fader(Double currentValue) {
+ super(0.0, 1.0, false, currentValue);
}
}
*
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
-public abstract class Switch extends AbstractController {
+public abstract class Switch extends AbstractController<Boolean> {
/** Creates a new switch that is off. */
public Switch() {
* The state of the switch
*/
public Switch(boolean active) {
- super(0, 1, false, active ? 1 : 0);
+ super(false, true, false, active);
}
}
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
volumeFader = new Fader() {
@Override
- protected void valueSet(int value) {
+ protected void valueSet(Double value) {
if (sourceDataLine != null) {
FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
- volumeControl.setValue(value * volumeControl.getMaximum() / (float) maximum());
+ volumeControl.setValue((float) (value * volumeControl.getMaximum()));
}
}
};
private float previousValue;
@Override
- protected void valueSet(int value) {
+ protected void valueSet(Boolean value) {
if (sourceDataLine != null) {
FloatControl volumeControl = (FloatControl) sourceDataLine.getControl(VOLUME);
- if (value == 1) {
+ if (value) {
previousValue = volumeControl.getValue();
volumeControl.setValue(0);
} else {
//
@Override
- public List<Controller> controllers() {
- return Arrays.<Controller>asList(volumeFader, muteSwitch);
+ public List<Controller<?>> controllers() {
+ return Arrays.<Controller<?>>asList(volumeFader, muteSwitch);
}
//
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
//
@Override
- public List<Controller> controllers() {
+ public List<Controller<?>> controllers() {
return Collections.emptyList();
}
add(new JLabel("0"), new GridBagConstraints(3, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
/* create fader. */
- JSlider slider = new JSlider(new DefaultBoundedRangeModel(fader.value(), 0, fader.minimum(), fader.maximum()));
+ JSlider slider = new JSlider(new DefaultBoundedRangeModel((int) (fader.value() * Integer.MAX_VALUE), 0, 0, Integer.MAX_VALUE));
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent changeEvent) {
- fader.value(((JSlider) changeEvent.getSource()).getValue());
+ fader.value(((JSlider) changeEvent.getSource()).getValue() / (double) Integer.MAX_VALUE);
}
});
add(slider, new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
* The controlled to add
*/
public void addControllers(Controlled controlled) {
- List<Controller> controllers = controlled.controllers();
+ List<Controller<?>> controllers = controlled.controllers();
if (controllers.isEmpty()) {
return;
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
- switchController.value(((JCheckBox) actionEvent.getSource()).isSelected() ? 1 : 0);
+ switchController.value(((JCheckBox) actionEvent.getSource()).isSelected());
}
});
add(checkBox, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));