KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > sound > sampled > EnumControl


1 /*
2  * @(#)EnumControl.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.sound.sampled;
9
10 /**
11  * A <code>EnumControl</code> provides control over a set of
12  * discrete possible values, each represented by an object. In a
13  * graphical user interface, such a control might be represented
14  * by a set of buttons, each of which chooses one value or setting. For
15  * example, a reverb control might provide several preset reverberation
16  * settings, instead of providing continuously adjustable parameters
17  * of the sort that would be represented by <code>{@link FloatControl}</code>
18  * objects.
19  * <p>
20  * Controls that provide a choice between only two settings can often be implemented
21  * instead as a <code>{@link BooleanControl}</code>, and controls that provide
22  * a set of values along some quantifiable dimension might be implemented
23  * instead as a <code>FloatControl</code> with a coarse resolution.
24  * However, a key feature of <code>EnumControl</code> is that the returned values
25  * are arbitrary objects, rather than numerical or boolean values. This means that each
26  * returned object can provide further information. As an example, the settings
27  * of a <code>{@link EnumControl.Type#REVERB REVERB}</code> control are instances of
28  * <code>{@link ReverbType}</code> that can be queried for the parameter values
29  * used for each setting.
30  *
31  * @author Kara Kytle
32  * @version 1.14, 03/12/19
33  * @since 1.3
34  */

35 public abstract class EnumControl extends Control JavaDoc {
36     
37     
38     // TYPE DEFINES
39

40     
41     // INSTANCE VARIABLES
42

43     
44     /**
45      * The set of possible values.
46      */

47     private Object JavaDoc[] values;
48     
49     
50     /**
51      * The current value.
52      */

53     private Object JavaDoc value;
54     
55     
56     
57     // CONSTRUCTORS
58

59     
60     /**
61      * Constructs a new enumerated control object with the given parameters.
62      *
63      * @param type the type of control represented this enumerated control object
64      * @param values the set of possible values for the control
65      * @param value the initial control value
66      */

67     protected EnumControl(Type type, Object JavaDoc[] values, Object JavaDoc value) {
68     
69     super(type);
70     
71     this.values = values;
72     this.value = value;
73     }
74     
75     
76     
77     // METHODS
78

79     
80     /**
81      * Sets the current value for the control. The default implementation
82      * simply sets the value as indicated. If the value indicated is not
83      * supported, an IllegalArgumentException is thrown.
84      * Some controls require that their line be open before they can be affected
85      * by setting a value.
86      * @param value the desired new value
87      * @throws IllegalArgumentException if the value indicated does not fall
88      * within the allowable range
89      */

90     public void setValue(Object JavaDoc value) {
91     if (!isValueSupported(value)) {
92         throw new IllegalArgumentException JavaDoc("Requested value " + value + " is not supported.");
93     }
94     
95     this.value = value;
96     }
97     
98     
99     /**
100      * Obtains this control's current value.
101      * @return the current value
102      */

103     public Object JavaDoc getValue() {
104     return value;
105     }
106     
107     
108     /**
109      * Returns the set of possible values for this control.
110      * @return the set of possible values
111      */

112     public Object JavaDoc[] getValues() {
113     
114     Object JavaDoc[] localArray = new Object JavaDoc[values.length];
115     
116     for (int i = 0; i < values.length; i++) {
117         localArray[i] = values[i];
118     }
119     
120     return localArray;
121     }
122     
123     
124     /**
125      * Indicates whether the value specified is supported.
126      * @param value the value for which support is queried
127      * @return <code>true</code> if the value is supported,
128      * otherwise <code>false</code>
129      */

130     private boolean isValueSupported(Object JavaDoc value) {
131     
132     for (int i = 0; i < values.length; i++) {
133         //$$fb 2001-07-20: Fix for bug 4400392: setValue() in ReverbControl always throws Exception
134
//if (values.equals(values[i])) {
135
if (value.equals(values[i])) {
136         return true;
137         }
138     }
139     
140     return false;
141     }
142     
143     
144     
145     // ABSTRACT METHOD IMPLEMENTATIONS: CONTROL
146

147     
148     /**
149      * Provides a string representation of the control.
150      * @return a string description
151      */

152     public String JavaDoc toString() {
153     return new String JavaDoc(getType() + " with current value: " + getValue());
154     }
155     
156     
157     // INNER CLASSES
158

159     
160     /**
161      * An instance of the <code>EnumControl.Type</code> inner class identifies one kind of
162      * enumerated control. Static instances are provided for the
163      * common types.
164      *
165      * @see EnumControl
166      *
167      * @author Kara Kytle
168      * @version 1.14, 03/12/19
169      * @since 1.3
170      */

171     public static class Type extends Control.Type JavaDoc {
172     
173     
174     // TYPE DEFINES
175

176     /**
177      * Represents a control over a set of possible reverberation settings.
178      * Each reverberation setting is described by an instance of the
179      * {@link ReverbType} class. (To access these settings,
180      * invoke <code>{@link EnumControl#getValues}</code> on an
181      * enumerated control of type <code>REVERB</code>.)
182      */

183     public static final Type REVERB = new Type("Reverb");
184     
185     
186     // CONSTRUCTOR
187

188     
189     /**
190      * Constructs a new enumerated control type.
191      * @param name the name of the new enumerated control type
192      */

193     protected Type(String JavaDoc name) {
194         super(name);
195     }
196     } // class Type
197

198 } // class EnumControl
199
Popular Tags