forked from dlsc-software-consulting-gmbh/PreferencesFX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group.java
146 lines (128 loc) · 4.3 KB
/
Group.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
package com.dlsc.preferencesfx.model;
import com.dlsc.preferencesfx.formsfx.view.renderer.PreferencesFxGroup;
import com.dlsc.preferencesfx.util.Constants;
import java.util.Arrays;
import java.util.List;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents a group, which is used to structure one to multiple settings in a category.
*
* @author François Martin
* @author Marco Sanfratello
*/
public class Group {
private static final Logger LOGGER =
LoggerFactory.getLogger(Group.class.getName());
private static final String MARKED_STYLE_CLASS = "group-marked";
private String description;
private List<Setting> settings;
private PreferencesFxGroup preferencesGroup;
private boolean marked = false;
private final EventHandler<MouseEvent> unmarker = event -> unmark();
private final StringProperty breadcrumb = new SimpleStringProperty("");
private Group(String description, Setting... settings) {
this.description = description;
this.settings = Arrays.asList(settings);
}
/**
* Constructs a new group with a {@code description} and {@code settings}.
*
* @param description the title of this group
* @param settings the settings that belong to this group
* @return this object for chaining with the fluent API
*/
public static Group of(String description, Setting... settings) {
return new Group(description, settings);
}
/**
* Constructs a new group with {@code settings}, without a {@code description}.
*
* @param settings the settings that belong to this group
* @return this object for chaining with the fluent API
*/
public static Group of(Setting... settings) {
return new Group(null, settings);
}
/**
* Sets a {@code description} for this group.
*
* @param description the title of this group
* @return this object for chaining with the fluent API
*/
public Group description(String description) {
this.description = description;
return this;
}
/**
* Returns the description of this group or if i18n is used, it will return the translated
* description in the current locale.
*
* @return the description
*/
public String getDescription() {
if (preferencesGroup != null) {
return preferencesGroup.getTitle();
}
return description;
}
public List<Setting> getSettings() {
return settings;
}
public PreferencesFxGroup getPreferencesGroup() {
return preferencesGroup;
}
public void setPreferencesGroup(PreferencesFxGroup preferencesGroup) {
this.preferencesGroup = preferencesGroup;
}
/**
* Marks this group in the GUI.
* Is used for the search, which marks and unmarks items depending on the match as a form of
* visual feedback.
*/
public void mark() {
// ensure it's not marked yet - so a control doesn't contain the same styleClass multiple times
if (!marked) {
preferencesGroup.getRenderer().addStyleClass(MARKED_STYLE_CLASS);
preferencesGroup.getRenderer().getTitleLabel().setOnMouseExited(unmarker);
marked = !marked;
}
}
/**
* Unmarks this group in the GUI.
* Is used for the search, which marks and unmarks items depending on the match as a form of
* visual feedback.
*/
public void unmark() {
// check if it's marked before removing the style class
if (marked) {
preferencesGroup.getRenderer().removeStyleClass(MARKED_STYLE_CLASS);
preferencesGroup.getRenderer().getTitleLabel().removeEventHandler(
MouseEvent.MOUSE_EXITED, unmarker
);
marked = !marked;
}
}
/**
* Adds the {@code breadCrumb} to this breadcrumb and updates all of its settings accordingly.
*
* @param breadCrumb the breadcrumb to add to this group's breadcrumb
*/
public void addToBreadcrumb(String breadCrumb) {
setBreadcrumb(breadCrumb + Constants.BREADCRUMB_DELIMITER + description);
settings.forEach(setting -> setting.addToBreadcrumb(getBreadcrumb()));
}
public String getBreadcrumb() {
return breadcrumb.get();
}
public StringProperty breadcrumbProperty() {
return breadcrumb;
}
public void setBreadcrumb(String breadcrumb) {
this.breadcrumb.set(breadcrumb);
}
}