parent
5d4ed5441b
commit
2a11c4a1fe
2 changed files with 89 additions and 27 deletions
|
@ -46,6 +46,10 @@ import java.util.Comparator;
|
||||||
*/
|
*/
|
||||||
public class DictionaryController extends SharedCodeController {
|
public class DictionaryController extends SharedCodeController {
|
||||||
|
|
||||||
|
// /////////////////// //
|
||||||
|
// Instance variables. //
|
||||||
|
// /////////////////// //
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ImageView alphaSort;
|
private ImageView alphaSort;
|
||||||
@FXML
|
@FXML
|
||||||
|
@ -61,6 +65,10 @@ public class DictionaryController extends SharedCodeController {
|
||||||
|
|
||||||
public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList();
|
public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList();
|
||||||
|
|
||||||
|
// //////// //
|
||||||
|
// Methods. //
|
||||||
|
// //////// //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to switch the language used to sort the dictionary list.
|
* Method to switch the language used to sort the dictionary list.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -138,6 +146,8 @@ public class DictionaryController extends SharedCodeController {
|
||||||
*/
|
*/
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
|
// Hide TableViewHeader
|
||||||
table.widthProperty().addListener(new ChangeListener<Number>() {
|
table.widthProperty().addListener(new ChangeListener<Number>() {
|
||||||
@Override
|
@Override
|
||||||
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
|
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
|
||||||
|
@ -151,6 +161,8 @@ public class DictionaryController extends SharedCodeController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Compare words while ignoring the "to"
|
||||||
english.setComparator(new Comparator<String>() {
|
english.setComparator(new Comparator<String>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(String s, String t1) {
|
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");
|
currentPageText.setText("Dictionary");
|
||||||
|
|
||||||
alphaSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-alpha-up-50.png"));
|
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 -> {
|
row.setOnMouseClicked(mouseEvent -> {
|
||||||
if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
|
if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
|
||||||
if (row.getItem().isPracticeWord()) {
|
if (row.getItem().isPracticeWord()) {
|
||||||
|
@ -201,11 +215,9 @@ public class DictionaryController extends SharedCodeController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Application.practiceList.removeAll(toRemove);
|
Application.practiceList.removeAll(toRemove);
|
||||||
// row.getItem().setPracticeWord(false);
|
|
||||||
} else if (!row.getItem().isPracticeWord()) {
|
} else if (!row.getItem().isPracticeWord()) {
|
||||||
Application.dictionary.get(list.indexOf(row.getItem())).setPracticeWord(true);
|
Application.dictionary.get(list.indexOf(row.getItem())).setPracticeWord(true);
|
||||||
Application.practiceList.add(row.getItem());
|
Application.practiceList.add(row.getItem());
|
||||||
// row.getItem().setPracticeWord(true);
|
|
||||||
}
|
}
|
||||||
table.getSelectionModel().clearSelection();
|
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 -> {
|
welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
||||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
|
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
|
||||||
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {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 -> {
|
english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
||||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
||||||
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
|
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
|
||||||
|
@ -232,31 +246,41 @@ public class DictionaryController extends SharedCodeController {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList
|
// Wrap list in a FilteredList
|
||||||
|
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true);
|
||||||
|
|
||||||
searchBox.textProperty().addListener((observable, oldSearchTerm, newSearchTerm) -> {
|
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;
|
result = true;
|
||||||
} else {
|
} else {
|
||||||
// need all same case for compare.
|
// need all same case for compare.
|
||||||
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
|
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
|
||||||
if (isSortedByEnglish) {
|
if (isSortedByEnglish) {
|
||||||
if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
||||||
result = true; // Filter matches English
|
|
||||||
|
// Filter matches English
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
else if(lowerCaseSearchFilter.startsWith("to ")){
|
else if(lowerCaseSearchFilter.startsWith("to ")){
|
||||||
if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
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 {
|
else {
|
||||||
if (dictionaryEntry.getWelsh().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
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<DictionaryEntry> sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList
|
//Wrap the filtered list in a SortedList
|
||||||
sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator
|
SortedList<DictionaryEntry> sortedList = new SortedList<>(filteredList);
|
||||||
|
|
||||||
|
//Bind the sorted list comparator to the table comparator
|
||||||
|
sortedList.comparatorProperty().bind(table.comparatorProperty());
|
||||||
|
|
||||||
table.setItems(sortedList);
|
table.setItems(sortedList);
|
||||||
|
|
||||||
|
// Change Table sorting based on boolean value
|
||||||
if(isSortedByEnglish){
|
if(isSortedByEnglish){
|
||||||
table.getSortOrder().add(english);
|
table.getSortOrder().add(english);
|
||||||
|
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-eng-50.png"));
|
||||||
} else{
|
} else{
|
||||||
table.getSortOrder().add(welsh);
|
table.getSortOrder().add(welsh);
|
||||||
|
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-welsh-50.png"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,10 @@ import java.util.Comparator;
|
||||||
*/
|
*/
|
||||||
public class PracticeListController extends SharedCodeController {
|
public class PracticeListController extends SharedCodeController {
|
||||||
|
|
||||||
|
// /////////////////// //
|
||||||
|
// Instance variables. //
|
||||||
|
// /////////////////// //
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private ImageView alphaSort;
|
private ImageView alphaSort;
|
||||||
@FXML
|
@FXML
|
||||||
|
@ -57,6 +61,10 @@ public class PracticeListController extends SharedCodeController {
|
||||||
|
|
||||||
public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList();
|
public ObservableList<DictionaryEntry> list = FXCollections.observableArrayList();
|
||||||
|
|
||||||
|
// //////// //
|
||||||
|
// Methods. //
|
||||||
|
// //////// //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method to switch the language used to sort the dictionary list.
|
* Method to switch the language used to sort the dictionary list.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -132,6 +140,8 @@ public class PracticeListController extends SharedCodeController {
|
||||||
*/
|
*/
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
|
// Hide TableViewHeader
|
||||||
table.widthProperty().addListener(new ChangeListener<Number>() {
|
table.widthProperty().addListener(new ChangeListener<Number>() {
|
||||||
@Override
|
@Override
|
||||||
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
|
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
|
||||||
|
@ -146,6 +156,7 @@ public class PracticeListController extends SharedCodeController {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Compare words while ignoring the "to"
|
||||||
english.setComparator(new Comparator<String>() {
|
english.setComparator(new Comparator<String>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(String s, String t1) {
|
public int compare(String s, String t1) {
|
||||||
|
@ -172,20 +183,19 @@ public class PracticeListController extends SharedCodeController {
|
||||||
list.addAll(Application.practiceList);
|
list.addAll(Application.practiceList);
|
||||||
|
|
||||||
table.setPlaceholder(new Label("No practice words found. Please try adding a practice word from the 'Dictionary' page."));
|
table.setPlaceholder(new Label("No practice words found. Please try adding a practice word from the 'Dictionary' page."));
|
||||||
|
|
||||||
table.setRowFactory(tv -> {
|
table.setRowFactory(tv -> {
|
||||||
TableRow<DictionaryEntry> row = new TableRow<DictionaryEntry>() {
|
TableRow<DictionaryEntry> row = new TableRow<DictionaryEntry>() {
|
||||||
@Override
|
@Override
|
||||||
protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) {
|
protected void updateItem(DictionaryEntry dictionaryEntry, boolean b) {
|
||||||
super.updateItem(dictionaryEntry, b);
|
super.updateItem(dictionaryEntry, b);
|
||||||
if (!isEmpty()) {
|
if (!isEmpty()) {
|
||||||
// if (dictionaryEntry.isPracticeWord()) {
|
|
||||||
// setStyle("-fx-background-color: gray;");
|
|
||||||
// } else {
|
|
||||||
setStyle(" ");
|
setStyle(" ");
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Remove practice word when they are clicked on
|
||||||
row.setOnMouseClicked(mouseEvent -> {
|
row.setOnMouseClicked(mouseEvent -> {
|
||||||
if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
|
if (mouseEvent.getClickCount() == 1 && (!row.isEmpty())) {
|
||||||
for (DictionaryEntry entry : Application.dictionary) {
|
for (DictionaryEntry entry : Application.dictionary) {
|
||||||
|
@ -212,6 +222,8 @@ public class PracticeListController extends SharedCodeController {
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Render Welsh nouns as "[masculine noun] {nm}" and "[feminine noun] {nm}" respectively.
|
||||||
welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
welsh.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
||||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
|
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.nm)) {
|
||||||
return new SimpleStringProperty(dictionaryEntryStringCellDataFeatures.getValue().getWelsh() + " {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 -> {
|
english.setCellValueFactory(dictionaryEntryStringCellDataFeatures -> {
|
||||||
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
if (dictionaryEntryStringCellDataFeatures.getValue().getWordType().equals(DictionaryEntry.wordTypeEnum.verb)) {
|
||||||
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
|
return new SimpleStringProperty("to " + dictionaryEntryStringCellDataFeatures.getValue().getEnglish());
|
||||||
|
@ -230,29 +243,42 @@ public class PracticeListController extends SharedCodeController {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true); // Wrap list in a FilteredList
|
// Wrap list in a FilteredList
|
||||||
|
FilteredList<DictionaryEntry> filteredList = new FilteredList<>(list, p -> true);
|
||||||
|
|
||||||
searchBox.textProperty().addListener((observable, oldSearchTerm, newSearchTerm) -> {
|
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;
|
result = true;
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// need all same case for compare.
|
// need all same case for compare.
|
||||||
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
|
final String lowerCaseSearchFilter = newSearchTerm.toLowerCase();
|
||||||
if (isSortedByEnglish) {
|
if (isSortedByEnglish) {
|
||||||
if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
if (dictionaryEntry.getEnglish().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
||||||
result = true; // Filter matches English
|
|
||||||
|
// Filter matches English
|
||||||
|
result = true;
|
||||||
} else if (lowerCaseSearchFilter.startsWith("to ")) {
|
} else if (lowerCaseSearchFilter.startsWith("to ")) {
|
||||||
if (dictionaryEntry.getWordType().equals(DictionaryEntry.wordTypeEnum.verb) && ("to " + dictionaryEntry.getEnglish()).toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
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 {
|
} else {
|
||||||
if (dictionaryEntry.getWelsh().toLowerCase().startsWith(lowerCaseSearchFilter)) {
|
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<DictionaryEntry> sortedList = new SortedList<>(filteredList); //Wrap the filtered list in a SortedList
|
// Wrap the filtered list in a SortedList
|
||||||
sortedList.comparatorProperty().bind(table.comparatorProperty()); //Bind the sorted list comparator to the table comparator
|
SortedList<DictionaryEntry> sortedList = new SortedList<>(filteredList);
|
||||||
|
|
||||||
|
// Bind the sorted list comparator to the table comparator
|
||||||
|
sortedList.comparatorProperty().bind(table.comparatorProperty());
|
||||||
|
|
||||||
table.setItems(sortedList);
|
table.setItems(sortedList);
|
||||||
|
|
||||||
|
// Change Table sorting based on boolean value
|
||||||
if (isSortedByEnglish) {
|
if (isSortedByEnglish) {
|
||||||
table.getSortOrder().add(english);
|
table.getSortOrder().add(english);
|
||||||
|
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-eng-50.png"));
|
||||||
} else {
|
} else {
|
||||||
table.getSortOrder().add(welsh);
|
table.getSortOrder().add(welsh);
|
||||||
|
langSort.setImage(new Image("file:src/main/resources/assets/icons/black_icons/50px/sort-lang-welsh-50.png"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue