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 fontAwesome.FontIcon;
import fontAwesome.FontIconType;
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.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
@FXML private DatePicker datePicker1;
@FXML private DatePicker datePicker2;
@FXML private Parent timePicker1;
@FXML private TimePickerController timePicker1Controller;
@FXML private Parent timePicker2;
@FXML private TimePickerController timePicker2Controller;
@FXML private Button buttonUseCurrentTime;
@FXML private Button buttonAdd;
@FXML private Button buttonCancel;
@FXML private Label labelDuration;
@FXML private ComboBox<String> comboBoxProject;
@FXML private 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;
buttonUseCurrentTime.setGraphic(new FontIcon(FontIconType.CLOCK_ALT, 14, Color.BLACK));
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
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 != null && !project.equals("") && task != null && !task.equals(""))
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
{
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);
public void buttonCancel()
{
stage.close();
}
@FXML
public void useCurrentTime()
{
LocalDateTime now = LocalDateTime.now();
timePicker2Controller.setTime(now.getHour(), now.getMinute(), now.getSecond());
timePicker2Controller.init();
setLabelDuration();
}
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
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();
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
}
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()));
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
}
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)
{