1. /*
  2. * @(#)EnumControl.java 1.13 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>EnumControl</code> provides control over a set of
  10. * discrete possible values, each represented by an object. In a
  11. * graphical user interface, such a control might be represented
  12. * by a set of buttons, each of which chooses one value or setting. For
  13. * example, a reverb control might provide several preset reverberation
  14. * settings, instead of providing continuously adjustable parameters
  15. * of the sort that would be represented by <code>{@link FloatControl}</code>
  16. * objects.
  17. * <p>
  18. * Controls that provide a choice between only two settings can often be implemented
  19. * instead as a <code>{@link BooleanControl}</code>, and controls that provide
  20. * a set of values along some quantifiable dimension might be implemented
  21. * instead as a <code>FloatControl</code> with a coarse resolution.
  22. * However, a key feature of <code>EnumControl</code> is that the returned values
  23. * are arbitrary objects, rather than numerical or boolean values. This means that each
  24. * returned object can provide further information. As an example, the settings
  25. * of a <code>{@link EnumControl.Type#REVERB REVERB}</code> control are instances of
  26. * <code>{@link ReverbType}</code> that can be queried for the parameter values
  27. * used for each setting.
  28. *
  29. * @author Kara Kytle
  30. * @version 1.13, 03/01/27
  31. * @since 1.3
  32. */
  33. public abstract class EnumControl extends Control {
  34. // TYPE DEFINES
  35. // INSTANCE VARIABLES
  36. /**
  37. * The set of possible values.
  38. */
  39. private Object[] values;
  40. /**
  41. * The current value.
  42. */
  43. private Object value;
  44. // CONSTRUCTORS
  45. /**
  46. * Constructs a new enumerated control object with the given parameters.
  47. *
  48. * @param type the type of control represented this enumerated control object
  49. * @param values the set of possible values for the control
  50. * @param value the initial control value
  51. */
  52. protected EnumControl(Type type, Object[] values, Object value) {
  53. super(type);
  54. this.values = values;
  55. this.value = value;
  56. }
  57. // METHODS
  58. /**
  59. * Sets the current value for the control. The default implementation
  60. * simply sets the value as indicated. If the value indicated is not
  61. * supported, an IllegalArgumentException is thrown.
  62. * Some controls require that their line be open before they can be affected
  63. * by setting a value.
  64. * @param value the desired new value
  65. * @throws IllegalArgumentException if the value indicated does not fall
  66. * within the allowable range
  67. */
  68. public void setValue(Object value) {
  69. if (!isValueSupported(value)) {
  70. throw new IllegalArgumentException("Requested value " + value + " is not supported.");
  71. }
  72. this.value = value;
  73. }
  74. /**
  75. * Obtains this control's current value.
  76. * @return the current value
  77. */
  78. public Object getValue() {
  79. return value;
  80. }
  81. /**
  82. * Returns the set of possible values for this control.
  83. * @return the set of possible values
  84. */
  85. public Object[] getValues() {
  86. Object[] localArray = new Object[values.length];
  87. for (int i = 0; i < values.length; i++) {
  88. localArray[i] = values[i];
  89. }
  90. return localArray;
  91. }
  92. /**
  93. * Indicates whether the value specified is supported.
  94. * @param value the value for which support is queried
  95. * @return <code>true</code> if the value is supported,
  96. * otherwise <code>false</code>
  97. */
  98. private boolean isValueSupported(Object value) {
  99. for (int i = 0; i < values.length; i++) {
  100. //$$fb 2001-07-20: Fix for bug 4400392: setValue() in ReverbControl always throws Exception
  101. //if (values.equals(values[i])) {
  102. if (value.equals(values[i])) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. // ABSTRACT METHOD IMPLEMENTATIONS: CONTROL
  109. /**
  110. * Provides a string representation of the control.
  111. * @return a string description
  112. */
  113. public String toString() {
  114. return new String(getType() + " with current value: " + getValue());
  115. }
  116. // INNER CLASSES
  117. /**
  118. * An instance of the <code>EnumControl.Type</code> inner class identifies one kind of
  119. * enumerated control. Static instances are provided for the
  120. * common types.
  121. *
  122. * @see EnumControl
  123. *
  124. * @author Kara Kytle
  125. * @version 1.13, 03/01/27
  126. * @since 1.3
  127. */
  128. public static class Type extends Control.Type {
  129. // TYPE DEFINES
  130. /**
  131. * Represents a control over a set of possible reverberation settings.
  132. * Each reverberation setting is described by an instance of the
  133. * {@link ReverbType} class. (To access these settings,
  134. * invoke <code>{@link EnumControl#getValues}</code> on an
  135. * enumerated control of type <code>REVERB</code>.)
  136. */
  137. public static final Type REVERB = new Type("Reverb");
  138. // CONSTRUCTOR
  139. /**
  140. * Constructs a new enumerated control type.
  141. * @param name the name of the new enumerated control type
  142. */
  143. protected Type(String name) {
  144. super(name);
  145. }
  146. } // class Type
  147. } // class EnumControl