aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/gui/settings/testcase/TestCaseTable.java
blob: d7b2e2b95120f20ef5e204cee1e0b1627c1003fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<TestCase<Object>> table;

	public TestCaseTable(final TestCaseProblem<Object> testCaseProblem, final GUI gui) {
		super();
		
		table = new TableView<TestCase<Object>>();
		ObservableList<TestCase<Object>> testCaseList = testCaseProblem.getTestCases();
		
		ArrayList<TableColumn<TestCase<Object>, String>> inputs = new ArrayList<TableColumn<TestCase<Object>, String>>(testCaseProblem.getInputCount());
		ArrayList<TableColumn<TestCase<Object>, String>> outputs = new ArrayList<TableColumn<TestCase<Object>, String>>(testCaseProblem.getOutputCount());

		TableColumn<TestCase<Object>, String> tc;
		for (int i = 0; i < testCaseProblem.getInputCount(); i++) {
			tc = new TableColumn<TestCase<Object>, String>("I: " + i);
			inputs.add(tc);
			final int index = i;
			tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestCase<Object>,String>, ObservableValue<String>>() {
				@Override
				public ObservableValue<String> call(CellDataFeatures<TestCase<Object>, 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<TestCase<Object>, String>("O: " + o);
			outputs.add(tc);
			final int index = o;
			tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<TestCase<Object>,String>, ObservableValue<String>>() {
				@Override
				public ObservableValue<String> call(CellDataFeatures<TestCase<Object>, 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<TestCase<Object>>() {
			@Override
			public void changed(
					ObservableValue<? extends TestCase<Object>> observable,	TestCase<Object> oldValue, TestCase<Object> newValue) {
				gui.evaluateTestCase(newValue);
			}
		});
		
		setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				gui.hideGeneValues();
				table.getSelectionModel().select(null);
			}
		});
		
		setScene(new Scene(table));
	}

	public TableView<TestCase<Object>> getTable() {
		return table;
	}
}