-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExternalProjectsMapController.java
More file actions
308 lines (262 loc) · 13 KB
/
ExternalProjectsMapController.java
File metadata and controls
308 lines (262 loc) · 13 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
// Copyright 2025 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 de.doubleslash.keeptime.common.ColorHelper;
import de.doubleslash.keeptime.common.Resources;
import de.doubleslash.keeptime.controller.Controller;
import de.doubleslash.keeptime.controller.HeimatController;
import de.doubleslash.keeptime.model.Model;
import de.doubleslash.keeptime.model.Project;
import de.doubleslash.keeptime.rest.integration.heimat.model.ExistingAndInvalidMappings;
import de.doubleslash.keeptime.rest.integration.heimat.model.HeimatTask;
import javafx.application.Platform;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
@Component
public class ExternalProjectsMapController {
private static final Logger LOG = LoggerFactory.getLogger(ExternalProjectsMapController.class);
private final Model model;
private final Controller controller;
private final HeimatController heimatController;
private Stage thisStage;
@FXML
private TableView<HeimatController.ProjectMapping> mappingTableView;
@FXML
private Button saveButton;
@FXML
private Button cancelButton;
@FXML
private Button addNewProjectButton;
@FXML
private DatePicker tasksForDateDatePicker;
public ExternalProjectsMapController(final Model model, Controller controller, HeimatController heimatController) {
this.model = model;
this.controller = controller;
this.heimatController = heimatController;
}
public void setStage(final Stage thisStage) {
this.thisStage = thisStage;
}
@FXML
private void initialize() {
tasksForDateDatePicker.setValue(LocalDate.now());
tasksForDateDatePicker.setDisable(true);
// TODO add listener on this thing
// but what happens with mapped projects not existing at that date? but actually not related to this feature alone
final List<HeimatTask> externalProjects = heimatController.getTasks(tasksForDateDatePicker.getValue());
final ExistingAndInvalidMappings existingAndInvalidMappings = heimatController.getExistingProjectMappings(
externalProjects);
final List<HeimatController.ProjectMapping> previousProjectMappings = existingAndInvalidMappings.validMappings();
final ObservableList<HeimatController.ProjectMapping> newProjectMappings = FXCollections.observableArrayList(
previousProjectMappings);
final FilteredList<HeimatController.ProjectMapping> value = new FilteredList<>(newProjectMappings,
pm -> pm.getProject().isWork());
mappingTableView.setItems(value);
// KeepTime Project column
TableColumn<HeimatController.ProjectMapping, String> keepTimeColumn = new TableColumn<>("KeepTime project");
keepTimeColumn.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getProject().getName()));
// External Project column with dropdown
final ObservableList<HeimatTask> externalProjectsObservableList = FXCollections.observableArrayList(
externalProjects);
externalProjectsObservableList.add(0, null); // option to clear selection
TableColumn<HeimatController.ProjectMapping, HeimatTask> externalColumn = new TableColumn<>("Heimat project");
externalColumn.setCellValueFactory(data -> new SimpleObjectProperty<>(data.getValue().getHeimatTask()));
externalColumn.setCellFactory(col -> new TableCell<>() {
// TODO search in box would be nice
private final ComboBox<HeimatTask> comboBox = new ComboBox<>(externalProjectsObservableList);
@Override
protected void updateItem(HeimatTask item, boolean empty) {
super.updateItem(item, empty);
// selected item
comboBox.setButtonCell(new ListCell<>() {
@Override
protected void updateItem(HeimatTask item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item.taskHolderName() + " - " + item.name());
}
}
});
// Dropdown
comboBox.setCellFactory(param -> new ListCell<>() {
@Override
protected void updateItem(HeimatTask item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
setText(null);
} else {
// TODO maybe show if the project was already mapped
setText(item.taskHolderName() + " - " + item.name());
}
}
});
if (empty) {
setGraphic(null);
setText(null);
} else {
comboBox.setValue(getTableView().getItems().get(getIndex()).getHeimatTask());
comboBox.setOnAction(e -> {
HeimatController.ProjectMapping mapping = getTableView().getItems().get(getIndex());
mapping.setHeimatTask(comboBox.getValue());
});
setGraphic(comboBox);
setText(null);
}
}
});
double scrollbarWidth = 17; // Approximate width of a vertical scrollbar
keepTimeColumn.prefWidthProperty().bind(mappingTableView.widthProperty().subtract(scrollbarWidth).multiply(.4));
externalColumn.prefWidthProperty().bind(mappingTableView.widthProperty().subtract(scrollbarWidth).multiply(.6));
mappingTableView.getColumns().addAll(keepTimeColumn, externalColumn);
addNewProjectButton.setOnAction(e -> {
final List<HeimatTask> unmappedHeimatTasks = externalProjects.stream().filter(ht -> {
final boolean alreadyMapped = value.stream()
.anyMatch(mapping -> mapping.getHeimatTask() != null
&& mapping.getHeimatTask().id() == ht.id());
return !alreadyMapped;
}).toList();
List<HeimatTask> selectedItems = showMultiSelectDialog(externalProjects, unmappedHeimatTasks);
for (HeimatTask toBeCreatedHeimatTask : selectedItems) {
final int sortIndex = model.getAvailableProjects().size();
final Project project = controller.addNewProject(
new Project(toBeCreatedHeimatTask.name() + " - " + toBeCreatedHeimatTask.taskHolderName(),
toBeCreatedHeimatTask.bookingHint(), ColorHelper.randomColor(), true, sortIndex));
newProjectMappings.add(new HeimatController.ProjectMapping(project, toBeCreatedHeimatTask));
}
});
saveButton.setOnAction(ae -> {
heimatController.updateMappings(newProjectMappings);
thisStage.close();
});
cancelButton.setOnAction(ae -> thisStage.close());
List<String> warnings = existingAndInvalidMappings.invalidMappingsAsString();
if (!warnings.isEmpty()) {
Platform.runLater(() -> showInvalidMappingsDialog(warnings));
}
}
private List<HeimatTask> showMultiSelectDialog(final List<HeimatTask> externalProjects,
List<HeimatTask> unmappedHeimatTasks) {
Dialog<List<HeimatTask>> dialog = new Dialog<>();
dialog.setTitle("Import Heimat projects");
dialog.setHeaderText("You can select mutliple items");
dialog.initOwner(this.thisStage);
dialog.setWidth(600);
dialog.setHeight(500);
// Buttons
ButtonType okButtonType = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
ButtonType cancelButtonType = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
dialog.getDialogPane().getButtonTypes().addAll(okButtonType, cancelButtonType);
TableView<HeimatTask> tableView = new TableView<>();
TableColumn<HeimatTask, HeimatTask> nameColumn = new TableColumn<>("Heimat project");
nameColumn.setCellValueFactory(data -> new SimpleObjectProperty<>(data.getValue()));
nameColumn.setCellFactory(param -> new TableCell<>() {
@Override
protected void updateItem(HeimatTask item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
setText(null);
} else {
setText(item.taskHolderName() + " - " + item.name());
}
}
});
// Column for Mapped Status (Read-Only CheckBox)
TableColumn<HeimatTask, Boolean> mappedColumn = new TableColumn<>("Mapped");
mappedColumn.setCellValueFactory(
cellData -> new SimpleBooleanProperty(!unmappedHeimatTasks.contains(cellData.getValue())));
mappedColumn.setCellFactory(CheckBoxTableCell.forTableColumn(mappedColumn));
mappedColumn.setEditable(false);
mappedColumn.setPrefWidth(75);
tableView.getColumns().addAll(mappedColumn, nameColumn);
tableView.setEditable(false);
tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableView.setItems(FXCollections.observableArrayList(externalProjects));
Button selectAllUnmappedButton = new Button("Select unmapped projects (" + unmappedHeimatTasks.size() + ")");
selectAllUnmappedButton.getStyleClass().add("secondary-button");
selectAllUnmappedButton.setOnAction(e -> {
tableView.getSelectionModel().clearSelection();
for (HeimatTask ht : unmappedHeimatTasks) {
tableView.getSelectionModel().select(ht);
}
tableView.requestFocus();
});
VBox content = new VBox(10, selectAllUnmappedButton, tableView);
dialog.getDialogPane().setContent(content);
final List<HeimatTask> emptyList = List.of();
dialog.setResultConverter(dialogButton -> {
if (dialogButton == okButtonType) {
return tableView.getSelectionModel().getSelectedItems().stream().toList();
}
return emptyList; // cancel was clicked
});
Button okButton = (Button) dialog.getDialogPane().lookupButton(okButtonType);
okButton.setText("Import (0)");
okButton.setPrefWidth(100);
okButton.getStyleClass().add("primary-button");
Button dialogCancelButton = (Button) dialog.getDialogPane().lookupButton(cancelButtonType);
dialogCancelButton.getStyleClass().add("secondary-button");
dialog.getDialogPane()
.getStylesheets()
.add(Resources.getResource(Resources.RESOURCE.CSS_DS_STYLE).toExternalForm());
tableView.getSelectionModel().getSelectedItems().addListener((ListChangeListener<HeimatTask>) change -> {
int selectedCount = tableView.getSelectionModel().getSelectedItems().size();
okButton.setText("Import (" + selectedCount + ")");
});
Optional<List<HeimatTask>> result = dialog.showAndWait();
return result.orElse(emptyList);
}
private void showInvalidMappingsDialog(final List<String> warnings) {
Dialog<Void> dialog = new Dialog<>();
dialog.initOwner(this.thisStage);
dialog.setTitle("Invalid mappings");
dialog.setHeaderText("Please note to following issue:");
VBox warningBox = new VBox(10);
for (String warning : warnings) {
Label label = new Label(warning);
label.setWrapText(true);
warningBox.getChildren().add(label);
}
ScrollPane scrollPane = new ScrollPane(warningBox);
scrollPane.setFitToWidth(true);
scrollPane.setPrefHeight(Math.min(300, warnings.size() * 30 + 50)); // Adjust height dynamically
dialog.getDialogPane().setContent(scrollPane);
dialog.getDialogPane().setMinWidth(400);
// Add OK button
ButtonType okButton = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(okButton);
dialog.showAndWait();
}
}