Updated the search & ordering on the PracticeList
This commit is contained in:
parent
e523f0f21a
commit
2c7f4609c5
1 changed files with 196 additions and 185 deletions
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* @(#) DictionaryController.java 0,1 2020/04/07
|
||||
* @(#) PracticeListController.java 0.2 2020/04/30
|
||||
* <p>
|
||||
* Copyright (c) 2020 Aberystwyth University.
|
||||
* All rights reserved.
|
||||
|
@ -11,16 +11,15 @@ import javafx.collections.FXCollections;
|
|||
import javafx.collections.ObservableList;
|
||||
import javafx.collections.transformation.FilteredList;
|
||||
import javafx.collections.transformation.SortedList;
|
||||
import javafx.css.converter.DurationConverter;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.stage.Stage;
|
||||
import uk.ac.aber.cs22120.group20.json.DictionaryEntry;
|
||||
|
||||
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
|
||||
|
@ -38,207 +37,219 @@ import java.util.ArrayList;
|
|||
* @see DictionaryEntry
|
||||
* @see Application
|
||||
*/
|
||||
public class PracticeListController extends SharedCodeController{
|
||||
public static Stage primaryStage = null;
|
||||
public class PracticeListController extends SharedCodeController {
|
||||
|
||||
@FXML
|
||||
private ImageView alphaSort;
|
||||
@FXML
|
||||
private ImageView langSort;
|
||||
@FXML
|
||||
private TextField searchBox;
|
||||
@FXML
|
||||
private TableView<DictionaryEntry> table;
|
||||
@FXML
|
||||
private TableColumn<DictionaryEntry, String> english = new TableColumn<>();
|
||||
@FXML
|
||||
private TableColumn<DictionaryEntry, String> welsh = new TableColumn<>();
|
||||
@FXML
|
||||
private ImageView alphaSort;
|
||||
@FXML
|
||||
private ImageView langSort;
|
||||
@FXML
|
||||
private TextField searchBox;
|
||||
@FXML
|
||||
private TableView<DictionaryEntry> table;
|
||||
@FXML
|
||||
private TableColumn<DictionaryEntry, String> english = new TableColumn<>();
|
||||
@FXML
|
||||
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.
|
||||
* 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();
|
||||
currentPageIcon.setImage(new Image("file:src/main/resources/assets/icons/white_icons/50px/rating-50.png"));
|
||||
currentPageText.setText("Practice List");
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@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();
|
||||
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"));
|
||||
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-50.png"));
|
||||
|
||||
// list.addAll(Application.dictionary);
|
||||
list.addAll(Application.practiceList);
|
||||
// for (DictionaryEntry entry : Application.dictionary) {
|
||||
// if (entry.isPracticeWord())
|
||||
// list.add(entry);
|
||||
// }
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@FXML
|
||||
private void switchAlphaSort() {
|
||||
if (isSortedByEnglish) {
|
||||
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 (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) -> {
|
||||
filteredList.setPredicate(dictionaryEntry -> { // returns true on a filter match, false if no match
|
||||
boolean result = false;
|
||||
english.setComparator(new Comparator<String>() {
|
||||
@Override
|
||||
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
|
||||
result = true;
|
||||
} else {
|
||||
// need all same case for compare.
|
||||
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
|
||||
if (dictionaryEntry.getWelsh().toLowerCase().contains(lowerCaseSearchFilter)) {
|
||||
result = true; // Filter matches Welsh
|
||||
} else if (dictionaryEntry.getEnglish().toLowerCase().contains(lowerCaseSearchFilter)) {
|
||||
result = true; // Filter matches English
|
||||
// } else if (dictionaryEntry.getWordType().toLowerCase().contains(lowerCaseSearchFilter)) {
|
||||
// result = true; // Filter matches Word Type
|
||||
} else if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().contains(lowerCaseSearchFilter)) {
|
||||
result = true; // Filter matches ['to' + a word] or [a word] if word is a verb
|
||||
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
|
||||
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-50.png"));
|
||||
|
||||
practiceListIcon.setImage(new Image(getClass().getResourceAsStream("/assets/icons/black_icons/50px/rating-50.png")));
|
||||
practiceListTest.setFill(Color.BLACK);
|
||||
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<DictionaryEntry> row = new TableRow<DictionaryEntry>() {
|
||||
@Override
|
||||
protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) {
|
||||
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
|
||||
sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator
|
||||
// welsh.setCellValueFactory(new PropertyValueFactory<DictionaryEntry, String>("welsh"));
|
||||
// english.setCellValueFactory(new PropertyValueFactory<DictionaryEntry, String>("english"));
|
||||
english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
||||
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
|
||||
} 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."));
|
||||
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();
|
||||
}
|
||||
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList
|
||||
|
||||
}
|
||||
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>();
|
||||
for (DictionaryEntry entry : Application.practiceList) {
|
||||
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}");
|
||||
if (newSearchTerm == null || newSearchTerm.isEmpty()) { // If filter text is empty, display all dictionary entries
|
||||
result = true;
|
||||
} 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 -> {
|
||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
||||
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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
SortedList<DictionaryEntry> 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
|
||||
|
||||
table.setItems(sortedList);
|
||||
|
||||
if(isSortedByEnglish){
|
||||
table.getSortOrder().add(english);
|
||||
} else{
|
||||
table.getSortOrder().add(welsh);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue