1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-11-13 07:33:44 +01:00

Table: removed the possibility for repeated questions

This commit is contained in:
Carlos Galindo 2019-09-13 01:30:34 +02:00
parent 5d5f400495
commit a599508fc3
Signed by: kauron
GPG key ID: 83E68706DEE119A3
2 changed files with 37 additions and 22 deletions

View file

@ -41,7 +41,14 @@ public class TableController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
addButton.textProperty().bind(Bindings.when(editing.isNull()).then("_Add item").otherwise("_Save item"));
addButton.textProperty().bind(Bindings.when(addButton.disableProperty())
.then("Question repeated")
.otherwise(Bindings.when(editing.isNull())
.then("_Add item")
.otherwise("_Save item")));
addButton.getTooltip().textProperty().bind(Bindings.when(editing.isNull())
.then("Add an entry to the table")
.otherwise("Save changes in the selected entry"));
// Add context menu to Table
MenuItem menuEdit = new MenuItem("_Edit");
menuEdit.setOnAction(this::onEditAction);
@ -62,6 +69,16 @@ public class TableController implements Initializable {
(ListChangeListener<? super Integer>) obs -> menuEdit.setDisable(obs.getList().size() != 1));
searchField.textProperty().addListener((obj, o, n) ->
filtered.setPredicate((item) -> item.getQuestion().contains(n) || item.getAnswer().contains(n)));
newQuestionField.textProperty().addListener((obj, o, n) -> {
for (TestItem item : data) {
if (item == editing.get()) continue;
if (item.getQuestion().equals(n.trim())) {
addButton.setDisable(true);
return;
}
}
addButton.setDisable(false);
});
}
void setData(String name, List<TestItem> list, Controller controller, File file) {
@ -98,6 +115,10 @@ public class TableController implements Initializable {
@FXML
protected void onAddAction(ActionEvent event) {
if (newQuestionField.getText().trim().isEmpty() && newAnswerField.getText().trim().isEmpty()) {
newQuestionField.requestFocus();
return;
}
if (editing.get() == null) {
TestItem item = new TestItem(newQuestionField.getText().trim(), newAnswerField.getText().trim());
data.add(item);
@ -139,8 +160,11 @@ public class TableController implements Initializable {
@FXML
protected void onDuplicateAction(ActionEvent event) {
if (table.getSelectionModel().getSelectedIndices().size() > 0) saved.set(false);
for (int i : table.getSelectionModel().getSelectedIndices())
data.add(new TestItem(filtered.get(i)));
for (int i : table.getSelectionModel().getSelectedIndices()) {
TestItem item = new TestItem(filtered.get(i));
item.questionProperty().setValue(item.getQuestion() + " 2");
data.add(item);
}
table.requestFocus();
}

View file

@ -7,26 +7,19 @@
<children>
<HBox alignment="CENTER_LEFT" spacing="5.0">
<children>
<TextField fx:id="newQuestionField" promptText="Question" />
<TextField fx:id="newAnswerField" promptText="Answer" />
<Button defaultButton="true" fx:id="addButton" onAction="#onAddAction" prefWidth="105.0" text="_Add new">
<TextField fx:id="newQuestionField" promptText="Question" HBox.hgrow="ALWAYS" />
<TextField fx:id="newAnswerField" promptText="Answer" HBox.hgrow="ALWAYS" />
<Button fx:id="addButton" defaultButton="true" maxWidth="1.7976931348623157E308" onAction="#onAddAction" prefWidth="105.0" text="_Add new" HBox.hgrow="SOMETIMES">
<tooltip>
<Tooltip text="Add a new entry to the table" />
</tooltip>
</Button>
<TextField fx:id="searchField" promptText="Search..." />
<HBox alignment="CENTER_RIGHT" spacing="5.0" HBox.hgrow="ALWAYS" VBox.vgrow="ALWAYS">
<children>
<Button layoutX="10.0" layoutY="273.0" onAction="#onTestAction" prefWidth="105.0" text="_Test all">
<tooltip>
<Tooltip text="Begin a test for the whole table" />
</tooltip>
</Button>
</children>
<HBox.margin>
<Insets />
</HBox.margin>
</HBox>
<TextField fx:id="searchField" maxWidth="1.7976931348623157E308" promptText="Search..." HBox.hgrow="ALWAYS" />
<Button onAction="#onTestAction" prefWidth="105.0" text="_Test all">
<tooltip>
<Tooltip text="Begin a test for the whole table" />
</tooltip>
</Button>
</children>
<HBox.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
@ -38,9 +31,7 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</VBox.margin>
</HBox>
<TableView fx:id="table" onMouseClicked="#onTableMouseClicked" onKeyPressed="#onTableKeyEvent" maxHeight="1.7976931348623157E308"
maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity"
prefHeight="200.0" prefWidth="400.0" HBox.hgrow="ALWAYS" VBox.vgrow="ALWAYS">
<TableView fx:id="table" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#onTableKeyEvent" onMouseClicked="#onTableMouseClicked" prefHeight="200.0" prefWidth="400.0" HBox.hgrow="ALWAYS" VBox.vgrow="ALWAYS">
<columns>
<TableColumn fx:id="questionCol" prefWidth="75.0" text="Question" />
<TableColumn fx:id="answerCol" prefWidth="75.0" text="Answer" />