package jcgp.gui.settings.testcase; import java.util.ArrayList; import javafx.beans.property.SimpleStringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.control.TableView; import javafx.stage.Stage; import javafx.stage.WindowEvent; import javafx.util.Callback; import jcgp.backend.modules.problem.TestCaseProblem; import jcgp.backend.modules.problem.TestCaseProblem.TestCase; import jcgp.backend.resources.Resources; import jcgp.gui.GUI; /** * * * @author Eduardo Pedroni * */ public class TestCaseTable extends Stage { private TableView> table; public TestCaseTable(final TestCaseProblem testCaseProblem, final GUI gui) { super(); Resources resources = gui.getExperiment().getResources(); table = new TableView>(); ObservableList> testCaseList = testCaseProblem.getTestCases(); ArrayList, String>> inputs = new ArrayList, String>>(resources.inputs()); ArrayList, String>> outputs = new ArrayList, String>>(resources.outputs()); TableColumn, String> tc; for (int i = 0; i < resources.inputs(); i++) { tc = new TableColumn, String>("I: " + i); inputs.add(tc); final int index = i; tc.setCellValueFactory(new Callback,String>, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures, String> param) { return new SimpleStringProperty(param.getValue().getInput(index).toString()); } }); tc.setSortable(false); tc.prefWidthProperty().bind(table.widthProperty().divide(resources.inputs() + resources.outputs())); } for (int o = 0; o < resources.outputs(); o++) { tc = new TableColumn, String>("O: " + o); outputs.add(tc); final int index = o; tc.setCellValueFactory(new Callback,String>, ObservableValue>() { @Override public ObservableValue call(CellDataFeatures, String> param) { return new SimpleStringProperty(param.getValue().getOutput(index).toString()); } }); tc.setSortable(false); tc.prefWidthProperty().bind(table.widthProperty().divide(resources.inputs() + resources.outputs())); } table.getColumns().addAll(inputs); table.getColumns().addAll(outputs); table.setItems(testCaseList); table.getSelectionModel().selectedItemProperty().addListener(new ChangeListener>() { @Override public void changed( ObservableValue> observable, TestCase oldValue, TestCase newValue) { gui.evaluateTestCase(newValue); } }); setOnCloseRequest(new EventHandler() { @Override public void handle(WindowEvent event) { gui.hideGeneValues(); table.getSelectionModel().select(null); } }); setScene(new Scene(table)); } public TableView> getTable() { return table; } }