diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-03-09 17:03:48 -0300 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2015-03-09 17:03:48 -0300 |
commit | d69fa8746728367646494fd8c2c18944f306c6a2 (patch) | |
tree | f063f9efc6c93a5520991f509f3a481543b73a91 /src/jcgp/gui/settings/parameters | |
parent | 9062115b7d15cb05552632dc5486a5cd15a45289 (diff) |
Added existing source code
Diffstat (limited to 'src/jcgp/gui/settings/parameters')
4 files changed, 534 insertions, 0 deletions
diff --git a/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java new file mode 100644 index 0000000..a1f03fe --- /dev/null +++ b/src/jcgp/gui/settings/parameters/GUIBooleanParameter.java @@ -0,0 +1,82 @@ +package jcgp.gui.settings.parameters; + +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.scene.control.CheckBox; +import javafx.scene.control.Control; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.constants.Constants; +import jcgp.gui.settings.SettingsPane; + +/** + * This extension of @code{GUIParameter} uses a @code{CheckBox} to display + * the value of a @code{BooleanParameter}. It cannot be constructed + * directly - instead, use @code{GUIParameter.create()}. + * <br><br> + * See {@link GUIParameter} for more information. + * + * @author Eduardo Pedroni + */ +public class GUIBooleanParameter extends GUIParameter<Boolean> { + + private CheckBox checkBox; + + /** + * This protected constructor is intended for use + * by the factory method only. + * + */ + protected GUIBooleanParameter(BooleanParameter parameter, SettingsPane sp) { + super(parameter, sp); + } + + @Override + protected Control makeControl() { + checkBox = new CheckBox(); + checkBox.setSelected(parameter.get()); + + return checkBox; + } + + @Override + protected void setControlListeners() { + /* pass the CheckBox value back to the parameter whenever it gets + * modified, provided the experiment isn't running */ + checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() { + @Override + public void changed( + ObservableValue<? extends Boolean> observable, + Boolean oldValue, Boolean newValue) { + if (!settingsPane.isExperimentRunning()) { + parameter.set(newValue); + settingsPane.revalidateParameters(); + } + } + }); + } + + @Override + protected void setValidityStyle() { + // update the Control's style and tooltip based on the status of the parameter + if (parameter.getStatus() == ParameterStatus.INVALID) { + checkBox.setStyle(Constants.BASE_CHECKBOX_STYLE + Constants.INVALID_PARAMETER_STYLE); + checkBox.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING + || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + checkBox.setStyle(Constants.BASE_CHECKBOX_STYLE + Constants.WARNING_PARAMETER_STYLE); + checkBox.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + checkBox.setStyle(Constants.BASE_CHECKBOX_STYLE + Constants.VALID_PARAMETER_STYLE); + checkBox.setTooltip(null); + } + } + + @Override + public void refreshValue() { + checkBox.setSelected(parameter.get()); + } + +} diff --git a/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java new file mode 100644 index 0000000..feee34c --- /dev/null +++ b/src/jcgp/gui/settings/parameters/GUIDoubleParameter.java @@ -0,0 +1,110 @@ +package jcgp.gui.settings.parameters; + +import java.text.DecimalFormat; + +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.geometry.Pos; +import javafx.scene.control.Control; +import javafx.scene.control.TextField; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.constants.Constants; +import jcgp.gui.settings.SettingsPane; + +/** + * This extension of @code{GUIParameter} uses a @code{TextField} to display + * the value of a @code{DoubleParameter}. It cannot be constructed + * directly - instead, use @code{GUIParameter.create()}. + * <br><br> + * See {@link GUIParameter} for more information. + * + * @author Eduardo Pedroni + */ +public class GUIDoubleParameter extends GUIParameter<Number> { + + private TextField textField; + private DecimalFormat decimalFormat; + + /** + * This protected constructor is intended for use + * by the factory method only. + * + */ + protected GUIDoubleParameter(DoubleParameter parameter, SettingsPane sp) { + super(parameter, sp); + } + + @Override + protected Control makeControl() { + // we use a text field, and a formatting class to enforce decimals + decimalFormat = new DecimalFormat(); + decimalFormat.setMaximumFractionDigits(10); + textField = new TextField(decimalFormat.format(parameter.get().doubleValue())); + textField.setStyle(Constants.VALID_PARAMETER_STYLE); + textField.setAlignment(Pos.CENTER_RIGHT); + textField.prefWidthProperty().bind(widthProperty().divide(2)); + return textField; + } + + @Override + protected void setControlListeners() { + /* pass the TextField value back to the parameter whenever it gets + * modified, provided it is not empty, the experiment isn't running + * and it matches the double-precision regex filter */ + textField.textProperty().addListener(new ChangeListener<String>() { + @Override + public void changed( + ObservableValue<? extends String> observable, + String oldValue, String newValue) { + if (!settingsPane.isExperimentRunning()) { + if (newValue.matches("^[-+]?[0-9]*\\.?[0-9]+$")) { + if (!newValue.isEmpty()) { + double value = Double.parseDouble(newValue); + parameter.set(value); + settingsPane.revalidateParameters(); + } + } else { + refreshValue(); + } + } + } + }); + /* if the TextField loses focus and is empty, set it to the current + * value of the parameter */ + textField.focusedProperty().addListener(new ChangeListener<Boolean>() { + @Override + public void changed( + ObservableValue<? extends Boolean> observable, + Boolean oldValue, Boolean newValue) { + if (!newValue) { + refreshValue(); + } + } + }); + } + + @Override + protected void setValidityStyle() { + // update the Control's style and tooltip based on the status of the parameter + if (parameter.getStatus() == ParameterStatus.INVALID) { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.INVALID_PARAMETER_STYLE); + textField.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.WARNING_PARAMETER_STYLE); + textField.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.VALID_PARAMETER_STYLE); + textField.setTooltip(null); + } + } + + @Override + public void refreshValue() { + if (!textField.isFocused()) { + textField.setText(decimalFormat.format(parameter.get().doubleValue())); + } + } +} diff --git a/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java new file mode 100644 index 0000000..bcfbe50 --- /dev/null +++ b/src/jcgp/gui/settings/parameters/GUIIntegerParameter.java @@ -0,0 +1,107 @@ +package jcgp.gui.settings.parameters; + +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.geometry.Pos; +import javafx.scene.control.Control; +import javafx.scene.control.TextField; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.constants.Constants; +import jcgp.gui.settings.SettingsPane; + +/** + * This extension of @code{GUIParameter} uses a @code{TextField} to display + * the value of a @code{IntegerParameter}. It cannot be constructed + * directly - instead, use @code{GUIParameter.create()}. + * <br><br> + * See {@link GUIParameter} for more information. + * + * @author Eduardo Pedroni + */ +public class GUIIntegerParameter extends GUIParameter<Number> { + + private TextField textField; + + /** + * This protected constructor is intended for use + * by the factory method only. + * + */ + protected GUIIntegerParameter(IntegerParameter parameter, SettingsPane sp) { + super(parameter, sp); + } + + @Override + protected Control makeControl() { + // this uses a text field + textField = new TextField(String.valueOf(parameter.get())); + textField.setStyle(Constants.VALID_PARAMETER_STYLE); + textField.setAlignment(Pos.CENTER_RIGHT); + textField.prefWidthProperty().bind(widthProperty().divide(2)); + + return textField; + } + + @Override + protected void setControlListeners() { + /* pass the TextField value back to the parameter whenever it gets + * modified, provided it is not empty, the experiment isn't running + * and it matches the integer regex pattern */ + textField.textProperty().addListener(new ChangeListener<String>() { + @Override + public void changed( + ObservableValue<? extends String> observable, + String oldValue, String newValue) { + if (!settingsPane.isExperimentRunning()) { + if (newValue.matches("[0-9]*")) { + if (!newValue.isEmpty()) { + int value = Integer.parseInt(newValue); + parameter.set(value); + settingsPane.revalidateParameters(); + } + } else { + refreshValue(); + } + } + } + }); + /* if the TextField loses focus and is empty, set it to the current + * value of the parameter */ + textField.focusedProperty().addListener(new ChangeListener<Boolean>() { + @Override + public void changed( + ObservableValue<? extends Boolean> observable, + Boolean oldValue, Boolean newValue) { + if (!newValue) { + refreshValue(); + } + } + }); + } + + @Override + protected void setValidityStyle() { + // update the Control's style and tooltip based on the status of the parameter + if (parameter.getStatus() == ParameterStatus.INVALID) { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.INVALID_PARAMETER_STYLE); + textField.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else if (parameter.getStatus() == ParameterStatus.WARNING + || parameter.getStatus() == ParameterStatus.WARNING_RESET) { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.WARNING_PARAMETER_STYLE); + textField.setTooltip(tooltip); + tooltip.setText(parameter.getStatus().getDetails()); + } else { + textField.setStyle(Constants.BASE_TEXT_STYLE + Constants.VALID_PARAMETER_STYLE); + textField.setTooltip(null); + } + } + + @Override + public void refreshValue() { + if (!textField.isFocused()) { + textField.setText(parameter.get().toString()); + } + } +} diff --git a/src/jcgp/gui/settings/parameters/GUIParameter.java b/src/jcgp/gui/settings/parameters/GUIParameter.java new file mode 100644 index 0000000..59aecf6 --- /dev/null +++ b/src/jcgp/gui/settings/parameters/GUIParameter.java @@ -0,0 +1,235 @@ +package jcgp.gui.settings.parameters; + +import java.util.concurrent.atomic.AtomicBoolean; + +import javafx.application.Platform; +import javafx.beans.value.ChangeListener; +import javafx.beans.value.ObservableValue; +import javafx.geometry.Pos; +import javafx.scene.control.Control; +import javafx.scene.control.Label; +import javafx.scene.control.Tooltip; +import javafx.scene.layout.HBox; +import jcgp.backend.parameters.BooleanParameter; +import jcgp.backend.parameters.DoubleParameter; +import jcgp.backend.parameters.IntegerParameter; +import jcgp.backend.parameters.Parameter; +import jcgp.backend.parameters.ParameterStatus; +import jcgp.gui.settings.SettingsPane; + +/** + * + * This is the base class for all @code{GUIParameter}s. Using the factory method @code{GUIParameter.create()} + * generates an appropriate instance of this class for the specified parameter. + * <br><br> + * A @code{GUIParameter} is an @code{HBox} containing a @code{Text} for the parameter name + * and a @code{Control} for interaction. + * It stores an instance of its associated @code{Parameter} object and also contains a @code{Tooltip} for + * displaying status information. + * <br><br> + * Monitor parameters are updated automatically and have their @code{Control} disabled so + * that no changes can be made via the GUI. + * Non-monitor parameters are updated automatically as well, but may be changed by the user + * if the program is not evolving. + * + * @see Parameter + * @author Eduardo Pedroni + * @param <T> the parameter data type + */ +public abstract class GUIParameter<T> extends HBox { + + private Label name; + private Control valueControl; + + protected SettingsPane settingsPane; + protected Tooltip tooltip; + protected Parameter<T> parameter; + + /** This is the lock used to prevent more than one update task to be scheduled + * at the same time on the same GUIParameter. */ + private AtomicBoolean updateLock = new AtomicBoolean(false); + + /** + * This value is used to assert whether the control has changed values since + * the program last ran. Therefore, it is updated whenever a generation occurs + * or the experiment is reset. + */ + private T referenceValue; + + /** + * This protected template constructor contains the common elements to all + * @code{GUIParameter}s and should be invoked by any subclasses using @code{super()}. It + * defers the creation of the parameter {@code Control} object to the subclass + * currently being built (which in turn is defined by the factory method). + * + * @param parameter a @code{Parameter} for which to generate a @code{GUIParameter}. + * @param sp a reference to the @code{SettingsPane}. + */ + protected GUIParameter(Parameter<T> parameter, final SettingsPane settingsPane) { + this.parameter = parameter; + this.referenceValue = parameter.get(); + + this.settingsPane = settingsPane; + + setAlignment(Pos.CENTER_LEFT); + setSpacing(5); + + name = new Label(parameter.toString()); + // set text width to half of the total width of the GUIParameter + name.prefWidthProperty().bind(widthProperty().divide(2)); + + // the tooltip is the hover-over label containing status information, when appropriate + tooltip = new Tooltip(); + tooltip.setSkin(null); + + valueControl = makeControl(); + + // if the parameter is a monitor, it should be permanently disabled + valueControl.setDisable(parameter.isMonitor()); + + // bind to parameter value property in a thread-safe way + makeThreadSafeBinding(); + + // if parameter is not a monitor, make sure the control is constrained appropriately + if (!parameter.isMonitor()) { + setControlListeners(); + } + + getChildren().addAll(name, valueControl); + } + + /** + * Factory method to create @code{GUIParameter}s from @code{Parameter}s. + * Use this to create an appropriate @code{GUIParameter} from any instance of @code{Parameter}, + * rather than manually downcasting the @code{Parameter} object every time. + * + * @param parameter a parameter for which to generate a @code{GUIParameter}. + * @param sp a reference to the @code{SettingsPane}. + * @return an appropriate instance of @code{GUIParameter}. + */ + public static GUIParameter<?> create(Parameter<?> parameter, SettingsPane sp) { + if (parameter instanceof IntegerParameter) { + return new GUIIntegerParameter((IntegerParameter) parameter, sp); + } else if (parameter instanceof DoubleParameter) { + return new GUIDoubleParameter((DoubleParameter) parameter, sp); + } else if (parameter instanceof BooleanParameter) { + return new GUIBooleanParameter((BooleanParameter) parameter, sp); + } else { + throw new ClassCastException("No GUIParameter subclass exists for argument of type " + parameter.getClass()); + } + } + + /** + * Parameters are intended to communicate information from the experiment + * to the GUI. Since the experiment runs on a separate threads and it is illegal + * to modify JavaFX objects from outside the JavaFX Application thread, this + * special ChangeListener updates the GUIParameter in a safe way. + * <br><br> + * Note that this is applied to all parameters regardless of whether they are + * monitors or not; the only difference between monitor and non-monitor parameters + * is that monitor parameters cannot be modified from the GUI. + */ + private void makeThreadSafeBinding() { + parameter.valueProperty().addListener(new ChangeListener<Object>() { + @Override + public void changed( + ObservableValue<? extends Object> observable, + Object oldValue, Object newValue) { + // only do this if the experiment is running + if (settingsPane.isExperimentRunning() || !isFocused()) { + /* here's the catch - atomically get the lock state and set it to true + * the lock will only be false again when the runnable is finished executing, + * preventing multiple runnables to concurrently update the same GUIParameter + */ + if (!updateLock.getAndSet(true)) { + Platform.runLater(new Runnable() { + @Override + public void run() { + refreshValue(); + updateLock.set(false); + } + }); + } + } + } + }); + } + + /** + * @return true if the current value of the parameter does not prevent the + * experiment from running. + */ + public boolean isValid() { + return parameter.getStatus() != ParameterStatus.INVALID; + } + + /** + * Force the parameter to validate its current value, and apply the associated + * style to the @code{GUIParameter}. + */ + public void validate() { + parameter.validate(parameter.get()); + setValidityStyle(); + } + + /** + * Certain parameter changes might require the experiment to be reset, either + * because the parameter is critical or because its status requires a reset. + * + * @return true if an experiment reset is required due to this parameter changing. + */ + public boolean requiresReset() { + return (parameter.isCritical() && !parameter.get().equals(referenceValue)) + || parameter.getStatus() == ParameterStatus.WARNING_RESET; + } + + /** + * Set the current parameter value as the reference value of the @code{GUIParameter}. + * The new reference value will be used to determine the validity of the parameter, + * should its value change. + */ + public void applyValue() { + referenceValue = parameter.get(); + } + + /* + * The following prototypes are instance-dependent and are called from + * GUIParameter() as necessary. + */ + /** + * This method returns the @code{Control} object used to control the parameter. + * <br><br> + * Implementations of @code{GUIParameter} must override this method and return + * a @code{Control} appropriate to the type of parameter. This will typically be + * done by referencing the protected field @code{GUIParameter.parameter}. + * + * @return the Control object to be added to the GUIParameter. + */ + protected abstract Control makeControl(); + + /** + * Adds the necessary handlers to the @code{Control} object in order to modify + * the underlying parameter. This will typically consist of filtering key + * presses to ensure no invalid characters are inserted, applying the new + * value to the underlying parameter and revalidating the parameters to + * reflect the changes made. + */ + protected abstract void setControlListeners(); + + /** + * This method is called to style the @code{GUIParameter} according to the status of + * the parameter, which can be obtained with @code{parameter.getStatus()}. While the + * subclass is free to style itself in any way, the CSS strings defined here + * (INVALID_PARAMETER_STYLE, WARNING_PARAMETER_STYLE, VALID_PARAMETER_STYLE) + * provide a way to keep the GUI consistent. + * + * @see ParameterStatus + */ + protected abstract void setValidityStyle(); + + /** + * Update the control so it shows the correct value of the parameter. This method + * is used exclusively by the thread-safe binding created if the module is a monitor. + */ + protected abstract void refreshValue(); +} |