|
20 | 20 | import de.doubleslash.keeptime.exceptions.FXMLLoaderException; |
21 | 21 | import de.doubleslash.keeptime.model.Model; |
22 | 22 | import de.doubleslash.keeptime.model.Project; |
| 23 | +import javafx.collections.ListChangeListener; |
23 | 24 | import javafx.collections.ObservableList; |
24 | 25 | import javafx.collections.transformation.FilteredList; |
25 | 26 | import javafx.fxml.FXMLLoader; |
@@ -69,18 +70,23 @@ public ProjectsListViewController(final Model model, final Controller controller |
69 | 70 | this.hideable = hideable; |
70 | 71 | this.mainStage = mainStage; |
71 | 72 | this.availableProjectsListView = availableProjectsListView; |
72 | | - availableProjectsListView.setCellFactory(callback -> returnListCellOfProject()); |
| 73 | + availableProjectsListView.setCellFactory(listView -> returnListCellOfProject()); |
73 | 74 |
|
74 | 75 | filteredData = new FilteredList<>(model.getSortedAvailableProjects(), p -> true); |
75 | 76 | availableProjectsListView.setItems(filteredData); |
76 | 77 |
|
77 | 78 | projectSelectionNodeMap = new HashMap<>(model.getAvailableProjects().size()); |
78 | 79 |
|
79 | | - for (final Project project : model.getSortedAvailableProjects()) { |
80 | | - if (project.isEnabled()) { |
81 | | - final Node node = addProjectToProjectList(project); |
82 | | - projectSelectionNodeMap.put(project, node); |
| 80 | + model.getSortedAvailableProjects().addListener((ListChangeListener<? super Project>) listener -> { |
| 81 | + listener.next(); |
| 82 | + if (listener.wasAdded()) { |
| 83 | + final Project addedProject = listener.getAddedSubList().get(0); |
| 84 | + addProjectToProjectSelectionNodeMap(addedProject); |
83 | 85 | } |
| 86 | + }); |
| 87 | + |
| 88 | + for (final Project project : model.getSortedAvailableProjects()) { |
| 89 | + addProjectToProjectSelectionNodeMap(project); |
84 | 90 | } |
85 | 91 |
|
86 | 92 | // TODO why is there no nice way for listview height? |
@@ -151,13 +157,26 @@ public ProjectsListViewController(final Model model, final Controller controller |
151 | 157 | searchTextField.setPromptText("Search"); |
152 | 158 | } |
153 | 159 |
|
| 160 | + private void addProjectToProjectSelectionNodeMap(final Project project) { |
| 161 | + if (project.isEnabled()) { |
| 162 | + final Pane projectElement = addProjectToProjectList(project); |
| 163 | + final Label projectNameLabel = (Label) projectElement.getChildren().get(0); |
| 164 | + projectNameLabel.setTooltip(new Tooltip(projectNameLabel.getText())); |
| 165 | + projectSelectionNodeMap.put(project, projectElement); |
| 166 | + } |
| 167 | + } |
| 168 | + |
154 | 169 | public void changeProject(final Project newProject, final long minusSeconds) { |
155 | 170 | if (hideable) { |
156 | 171 | mainStage.hide(); |
157 | 172 | } |
158 | 173 | controller.changeProject(newProject, minusSeconds); |
159 | 174 | } |
160 | 175 |
|
| 176 | + public Map<Project, Node> getProjectSelectionNodeMap() { |
| 177 | + return projectSelectionNodeMap; |
| 178 | + } |
| 179 | + |
161 | 180 | private GridPane setUpGridPane(final String projectName, final Color projectColor, final boolean isWork) { |
162 | 181 | final GridPane grid = new GridPane(); |
163 | 182 | grid.setHgap(10); |
@@ -203,7 +222,7 @@ private void realignProjectList() { |
203 | 222 | model.getSortedAvailableProjects().setComparator(comparator); |
204 | 223 | } |
205 | 224 |
|
206 | | - private Node addProjectToProjectList(final Project p) { |
| 225 | + private Pane addProjectToProjectList(final Project p) { |
207 | 226 | final ContextMenu contextMenu = new ContextMenu(); |
208 | 227 |
|
209 | 228 | final FXMLLoader loader = new FXMLLoader(); |
@@ -375,10 +394,6 @@ protected void updateItem(final Project item, final boolean empty) { |
375 | 394 | if (item == null || empty) { |
376 | 395 | setGraphic(null); |
377 | 396 | } else { |
378 | | - LOG.debug("Adding tooltip to Project Label."); |
379 | | - final Pane pane = (Pane) projectSelectionNodeMap.get(item); |
380 | | - final Label projectNameLabel = (Label) pane.getChildren().get(0); |
381 | | - projectNameLabel.setTooltip(new Tooltip(item.getName())); |
382 | 397 | LOG.trace("Item: '{}' -> '{}'", item.getName(), projectSelectionNodeMap.get(item)); |
383 | 398 | setGraphic(projectSelectionNodeMap.get(item)); |
384 | 399 | } |
|
0 commit comments