View Javadoc
1   /*
2    * Tethys: GUI Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.tethys.javafx.pane;
18  
19  import java.util.Iterator;
20  
21  import javafx.scene.Node;
22  import javafx.scene.layout.FlowPane;
23  
24  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
25  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
26  import io.github.tonywasher.joceanus.tethys.core.pane.TethysUICoreFlowPaneManager;
27  import io.github.tonywasher.joceanus.tethys.javafx.base.TethysUIFXNode;
28  
29  /**
30   * FX Flow Pane Manager.
31   */
32  public class TethysUIFXFlowPaneManager
33          extends TethysUICoreFlowPaneManager {
34      /**
35       * The Node.
36       */
37      private final TethysUIFXNode theNode;
38  
39      /**
40       * The FlowPane.
41       */
42      private final FlowPane theFlowPane;
43  
44      /**
45       * Constructor.
46       *
47       * @param pFactory the factory
48       */
49      TethysUIFXFlowPaneManager(final TethysUICoreFactory<?> pFactory) {
50          super(pFactory);
51          theFlowPane = new FlowPane();
52          theNode = new TethysUIFXNode(theFlowPane);
53      }
54  
55      @Override
56      public TethysUIFXNode getNode() {
57          return theNode;
58      }
59  
60      @Override
61      public void setVisible(final boolean pVisible) {
62          theNode.setManaged(pVisible);
63          theNode.setVisible(pVisible);
64      }
65  
66      @Override
67      public void addNode(final TethysUIComponent pNode) {
68          super.addNode(pNode);
69          theFlowPane.getChildren().add(TethysUIFXNode.getNode(pNode));
70      }
71  
72      @Override
73      public void setChildVisible(final TethysUIComponent pChild,
74                                  final boolean pVisible) {
75          /* Handle nothing to do */
76          final Node myChildNode = TethysUIFXNode.getNode(pChild);
77          final boolean isVisible = myChildNode.isVisible();
78          if (isVisible == pVisible) {
79              return;
80          }
81  
82          /* If the node is not visible */
83          if (pVisible) {
84              /* Count visible prior siblings */
85              final int myId = pChild.getId();
86              int myIndex = 0;
87              final Iterator<TethysUIComponent> myIterator = iterator();
88              while (myIterator.hasNext()) {
89                  final TethysUIComponent myNode = myIterator.next();
90                  final Integer myNodeId = myNode.getId();
91  
92                  /* If we have found the node */
93                  if (myNodeId == myId) {
94                      /* Set visible and add into the list */
95                      myChildNode.setVisible(true);
96                      theFlowPane.getChildren().add(myIndex, myChildNode);
97                      break;
98                  }
99  
100                 /* Increment count if node is visible */
101                 if (TethysUIFXNode.getNode(myNode).isVisible()) {
102                     myIndex++;
103                 }
104             }
105 
106             /* else we must hide the node */
107         } else {
108             /* set invisible and remove from the list */
109             myChildNode.setVisible(false);
110             theFlowPane.getChildren().remove(myChildNode);
111         }
112     }
113 
114     @Override
115     public void setPreferredWidth(final Integer pWidth) {
116         theFlowPane.setPrefWidth(pWidth);
117     }
118 
119     @Override
120     public void setPreferredHeight(final Integer pHeight) {
121         theFlowPane.setPrefHeight(pHeight);
122     }
123 
124     @Override
125     public void setBorderPadding(final Integer pPadding) {
126         super.setBorderPadding(pPadding);
127         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
128     }
129 
130     @Override
131     public void setBorderTitle(final String pTitle) {
132         super.setBorderTitle(pTitle);
133         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
134     }
135 }