-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReportController.java
More file actions
592 lines (489 loc) · 22.5 KB
/
ReportController.java
File metadata and controls
592 lines (489 loc) · 22.5 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
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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// 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.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.scene.shape.SVGPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import de.doubleslash.keeptime.common.DateFormatter;
import de.doubleslash.keeptime.common.Resources;
import de.doubleslash.keeptime.common.Resources.RESOURCE;
import de.doubleslash.keeptime.common.SvgNodeProvider;
import de.doubleslash.keeptime.controller.Controller;
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
import de.doubleslash.keeptime.model.Model;
import de.doubleslash.keeptime.model.Project;
import de.doubleslash.keeptime.model.Work;
import de.doubleslash.keeptime.view.worktable.ProjectTableRow;
import de.doubleslash.keeptime.view.worktable.TableRow;
import de.doubleslash.keeptime.view.worktable.WorkTableRow;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.*;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import javafx.scene.control.skin.DatePickerSkin;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Callback;
import static de.doubleslash.keeptime.App.showErrorDialogAndWait;
import static de.doubleslash.keeptime.view.ViewController.createFXMLLoader;
@Component
public class ReportController {
public static final String NOTE_DELIMETER = "; ";
public static final String EMPTY_NOTE = "- No notes -";
private static final String FX_BACKGROUND_COLOR_NOT_WORKED = "-fx-background-color: #BBBBBB;";
private static final String FX_BACKGROUND_COLOR_WORKED = "-fx-background-color: #00a5e1;";
private static final String EDIT_WORK_DIALOG_TITLE = "Edit work";
@FXML
private BorderPane topBorderPane;
@FXML
private Label currentDayLabel;
@FXML
private Label currentDayWorkTimeLabel;
@FXML
private Label currentDayTimeLabel;
@FXML
private TreeTableView<TableRow> workTableTreeView;
@FXML
private AnchorPane reportRoot;
@FXML
private Canvas colorTimeLineCanvas;
@FXML
private Button expandCollapseButton;
@FXML
private Button heimatSyncButton;
@FXML
private Button addWorkButton;
private static final Logger LOG = LoggerFactory.getLogger(ReportController.class);
private final Model model;
private final Controller controller;
private Stage stage;
private ColorTimeLine colorTimeLine;
private LocalDate currentReportDate;
private final TreeItem<TableRow> rootItem = new TreeItem<>();
private boolean expanded = true;
private List<Work> currentWorkItems;
public ReportController(final Model model, final Controller controller) {
this.model = model;
this.controller = controller;
}
@FXML
private void initialize() {
LOG.info("Init reportController");
currentReportDate = LocalDate.now();
colorTimeLine = new ColorTimeLine(colorTimeLineCanvas);
expandCollapseButton.setOnMouseClicked(event ->toggleCollapseExpandReport());
if (addWorkButton != null) {
addWorkButton.setOnAction(e -> onAddWork());
}
initTableView();
initHeimatIntegration();
}
private void onAddWork() {
// Default values: current report date window or now if today
final boolean isToday = LocalDate.now().equals(currentReportDate);
final LocalDateTime now = LocalDateTime.now();
final LocalDateTime defaultStart = isToday ? now.minusMinutes(15) : currentReportDate.atTime(LocalTime.of(9, 0));
final LocalDateTime defaultEnd = isToday ? now : currentReportDate.atTime(LocalTime.of(10, 0));
final Project defaultProject = model.activeWorkItem.get() != null
? model.activeWorkItem.get().getProject()
: model.getIdleProject();
final Work newWorkDefaults = new Work(defaultStart, defaultEnd, defaultProject, "");
final Dialog<Work> dialog = setupAddWorkDialog(newWorkDefaults);
final Optional<Work> result = dialog.showAndWait();
result.ifPresent(createdWork -> {
controller.addWork(createdWork);
this.update();
});
}
private void initHeimatIntegration() {
heimatSyncButton.setVisible(model.getHeimatSettings().isHeimatActive());
final SVGPath svgNodeWithScale = SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_ROTATE_ICON, 0.03, 0.03);
svgNodeWithScale.setStyle("-fx-fill: #00759e");
heimatSyncButton.setMaxSize(25,25);
heimatSyncButton.setMinSize(25, 25);
heimatSyncButton.setGraphic(svgNodeWithScale);
heimatSyncButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
heimatSyncButton.setTooltip(new Tooltip("Synchronize to HEIMAT..."));
heimatSyncButton.setOnAction(ae-> {
try {
showSyncStage();
} catch (FXMLLoaderException e) {
LOG.error("Error while loading sync stage", e);
showErrorDialogAndWait("Error", "Could not load sync stage", "Please make sure your AccessToken is valid and you have internet.", e, this.stage);
}
});
}
private void showSyncStage(){
try{
// Settings stage
final FXMLLoader fxmlLoader2 = createFXMLLoader(RESOURCE.FXML_EXT_PROJECT_SYNC);
fxmlLoader2.setControllerFactory(model.getSpringContext()::getBean);
final Parent syncRoot = fxmlLoader2.load();
ExternalProjectsSyncController syncController = fxmlLoader2.getController();
syncController.initForDate(currentReportDate, currentWorkItems);
Stage syncStage = new Stage();
syncController.setStage(syncStage);
syncStage.initOwner(this.stage);
syncStage.setTitle("External Project Sync");
syncStage.setResizable(true);
syncStage.getIcons().add(new Image(Resources.getResource(RESOURCE.ICON_MAIN).toString()));
final Scene settingsScene = new Scene(syncRoot);
settingsScene.addEventFilter(KeyEvent.KEY_PRESSED, ke -> {
if (ke.getCode() == KeyCode.ESCAPE) {
LOG.info("pressed ESCAPE");
syncStage.close();
}
});
syncStage.setScene(settingsScene);
syncStage.showAndWait();
} catch (final Exception e) {
throw new FXMLLoaderException(e);
}
}
private void initTableView() {
final TreeTableColumn<TableRow, TableRow> noteColumn = new TreeTableColumn<>("Notes");
noteColumn.setCellFactory(new Callback<TreeTableColumn<TableRow, TableRow>, TreeTableCell<TableRow, TableRow>>() {
@Override
public TreeTableCell<TableRow, TableRow> call(final TreeTableColumn<TableRow, TableRow> column) {
return new TreeTableCell<TableRow, TableRow>() {
@Override
protected void updateItem(final TableRow item, final boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
setText(null);
} else {
final String notes = item.getNotes();
final String text = notes.isEmpty() ? EMPTY_NOTE : notes;
this.setText(text);
if (item.getProjectColor() != null) {
final Circle circle = new Circle(6, item.getProjectColor());
this.setGraphic(circle);
} else {
this.setGraphic(null);
}
}
}
};
}
});
noteColumn.setCellValueFactory(
(final TreeTableColumn.CellDataFeatures<TableRow, TableRow> entry) -> new ReadOnlyObjectWrapper<>(
entry.getValue().getValue()));
noteColumn.setMinWidth(200);
noteColumn.setReorderable(false);
this.workTableTreeView.getColumns().add(noteColumn);
final TreeTableColumn<TableRow, String> timeRangeColumn = new TreeTableColumn<>("Timeslot");
timeRangeColumn.setCellValueFactory(new TreeItemPropertyValueFactory<TableRow, String>("timeRange"));
timeRangeColumn.setMinWidth(120);
timeRangeColumn.setReorderable(false);
this.workTableTreeView.getColumns().add(timeRangeColumn);
final TreeTableColumn<TableRow, TableRow> timeSumColumn = new TreeTableColumn<>("Duration");
timeSumColumn.setCellFactory(new Callback<>() {
@Override
public TreeTableCell<TableRow, TableRow> call(
TreeTableColumn<TableRow, TableRow> tableRowStringTreeTableColumn) {
return new TreeTableCell<>() {
@Override
protected void updateItem(TableRow workItem, boolean empty) {
super.updateItem(workItem, empty);
if (workItem == null || empty) {
this.setGraphic(null);
this.setText(null);
} else {
this.setText(workItem.getTimeSum());
this.setUnderline(workItem.isUnderlined());
}
}
};
}
});
timeSumColumn.setCellValueFactory(
(final TreeTableColumn.CellDataFeatures<TableRow, TableRow> entry) -> new ReadOnlyObjectWrapper<>(
entry.getValue().getValue()));
timeSumColumn.setMinWidth(60);
timeSumColumn.setReorderable(false);
this.workTableTreeView.getColumns().add(timeSumColumn);
final TreeTableColumn<TableRow, Button> buttonColumn = new TreeTableColumn<>("Controls");
buttonColumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("buttonBox"));
buttonColumn.setMinWidth(95);
buttonColumn.setSortable(false);
buttonColumn.setReorderable(false);
this.workTableTreeView.getColumns().add(buttonColumn);
workTableTreeView.setShowRoot(false);
workTableTreeView.setRoot(rootItem);
rootItem.setExpanded(true);
}
private void toggleCollapseExpandReport(){
if(expanded){
expandAll(false);
expandCollapseButton.setText("Expand");
}else {
expandAll(true);
expandCollapseButton.setText("Collapse");
}
expanded = !expanded;
}
private void expandAll(boolean expand){
for (int i=0; i<rootItem.getChildren().size(); i++){
rootItem.getChildren().get(i).setExpanded(expand);
}
}
private void updateReport(final LocalDate dateToShow) {
this.currentReportDate = dateToShow;
rootItem.getChildren().clear();
reportRoot.requestFocus();
this.currentDayLabel.setText(DateFormatter.toDayDateString(this.currentReportDate));
currentWorkItems = model.getWorkRepository()
.findByStartDateOrderByStartTimeAsc(this.currentReportDate);
colorTimeLine.update(currentWorkItems, controller.calcSeconds(currentWorkItems));
final SortedSet<Project> workedProjectsSet = currentWorkItems.stream()
.map(Work::getProject)
.collect(Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(Project::getIndex))));
long currentWorkSeconds = 0;
long currentSeconds = 0;
for (final Project project : workedProjectsSet) {
final List<Work> onlyCurrentProjectWork = currentWorkItems.stream()
.filter(w -> w.getProject() == project)
.collect(Collectors.toList());
final long projectWorkSeconds = controller.calcSeconds(onlyCurrentProjectWork);
currentSeconds += projectWorkSeconds;
if (project.isWork()) {
currentWorkSeconds += projectWorkSeconds;
}
final HBox projectButtonBox = new HBox();
projectButtonBox.getChildren().add(createCopyProjectNameButton(project.getName()));
projectButtonBox.getChildren().add(createCopyProjectNotesButton(onlyCurrentProjectWork));
final TreeItem<TableRow> projectRow = new TreeItem<>(
new ProjectTableRow(project, projectWorkSeconds, projectButtonBox));
for (final Work w : onlyCurrentProjectWork) {
final HBox workButtonBox = new HBox(5.0);
if(w.getId()==model.activeWorkItem.get().getId()){
Label label = new Label("Active Work");
label.setTooltip(new Tooltip("The active work item cannot be edited as it is currently active. To edit it you need to switch to another work first."));
label.setStyle("-fx-font-weight: bold");
workButtonBox.getChildren().add(label);
}else {
workButtonBox.getChildren().add(createCopyWorkButton(w));
workButtonBox.getChildren().add(createEditWorkButton(w));
workButtonBox.getChildren().add(createDeleteWorkButton(w));
}
final TreeItem<TableRow> workRow = new TreeItem<>(new WorkTableRow(w, workButtonBox));
projectRow.getChildren().add(workRow);
}
projectRow.setExpanded(expanded);
rootItem.getChildren().add(projectRow);
}
this.currentDayTimeLabel.setText(DateFormatter.secondsToHHMMSS(currentSeconds));
this.currentDayWorkTimeLabel.setText(DateFormatter.secondsToHHMMSS(currentWorkSeconds));
loadCalenderWidget();
}
private void loadCalenderWidget() {
final DatePicker myDatePicker = new DatePicker(this.currentReportDate);
myDatePicker.valueProperty().addListener((observable, oldvalue, newvalue) -> {
LOG.info("Datepicker selected value changed to {}", newvalue);
updateReport(newvalue);
});
// HACK to show calendar from datepicker
// https://stackoverflow.com/questions/34681975/javafx-extract-calendar-popup-from-datepicker-only-show-popup
final DatePickerSkin datePickerSkin = new DatePickerSkin(myDatePicker);
final Callback<DatePicker, DateCell> dayCellFactory = callback -> new DateCell() {
@Override
public void updateItem(final LocalDate item, final boolean empty) {
super.updateItem(item, empty);
if (model.getWorkRepository().findByStartDateOrderByStartTimeAsc(item).isEmpty()) {
setDisable(true);
setStyle(FX_BACKGROUND_COLOR_NOT_WORKED);
} else {
setDisable(false);
setStyle(FX_BACKGROUND_COLOR_WORKED);
}
}
};
myDatePicker.setDayCellFactory(dayCellFactory);
final Node popupContent = datePickerSkin.getPopupContent();
this.topBorderPane.setRight(popupContent);
}
private Button createDeleteWorkButton(final Work w) {
final Button deleteButton = new Button("",
SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_TRASH_ICON, 0.03, 0.03));
deleteButton.setMaxSize(20, 18);
deleteButton.setMinSize(20, 18);
deleteButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
deleteButton.getStyleClass().add("tertiary-button");
deleteButton.setOnAction(e -> {
LOG.info("Delete work clicked.");
final Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Delete Work");
alert.setHeaderText("Delete work item");
alert.setContentText(w.toString());
alert.initOwner(stage);
final Optional<ButtonType> result = alert.showAndWait();
result.ifPresent(buType -> {
if (buType.equals(ButtonType.OK)) {
controller.deleteWork(w);
this.update();
}
});
});
return deleteButton;
}
private Button createEditWorkButton(final Work work) {
final Button editButton = new Button("",
SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_PENCIL_ICON, 0.03, 0.03));
editButton.setMaxSize(20, 18);
editButton.setMinSize(20, 18);
editButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
editButton.getStyleClass().add("tertiary-button");
editButton.setOnAction(e -> {
LOG.info("Edit work clicked.");
final Dialog<Work> dialog = setupEditWorkDialog(work);
final Optional<Work> result = dialog.showAndWait();
result.ifPresent(editedWork -> {
controller.editWork(work, editedWork);
this.update();
});
});
return editButton;
}
private Dialog<Work> setupEditWorkDialog(final Work work) {
final Dialog<Work> dialog = new Dialog<>();
dialog.initOwner(stage);
dialog.setTitle(EDIT_WORK_DIALOG_TITLE);
dialog.setHeaderText(EDIT_WORK_DIALOG_TITLE);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
final GridPane grid = setUpEditWorkGridPane(work, dialog);
dialog.getDialogPane().setContent(grid);
return dialog;
}
private Dialog<Work> setupAddWorkDialog(final Work work) {
final Dialog<Work> dialog = new Dialog<>();
dialog.initOwner(stage);
dialog.setTitle("Add work");
dialog.setHeaderText("Add work");
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
final GridPane grid = setUpEditWorkGridPane(work, dialog);
dialog.getDialogPane().setContent(grid);
return dialog;
}
private GridPane setUpEditWorkGridPane(final Work work, final Dialog<Work> dialog) {
final GridPane grid;
final FXMLLoader loader = new FXMLLoader(Resources.getResource(RESOURCE.FXML_MANAGE_WORK));
try {
grid = loader.load();
} catch (final IOException e) {
throw new FXMLLoaderException("Error while loading '" + Resources.RESOURCE.FXML_MANAGE_WORK + "'.", e);
}
final ManageWorkController manageWorkController = loader.getController();
manageWorkController.setModel(model);
manageWorkController.initializeWith(work);
dialog.getDialogPane()
.lookupButton(ButtonType.OK)
.disableProperty()
.bind(manageWorkController.validProperty().not());
dialog.setResultConverter(dialogButton -> {
if (dialogButton == ButtonType.OK) {
return manageWorkController.getWorkFromUserInput();
}
return null;
});
return grid;
}
private Button createCopyProjectNotesButton(final List<Work> projectWork) {
final Button copyNotesButton = new Button("", SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_MULTIPLE_CLIPBOARD_ICON, 0.03, 0.03));
copyNotesButton.setMaxSize(20, 18);
copyNotesButton.setMinSize(20, 18);
copyNotesButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
copyNotesButton.setTooltip(new Tooltip("Copy Project Notes"));
copyNotesButton.getStyleClass().add("tertiary-button");
final EventHandler<ActionEvent> eventListener = actionEvent -> {
LOG.debug("Copy to Clipboard clicked.");
final ProjectReport pr = new ProjectReport();
for (int j = 0; j < projectWork.size(); j++) {
final Work work = projectWork.get(j);
final String currentWorkNote = work.getNotes();
pr.appendToWorkNotes(currentWorkNote);
}
copyToClipboard(pr.getNotes());
};
copyNotesButton.setOnAction(eventListener);
return copyNotesButton;
}
private Button createCopyProjectNameButton(String projectName) {
final Button copyProjectNameButton = new Button("", SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_CLIPBOARD_ICON, 0.03, 0.03));
copyProjectNameButton.setMaxSize(20, 18);
copyProjectNameButton.setMinSize(20, 18);
copyProjectNameButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
copyProjectNameButton.setTooltip(new Tooltip("Copy Project Name"));
copyProjectNameButton.getStyleClass().add("tertiary-button");
final EventHandler<ActionEvent> eventListener = actionEvent -> {
LOG.debug("Copy to Clipboard clicked.");
copyToClipboard(projectName);
};
copyProjectNameButton.setOnAction(eventListener);
return copyProjectNameButton;
}
public static void copyToClipboard(final String stringToCopy) {
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putString(stringToCopy);
clipboard.setContent(content);
}
private Node createCopyWorkButton(final Work w) {
final Button copyButton = new Button("", SvgNodeProvider.getSvgNodeWithScale(RESOURCE.SVG_CLIPBOARD_ICON, 0.03, 0.03));
copyButton.setMaxSize(20, 18);
copyButton.setMinSize(20, 18);
copyButton.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
copyButton.getStyleClass().add("tertiary-button");
final EventHandler<ActionEvent> eventListener = actionEvent -> {
LOG.debug("Copy to Clipboard clicked.");
copyToClipboard(w.getNotes());
};
copyButton.setOnAction(eventListener);
return copyButton;
}
public void update() {
heimatSyncButton.setVisible(model.getHeimatSettings().isHeimatActive());
// TODO save work so it appears directly in report. Quick fixes #170. Use #176 for this instead.
controller.saveCurrentWork();
updateReport(this.currentReportDate);
}
public void setStage(final Stage stage) {
this.stage = stage;
}
}