Newer
Older
package userInterface;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import core.LogObject;
import core.SQL;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DateCell;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.util.Callback;
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
public class InsertTimeController
{
@FXML DatePicker datePicker1;
@FXML DatePicker datePicker2;
@FXML Parent timePicker1;
@FXML TimePickerController timePicker1Controller;
@FXML Parent timePicker2;
@FXML TimePickerController timePicker2Controller;
@FXML Button buttonAdd;
@FXML Button buttonCancel;
@FXML Label labelDuration;
@FXML ComboBox<String> comboBoxProject;
@FXML ComboBox<String> comboBoxTask;
private Stage stage;
private UserInterfaceController controller;
private String savePath;
private Image icon;
public void init(Stage stage, UserInterfaceController controller, String savePath, Image icon)
{
this.savePath = savePath;
this.stage = stage;
this.controller = controller;
this.icon = icon;
ArrayList<String> objects = new ArrayList<String>();
SQL sql = new SQL(savePath);
try
{
objects = sql.getProjectNames();
Collections.sort(objects);
}
catch(Exception e)
{
}
comboBoxProject.getItems().addAll(objects);
comboBoxProject.setStyle("-fx-font-family: \"Arial\";-fx-font-size: 15px;");
comboBoxTask.setStyle("-fx-font-family: \"Arial\";-fx-font-size: 15px;");
comboBoxProject.valueProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
comboBoxTask.getItems().clear();
if(newValue != null && !newValue.equals(""))
{
try
{
ArrayList<String> tasks = sql.getTaskNamesByProject(newValue);
Collections.sort(tasks);
comboBoxTask.getItems().addAll(tasks);
}
catch(Exception e)
{
}
}
}
});
timePicker1Controller.setController(this);
timePicker2Controller.setController(this);
datePicker1.setValue(LocalDate.now());
datePicker2.setValue(LocalDate.now());
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>()
{
@Override
public DateCell call(final DatePicker datePicker)
{
return new DateCell()
{
@Override
public void updateItem(LocalDate item, boolean empty)
{
super.updateItem(item, empty);
if(item.isBefore(datePicker1.getValue()))
{
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
}
};
datePicker2.setDayCellFactory(dayCellFactory);
datePicker1.valueProperty().addListener(new ChangeListener<LocalDate>()
{
@Override
public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue)
{
if(isEndDateAfterStartDate())
{
setLabelDuration();
}
else
{
labelDuration.setText("Endzeit liegt vor Startzeit");
}
}
});
datePicker2.valueProperty().addListener(new ChangeListener<LocalDate>()
{
@Override
public void changed(ObservableValue<? extends LocalDate> observable, LocalDate oldValue, LocalDate newValue)
{
if(isEndDateAfterStartDate())
{
setLabelDuration();
}
else
{
labelDuration.setText("Endzeit liegt vor Startzeit");
}
}
});
comboBoxProject.requestFocus();
}
public void buttonAdd()
{
String project = comboBoxProject.getValue();
String task = comboBoxTask.getValue();
if(!project.equals("") && !task.equals(""))
{
if(isEndDateAfterStartDate())
{
int hours1 = timePicker1Controller.getHours();
int minutes1 = timePicker1Controller.getMinutes();
int seconds1 = timePicker1Controller.getSeconds();
int hours2 = timePicker2Controller.getHours();
int minutes2 = timePicker2Controller.getMinutes();
int seconds2 = timePicker2Controller.getSeconds();
LocalDate dateOne = datePicker1.getValue();
LocalDate dateTwo = datePicker2.getValue();
Timestamp timestampStart = Timestamp.valueOf(dateOne.toString() + " " + hours1 + ":" + minutes1 +":" + seconds1 + ".000");
Timestamp timestampEnd = Timestamp.valueOf(dateTwo.toString() + " " + hours2 + ":" + minutes2 +":" + seconds2 + ".000");
LogObject log = new LogObject(
dateOne.getYear(),
dateOne.getMonthValue(),
dateOne.getDayOfMonth(),
getCorrectedString(hours1) + ":" + getCorrectedString(minutes1) +":" + getCorrectedString(seconds1),
getCorrectedString(hours2) + ":" + getCorrectedString(minutes2) +":" + getCorrectedString(seconds2),
timestampEnd.getTime()-timestampStart.getTime(),
project,
task
);
SQL sql = new SQL(savePath);
try
{
sql.insert(log);
}
catch(Exception e)
{
AlertGenerator.showAlert(AlertType.ERROR, "Fehler", "", "Fehler beim Speichern des Eintrags.", icon, stage, null, false);
AlertGenerator.showAlert(AlertType.INFORMATION, "Gespeichert", "", "Der Eintrag wurde erfolgreich gespeichert.", icon, stage, null, false);
stage.close();
controller.loadAll();
}
else
{
AlertGenerator.showAlert(AlertType.WARNING, "Warnung", "", "Endzeit muss vor Startzeit liegen.", icon, stage, null, false);
AlertGenerator.showAlert(AlertType.WARNING, "Warnung", "", "Die Felder für Projekt und Task dürfen nicht leer sein.", icon, stage, null, false);
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
}
}
public void buttonCancel()
{
stage.close();
}
public void refresh(TimePickerController controller, int hours, int minutes, int seconds, String item, String direction)
{
if(controller == timePicker1Controller)
{
if(item.equals("hours"))
{
if(direction.equals("up"))
{
hours++;
if(hours == 24)
{
hours = 0;
}
}
else
{
hours--;
if(hours == -1 )
{
hours = 23;
}
}
}
else if(item.equals("minutes"))
{
if(direction.equals("up"))
{
minutes++;
if(minutes == 60)
{
minutes = 0;
}
}
else
{
minutes--;
if(minutes == -1)
{
minutes = 59;
}
}
}
else
{
if(direction.equals("up"))
{
seconds++;
if(seconds == 60)
{
seconds = 0;
}
}
else
{
seconds--;
if(seconds == -1)
{
seconds = 59;
}
}
}
timePicker1Controller.setTime(hours, minutes, seconds);
timePicker1Controller.init();
}
else
{
if(item.equals("hours"))
{
if(direction.equals("up"))
{
hours++;
if(hours == 24)
{
hours = 0;
}
}
else
{
hours--;
if(hours == -1 )
{
hours = 23;
}
}
}
else if(item.equals("minutes"))
{
if(direction.equals("up"))
{
minutes++;
if(minutes == 60)
{
minutes = 0;
}
}
else
{
minutes--;
if(minutes == -1)
{
minutes = 59;
}
}
}
else
{
if(direction.equals("up"))
{
seconds++;
if(seconds == 60)
{
seconds = 0;
}
}
else
{
seconds--;
if(seconds == -1)
{
seconds = 59;
}
}
}
timePicker2Controller.setTime(hours, minutes, seconds);
timePicker2Controller.init();
}
if(isEndDateAfterStartDate())
{
setLabelDuration();
}
else
{
labelDuration.setText("Endzeit liegt vor Startzeit");
}
}
public void setLabelDuration()
{
int hours1 = timePicker1Controller.getHours();
int minutes1 = timePicker1Controller.getMinutes();
int seconds1 = timePicker1Controller.getSeconds();
int hours2 = timePicker2Controller.getHours();
int minutes2 = timePicker2Controller.getMinutes();
int seconds2 = timePicker2Controller.getSeconds();
String dateString = datePicker1.getValue().getYear() + "-" + getCorrectedString(datePicker1.getValue().getMonthValue()) + "-" + getCorrectedString(datePicker1.getValue().getDayOfMonth()) + "-" + getCorrectedString(hours1) + ":" + getCorrectedString(minutes1) + ":" + getCorrectedString(seconds1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
dateString = datePicker2.getValue().getYear() + "-" + getCorrectedString(datePicker2.getValue().getMonthValue()) + "-" + getCorrectedString(datePicker2.getValue().getDayOfMonth()) + "-" + getCorrectedString(hours2) + ":" + getCorrectedString(minutes2) + ":" + getCorrectedString(seconds2);
LocalDateTime dateTime2 = LocalDateTime.parse(dateString, formatter);
Duration d= Duration.between(dateTime, dateTime2);
labelDuration.setText(ConvertTo.ConvertSecondsToTime(d.getSeconds()));
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
}
public boolean isEndDateAfterStartDate()
{
int hours1 = timePicker1Controller.getHours();
int minutes1 = timePicker1Controller.getMinutes();
int seconds1 = timePicker1Controller.getSeconds();
int hours2 = timePicker2Controller.getHours();
int minutes2 = timePicker2Controller.getMinutes();
int seconds2 = timePicker2Controller.getSeconds();
String dateOne = java.sql.Date.valueOf(datePicker1.getValue()).toString();
String dateTwo = java.sql.Date.valueOf(datePicker2.getValue()).toString();
DateFormat format = new SimpleDateFormat("yy-MM-dd - HH:mm:ss");
dateOne = dateOne + " - " + getCorrectedString(hours1) + ":"+ getCorrectedString(minutes1) + ":" + getCorrectedString(seconds1);
dateTwo = dateTwo + " - " + getCorrectedString(hours2) + ":"+ getCorrectedString(minutes2) + ":" + getCorrectedString(seconds2);
try
{
Date startDate = format.parse(dateOne);
Date endDate = format.parse(dateTwo);
if(startDate.before(endDate))
{
return true;
}
else
{
return false;
}
}
catch(ParseException e)
{