forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateManager.java
264 lines (215 loc) · 10.4 KB
/
StateManager.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package org.jabref.gui;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyListProperty;
import javafx.beans.property.ReadOnlyListWrapper;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.concurrent.Task;
import javafx.scene.Node;
import javafx.util.Pair;
import org.jabref.gui.edit.automaticfiededitor.LastAutomaticFieldEditorEdit;
import org.jabref.gui.sidepane.SidePaneType;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.DefaultDirectoryMonitor;
import org.jabref.gui.util.DialogWindowState;
import org.jabref.gui.util.OptionalObjectProperty;
import org.jabref.logic.search.SearchQuery;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.groups.GroupTreeNode;
import com.tobiasdiez.easybind.EasyBind;
import com.tobiasdiez.easybind.EasyBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class manages the GUI-state of JabRef, including:
*
* <ul>
* <li>currently selected database</li>
* <li>currently selected group</li>
* <li>active search</li>
* <li>active number of search results</li>
* <li>focus owner</li>
* <li>dialog window sizes/positions</li>
* </ul>
*/
public class StateManager {
private static final Logger LOGGER = LoggerFactory.getLogger(StateManager.class);
private final CustomLocalDragboard localDragboard = new CustomLocalDragboard();
private final ObservableList<BibDatabaseContext> openDatabases = FXCollections.observableArrayList();
private final OptionalObjectProperty<BibDatabaseContext> activeDatabase = OptionalObjectProperty.empty();
private final OptionalObjectProperty<LibraryTab> activeTab = OptionalObjectProperty.empty();
private final ReadOnlyListWrapper<GroupTreeNode> activeGroups = new ReadOnlyListWrapper<>(FXCollections.observableArrayList());
private final ObservableList<BibEntry> selectedEntries = FXCollections.observableArrayList();
private final ObservableMap<BibDatabaseContext, ObservableList<GroupTreeNode>> selectedGroups = FXCollections.observableHashMap();
private final OptionalObjectProperty<SearchQuery> activeSearchQuery = OptionalObjectProperty.empty();
private final OptionalObjectProperty<SearchQuery> activeGlobalSearchQuery = OptionalObjectProperty.empty();
private final IntegerProperty globalSearchResultSize = new SimpleIntegerProperty(0);
private final ObservableMap<BibDatabaseContext, IntegerProperty> searchResultMap = FXCollections.observableHashMap();
private final OptionalObjectProperty<Node> focusOwner = OptionalObjectProperty.empty();
private final ObservableList<Pair<BackgroundTask<?>, Task<?>>> backgroundTasks = FXCollections.observableArrayList(task -> new Observable[]{task.getValue().progressProperty(), task.getValue().runningProperty()});
private final EasyBinding<Boolean> anyTaskRunning = EasyBind.reduce(backgroundTasks, tasks -> tasks.map(Pair::getValue).anyMatch(Task::isRunning));
private final EasyBinding<Boolean> anyTasksThatWillNotBeRecoveredRunning = EasyBind.reduce(backgroundTasks, tasks -> tasks.anyMatch(task -> !task.getKey().willBeRecoveredAutomatically() && task.getValue().isRunning()));
private final EasyBinding<Double> tasksProgress = EasyBind.reduce(backgroundTasks, tasks -> tasks.map(Pair::getValue).filter(Task::isRunning).mapToDouble(Task::getProgress).average().orElse(1));
private final ObservableMap<String, DialogWindowState> dialogWindowStates = FXCollections.observableHashMap();
private final ObservableList<SidePaneType> visibleSidePanes = FXCollections.observableArrayList();
private final ObjectProperty<LastAutomaticFieldEditorEdit> lastAutomaticFieldEditorEdit = new SimpleObjectProperty<>();
private final ObservableList<String> searchHistory = FXCollections.observableArrayList();
private final DefaultDirectoryMonitor directoryMonitor = new DefaultDirectoryMonitor();
public StateManager() {
activeGroups.bind(Bindings.valueAt(selectedGroups, activeDatabase.orElseOpt(null)));
}
public ObservableList<SidePaneType> getVisibleSidePaneComponents() {
return visibleSidePanes;
}
public CustomLocalDragboard getLocalDragboard() {
return localDragboard;
}
public ObservableList<BibDatabaseContext> getOpenDatabases() {
return openDatabases;
}
public OptionalObjectProperty<BibDatabaseContext> activeDatabaseProperty() {
return activeDatabase;
}
public OptionalObjectProperty<LibraryTab> activeTabProperty() {
return activeTab;
}
public OptionalObjectProperty<SearchQuery> activeSearchQueryProperty() {
return activeSearchQuery;
}
public void setActiveSearchResultSize(BibDatabaseContext database, IntegerProperty resultSize) {
searchResultMap.put(database, resultSize);
}
public IntegerProperty getSearchResultSize() {
return searchResultMap.getOrDefault(activeDatabase.getValue().orElse(new BibDatabaseContext()), new SimpleIntegerProperty(0));
}
public OptionalObjectProperty<SearchQuery> activeGlobalSearchQueryProperty() {
return activeGlobalSearchQuery;
}
public IntegerProperty getGlobalSearchResultSize() {
return globalSearchResultSize;
}
public IntegerProperty getSearchResultSize(OptionalObjectProperty<SearchQuery> searchQueryProperty) {
if (searchQueryProperty.equals(activeSearchQuery)) {
return getSearchResultSize();
} else {
return getGlobalSearchResultSize();
}
}
public ReadOnlyListProperty<GroupTreeNode> activeGroupProperty() {
return activeGroups.getReadOnlyProperty();
}
public ObservableList<BibEntry> getSelectedEntries() {
return selectedEntries;
}
public void setSelectedEntries(List<BibEntry> newSelectedEntries) {
selectedEntries.setAll(newSelectedEntries);
}
public void setSelectedGroups(BibDatabaseContext database, List<GroupTreeNode> newSelectedGroups) {
Objects.requireNonNull(newSelectedGroups);
selectedGroups.put(database, FXCollections.observableArrayList(newSelectedGroups));
}
public ObservableList<GroupTreeNode> getSelectedGroup(BibDatabaseContext database) {
ObservableList<GroupTreeNode> selectedGroupsForDatabase = selectedGroups.get(database);
return selectedGroupsForDatabase != null ? selectedGroupsForDatabase : FXCollections.observableArrayList();
}
public void clearSelectedGroups(BibDatabaseContext database) {
selectedGroups.remove(database);
}
public Optional<BibDatabaseContext> getActiveDatabase() {
return activeDatabase.get();
}
public void setActiveDatabase(BibDatabaseContext database) {
if (database == null) {
LOGGER.info("No open database detected");
activeDatabaseProperty().set(Optional.empty());
} else {
activeDatabaseProperty().set(Optional.of(database));
}
}
public void clearSearchQuery() {
activeSearchQuery.setValue(Optional.empty());
}
public void setSearchQuery(OptionalObjectProperty<SearchQuery> searchQueryProperty, SearchQuery query) {
searchQueryProperty.setValue(Optional.of(query));
}
public void clearSearchQuery(OptionalObjectProperty<SearchQuery> searchQueryProperty) {
searchQueryProperty.setValue(Optional.empty());
}
public OptionalObjectProperty<Node> focusOwnerProperty() {
return focusOwner;
}
public Optional<Node> getFocusOwner() {
return focusOwner.get();
}
public ObservableList<Task<?>> getBackgroundTasks() {
return EasyBind.map(backgroundTasks, Pair::getValue);
}
public void addBackgroundTask(BackgroundTask<?> backgroundTask, Task<?> task) {
this.backgroundTasks.addFirst(new Pair<>(backgroundTask, task));
}
public EasyBinding<Boolean> getAnyTaskRunning() {
return anyTaskRunning;
}
public EasyBinding<Boolean> getAnyTasksThatWillNotBeRecoveredRunning() {
return anyTasksThatWillNotBeRecoveredRunning;
}
public EasyBinding<Double> getTasksProgress() {
return tasksProgress;
}
public DialogWindowState getDialogWindowState(String className) {
return dialogWindowStates.get(className);
}
public void setDialogWindowState(String className, DialogWindowState state) {
dialogWindowStates.put(className, state);
}
public ObjectProperty<LastAutomaticFieldEditorEdit> lastAutomaticFieldEditorEditProperty() {
return lastAutomaticFieldEditorEdit;
}
public LastAutomaticFieldEditorEdit getLastAutomaticFieldEditorEdit() {
return lastAutomaticFieldEditorEditProperty().get();
}
public void setLastAutomaticFieldEditorEdit(LastAutomaticFieldEditorEdit automaticFieldEditorEdit) {
lastAutomaticFieldEditorEditProperty().set(automaticFieldEditorEdit);
}
public List<String> collectAllDatabasePaths() {
List<String> list = new ArrayList<>();
getOpenDatabases().stream()
.map(BibDatabaseContext::getDatabasePath)
.forEachOrdered(pathOptional -> pathOptional.ifPresentOrElse(
path -> list.add(path.toAbsolutePath().toString()),
() -> list.add("")));
return list;
}
public void addSearchHistory(String search) {
searchHistory.remove(search);
searchHistory.add(search);
}
public ObservableList<String> getWholeSearchHistory() {
return searchHistory;
}
public List<String> getLastSearchHistory(int size) {
int sizeSearches = searchHistory.size();
if (size < sizeSearches) {
return searchHistory.subList(sizeSearches - size, sizeSearches);
}
return searchHistory;
}
public void clearSearchHistory() {
searchHistory.clear();
}
public DefaultDirectoryMonitor getDirectoryMonitor() {
return directoryMonitor;
}
}