From 889295a0b2ca045ad7288ed466d10dfebc467833 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Wed, 11 Sep 2019 22:07:59 +0200 Subject: [PATCH] Typos and warnings fix --- .../kauron/jstudy/controller/Controller.java | 41 ++++++++++--------- .../jstudy/controller/EditController.java | 4 +- .../jstudy/controller/TableController.java | 22 +++++----- .../jstudy/controller/TestController.java | 12 +++--- .../java/es/kauron/jstudy/model/TestItem.java | 15 +++---- .../resources/es/kauron/jstudy/view/main.fxml | 16 +++++++- .../es/kauron/jstudy/view/settings.fxml | 2 +- .../es/kauron/jstudy/view/table.fxml | 2 +- src/test/java/es/kauron/jstudy/AppTest.java | 2 +- 9 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index 5691303..7487d31 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -41,15 +41,18 @@ public class Controller implements Initializable { private BorderPane root; @FXML - private MenuItem menuCloseTab; + private MenuItem menuCloseTab, menuSave; - private BooleanProperty table = new SimpleBooleanProperty(false); - private Map tabMap = new HashMap<>(); + private final BooleanProperty tabIsTable = new SimpleBooleanProperty(false); + private final Map tabMap = new HashMap<>(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { - tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> table.set(tabMap.get(n) != null)); - tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> menuCloseTab.setDisable(!n.isClosable())); + tabPane.getSelectionModel().selectedItemProperty().addListener((ob, o, n) -> { + tabIsTable.set(tabMap.get(n) != null); + menuCloseTab.setDisable(!n.isClosable()); + menuSave.setDisable(!tabMap.containsKey(n) || tabMap.get(n).saved.get()); + }); Platform.runLater(() -> root.getScene().getWindow().setOnCloseRequest(event -> { for (Tab tab : tabPane.getTabs()) { @@ -120,18 +123,22 @@ public class Controller implements Initializable { } else if (result.isPresent() && result.get().equals(openBT)){ for (File file : list) { List aux = TestItem.loadFrom(file, TestItem.COLONS); - if (aux != null) { - tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); - } + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); } } } else { File file = list.get(0); List aux = TestItem.loadFrom(file, TestItem.COLONS); - if (aux != null) { - tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); - tabPane.getSelectionModel().selectLast(); - } + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); + tabPane.getSelectionModel().selectLast(); + } + } + + @FXML + private void onSaveAction(ActionEvent event) { + Tab tab = tabPane.getSelectionModel().getSelectedItem(); + if (tabMap.containsKey(tab)) { + tabMap.get(tab).onSaveAction(event); } } @@ -151,10 +158,8 @@ public class Controller implements Initializable { else separator = TestItem.COMMA; List aux = TestItem.loadFrom(file, separator); - if (aux != null) { - tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, null)); - tabPane.getSelectionModel().selectLast(); - } + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, null)); + tabPane.getSelectionModel().selectLast(); } private Tab createTableTab(String name, List list, File file) { @@ -255,9 +260,7 @@ public class Controller implements Initializable { List files = event.getDragboard().getFiles(); for (File file : files) { List aux = TestItem.loadFrom(file, TestItem.COLONS); - if (aux != null) { - tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); - } + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); } event.consume(); } diff --git a/src/main/java/es/kauron/jstudy/controller/EditController.java b/src/main/java/es/kauron/jstudy/controller/EditController.java index f68e747..30be275 100644 --- a/src/main/java/es/kauron/jstudy/controller/EditController.java +++ b/src/main/java/es/kauron/jstudy/controller/EditController.java @@ -26,11 +26,11 @@ public class EditController implements Initializable { public void initialize(URL url, ResourceBundle rb) { } - protected void setList(ObservableList list, BooleanProperty saved) { + void setList(ObservableList list, BooleanProperty saved) { setList(list, -1, saved); } - protected void setList(ObservableList list, int index, BooleanProperty saved) { + void setList(ObservableList list, int index, BooleanProperty saved) { this.list = list; // Save attributes correctly this.index = index; this.saved = saved; diff --git a/src/main/java/es/kauron/jstudy/controller/TableController.java b/src/main/java/es/kauron/jstudy/controller/TableController.java index c5c1622..ae265ca 100644 --- a/src/main/java/es/kauron/jstudy/controller/TableController.java +++ b/src/main/java/es/kauron/jstudy/controller/TableController.java @@ -43,7 +43,7 @@ public class TableController implements Initializable { private Controller parent; private File file; StringProperty name; - BooleanProperty saved = new SimpleBooleanProperty(); + final BooleanProperty saved = new SimpleBooleanProperty(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -74,7 +74,7 @@ public class TableController implements Initializable { @FXML protected void onSaveAction(ActionEvent event) { - if (file == null) { + while (file == null) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JStudy file", "*.jsdb")); if (AppPrefs.lastDir != null) chooser.setInitialDirectory(AppPrefs.lastDir); @@ -82,11 +82,11 @@ public class TableController implements Initializable { file = chooser.showSaveDialog(table.getScene().getWindow()); if (!file.getName().endsWith(".jsdb")) file = new File(file.getPath() + ".jsdb"); - if (file != null) + if (file != null) { AppPrefs.lastDir = file.getParentFile(); - name.set(file.getName().substring(0, file.getName().lastIndexOf('.'))); + name.set(file.getName().substring(0, file.getName().lastIndexOf('.'))); + } } - if (file == null) return; TestItem.saveTo(file, data); saved.set(true); } @@ -94,10 +94,10 @@ public class TableController implements Initializable { @FXML protected void onAddAction(ActionEvent event) { try { - FXMLLoader cargador = new FXMLLoader(Main.class.getResource("view/edit.fxml")); - Parent pRoot = cargador.load(); + FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/edit.fxml")); + Parent pRoot = loader.load(); - ((EditController) cargador.getController()).setList(data, saved); + ((EditController) loader.getController()).setList(data, saved); Stage stage = new Stage(); stage.setTitle("New entry"); @@ -115,10 +115,10 @@ public class TableController implements Initializable { if (list.size() != 1) return; int index = list.get(0); try { - FXMLLoader cargador = new FXMLLoader(Main.class.getResource("view/edit.fxml")); - Parent root = cargador.load(); + FXMLLoader loader = new FXMLLoader(Main.class.getResource("view/edit.fxml")); + Parent root = loader.load(); - ((EditController) cargador.getController()).setList(table.getItems(), index, saved); + ((EditController) loader.getController()).setList(table.getItems(), index, saved); Stage stage = new Stage(); stage.setTitle("Editing entry..."); diff --git a/src/main/java/es/kauron/jstudy/controller/TestController.java b/src/main/java/es/kauron/jstudy/controller/TestController.java index c9dec7a..375cf85 100644 --- a/src/main/java/es/kauron/jstudy/controller/TestController.java +++ b/src/main/java/es/kauron/jstudy/controller/TestController.java @@ -29,11 +29,11 @@ public class TestController implements Initializable { @FXML private Button skipButton; - private SimpleBooleanProperty correctingError = new SimpleBooleanProperty(false); + private final SimpleBooleanProperty correctingError = new SimpleBooleanProperty(false); private List list; - private IntegerProperty errors = new SimpleIntegerProperty(0); - private ObjectProperty item = new SimpleObjectProperty<>(); - private IntegerProperty done = new SimpleIntegerProperty(0); + private final IntegerProperty errors = new SimpleIntegerProperty(0); + private final ObjectProperty item = new SimpleObjectProperty<>(); + private final IntegerProperty done = new SimpleIntegerProperty(0); @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -63,7 +63,7 @@ public class TestController implements Initializable { private void onNextAction(ActionEvent event) { prevAnswer.setText(answer.getText()); - boolean right = item.get().getAnswer().equals(answer.getText()); + boolean right = item.get().checkAnswer(answer.getText()); correctAnswer.setVisible(!right); correctLabel.setVisible(!right); @@ -93,7 +93,7 @@ public class TestController implements Initializable { private void chooseQuestion() { answer.setText(""); - TestItem next = item.get(); + TestItem next; do next = list.get((int) (Math.random() * list.size())); while (list.size() > 1 && item.get() == next); diff --git a/src/main/java/es/kauron/jstudy/model/TestItem.java b/src/main/java/es/kauron/jstudy/model/TestItem.java index 8e4444e..2afec3e 100644 --- a/src/main/java/es/kauron/jstudy/model/TestItem.java +++ b/src/main/java/es/kauron/jstudy/model/TestItem.java @@ -4,7 +4,7 @@ import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import java.io.*; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; @@ -13,9 +13,10 @@ import java.util.Scanner; public class TestItem { public static final String TAB = "\t", COLONS = "::", SEMICOLON = ";", COMMA = ","; - private StringProperty question, answer; + private final StringProperty question; + private final StringProperty answer; - public TestItem(SimpleStringProperty question, SimpleStringProperty answer) { + private TestItem(SimpleStringProperty question, SimpleStringProperty answer) { this.question = question; this.answer = answer; } @@ -25,7 +26,7 @@ public class TestItem { } public TestItem(String question, String answer) { - this(new SimpleStringProperty(new String(question)), new SimpleStringProperty(new String(answer))); + this(new SimpleStringProperty(question), new SimpleStringProperty(answer)); } public String getQuestion() { @@ -48,14 +49,14 @@ public class TestItem { return !question.get().isEmpty() && !answer.get().isEmpty(); } - public boolean checkAnswer(String answer, boolean sensible) { - return sensible ? getAnswer().equals(answer) : getAnswer().equalsIgnoreCase(answer); + public boolean checkAnswer(String answer) { + return getAnswer().equals(answer); } public static void saveTo(File file, List data) { try { OutputStreamWriter writer = new OutputStreamWriter( - new FileOutputStream(file), Charset.forName("UTF-8").newEncoder()); + new FileOutputStream(file), StandardCharsets.UTF_8.newEncoder()); for (TestItem item : data) { writer.write(item.getQuestion() + "::" + item.getAnswer() + "\n"); } diff --git a/src/main/resources/es/kauron/jstudy/view/main.fxml b/src/main/resources/es/kauron/jstudy/view/main.fxml index 5129bcd..639c55e 100644 --- a/src/main/resources/es/kauron/jstudy/view/main.fxml +++ b/src/main/resources/es/kauron/jstudy/view/main.fxml @@ -3,7 +3,7 @@ - +
@@ -102,7 +102,6 @@ - @@ -115,6 +114,19 @@ + + + + + + + + + + + + + diff --git a/src/main/resources/es/kauron/jstudy/view/settings.fxml b/src/main/resources/es/kauron/jstudy/view/settings.fxml index 0bcfb45..2940ba2 100644 --- a/src/main/resources/es/kauron/jstudy/view/settings.fxml +++ b/src/main/resources/es/kauron/jstudy/view/settings.fxml @@ -7,7 +7,7 @@ xmlns:fx="http://javafx.com/fxml/1" fx:controller="es.kauron.jstudy.controller.SettingsController"> - + diff --git a/src/main/resources/es/kauron/jstudy/view/table.fxml b/src/main/resources/es/kauron/jstudy/view/table.fxml index 892c3a8..0a447a1 100644 --- a/src/main/resources/es/kauron/jstudy/view/table.fxml +++ b/src/main/resources/es/kauron/jstudy/view/table.fxml @@ -62,7 +62,7 @@ - +