View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/steelblue-src
22   * git clone https://github.com/tidalwave-it/steelblue-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.ui.javafx;
27  
28  import jakarta.annotation.Nonnull;
29  import java.util.WeakHashMap;
30  import javafx.scene.Node;
31  import javafx.scene.layout.StackPane;
32  import lombok.extern.slf4j.Slf4j;
33  
34  /***************************************************************************************************************************************************************
35   *
36   * A facility that is used to manage areas in the UI where multiple contents should appear in a mutually exclusive
37   * way.
38   *
39   * @author Fabrizio Giudici
40   *
41   **************************************************************************************************************************************************************/
42  @Slf4j
43  public class StackPaneSelector // FIXME: rename, introduce interface
44    {
45      private final WeakHashMap<String, StackPane> stackPaneMapByArea = new WeakHashMap<>();
46  
47      /***********************************************************************************************************************************************************
48       * Register a new area associated to a {@link StackPane}.
49       *
50       * @param   area        the area name
51       * @param   stackPane   the {@code StackPane}
52       **********************************************************************************************************************************************************/
53      public void registerArea (@Nonnull final String area, @Nonnull final StackPane stackPane)
54        {
55          log.debug("registerArea({}, {})", area, stackPane);
56          stackPaneMapByArea.put(area, stackPane);
57        }
58  
59      /***********************************************************************************************************************************************************
60       * Add a {@link Node} to a previously registered area.
61       *
62       * @param   area        the area name
63       * @param   node        the {@code Node}
64       **********************************************************************************************************************************************************/
65      public void add (@Nonnull final String area, @Nonnull final Node node)
66        {
67          node.setVisible(false);
68          findStackPaneFor(area).getChildren().add(node);
69        }
70  
71      /***********************************************************************************************************************************************************
72       * Sets the given {@link Node} as the shown one in the area where it is contained.
73       *
74       * @param   node        the {@code Node}
75       **********************************************************************************************************************************************************/
76      public void setShownNode (@Nonnull final Node node)
77        {
78          log.info("setShownNode({})", node);
79  
80          for (final var stackPane : stackPaneMapByArea.values())
81            {
82              final var children = stackPane.getChildren();
83  
84              if (children.contains(node))
85                {
86                  children.forEach(child -> child.setVisible(false));
87                  node.setVisible(true); // at last
88                  return;
89                }
90            }
91  
92          throw new IllegalArgumentException("Node not in a managed StackPange: " + node);
93  
94        }
95  
96      /***********************************************************************************************************************************************************
97       *
98       **********************************************************************************************************************************************************/
99      @Nonnull
100     private StackPane findStackPaneFor (@Nonnull final String area)
101       {
102         final var stackPane = stackPaneMapByArea.get(area);
103 
104         if (stackPane == null)
105           {
106             throw new IllegalArgumentException("Area not handled: " + area);
107           }
108 
109         return stackPane;
110       }
111  }