package jcgp.gui; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Cursor; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.scene.text.Text; import jcgp.GUI; import jcgp.JCGP; import jcgp.function.FunctionSet; import jcgp.gui.settings.GUIBooleanParameter; import jcgp.gui.settings.GUIDoubleParameter; import jcgp.gui.settings.GUIIntegerParameter; import jcgp.gui.settings.GUIParameter; import jcgp.modules.ea.EvolutionaryAlgorithm; import jcgp.modules.fitness.FitnessFunction; import jcgp.modules.mutator.Mutator; import jcgp.parameters.BooleanParameter; import jcgp.parameters.DoubleParameter; import jcgp.parameters.IntegerParameter; import jcgp.parameters.Parameter; public class SettingsPane extends AnchorPane { private VBox mainContainer, eas, mutators, ffs; private boolean dragging = false; public SettingsPane(JCGP cgp) { super(); setResizeListeners(); //setStyle("-fx-border-width: 0 0 0 5; -fx-border-color: #D1D1D1"); mainContainer = new VBox(); AnchorPane.setTopAnchor(mainContainer, 0.0); AnchorPane.setBottomAnchor(mainContainer, 0.0); AnchorPane.setRightAnchor(mainContainer, 0.0); AnchorPane.setLeftAnchor(mainContainer, GUI.RESIZE_MARGIN); mainContainer.setPadding(new Insets(5, 0, 0, 0)); mainContainer.setSpacing(6); setMinWidth(GUI.SETTINGS_WIDTH); setPrefWidth(GUI.SETTINGS_WIDTH); makeBaseParameters(cgp); makeEAParameters(cgp); mainContainer.getChildren().add(eas); makeMutatorParameters(cgp); mainContainer.getChildren().add(mutators); makeFitnessFunctionParameters(cgp); mainContainer.getChildren().add(ffs); makeNodeFunctionParameters(cgp); getChildren().add(mainContainer); } private void makeBaseParameters(JCGP cgp) { VBox vb = new VBox(); vb.setSpacing(2); Text header = new Text("Base Parameters"); header.setFont(Font.font("Arial", 14)); header.setUnderline(true); vb.getChildren().add(header); ArrayList gp = new ArrayList(); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("rows"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("columns"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("inputs"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("outputs"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("levelsBack"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("popSize"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("generations"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("runs"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("seed"))); gp.add(new GUIIntegerParameter((IntegerParameter) cgp.getResources().getParameter("report"))); gp.add(new GUIBooleanParameter((BooleanParameter) cgp.getResources().getParameter("debug"))); vb.getChildren().addAll(gp); mainContainer.getChildren().add(vb); } private void makeEAParameters(JCGP cgp) { eas = new VBox(); eas.setSpacing(2); Text header = new Text("EA"); header.setFont(Font.font("Arial", 14)); header.setUnderline(true); eas.getChildren().add(header); ComboBox eab = new ComboBox(); eab.getItems().addAll(cgp.getEvolutionaryAlgorithms()); eab.getSelectionModel().select(cgp.getEvolutionaryAlgorithm()); eab.prefWidthProperty().bind(mainContainer.widthProperty()); eas.getChildren().add(eab); addParameters(cgp.getEvolutionaryAlgorithm().getLocalParameters().entrySet().iterator(), eas); } private void makeMutatorParameters(JCGP cgp) { mutators = new VBox(); mutators.setSpacing(2); Text header = new Text("Mutator"); header.setFont(Font.font("Arial", 14)); header.setUnderline(true); mutators.getChildren().add(header); ComboBox mb = new ComboBox(); mb.getItems().addAll(cgp.getMutators()); mb.getSelectionModel().select(cgp.getMutator()); mb.prefWidthProperty().bind(mainContainer.widthProperty()); mutators.getChildren().add(mb); addParameters(cgp.getMutator().getLocalParameters().entrySet().iterator(), mutators); } private void makeFitnessFunctionParameters(JCGP cgp) { ffs = new VBox(); ffs.setSpacing(2); Text header = new Text("Fitness Function"); header.setFont(Font.font("Arial", 14)); header.setUnderline(true); ffs.getChildren().add(header); ComboBox ff = new ComboBox(); ff.getItems().addAll(cgp.getFitnessFunctions()); ff.getSelectionModel().select(cgp.getFitnessFunction()); ff.prefWidthProperty().bind(mainContainer.widthProperty()); ffs.getChildren().add(ff); addParameters(cgp.getFitnessFunction().getLocalParameters().entrySet().iterator(), ffs); } private void makeNodeFunctionParameters(JCGP cgp) { VBox vb = new VBox(); vb.setSpacing(2); Text header = new Text("Node Functions"); header.setFont(Font.font("Arial", 14)); header.setUnderline(true); vb.getChildren().add(header); ComboBox nf = new ComboBox(); nf.getItems().addAll(cgp.getResources().getFunctionSets()); nf.getSelectionModel().select(cgp.getResources().getFunctionSet()); nf.prefWidthProperty().bind(mainContainer.widthProperty()); vb.getChildren().add(nf); // TODO make this function re-usable addFunctions(cgp.getResources().getFunctionSet(), vb); mainContainer.getChildren().add(vb); } /** * @param cgp * @param vb */ private void addParameters(Iterator> it, VBox vb) { while (it.hasNext()) { Parameter parameter = ((Map.Entry) it.next()).getValue(); if (parameter instanceof IntegerParameter) { vb.getChildren().add(new GUIIntegerParameter((IntegerParameter) parameter)); } else if (parameter instanceof DoubleParameter) { vb.getChildren().add(new GUIDoubleParameter((DoubleParameter) parameter)); } else if (parameter instanceof BooleanParameter) { vb.getChildren().add(new GUIBooleanParameter((BooleanParameter) parameter)); } } } private void addFunctions(final FunctionSet fs, VBox vb) { CheckBox cb; for (int i = 0; i < fs.getTotalFunctionCount(); i++) { cb = new CheckBox(fs.getFunction(i).getName()); cb.setId(String.valueOf(i)); cb.setSelected(true); final int index = i; cb.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { if (((CheckBox) event.getSource()).isSelected()) { fs.enableFunction(index); } else { fs.disableFunction(index); } } }); vb.getChildren().add(cb); } } private void setResizeListeners() { setOnMousePressed(new EventHandler() { @Override public void handle(MouseEvent event) { if(isInDraggableZone(event)) { dragging = true; } } }); setOnMouseDragged(new EventHandler() { @Override public void handle(MouseEvent event) { if(dragging) { double newWidth = getWidth() - event.getX(); if (newWidth >= getMinWidth()) { setPrefWidth(newWidth); } else { setPrefWidth(getMinWidth()); } } } }); setOnMouseMoved(new EventHandler() { @Override public void handle(MouseEvent event) { if(isInDraggableZone(event) || dragging) { setCursor(Cursor.H_RESIZE); } else { setCursor(Cursor.DEFAULT); } } }); setOnMouseReleased(new EventHandler() { @Override public void handle(MouseEvent event) { dragging = false; setCursor(Cursor.DEFAULT); } }); } private boolean isInDraggableZone(MouseEvent event) { return event.getX() < (GUI.RESIZE_MARGIN); } }