forked from dlsc-software-consulting-gmbh/PreferencesFX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryController.java
129 lines (114 loc) · 4.33 KB
/
CategoryController.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
package com.dlsc.preferencesfx.view;
import static com.dlsc.preferencesfx.util.Constants.SCROLLBAR_SUBTRACT;
import com.dlsc.preferencesfx.model.Category;
import java.util.HashMap;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.ScrollPane;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Acts as a proxy for the CategoryViews.
* Can be used to switch between different categories, which will be displayed in this view.
*
* @author François Martin
* @author Marco Sanfratello
*/
public class CategoryController extends ScrollPane {
private static final Logger LOGGER =
LoggerFactory.getLogger(CategoryController.class.getName());
private ObjectProperty<CategoryView> displayedCategoryView = new SimpleObjectProperty<>();
private ObjectProperty<CategoryPresenter> displayedCategoryPresenter =
new SimpleObjectProperty<>();
private HashMap<Category, CategoryView> views = new HashMap<>();
private HashMap<Category, CategoryPresenter> presenters = new HashMap<>();
/**
* Initializes the category controller.
*/
public CategoryController() {
// removes the border around the scrollpane
setStyle("-fx-background-color:transparent;");
}
/**
* Returns an already loaded presenter.
*
* @param category of the view / presenter
* @return presenter object
*/
public CategoryPresenter getPresenter(Category category) {
return presenters.get(category);
}
/**
* Adds a view / presenter pair to the respective HashMaps.
*
* @param category of the categoryView
* @param view the view to add to the map
* @param presenter the presenter to add to the map
*/
public void addView(Category category, CategoryView view, CategoryPresenter presenter) {
views.put(category, view);
presenters.put(category, presenter);
}
/**
* Sets the current view to the one of the respective category.
* This method can be called in the presenter to switch to a different CategoryView.
* Controls the way views are being transitioned from one to another.
*
* @param category of the categoryView that will be set
* @return true if successfully loaded, false if view is nonexistent
*/
public boolean setView(final Category category) {
LOGGER.trace("CategoryController, setView: " + category);
CategoryView categoryView = views.get(category);
if (categoryView != null) { // view is loaded
setContent(categoryView);
// Binding for ScrollPane
categoryView.minWidthProperty().bind(widthProperty().subtract(SCROLLBAR_SUBTRACT));
displayedCategoryView.setValue(categoryView);
displayedCategoryPresenter.setValue(getPresenter(category));
return true;
} else {
LOGGER.info("Category " + category.getDescription() + " hasn't been loaded!");
return false;
}
}
/**
* Unloads a view / presenter pair from the HashMaps.
* This method can be used in case a view / presenter pair needs to be reloaded.
*
* @param category of the view / presenter pair to be unloaded.
* @return true if the view and presenter were unloaded and false if view doesn't exist.
*/
public boolean unloadView(Category category) {
if (views.remove(category) == null | presenters.remove(category) == null) {
LOGGER.info("Category " + category.getDescription() + " doesn't exist!");
return false;
} else {
return true;
}
}
/**
* Sets the view according to the current category in categoryProperty.
* Must ensure that the category is already loaded, else it will fail.
*
* @param categoryProperty with category to be listened for
*/
public void addListener(ReadOnlyObjectProperty<Category> categoryProperty) {
categoryProperty.addListener((observable, oldCategory, newCategory) -> {
setView(newCategory);
});
}
public CategoryView getDisplayedCategoryView() {
return displayedCategoryView.get();
}
public ReadOnlyObjectProperty<CategoryView> displayedCategoryViewProperty() {
return displayedCategoryView;
}
public CategoryPresenter getDisplayedCategoryPresenter() {
return displayedCategoryPresenter.get();
}
public ReadOnlyObjectProperty<CategoryPresenter> displayedCategoryPresenterProperty() {
return displayedCategoryPresenter;
}
}