Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/BooleanControl.java
41159 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627/**28* A {@code BooleanControl} provides the ability to switch between two possible29* settings that affect a line's audio. The settings are boolean values30* ({@code true} and {@code false}). A graphical user interface might represent31* the control by a two-state button, an on/off switch, two mutually exclusive32* buttons, or a checkbox (among other possibilities). For example, depressing a33* button might activate a {@link BooleanControl.Type#MUTE MUTE} control to34* silence the line's audio.35* <p>36* As with other {@code Control} subclasses, a method is provided that returns37* string labels for the values, suitable for display in the user interface.38*39* @author Kara Kytle40* @since 1.341*/42public abstract class BooleanControl extends Control {4344/**45* The {@code true} state label, such as "true" or "on".46*/47private final String trueStateLabel;4849/**50* The {@code false} state label, such as "false" or "off".51*/52private final String falseStateLabel;5354/**55* The current value.56*/57private boolean value;5859/**60* Constructs a new boolean control object with the given parameters.61*62* @param type the type of control represented this float control object63* @param initialValue the initial control value64* @param trueStateLabel the label for the state represented by65* {@code true}, such as "true" or "on"66* @param falseStateLabel the label for the state represented by67* {@code false}, such as "false" or "off"68*/69protected BooleanControl(Type type, boolean initialValue, String trueStateLabel, String falseStateLabel) {7071super(type);72this.value = initialValue;73this.trueStateLabel = trueStateLabel;74this.falseStateLabel = falseStateLabel;75}7677/**78* Constructs a new boolean control object with the given parameters. The79* labels for the {@code true} and {@code false} states default to "true"80* and "false".81*82* @param type the type of control represented by this float control object83* @param initialValue the initial control value84*/85protected BooleanControl(Type type, boolean initialValue) {86this(type, initialValue, "true", "false");87}8889/**90* Sets the current value for the control. The default implementation simply91* sets the value as indicated. Some controls require that their line be92* open before they can be affected by setting a value.93*94* @param value desired new value95*/96public void setValue(boolean value) {97this.value = value;98}99100/**101* Obtains this control's current value.102*103* @return current value104*/105public boolean getValue() {106return value;107}108109/**110* Obtains the label for the specified state.111*112* @param state the state whose label will be returned113* @return the label for the specified state, such as "true" or "on" for114* {@code true}, or "false" or "off" for {@code false}115*/116public String getStateLabel(boolean state) {117return ((state == true) ? trueStateLabel : falseStateLabel);118}119120/**121* Returns a string representation of the boolean control.122*123* @return a string representation of the boolean control124*/125@Override126public String toString() {127return String.format("%s with current value: %s", super.toString(),128getStateLabel(getValue()));129}130131/**132* An instance of the {@code BooleanControl.Type} class identifies one kind133* of boolean control. Static instances are provided for the common types.134*135* @author Kara Kytle136* @since 1.3137*/138public static class Type extends Control.Type {139140/**141* Represents a control for the mute status of a line. Note that mute142* status does not affect gain.143*/144public static final Type MUTE = new Type("Mute");145146/**147* Represents a control for whether reverberation is applied to a line.148* Note that the status of this control not affect the reverberation149* settings for a line, but does affect whether these settings are used.150*/151public static final Type APPLY_REVERB = new Type("Apply Reverb");152153/**154* Constructs a new boolean control type.155*156* @param name the name of the new boolean control type157*/158protected Type(final String name) {159super(name);160}161}162}163164165