Updated the search & ordering on the PracticeList

This commit is contained in:
osp1 2020-04-30 17:59:20 +01:00
parent e523f0f21a
commit 2c7f4609c5

View file

@ -1,5 +1,5 @@
/** /**
* @(#) DictionaryController.java 0,1 2020/04/07 * @(#) PracticeListController.java 0.2 2020/04/30
* <p> * <p>
* Copyright (c) 2020 Aberystwyth University. * Copyright (c) 2020 Aberystwyth University.
* All rights reserved. * All rights reserved.
@ -11,16 +11,15 @@ import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList; import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList; import javafx.collections.transformation.SortedList;
import javafx.css.converter.DurationConverter;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.image.ImageView; import javafx.scene.image.ImageView;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.stage.Stage;
import uk.ac.aber.cs22120.group20.json.DictionaryEntry; import uk.ac.aber.cs22120.group20.json.DictionaryEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
/** /**
* A class that handles the keyboard and mouse input and interaction for the 'Dictionary Page' which is * A class that handles the keyboard and mouse input and interaction for the 'Dictionary Page' which is
@ -38,207 +37,219 @@ import java.util.ArrayList;
* @see DictionaryEntry * @see DictionaryEntry
* @see Application * @see Application
*/ */
public class PracticeListController extends SharedCodeController{ public class PracticeListController extends SharedCodeController {
public static Stage primaryStage = null;
@FXML @FXML
private ImageView alphaSort; private ImageView alphaSort;
@FXML @FXML
private ImageView langSort; private ImageView langSort;
@FXML @FXML
private TextField searchBox; private TextField searchBox;
@FXML @FXML
private TableView<DictionaryEntry> table; private TableView<DictionaryEntry> table;
@FXML @FXML
private TableColumn<DictionaryEntry, String> english = new TableColumn<>(); private TableColumn<DictionaryEntry, String> english = new TableColumn<>();
@FXML @FXML
private TableColumn<DictionaryEntry, String> welsh = new TableColumn<>(); private TableColumn<DictionaryEntry, String> welsh = new TableColumn<>();
public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList(); public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList();
/** /**
* Initializes the table of dictionary entries. *
* <p> */
* An observable list of DictionaryEntries is loaded from the Application class into a local instance of ObservableList. @FXML
* It also sets up Lambda expressions related to live searching functionality and the display of DictionaryEntries. private void switchLangSort() {
* if (isSortedByEnglish) {
// * @param url if (welsh.getSortType().equals(TableColumn.SortType.ASCENDING)) {
// * @param resourceBundle alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
* @see Application }
* @see DictionaryEntry else if (welsh.getSortType().equals(TableColumn.SortType.DESCENDING)) {
*/ alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
public void initialize() { }
setup(); table.getSortOrder().clear();
currentPageIcon.setImage(new Image("file:src/main/resources/assets/icons/white_icons/50px/rating-50.png")); table.getSortOrder().add(welsh);
currentPageText.setText("Practice List"); isSortedByEnglish = false;
}
else {
if (english.getSortType().equals(TableColumn.SortType.ASCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
else if (english.getSortType().equals(TableColumn.SortType.DESCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
}
table.getSortOrder().clear();
table.getSortOrder().add(english);
isSortedByEnglish = true;
}
table.sort();
searchBox.textProperty().setValue(searchBox.textProperty().getValue() + " ");
searchBox.textProperty().setValue(searchBox.textProperty().getValue().substring(0, searchBox.textProperty().getValue().length()-1));
searchBox.positionCaret(searchBox.textProperty().getValue().length());
}
practiceListIcon.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/rating-50.png")); /**
practiceListTest.setFill(Color.BLACK); *
*/
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png")); @FXML
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-50.png")); private void switchAlphaSort() {
if (isSortedByEnglish) {
// list.addAll(Application.dictionary); if (english.getSortType().equals(TableColumn.SortType.ASCENDING)) {
list.addAll(Application.practiceList); english.setSortType(TableColumn.SortType.DESCENDING);
// for (DictionaryEntry entry : Application.dictionary) { alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
// if (entry.isPracticeWord()) } else {
// list.add(entry); english.setSortType(TableColumn.SortType.ASCENDING);
// } alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
} else {
if (welsh.getSortType().equals(TableColumn.SortType.ASCENDING)) {
welsh.setSortType(TableColumn.SortType.DESCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
} else {
welsh.setSortType(TableColumn.SortType.ASCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
}
}
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList /**
* Initializes the table of dictionary entries.
* <p>
* An observable list of DictionaryEntries is loaded from the Application class into a local instance of ObservableList.
* It also sets up Lambda expressions related to live searching functionality and the display of DictionaryEntries.
*
// * @param url
// * @param resourceBundle
* @see Application
* @see DictionaryEntry
*/
public void initialize() {
setup();
searchBox.textProperty().addListener((observable, oldSearchTerm, newSearchTerm) -> { english.setComparator(new Comparator<String>() {
filteredList.setPredicate(dictionaryEntry -> { // returns true on a filter match, false if no match @Override
boolean result = false; public int compare(String s, String t1) {
s = s.toLowerCase();
t1 = t1.toLowerCase();
if (s.startsWith("to ")) {
return s.substring(3).compareTo(t1);
}
if (t1.startsWith("to ")) {
return s.compareTo(t1.substring(3));
}
return s.compareTo(t1);
}
});
table.refresh(); // This fixes the table highlighting issue currentPageIcon.setImage(new Image(getClass().getResourceAsStream("/assets/icons/white_icons/50px/rating-50.png")));
currentPageText.setText("Practice List");
if (newSearchTerm == null || newSearchTerm.isEmpty()) { // If filter text is empty, display all dictionary entries alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
result = true; langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-50.png"));
} else {
// need all same case for compare. practiceListIcon.setImage(new Image(getClass().getResourceAsStream("/assets/icons/black_icons/50px/rating-50.png")));
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase(); practiceListTest.setFill(Color.BLACK);
if (dictionaryEntry.getWelsh().toLowerCase().contains(lowerCaseSearchFilter)) { list.addAll(Application.practiceList);
result = true; // Filter matches Welsh
} else if (dictionaryEntry.getEnglish().toLowerCase().contains(lowerCaseSearchFilter)) { table.setPlaceholder(new Label("No practice words found. Please try adding a practice word from the 'Dictionary' page."));
result = true; // Filter matches English table.setRowFactory(tv -> {
// } else if (dictionaryEntry.getWordType().toLowerCase().contains(lowerCaseSearchFilter)) { TableRow<DictionaryEntry> row = new TableRow<DictionaryEntry>() {
// result = true; // Filter matches Word Type @Override
} else if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().contains(lowerCaseSearchFilter)) { protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) {
result = true; // Filter matches ['to' + a word] or [a word] if word is a verb super.updateItem(dictionaryEntry, b);
if (!isEmpty()) {
// if (dictionaryEntry.isPracticeWord()) {
// setStyle("-fx-background-color: gray;");
// } else {
setStyle(" ");
// }
}
} }
} };
return result; row.setOnMouseClicked(mouseEvent -> {
}); if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
}); if (row.getItem().isPracticeWord()) {
Application.dictionary.get(list.indexOf(row.getItem())).setPracticeWord(false);
ArrayList<DictionaryEntry> toRemove = new ArrayList<DictionaryEntry>();
for (DictionaryEntry entry : Application.practiceList) {
if (entry.equals(row.getItem())) {
toRemove.add(entry);
}
}
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();
}
});
return row;
}
);
welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nm}");
} else if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nf)) {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nf}");
} else {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh());
}
});
SortedList<DictionaryEntry> sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
// welsh.setCellValueFactory(new PropertyValueFactory<DictionaryEntry, String>("welsh")); return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
// english.setCellValueFactory(new PropertyValueFactory<DictionaryEntry, String>("english")); } else {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
}
});
table.setPlaceholder(new Label("No practice words found. Please try adding a practice word from the 'Dictionary' page.")); FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList
table.setRowFactory(tv -> {
TableRow<DictionaryEntry> row = new TableRow<DictionaryEntry>() {
@Override
protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) {
super.updateItem(dictionaryEntry, b);
if (!isEmpty()) {
setStyle(" ");
// if (dictionaryEntry.isPracticeWord()) {
// setStyle("-fx-background-color: gray;");
// } else {
// setStyle(" ");
// }
}
}
};
row.setOnMouseClicked(mouseEvent -> {
if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
for (DictionaryEntry entry : Application.dictionary) {
if (entry.equals(row.getItem())) {
entry.setPracticeWord(false);
list.remove(row.getItem());
table.refresh();
}
} 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
ArrayList<DictionaryEntry> toRemove = new ArrayList<DictionaryEntry>(); if (newSearchTerm == null || newSearchTerm.isEmpty()) { // If filter text is empty, display all dictionary entries
for (DictionaryEntry entry : Application.practiceList) { result = true;
if (entry.equals(row.getItem())) {
toRemove.add(entry);
list.remove(row.getItem());
}
}
Application.practiceList.removeAll(toRemove);
table.getSelectionModel().clearSelection();
}
});
return row;
}
);
welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nm}");
} else if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nf)) {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {nf}");
} else { } else {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh()); // need all same case for compare.
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
if (isSortedByEnglish) {
if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) {
result = true; // Filter matches English
}
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
}
}
}
else {
if (dictionaryEntry.getWelsh().toLowerCase().startsWith(lowerCaseSearchFilter)) {
result = true; // Filter matches Welsh
}
}
} }
}); return result;
});
});
english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> { SortedList<DictionaryEntry> sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) { sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
} else {
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
}
});
table.setItems(sortedList);
if(isSortedByEnglish){
table.getSortOrder().add(english);
} else{
table.getSortOrder().add(welsh);
}
}
@FXML
private void switchLangSort() {
if (isSortedByEnglish) {
if (welsh.getSortType().equals(TableColumn.SortType.ASCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
else if (welsh.getSortType().equals(TableColumn.SortType.DESCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
}
table.getSortOrder().clear();
table.getSortOrder().add(welsh);
isSortedByEnglish = false;
}
else {
if (english.getSortType().equals(TableColumn.SortType.ASCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
else if (english.getSortType().equals(TableColumn.SortType.DESCENDING)) {
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
}
table.getSortOrder().clear();
table.getSortOrder().add(english);
isSortedByEnglish = true;
}
table.sort();
}
@FXML
private void switchAlphaSort() {
if (table.getSortOrder().contains(english)) {
if (english.getSortType().equals(TableColumn.SortType.ASCENDING)) {
english.setSortType(TableColumn.SortType.DESCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
} else {
english.setSortType(TableColumn.SortType.ASCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
} else if (table.getSortOrder().contains(welsh)) {
if (welsh.getSortType().equals(TableColumn.SortType.ASCENDING)) {
welsh.setSortType(TableColumn.SortType.DESCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-reversed-50.png"));
} else {
welsh.setSortType(TableColumn.SortType.ASCENDING);
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
}
}
}
table.setItems(sortedList);
if(isSortedByEnglish){
table.getSortOrder().add(english);
} else{
table.getSortOrder().add(welsh);
}
}
} }