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.gui.GUI; /** * * * @author Eduardo Pedroni * */ public class TestCaseTable extends Stage { private TableView> table; public TestCaseTable(final TestCaseProblem testCaseProblem, final GUI gui) { super(); table = new TableView>(); ObservableList> testCaseList = testCaseProblem.getTestCases(); ArrayList, String>> inputs = new ArrayList, String>>(testCaseProblem.getInputCount()); ArrayList, String>> outputs = new ArrayList, String>>(testCaseProblem.getOutputCount()); TableColumn, String> tc; for (int i = 0; i < testCaseProblem.getInputCount(); 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(testCaseProblem.getInputCount() + testCaseProblem.getOutputCount())); } for (int o = 0; o < testCaseProblem.getOutputCount(); 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(testCaseProblem.getInputCount() + testCaseProblem.getOutputCount())); } 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; } }