1. /*
  2. * @(#)BooleanControl.java 1.14 03/01/27
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.sampled;
  8. /**
  9. * A <code>BooleanControl</code> provides the ability to switch between
  10. * two possible settings that affect a line's audio. The settings are boolean
  11. * values (<code>true</code> and <code>false</code>). A graphical user interface
  12. * might represent the control by a two-state button, an on/off switch, two
  13. * mutually exclusive buttons, or a checkbox (among other possibilities).
  14. * For example, depressing a button might activate a
  15. * <code>{@link BooleanControl.Type#MUTE MUTE}</code> control to silence
  16. * the line's audio.
  17. * <p>
  18. * As with other <code>{@link Control}</code> subclasses, a method is
  19. * provided that returns string labels for the values, suitable for
  20. * display in the user interface.
  21. *
  22. * @author Kara Kytle
  23. * @version 1.14, 03/01/27
  24. * @since 1.3
  25. */
  26. public abstract class BooleanControl extends Control {
  27. // INSTANCE VARIABLES
  28. /**
  29. * The <code>true</code> state label, such as "true" or "on."
  30. */
  31. private final String trueStateLabel;
  32. /**
  33. * The <code>false</code> state label, such as "false" or "off."
  34. */
  35. private final String falseStateLabel;
  36. /**
  37. * The current value.
  38. */
  39. private boolean value;
  40. // CONSTRUCTORS
  41. /**
  42. * Constructs a new boolean control object with the given parameters.
  43. *
  44. * @param type the type of control represented this float control object
  45. * @param initialValue the initial control value
  46. * @param trueStateLabel the label for the state represented by <code>true</code>,
  47. * such as "true" or "on."
  48. * @param falseStateLabel the label for the state represented by <code>false</code>,
  49. * such as "false" or "off."
  50. */
  51. protected BooleanControl(Type type, boolean initialValue, String trueStateLabel, String falseStateLabel) {
  52. super(type);
  53. this.value = initialValue;
  54. this.trueStateLabel = trueStateLabel;
  55. this.falseStateLabel = falseStateLabel;
  56. }
  57. /**
  58. * Constructs a new boolean control object with the given parameters.
  59. * The labels for the <code>true</code> and <code>false</code> states
  60. * default to "true" and "false."
  61. *
  62. * @param type the type of control represented by this float control object
  63. * @param initialValue the initial control value
  64. */
  65. protected BooleanControl(Type type, boolean initialValue) {
  66. this(type, initialValue, "true", "false");
  67. }
  68. // METHODS
  69. /**
  70. * Sets the current value for the control. The default
  71. * implementation simply sets the value as indicated.
  72. * Some controls require that their line be open before they can be affected
  73. * by setting a value.
  74. * @param value desired new value.
  75. */
  76. public void setValue(boolean value) {
  77. this.value = value;
  78. }
  79. /**
  80. * Obtains this control's current value.
  81. * @return current value.
  82. */
  83. public boolean getValue() {
  84. return value;
  85. }
  86. /**
  87. * Obtains the label for the specified state.
  88. * @return the label for the specified state, such as "true" or "on"
  89. * for <code>true</code>, or "false" or "off" for <code>false</code>.
  90. */
  91. public String getStateLabel(boolean state) {
  92. return ((state == true) ? trueStateLabel : falseStateLabel);
  93. }
  94. // ABSTRACT METHOD IMPLEMENTATIONS: CONTROL
  95. /**
  96. * Provides a string representation of the control
  97. * @return a string description
  98. */
  99. public String toString() {
  100. return new String(super.toString() + " with current value: " + getStateLabel(getValue()));
  101. }
  102. // INNER CLASSES
  103. /**
  104. * An instance of the <code>BooleanControl.Type</code> class identifies one kind of
  105. * boolean control. Static instances are provided for the
  106. * common types.
  107. *
  108. * @author Kara Kytle
  109. * @version 1.14, 03/01/27
  110. * @since 1.3
  111. */
  112. public static class Type extends Control.Type {
  113. // TYPE DEFINES
  114. /**
  115. * Represents a control for the mute status of a line.
  116. * Note that mute status does not affect gain.
  117. */
  118. public static final Type MUTE = new Type("Mute");
  119. /**
  120. * Represents a control for whether reverberation is applied
  121. * to a line. Note that the status of this control not affect
  122. * the reverberation settings for a line, but does affect whether
  123. * these settings are used.
  124. */
  125. public static final Type APPLY_REVERB = new Type("Apply Reverb");
  126. // CONSTRUCTOR
  127. /**
  128. * Constructs a new boolean control type.
  129. * @param name the name of the new boolean control type
  130. */
  131. protected Type(String name) {
  132. super(name);
  133. }
  134. } // class Type
  135. }