forked from dlsc-software-consulting-gmbh/PreferencesFX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NavigationPresenter.java
170 lines (150 loc) · 4.86 KB
/
NavigationPresenter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.dlsc.preferencesfx.view;
import com.dlsc.preferencesfx.model.Category;
import com.dlsc.preferencesfx.model.PreferencesFxModel;
import com.dlsc.preferencesfx.util.SearchHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import javafx.application.Platform;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Contains presenter logic of the {@link NavigationView}.
*
* @author François Martin
* @author Marco Sanfratello
*/
public class NavigationPresenter implements Presenter {
private static final Logger LOGGER =
LoggerFactory.getLogger(NavigationPresenter.class.getName());
private PreferencesFxModel model;
private SearchHandler searchHandler;
private NavigationView navigationView;
private HashMap<Category, FilterableTreeItem<Category>> categoryTreeItemMap = new HashMap<>();
/**
* Constructs a new presenter for the {@link NavigationView}.
*
* @param model the model of PreferencesFX
* @param navigationView corresponding view to this presenter
*/
public NavigationPresenter(PreferencesFxModel model, NavigationView navigationView) {
this.model = model;
searchHandler = model.getSearchHandler();
this.navigationView = navigationView;
init();
}
/**
* {@inheritDoc}
*/
@Override
public void initializeViewParts() {
initializeTreeItems();
setSelectedCategory(model.getDisplayedCategory());
searchHandler.init(
model,
navigationView.searchFld.textProperty(),
navigationView.rootItem.predicateProperty()
);
setupCellValueFactory();
}
private void initializeTreeItems() {
addRecursive(navigationView.rootItem, model.getCategories());
}
/**
* {@inheritDoc}
*/
@Override
public void setupEventHandlers() {
}
/**
* {@inheritDoc}
*/
@Override
public void setupValueChangedListeners() {
// Update displayed category upon selection
navigationView.treeView.getSelectionModel().selectedItemProperty().addListener(
(observable, oldTreeItem, newTreeItem) -> {
if (newTreeItem != null) {
model.setDisplayedCategory(newTreeItem.getValue());
}
}
);
// Update selected category upon search changes
searchHandler.categoryMatchProperty().addListener(
(observable, oldCategory, newCategory) -> {
LOGGER.trace(
String.format("Category match changed! oldCategory: %s newCategory: %s selecting:"
+ " %s", oldCategory, newCategory, categoryTreeItemMap.get(newCategory)));
navigationView.treeView.getSelectionModel().select(
categoryTreeItemMap.get(newCategory)
);
}
);
// Listens when a breadcrumb was clicked or the last selected category is loaded
model.displayedCategoryProperty().addListener((observable, oldValue, newValue) ->
setSelectedCategory(newValue)
);
}
/**
* {@inheritDoc}
*/
@Override
public void setupBindings() {
}
private void addRecursive(FilterableTreeItem<Category> treeItem, List<Category> categories) {
for (Category category : categories) {
FilterableTreeItem<Category> item = new FilterableTreeItem<>(category);
if (category.isExpand()) {
item.setExpanded(true);
}
// If there are subcategories, add them recursively.
if (category.getChildren() != null) {
addRecursive(item, category.getChildren());
}
treeItem.add(item);
categoryTreeItemMap.put(category, item);
}
}
/**
* Retrieves the currently selected category in the TreeSearchView.
*
* @return the currently selected category
*/
public Category getSelectedCategory() {
TreeItem<Category> selectedTreeItem =
navigationView.treeView.getSelectionModel().getSelectedItem();
if (selectedTreeItem != null) {
return navigationView.treeView.getSelectionModel().getSelectedItem().getValue();
}
return null;
}
/**
* Selects the given category in the NavigationView.
*
* @param category to be selected
*/
public void setSelectedCategory(Category category) {
navigationView.setSelectedItem(categoryTreeItemMap.get(category));
}
/**
* Makes the TreeItems' text update when the description of a Category changes (due to i18n).
*/
public void setupCellValueFactory() {
navigationView.treeView.setCellFactory(param -> new TreeCell<Category>() {
@Override
protected void updateItem(Category category, boolean empty) {
super.updateItem(category, empty);
textProperty().unbind();
if (empty || category == null) {
setText(null);
setGraphic(null);
} else {
textProperty().bind(category.descriptionProperty());
setGraphic(category.getItemIcon());
}
}
});
}
}