-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathManageWorkController.java
More file actions
331 lines (261 loc) · 11.3 KB
/
ManageWorkController.java
File metadata and controls
331 lines (261 loc) · 11.3 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright 2019 doubleSlash Net Business GmbH
//
// This file is part of KeepTime.
// KeepTime is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package de.doubleslash.keeptime.view;
import java.time.LocalDateTime;
import javafx.scene.control.skin.ComboBoxListViewSkin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.doubleslash.keeptime.common.ColorHelper;
import de.doubleslash.keeptime.common.StyleUtils;
import de.doubleslash.keeptime.model.Model;
import de.doubleslash.keeptime.model.Project;
import de.doubleslash.keeptime.model.Work;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.util.StringConverter;
public class ManageWorkController {
private static final Logger LOG = LoggerFactory.getLogger(ManageWorkController.class);
private Model model;
@FXML
private GridPane grid;
@FXML
private DatePicker startDatePicker;
@FXML
private TimePickerComboBox startTimePicker;
@FXML
private TimePickerComboBox endTimePicker;
@FXML
private DatePicker endDatePicker;
@FXML
private TextArea noteTextArea;
@FXML
private ComboBox<Project> projectComboBox;
@FXML
private Label errorLabel;
private boolean comboChange;
private Project selectedProject;
private BooleanProperty isValidProperty = new SimpleBooleanProperty();
private FilteredList<Project> filteredList;
public void setModel(final Model model) {
this.model = model;
filteredList = new FilteredList<>(model.getSortedAvailableProjects());
projectComboBox.setItems(filteredList);
}
@FXML
private void initialize() {
setUpTimeRestriction();
setProjectUpComboBox();
Platform.runLater(() -> {
noteTextArea.end();
noteTextArea.requestFocus();
});
}
private void setUpTimeRestriction() {
BooleanBinding isValidBinding = Bindings.createBooleanBinding(() -> {
if (startTimePicker.getValue() == null || endTimePicker.getValue() == null
|| startDatePicker.getValue() == null || endDatePicker.getValue() == null) {
return false;
}
LocalDateTime start = LocalDateTime.of(startDatePicker.getValue(), startTimePicker.getValue());
LocalDateTime end = LocalDateTime.of(endDatePicker.getValue(), endTimePicker.getValue());
if (start.isAfter(end)) {
return false;
} else {
return true;
}
}, startTimePicker.valueProperty(), endTimePicker.valueProperty(), endDatePicker.valueProperty(),
startDatePicker.valueProperty());
this.isValidProperty.bind(isValidBinding);
errorLabel.textProperty().bind(Bindings.createStringBinding(() -> {
if (Boolean.TRUE.equals(isValidProperty.get())) {
return "";
} else {
return "startDate has to be before endDate";
}
}, isValidProperty));
}
private void setProjectUpComboBox() {
// color Dropdown Options
projectComboBox.setCellFactory(listView -> new ListCell<Project>() {
@Override
protected void updateItem(final Project project, final boolean empty) {
super.updateItem(project, empty);
if (project == null || empty) {
setGraphic(null);
} else {
setColor(this, model.hoverBackgroundColor.get());
setTextFill(project.getColor());
setText(project.getName());
setUnderline(project.isWork());
}
}
});
// set text of selected value
projectComboBox.setConverter(new StringConverter<Project>() {
@Override
public String toString(final Project project) {
if (project == null) {
return null;
} else {
return project.getName();
}
}
@Override
public Project fromString(final String string) {
// ignores String and gets selected Value of ComboBox
return projectComboBox.getValue();
}
});
// needs to set again because editable is ignored from fxml because of custom preselection of current Project
projectComboBox.setEditable(true);
projectComboBox.valueProperty()
.addListener((final ObservableValue<? extends Project> observable, final Project oldValue,
final Project newValue) -> {
if (newValue == null) {
return;
}
selectedProject = newValue;
comboChange = true;
// needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
Platform.runLater(() -> {
setTextColor(projectComboBox.getEditor(), newValue.getColor());
});
}
);
enableStrgA_combo();
projectComboBox.getEditor().textProperty().addListener(new ChangeListener<String>() {
boolean isValidProject = true;
@Override
public void changed(final ObservableValue<? extends String> observable, final String oldValue,
final String newValue) {
// ignore selectionChanges
if (comboChange) {
comboChange = false;
isValidProject = true;
return;
}
// is necessary to not autoselect same Project if Project was selected
if (isValidProject) {
isValidProject = false;
projectComboBox.getSelectionModel().clearSelection();
// needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
Platform.runLater(() -> {
setTextColor(projectComboBox.getEditor(), model.hoverFontColor.get());
});
}
// needed to avoid exception on empty textfield https://bugs.openjdk.java.net/browse/JDK-8081700
Platform.runLater(() -> {
projectComboBox.hide();
final String searchText = projectComboBox.getEditor().getText();
filteredList.setPredicate(
project -> ProjectsListViewController.doesProjectMatchSearchFilter(searchText, project));
if (projectComboBox.getEditor().focusedProperty().get()) {
projectComboBox.show();
}
});
}
});
// manages Focusbehaviour
projectComboBox.getEditor()
.focusedProperty()
.addListener((final ObservableValue<? extends Boolean> observable, final Boolean oldIsFocused,
final Boolean newIsFocused) -> {
if (newIsFocused) {
// needed to avoid exception on empty textfield
// https://bugs.openjdk.java.net/browse/JDK-8081700
Platform.runLater(() -> projectComboBox.getEditor().selectAll());
} else {
// needed to avoid exception on empty textfield
// https://bugs.openjdk.java.net/browse/JDK-8081700
Platform.runLater(() -> projectComboBox.hide());
}
});
// on
projectComboBox.setOnKeyReleased(ke -> {
if (ke.getCode() == KeyCode.ENTER && projectComboBox.getSelectionModel().isEmpty()) {
if (!projectComboBox.getItems().isEmpty()) {
projectComboBox.getSelectionModel().selectFirst();
comboChange = true;
}
}
});
}
public void initializeWith(final Work work) {
LOG.info("Setting values.");
selectedProject = work.getProject();
startDatePicker.setValue(work.getStartTime().toLocalDate());
endDatePicker.setValue(work.getEndTime().toLocalDate());
startTimePicker.setValue(work.getStartTime().toLocalTime());
endTimePicker.setValue(work.getEndTime().toLocalTime());
noteTextArea.setText(work.getNotes());
projectComboBox.getSelectionModel().select(work.getProject());
setColor(projectComboBox, model.hoverBackgroundColor.get());
setColor(projectComboBox.getEditor(), model.hoverBackgroundColor.get());
setTextColor(projectComboBox.getEditor(), model.hoverFontColor.get());
}
private void enableStrgA_combo() {
// strg+a Behaviour bug hack
// https://stackoverflow.com/questions/51943654/javafx-combobox-make-control-a-select-all-in-text-box-while-dropdown-is-visi
projectComboBox.setOnShown(e -> {
final ComboBoxListViewSkin<?> skin = (ComboBoxListViewSkin<?>) projectComboBox.getSkin();
final ListView<?> list = (ListView<?>) skin.getPopupContent();
final KeyCodeCombination ctrlA = new KeyCodeCombination(KeyCode.A, KeyCodeCombination.CONTROL_DOWN);
list.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
if (ctrlA.match(keyEvent)) {
projectComboBox.getEditor().selectAll();
}
});
projectComboBox.setOnShown(null);
});
}
private void setColor(final Node object, final Color color) {
final String style = StyleUtils.changeStyleAttribute(object.getStyle(), "fx-background-color",
"rgba(" + ColorHelper.colorToCssRgba(color) + ")");
object.setStyle(style);
}
public Work getWorkFromUserInput() {
return new Work(LocalDateTime.of(startDatePicker.getValue(), startTimePicker.getValue()),
LocalDateTime.of(endDatePicker.getValue(), endTimePicker.getValue()), selectedProject,
noteTextArea.getText());
}
private void setTextColor(final Node object, final Color color) {
final String style = StyleUtils.changeStyleAttribute(object.getStyle(), "fx-text-fill",
"rgba(" + ColorHelper.colorToCssRgba(color) + ")");
object.setStyle(style);
}
public BooleanProperty validProperty() {
return this.isValidProperty;
}
}