-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathManageWorkController.java
More file actions
143 lines (109 loc) · 4.42 KB
/
ManageWorkController.java
File metadata and controls
143 lines (109 loc) · 4.42 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
// 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 java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.time.format.FormatStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.doubleslash.keeptime.model.Model;
import de.doubleslash.keeptime.model.Work;
import de.doubleslash.keeptime.view.common.SearchableProjectCombobox;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.util.converter.LocalTimeStringConverter;
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 Spinner<LocalTime> startTimeSpinner;
@FXML
private Spinner<LocalTime> endTimeSpinner;
@FXML
private DatePicker endDatePicker;
@FXML
private TextArea noteTextArea;
@FXML
private SearchableProjectCombobox projectComboBox;
public void setModel(final Model model) {
this.model = model;
projectComboBox.setProjects(model.getSortedAvailableProjects());
}
@FXML
private void initialize() {
setUpTimeSpinner(startTimeSpinner);
setUpTimeSpinner(endTimeSpinner);
}
private void setUpTimeSpinner(final Spinner<LocalTime> spinner) {
spinner.focusedProperty().addListener((e) -> {
final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter(FormatStyle.MEDIUM);
final StringProperty text = spinner.getEditor().textProperty();
try {
stringConverter.fromString(text.get());
// needed to log in value from editor to spinner
spinner.increment(0); // TODO find better Solution
} catch (final DateTimeParseException ex) {
text.setValue(spinner.getValue().toString());
}
});
spinner.setValueFactory(new SpinnerValueFactory<LocalTime>() {
@Override
public void decrement(final int steps) {
if (getValue() == null) {
setValue(LocalTime.now());
} else {
final LocalTime time = getValue();
setValue(time.minusMinutes(steps));
}
}
@Override
public void increment(final int steps) {
if (getValue() == null) {
setValue(LocalTime.now());
} else {
final LocalTime time = getValue();
setValue(time.plusMinutes(steps));
}
}
});
spinner.getValueFactory().setConverter(new LocalTimeStringConverter(FormatStyle.MEDIUM));
}
public void initializeWith(final Work work) {
LOG.info("Setting values.");
projectComboBox.setProject(work.getProject(), model.hoverBackgroundColor.get(), model.hoverFontColor.get());
startDatePicker.setValue(work.getStartTime().toLocalDate());
endDatePicker.setValue(work.getEndTime().toLocalDate());
startTimeSpinner.getValueFactory().setValue(work.getStartTime().toLocalTime());
endTimeSpinner.getValueFactory().setValue(work.getEndTime().toLocalTime());
noteTextArea.setText(work.getNotes());
}
public Work getWorkFromUserInput() {
return new Work(startDatePicker.getValue(),
LocalDateTime.of(startDatePicker.getValue(), startTimeSpinner.getValue()),
LocalDateTime.of(endDatePicker.getValue(), endTimeSpinner.getValue()), projectComboBox.getValue(),
noteTextArea.getText());
}
}