From 2a11c4a1fedec65a4a93192e70d016e598e8fec2 Mon Sep 17 00:00:00 2001 From: osp1 Date: Fri, 1 May 2020 14:39:31 +0100 Subject: [PATCH] Commenting for Dictionary and Practice List Issue #47 Issue #37 --- .../group20/javafx/DictionaryController.java | 56 +++++++++++++---- .../javafx/PracticeListController.java | 60 ++++++++++++++----- 2 files changed, 89 insertions(+), 27 deletions(-) diff --git a/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/DictionaryController.java b/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/DictionaryController.java index 66cfb33..41e1313 100644 --- a/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/DictionaryController.java +++ b/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/DictionaryController.java @@ -46,6 +46,10 @@ import java.util.Comparator; */ public class DictionaryController extends SharedCodeController { + // /////////////////// // + // Instance variables. // + // /////////////////// // + @FXML private ImageView alphaSort; @FXML @@ -61,6 +65,10 @@ public class DictionaryController extends SharedCodeController { public ObservableList list = FXCollections.observableArrayList(); + // //////// // + // Methods. // + // //////// // + /** * Method to switch the language used to sort the dictionary list. *

@@ -138,6 +146,8 @@ public class DictionaryController extends SharedCodeController { */ public void initialize() { setup(); + + // Hide TableViewHeader table.widthProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Number number, Number t1) { @@ -151,6 +161,8 @@ public class DictionaryController extends SharedCodeController { } } }); + + // Compare words while ignoring the "to" english.setComparator(new Comparator() { @Override public int compare(String s, String t1) { @@ -166,7 +178,7 @@ public class DictionaryController extends SharedCodeController { } }); - currentPageIcon.setImage(new Image(getClass().getResourceAsStream("/assets/icons/white_icons/50px/read-50.png"))); + currentPageIcon.setImage(new Image(getClass().getResourceAsStream("/assets/icons/white_icons/50px/read-50.png"))); currentPageText.setText("Dictionary"); alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png")); @@ -190,6 +202,8 @@ public class DictionaryController extends SharedCodeController { } } }; + + // Unsets practice word when they are clicked on row.setOnMouseClicked(mouseEvent -> { if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) { if (row.getItem().isPracticeWord()) { @@ -201,11 +215,9 @@ public class DictionaryController extends SharedCodeController { } } Application.practiceList.removeAll(toRemove); -// row.getItem().setPracticeWord(false); } else if (!row.getItem().isPracticeWord()) { Application.dictionary.get(list.indexOf(row.getItem())).setPracticeWord(true); Application.practiceList.add(row.getItem()); -// row.getItem().setPracticeWord(true); } table.getSelectionModel().clearSelection(); } @@ -214,6 +226,7 @@ public class DictionaryController extends SharedCodeController { } ); + // Render Welsh nouns as "[masculine noun] {nm}" and "[feminine noun] {nm}" respectively. welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> { if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) { return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nm}"); @@ -224,6 +237,7 @@ public class DictionaryController extends SharedCodeController { } }); + // Render English verbs as "to [verb]" english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> { if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) { return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish()); @@ -232,31 +246,41 @@ public class DictionaryController extends SharedCodeController { } }); - FilteredList filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList + // Wrap list in a FilteredList + FilteredList filteredList = new FilteredList<>(list, p -> true); searchBox.textProperty().addListener((observable, oldSearchTerm, newSearchTerm) -> { - filteredList.setPredicate(dictionaryEntry -> { // returns true on a filter match, false if no match - boolean result = false; - table.refresh(); // This fixes the table highlighting issue - if (newSearchTerm == null || newSearchTerm.isEmpty()) { // If filter text is empty, display all dictionary entries + // returns true on a filter match, false if no match + filteredList.setPredicate(dictionaryEntry -> { + boolean result = false; + + // This fixes the table highlighting issue + table.refresh(); + + // If filter text is empty, display all dictionary entries + if (newSearchTerm == null || newSearchTerm.isEmpty()) { result = true; } else { // need all same case for compare. final String lowerCaseSearchFilter = newSearchTerm.toLowerCase(); if (isSortedByEnglish) { if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches English + + // Filter matches English + result = true; } else if(lowerCaseSearchFilter.startsWith("to ")){ if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches ['to' + a word] or [a word] if word is a verb + // Filter matches ['to' + a word] or [a word] if word is a verb + result = true; } } } else { if (dictionaryEntry.getWelsh().toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches Welsh + // Filter matches Welsh + result = true; } } } @@ -264,15 +288,21 @@ public class DictionaryController extends SharedCodeController { }); }); - SortedList sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList - sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator + //Wrap the filtered list in a SortedList + SortedList sortedList = new SortedList<>(filteredList); + + //Bind the sorted list comparator to the table comparator + sortedList.comparatorProperty().bind(table.comparatorProperty()); table.setItems(sortedList); + // Change Table sorting based on boolean value if(isSortedByEnglish){ table.getSortOrder().add(english); + langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-eng-50.png")); } else{ table.getSortOrder().add(welsh); + langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-welsh-50.png")); } } diff --git a/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/PracticeListController.java b/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/PracticeListController.java index 42d4149..907ea40 100644 --- a/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/PracticeListController.java +++ b/src/Welsh Vocabulary Tutor/src/main/java/uk/ac/aber/cs221/group20/javafx/PracticeListController.java @@ -42,6 +42,10 @@ import java.util.Comparator; */ public class PracticeListController extends SharedCodeController { + // /////////////////// // + // Instance variables. // + // /////////////////// // + @FXML private ImageView alphaSort; @FXML @@ -57,6 +61,10 @@ public class PracticeListController extends SharedCodeController { public ObservableList list = FXCollections.observableArrayList(); + // //////// // + // Methods. // + // //////// // + /** * Method to switch the language used to sort the dictionary list. *

@@ -132,6 +140,8 @@ public class PracticeListController extends SharedCodeController { */ public void initialize() { setup(); + + // Hide TableViewHeader table.widthProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Number number, Number t1) { @@ -146,6 +156,7 @@ public class PracticeListController extends SharedCodeController { } }); + // Compare words while ignoring the "to" english.setComparator(new Comparator() { @Override public int compare(String s, String t1) { @@ -172,20 +183,19 @@ public class PracticeListController extends SharedCodeController { list.addAll(Application.practiceList); table.setPlaceholder(new Label("No practice words found. Please try adding a practice word from the 'Dictionary' page.")); + table.setRowFactory(tv -> { TableRow row = new TableRow() { @Override protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) { super.updateItem(dictionaryEntry, b); if (!isEmpty()) { -// if (dictionaryEntry.isPracticeWord()) { -// setStyle("-fx-background-color: gray;"); -// } else { setStyle(" "); -// } } } }; + + // Remove practice word when they are clicked on row.setOnMouseClicked(mouseEvent -> { if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) { for (DictionaryEntry entry : Application.dictionary) { @@ -212,6 +222,8 @@ public class PracticeListController extends SharedCodeController { return row; } ); + + // Render Welsh nouns as "[masculine noun] {nm}" and "[feminine noun] {nm}" respectively. welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> { if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) { return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nm}"); @@ -222,6 +234,7 @@ public class PracticeListController extends SharedCodeController { } }); + // Render English verbs as "to [verb]" english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> { if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) { return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish()); @@ -230,29 +243,42 @@ public class PracticeListController extends SharedCodeController { } }); - FilteredList filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList + // Wrap list in a FilteredList + FilteredList filteredList = new FilteredList<>(list, p -> true); searchBox.textProperty().addListener((observable, oldSearchTerm, newSearchTerm) -> { - filteredList.setPredicate(dictionaryEntry -> { // returns true on a filter match, false if no match - boolean result = false; - table.refresh(); // This fixes the table highlighting issue - if (newSearchTerm == null || newSearchTerm.isEmpty()) { // If filter text is empty, display all dictionary entries + // Returns true on a filter match, false if no match + filteredList.setPredicate(dictionaryEntry -> { + boolean result = false; + + // This fixes the table highlighting issue + table.refresh(); + + // If filter text is empty, display all dictionary entries + if (newSearchTerm == null || newSearchTerm.isEmpty()) { result = true; } else { + // need all same case for compare. final String lowerCaseSearchFilter = newSearchTerm.toLowerCase(); if (isSortedByEnglish) { if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches English + + // Filter matches English + result = true; } else if (lowerCaseSearchFilter.startsWith("to ")) { if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches ['to' + a word] or [a word] if word is a verb + + // Filter matches ['to' + a word] or [a word] if word is a verb + result = true; } } } else { if (dictionaryEntry.getWelsh().toLowerCase().startsWith(lowerCaseSearchFilter)) { - result = true; // Filter matches Welsh + + // Filter matches Welsh + result = true; } } } @@ -260,15 +286,21 @@ public class PracticeListController extends SharedCodeController { }); }); - SortedList sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList - sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator + // Wrap the filtered list in a SortedList + SortedList sortedList = new SortedList<>(filteredList); + + // Bind the sorted list comparator to the table comparator + sortedList.comparatorProperty().bind(table.comparatorProperty()); table.setItems(sortedList); + // Change Table sorting based on boolean value if (isSortedByEnglish) { table.getSortOrder().add(english); + langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-eng-50.png")); } else { table.getSortOrder().add(welsh); + langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-welsh-50.png")); } }