149 lines
4.6 KiB
Java
149 lines
4.6 KiB
Java
package envoy.client;
|
|
|
|
import java.io.Serializable;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.function.Consumer;
|
|
|
|
import javax.swing.JComponent;
|
|
|
|
import envoy.client.ui.PrimaryToggleSwitch;
|
|
|
|
/**
|
|
* Encapsulates a persistent value that is directly or indirectly mutable by the
|
|
* user.<br>
|
|
* <br>
|
|
* Project: <strong>envoy-clientChess</strong><br>
|
|
* File: <strong>SettingsItem.java</strong><br>
|
|
* Created: <strong>23.12.2019</strong><br>
|
|
*
|
|
* @param <T> the type of this {@link SettingsItem}'s value
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public class SettingsItem<T> implements Serializable {
|
|
|
|
private T value;
|
|
private Class<? extends JComponent> componentClass;
|
|
private String userFriendlyName, description;
|
|
|
|
transient private Consumer<T> changeHandler;
|
|
|
|
private static final Map<Class<?>, Class<? extends JComponent>> componentClasses = new HashMap<>();
|
|
|
|
private static final long serialVersionUID = 2146837835556852218L;
|
|
|
|
static {
|
|
componentClasses.put(Boolean.class, PrimaryToggleSwitch.class);
|
|
}
|
|
|
|
/**
|
|
* Initializes a {@link SettingsItem}. The default value's class will be mapped
|
|
* to a {@link JComponent} that can be used to display this {@link SettingsItem}
|
|
* to the user.
|
|
*
|
|
* @param value the default value
|
|
* @param userFriendlyName the user friendly name (short)
|
|
* @param description the description (long)
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public SettingsItem(T value, String userFriendlyName, String description) {
|
|
this(value, componentClasses.get(value.getClass()));
|
|
this.userFriendlyName = userFriendlyName;
|
|
this.description = description;
|
|
}
|
|
|
|
/**
|
|
* Initializes a {@link SettingsItem}. The default value's class will be mapped
|
|
* to a specific {@link JComponent}. The mapping can also be disables if this
|
|
* parameter is {@code null}. In that case a {@link NullPointerException} will
|
|
* be thrown if the method {@link SettingsItem#getComponent()} is called.
|
|
*
|
|
* @param value the default value
|
|
* @param componentClass the class of the {@link JComponent} to represent this {@link SettingsItem} with
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public SettingsItem(T value, Class<? extends JComponent> componentClass) {
|
|
this.value = value;
|
|
this.componentClass = componentClass;
|
|
}
|
|
|
|
/**
|
|
* @return an instance of the {@link JComponent} that represents this {@link SettingsItem}
|
|
* @throws ReflectiveOperationException if the component initialization failed
|
|
* @throws SecurityException if the component initialization failed
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public JComponent getComponent() throws ReflectiveOperationException, SecurityException {
|
|
if (componentClass == null) throw new NullPointerException("Component class is null");
|
|
return componentClass.getConstructor(SettingsItem.class).newInstance(this);
|
|
}
|
|
|
|
/**
|
|
* @return the value
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public T get() { return value; }
|
|
|
|
/**
|
|
* Changes the value of this {@link SettingsItem}. If a {@code ChangeHandler} if
|
|
* defined, it will be invoked with this value.
|
|
*
|
|
* @param value the value to set
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void set(T value) {
|
|
if (changeHandler != null && value != this.value) changeHandler.accept(value);
|
|
this.value = value;
|
|
}
|
|
|
|
/**
|
|
* @return the componentClass
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public Class<? extends JComponent> getComponentClass() { return componentClass; }
|
|
|
|
/**
|
|
* @param componentClass the componentClass to set
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void setComponentClass(Class<? extends JComponent> componentClass) { this.componentClass = componentClass; }
|
|
|
|
/**
|
|
* @return the userFriendlyName
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public String getUserFriendlyName() { return userFriendlyName; }
|
|
|
|
/**
|
|
* @param userFriendlyName the userFriendlyName to set
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void setUserFriendlyName(String userFriendlyName) { this.userFriendlyName = userFriendlyName; }
|
|
|
|
/**
|
|
* @return the description
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public String getDescription() { return description; }
|
|
|
|
/**
|
|
* @param description the description to set
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void setDescription(String description) { this.description = description; }
|
|
|
|
/**
|
|
* Sets a {@code ChangeHandler} for this {@link SettingsItem}. It will be
|
|
* invoked with the current value once during the registration and every time
|
|
* when the value changes.
|
|
*
|
|
* @param changeHandler the changeHandler to set
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void setChangeHandler(Consumer<T> changeHandler) {
|
|
this.changeHandler = changeHandler;
|
|
changeHandler.accept(value);
|
|
}
|
|
}
|