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