NamedCardPane.java
001 /*
002  * SPDX-License-Identifier: Apache-2.0
003  *
004  * Copyright 2008-2017 the original author or authors.
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 package griffon.javafx.scene.layout;
019 
020 import javafx.application.Platform;
021 import javafx.beans.property.ObjectProperty;
022 import javafx.beans.property.ReadOnlyObjectProperty;
023 import javafx.beans.property.ReadOnlyStringProperty;
024 import javafx.beans.property.SimpleObjectProperty;
025 import javafx.beans.property.SimpleStringProperty;
026 import javafx.beans.property.StringProperty;
027 import javafx.collections.ListChangeListener;
028 import javafx.scene.Node;
029 import javafx.scene.layout.Region;
030 import javafx.scene.layout.StackPane;
031 
032 import javax.annotation.Nonnull;
033 import javax.annotation.Nullable;
034 import java.util.Map;
035 import java.util.concurrent.ConcurrentHashMap;
036 import java.util.concurrent.atomic.AtomicBoolean;
037 
038 import static griffon.util.GriffonClassUtils.requireState;
039 import static griffon.util.GriffonNameUtils.isBlank;
040 import static griffon.util.GriffonNameUtils.requireNonBlank;
041 import static java.util.Objects.requireNonNull;
042 
043 /**
044  @author Andres Almiray
045  @since 2.11.0
046  */
047 public class NamedCardPane extends StackPane {
048     private static final String ERROR_NODE_NULL = "Argument 'node' must not be null";
049     private static final String ERROR_ID_NULL = "Argument 'id' must not be null";
050 
051     private final Map<String, Node> nodes = new ConcurrentHashMap<>();
052     private final StringProperty selectedNodeId = new SimpleStringProperty(this, "selectedNodeId");
053     private final ObjectProperty<Node> selectedNode = new SimpleObjectProperty<>(this, "selectedNode");
054     private final AtomicBoolean adjusting = new AtomicBoolean(false);
055 
056     public NamedCardPane() {
057         getStyleClass().add("named-cardpane");
058 
059         getChildren().addListener((ListChangeListener<Node>c -> {
060             if (adjusting.get()) {
061                 return;
062             }
063 
064             while (c.next()) {
065                 if (c.wasAdded()) {
066                     Node lastAddedNode = null;
067                     for (Node node : c.getAddedSubList()) {
068                         String id = node.getId();
069                         requireNonBlank(id, ERROR_ID_NULL);
070                         if (!nodes.containsKey(id)) {
071                             nodes.put(id, node);
072                             lastAddedNode = node;
073                         }
074                     }
075 
076                     if (lastAddedNode != null) {
077                         String nextId = lastAddedNode.getId();
078                         doShow(nextId);
079                     }
080 
081                 else if (c.wasRemoved()) {
082                     String selectedId = getSelectedNodeId();
083                     boolean selectionChanged = false;
084                     for (Node node : c.getRemoved()) {
085                         String id = node.getId();
086                         requireNonBlank(id, ERROR_ID_NULL);
087                         if (!nodes.containsKey(id)) {
088                             continue;
089                         }
090 
091                         nodes.remove(id);
092 
093                         if (id.equals(selectedId)) {
094                             selectionChanged = true;
095                         }
096                     }
097 
098                     if (selectionChanged) {
099                         final String nextId = nodes.size() ? nodes.keySet().iterator().next() null;
100                         doShow(nextId);
101                     }
102                 }
103             }
104         });
105 
106         widthProperty().addListener((observable, oldValue, newValue-> updateBoundsInChildren());
107         heightProperty().addListener((observable, oldValue, newValue-> updateBoundsInChildren());
108     }
109 
110     protected void updateBoundsInChildren() {
111         nodes.values().forEach(this::updateChildBounds);
112         layout();
113     }
114 
115     protected void updateChildBounds(Node node) {
116         if (node instanceof Region) {
117             Region child = (Regionnode;
118             child.setPrefWidth(getWidth());
119             child.setPrefHeight(getHeight());
120         }
121     }
122 
123     @Nonnull
124     public ReadOnlyStringProperty selectedNodeIdProperty() {
125         return selectedNodeId;
126     }
127 
128     @Nonnull
129     public ReadOnlyObjectProperty<Node> selectedNodeProperty() {
130         return selectedNode;
131     }
132 
133     @Nullable
134     public String getSelectedNodeId() {
135         return selectedNodeId.get();
136     }
137 
138     @Nullable
139     public Node getSelectedNode() {
140         return selectedNode.get();
141     }
142 
143     public boolean isEmpty() {
144         return nodes.isEmpty();
145     }
146 
147     public int size() {
148         return nodes.size();
149     }
150 
151     public void clear() {
152         nodes.clear();
153         getChildren().clear();
154         selectedNodeId.set(null);
155         selectedNode.set(null);
156     }
157 
158     public void add(@Nonnull final String id, @Nonnull final Node node) {
159         requireNonNull(node, ERROR_NODE_NULL);
160         requireNonBlank(id, ERROR_ID_NULL);
161 
162         adjusting.set(true);
163         nodes.put(id, node);
164         adjusting.set(false);
165 
166         show(id);
167     }
168 
169     public void remove(@Nonnull String id) {
170         requireNonBlank(id, ERROR_ID_NULL);
171         if (!nodes.containsKey(id)) {
172             return;
173         }
174 
175         String selectedId = getSelectedNodeId();
176         adjusting.set(true);
177         nodes.remove(id);
178         adjusting.set(false);
179 
180         if (id.equals(selectedId)) {
181             String nextId = null;
182             if (nodes.size() 0) {
183                 nextId = nodes.keySet().iterator().next();
184             }
185             doShow(nextId);
186         }
187     }
188 
189     public void show(@Nonnull String id) {
190         requireNonBlank(id, ERROR_ID_NULL);
191         requireState(nodes.containsKey(id)"No content associated with id '" + id + "'");
192         doShow(id);
193     }
194 
195     protected void doShow(@Nullable String id) {
196         Platform.runLater(() -> {
197             if (isBlank(id)) {
198                 adjusting.set(true);
199                 getChildren().clear();
200                 adjusting.set(false);
201                 selectedNodeId.set(null);
202                 selectedNode.set(null);
203             else {
204                 Node node = nodes.get(id);
205                 adjusting.set(true);
206                 getChildren().setAll(node);
207                 adjusting.set(false);
208                 selectedNodeId.set(id);
209                 selectedNode.set(node);
210             }
211         });
212     }
213 }