-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExternalProjectsMapController.java
More file actions
355 lines (299 loc) · 14.9 KB
/
ExternalProjectsMapController.java
File metadata and controls
355 lines (299 loc) · 14.9 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
// 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 de.doubleslash.keeptime.viewpopup.SearchCombobox;
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.HBox;
import javafx.scene.layout.Priority;
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;
}
private ExistingAndInvalidMappings existingAndInvalidMappings;
private ObservableList<HeimatController.ProjectMapping> newProjectMappings;
public void setStage(final Stage thisStage) {
this.thisStage = thisStage;
// Show invalid mappings dialog when stage is shown
thisStage.setOnShown(e -> {
if (existingAndInvalidMappings != null && newProjectMappings != null) {
List<String> warnings = existingAndInvalidMappings.invalidMappingsAsString();
if (!warnings.isEmpty()) {
if (showInvalidMappingsDialog(warnings)) {
newProjectMappings.stream()
.filter(HeimatController.ProjectMapping::isPendingRemoval)
.forEach(pm -> pm.setHeimatTask(null));
mappingTableView.refresh();
}
}
}
});
}
@FXML
private void initialize() {
tasksForDateDatePicker.setValue(LocalDate.now());
tasksForDateDatePicker.setDisable(true);
// TODO add listener on this thing
final List<HeimatTask> externalProjects = heimatController.getAllKnownHeimatTasks(tasksForDateDatePicker.getValue());
existingAndInvalidMappings = heimatController.getExistingProjectMappings(
externalProjects);
final List<HeimatController.ProjectMapping> previousProjectMappings = existingAndInvalidMappings.validMappings();
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()));
keepTimeColumn.setCellFactory(col -> new TableCell<>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setTooltip(null);
} else {
setText(item);
Tooltip tooltip = new Tooltip(item);
setTooltip(tooltip);
}
}
});
// External Project column with dropdown
final ObservableList<HeimatTask> externalProjectsObservableList = FXCollections.observableArrayList(
externalProjects);
TableColumn<HeimatController.ProjectMapping, HeimatTask> externalColumn = new TableColumn<>("Heimat project");
externalColumn.setCellValueFactory(data -> new SimpleObjectProperty<>(data.getValue().getHeimatTask()));
externalColumn.setCellFactory(col -> new TableCell<>() {
private final SearchCombobox<HeimatTask> searchPopup = new SearchCombobox<>(externalProjectsObservableList);
{
searchPopup.setDisplayTextFunction(ht -> ht == null ? "" : ht.taskHolderName() + " - " + ht.name());
searchPopup.setClearFieldAfterSelection(false);
searchPopup.setPromptText("Search Project...");
searchPopup.setOnItemSelected((selectedTask, popup) -> {
HeimatController.ProjectMapping mapping = getTableView().getItems().get(getIndex());
mapping.setHeimatTask(selectedTask);
searchPopup.setComboBoxTooltip(selectedTask.name() + " - " + selectedTask.id());
updateItem(selectedTask, false);
});
}
@Override
protected void updateItem(HeimatTask item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
searchPopup.setSelectedItem(item);
if (item != null) {
searchPopup.setComboBoxTooltip(item.name() + " - " + item.id());
} else {
searchPopup.setComboBoxTooltip("");
}
setGraphic(searchPopup.getComboBox());
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, false));
}
});
saveButton.setOnAction(ae -> {
heimatController.updateMappings(newProjectMappings);
thisStage.close();
});
cancelButton.setOnAction(ae -> thisStage.close());
}
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);
// Observable and filtered list
ObservableList<HeimatTask> baseList = FXCollections.observableArrayList(externalProjects);
FilteredList<HeimatTask> filteredList = new FilteredList<>(baseList, t -> true);
// Name Column
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(filteredList);
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();
});
TextField searchField = new TextField();
searchField.setPromptText("Search...");
searchField.textProperty().addListener((obs, oldText, newText) -> {
String filter = newText == null ? "" : newText.trim().toLowerCase();
filteredList.setPredicate(task -> {
if (filter.isEmpty()) return true;
return task.taskHolderName().toLowerCase().contains(filter)
|| task.name().toLowerCase().contains(filter);
});
long visibleUnmapped = filteredList.stream().filter(unmappedHeimatTasks::contains).count();
selectAllUnmappedButton.setText("Select unmapped projects ("
+ visibleUnmapped + ")");
});
searchField.getStyleClass().add("text-field");
searchField.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(searchField, Priority.ALWAYS);
HBox headContent = new HBox(50, selectAllUnmappedButton, searchField);
VBox content = new VBox(10, headContent, 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 boolean showInvalidMappingsDialog(final List<String> warnings) {
Dialog<ButtonType> dialog = new Dialog<>();
dialog.initOwner(this.thisStage);
Stage dialogStage = (Stage) dialog.getDialogPane().getScene().getWindow();
dialogStage.getIcons().addAll(this.thisStage.getIcons());
dialog.setTitle("Invalid mappings");
dialog.setHeaderText("The following projects are no longer available.\n"
+ "Would you like to remove them from your mapping list?");
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);
ButtonType removeButton = new ButtonType("Remove", ButtonBar.ButtonData.YES);
ButtonType keepButton = new ButtonType("Keep", ButtonBar.ButtonData.NO);
dialog.getDialogPane().getButtonTypes().setAll(removeButton, keepButton);
Optional<ButtonType> result = dialog.showAndWait();
return result.isPresent() && result.get() == removeButton;
}
}