Skip to content

Instantly share code, notes, and snippets.

@Groostav
Last active November 15, 2015 01:22
Show Gist options
  • Save Groostav/0acd2d04581aca858522 to your computer and use it in GitHub Desktop.
Save Groostav/0acd2d04581aca858522 to your computer and use it in GitHub Desktop.
Awesome copy-pasta
package com.empowerops.front_end;
import com.empowerops.common.EventBus;
import com.empowerops.common.FormattedDuration;
import com.empowerops.common.SyncingUtilities;
import com.empowerops.common.ui.FXMLLoader;
import com.empowerops.common.ui.PreloadedFX;
import com.empowerops.jargon.events.OptimizationSettingsChanged;
import com.empowerops.jargon.model.OptimizerNode;
import com.empowerops.jargon.model.OptimizerSettingChangeSet;
import com.google.common.eventbus.Subscribe;
import com.google.inject.assistedinject.Assisted;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javax.inject.Inject;
/**
* Created by Justin Casol on 6/29/2015.
*/
public class OptimizerSettingsPanelFXController extends PreloadedFX {
private final EventBus eventBus;
private final SyncingUtilities syncingUtilities;
private final OptimizerSettingChangeSet optimizerSettingChangeSet;
private final OptimizerNode node;
@FXML CheckBox runTimeLimitCheckBox;
@FXML CheckBox objectiveTargetCheckBox;
@FXML CheckBox evaluationCountLimitCheckBox;
@FXML VBox view;
@FXML TextField runField;
@FXML TextField functionNumField;
@FXML TextField timeField;
@FXML TextField lowestFunctionValField;
@FXML ComboBox<FormattedDuration.TimeUnit> runTimeDropDown;
public static interface Factory {
public OptimizerSettingsPanelFXController create(OptimizerSettingChangeSet optimizerSettingChangeSet,
OptimizerNode node);
}
@Inject
public OptimizerSettingsPanelFXController(EventBus eventBus,
FXMLLoader fxmlLoader,
SyncingUtilities syncingUtilities,
@Assisted OptimizerSettingChangeSet optimizerSettingChangeSet,
@Assisted OptimizerNode node) {
super(fxmlLoader);
this.eventBus = eventBus;
this.syncingUtilities = syncingUtilities;
this.optimizerSettingChangeSet = optimizerSettingChangeSet;
this.node = node;
eventBus.register(this);
rebindModelToView();
functionNumField.disableProperty().bind(evaluationCountLimitCheckBox.selectedProperty().not());
timeField.disableProperty().bind(runTimeLimitCheckBox.selectedProperty().not());
lowestFunctionValField.disableProperty().bind(objectiveTargetCheckBox.selectedProperty().not());
runTimeDropDown.disableProperty().bind(runTimeLimitCheckBox.selectedProperty().not());
evaluationCountLimitCheckBox.selectedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) {
functionNumField.setText("" + Integer.MAX_VALUE);
}
syncingUtilities.runLaterOnFXThread(this::validAndCommitEvaluationCount);
});
runTimeLimitCheckBox.selectedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) {
timeField.setText("" + Double.POSITIVE_INFINITY);
}
syncingUtilities.runLaterOnFXThread(this::validAndCommitTimeLimit);
});
objectiveTargetCheckBox.selectedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) {
lowestFunctionValField.setText("" + Double.NEGATIVE_INFINITY);
}
syncingUtilities.runLaterOnFXThread(this::validAndCommitTargetObjectiveValue);
});
runTimeDropDown.getSelectionModel().selectedItemProperty().addListener(c -> {
syncingUtilities.runLaterOnFXThread(this::validAndCommitTimeLimit);
});
runField.focusedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) { validAndCommitRunCount(); }
});
functionNumField.focusedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) { validAndCommitEvaluationCount(); }
});
timeField.focusedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) { validAndCommitTimeLimit(); }
});
lowestFunctionValField.focusedProperty().addListener((source, oldValue, newValue) -> {
if (! newValue) { validAndCommitTargetObjectiveValue(); }
});
runField.setOnKeyPressed(key -> {
if (key.getCode().equals(KeyCode.ENTER)) {
view.requestFocus();
}
if (key.getCode().equals(KeyCode.ESCAPE)) {
runField.setText(Integer.toString(optimizerSettingChangeSet.maxNumRuns));
view.requestFocus();
}
});
functionNumField.setOnKeyPressed(key -> {
if (key.getCode().equals(KeyCode.ENTER)) {
view.requestFocus();
}
if (key.getCode().equals(KeyCode.ESCAPE)) {
functionNumField.setText(Integer.toString(optimizerSettingChangeSet.maxNumFunctionEvaluations));
view.requestFocus();
}
});
timeField.setOnKeyPressed(key -> {
if (key.getCode().equals(KeyCode.ENTER)) {
view.requestFocus();
}
if (key.getCode().equals(KeyCode.ESCAPE)) {
timeField.setText(Double.toString(optimizerSettingChangeSet.maxRunTime.getDuration()));
view.requestFocus();
}
});
lowestFunctionValField.setOnKeyPressed(key -> {
if (key.getCode().equals(KeyCode.ENTER)) {
view.requestFocus();
}
if (key.getCode().equals(KeyCode.ESCAPE)) {
lowestFunctionValField.setText(Double.toString(optimizerSettingChangeSet.minFunctionValue));
view.requestFocus();
}
});
checkAndUpdateNumberOfObjecitves();
}
@Subscribe
public void checkNumberOfObjectiveOn(OptimizationSettingsChanged.ViaProblemDefinitionEvent event) {
checkAndUpdateNumberOfObjecitves();
}
private void checkAndUpdateNumberOfObjecitves() {
objectiveTargetCheckBox.setSelected(false);
objectiveTargetCheckBox.setDisable(node.getAvailableObjectives().size() > 1);
}
private void rebindModelToView() {
runField.setText(Integer.toString(optimizerSettingChangeSet.maxNumRuns));
setupCheckBoxAndValue(Integer.MAX_VALUE, optimizerSettingChangeSet.maxNumFunctionEvaluations, evaluationCountLimitCheckBox, functionNumField);
setupCheckBoxAndValue(Double.POSITIVE_INFINITY, optimizerSettingChangeSet.maxRunTime.getDuration(), runTimeLimitCheckBox, timeField);
runTimeDropDown.setValue(optimizerSettingChangeSet.maxRunTime.getUnit());
setupCheckBoxAndValue(Double.NEGATIVE_INFINITY, optimizerSettingChangeSet.minFunctionValue, objectiveTargetCheckBox, lowestFunctionValField);
}
private void validAndCommitTargetObjectiveValue() {
double targetValue = getTargetValueOrDefaultFrom(Double.NEGATIVE_INFINITY, lowestFunctionValField);
node.optimizationSettings.targetObjectiveValue = targetValue;
eventBus.post(new OptimizationSettingsChanged.ViaOptimizerSettingsEvent());
}
private void validAndCommitTimeLimit() {
double duration = getDurationOrDefaultFrom(timeField, Double.POSITIVE_INFINITY);
node.optimizationSettings.maxRunTime = new FormattedDuration(duration, runTimeDropDown.getValue());
eventBus.post(new OptimizationSettingsChanged.ViaOptimizerSettingsEvent());
}
private void validAndCommitEvaluationCount() {
int evaluationCount = getPositiveIntegerOrDefaultFrom(functionNumField, Integer.MAX_VALUE);
node.optimizationSettings.functionEvaluationCount = evaluationCount;
eventBus.post(new OptimizationSettingsChanged.ViaOptimizerSettingsEvent());
}
private void validAndCommitRunCount() {
int result = getPositiveIntegerOrDefaultFrom(runField, 1);
node.optimizationSettings.requestedRunCount = result;
eventBus.post(new OptimizationSettingsChanged.ViaOptimizerSettingsEvent());
}
private void setupCheckBoxAndValue(Number defaultValue,
Number requestedValue,
CheckBox defaultCheckBox,
TextField textField) {
if (defaultValue.equals(requestedValue)) {
defaultCheckBox.setSelected(false);
textField.setText("" + defaultValue);
}
else {
defaultCheckBox.setSelected(true);
textField.setText(requestedValue.toString());
}
}
private double getTargetValueOrDefaultFrom(double negativeInfinity, TextField lowestFunctionValField) {
double targetValue;
try {
targetValue = Double.parseDouble(lowestFunctionValField.getText());
}
catch (NumberFormatException e) {
targetValue = negativeInfinity;
lowestFunctionValField.setText("" + targetValue);
}
return targetValue;
}
private int getPositiveIntegerOrDefaultFrom(TextField textField, int defaultValue) {
int result;
try {
result = Integer.parseInt(textField.getText());
}
catch (NumberFormatException e) {
textField.setText("" + defaultValue);
result = defaultValue;
}
if (result < 1) {
textField.setText("" + defaultValue);
result = defaultValue;
}
return result;
}
private double getDurationOrDefaultFrom(TextField timeField, double positiveInfinity) {
double duration;
try {
duration = Double.parseDouble(timeField.getText());
}
catch (NumberFormatException e) {
timeField.setText("" + positiveInfinity);
duration = positiveInfinity;
}
if (duration <= 0) {
timeField.setText("" + positiveInfinity);
duration = positiveInfinity;
}
return duration;
}
public VBox getView() {
return view;
}
public void dispose() {
eventBus.unregister(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment