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.swing.pane;
18  
19  import java.awt.Component;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.swing.Box;
24  import javax.swing.BoxLayout;
25  import javax.swing.JComponent;
26  import javax.swing.JPanel;
27  
28  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
29  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
30  import io.github.tonywasher.joceanus.tethys.core.pane.TethysUICoreBoxPaneManager;
31  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
32  
33  /**
34   * Swing Box Pane Manager.
35   */
36  public class TethysUISwingBoxPaneManager
37          extends TethysUICoreBoxPaneManager {
38      /**
39       * The Node.
40       */
41      private final TethysUISwingNode theNode;
42  
43      /**
44       * The Panel.
45       */
46      private final JPanel thePanel;
47  
48      /**
49       * NodeMap.
50       */
51      private final Map<Integer, JPanel> theNodeMap;
52  
53      /**
54       * Is the panel horizontal?
55       */
56      private final boolean isHorizontal;
57  
58      /**
59       * Constructor.
60       *
61       * @param pFactory    the factory
62       * @param pHorizontal horizontal box true/false
63       */
64      protected TethysUISwingBoxPaneManager(final TethysUICoreFactory<?> pFactory,
65                                            final boolean pHorizontal) {
66          super(pFactory);
67          isHorizontal = pHorizontal;
68          theNodeMap = new HashMap<>();
69  
70          /* Create the panel */
71          thePanel = new JPanel();
72          setLayout(thePanel);
73  
74          /* Create the node */
75          theNode = new TethysUISwingNode(thePanel);
76      }
77  
78      @Override
79      public TethysUISwingNode getNode() {
80          return theNode;
81      }
82  
83      @Override
84      public void setVisible(final boolean pVisible) {
85          thePanel.setVisible(pVisible);
86      }
87  
88      @Override
89      public void addNode(final TethysUIComponent pNode) {
90          super.addNode(pNode);
91          final JPanel myPanel = createContainer(TethysUISwingNode.getComponent(pNode));
92          theNodeMap.put(pNode.getId(), myPanel);
93          thePanel.add(myPanel);
94      }
95  
96      /**
97       * Create container panel.
98       *
99       * @param pNode the panel
100      * @return the container panel
101      */
102     private JPanel createContainer(final JComponent pNode) {
103         final JPanel myPanel = new JPanel();
104         setLayout(myPanel);
105         if (isHorizontal) {
106             pNode.setAlignmentY(Component.CENTER_ALIGNMENT);
107         } else {
108             pNode.setAlignmentX(Component.CENTER_ALIGNMENT);
109         }
110         myPanel.add(createStrut());
111         myPanel.add(pNode);
112         myPanel.add(createStrut());
113         return myPanel;
114     }
115 
116     /**
117      * Create strut.
118      *
119      * @return the strut
120      */
121     private Component createStrut() {
122         final int myGap = getGap() >> 1;
123         return isHorizontal
124                 ? Box.createHorizontalStrut(myGap)
125                 : Box.createVerticalStrut(myGap);
126     }
127 
128     /**
129      * Set layout.
130      *
131      * @param pPanel the panel
132      */
133     private void setLayout(final JPanel pPanel) {
134         pPanel.setLayout(new BoxLayout(pPanel, isHorizontal
135                 ? BoxLayout.X_AXIS
136                 : BoxLayout.Y_AXIS));
137     }
138 
139     @Override
140     public void setChildVisible(final TethysUIComponent pChild,
141                                 final boolean pVisible) {
142         /* Obtain the required node */
143         final JPanel myPanel = theNodeMap.get(pChild.getId());
144 
145         /* Set status */
146         if (myPanel != null) {
147             myPanel.setVisible(pVisible);
148         }
149     }
150 
151     @Override
152     public void setPreferredWidth(final Integer pWidth) {
153         theNode.setPreferredWidth(pWidth);
154     }
155 
156     @Override
157     public void setPreferredHeight(final Integer pHeight) {
158         theNode.setPreferredHeight(pHeight);
159     }
160 
161     @Override
162     public void setBorderPadding(final Integer pPadding) {
163         super.setBorderPadding(pPadding);
164         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
165     }
166 
167     @Override
168     public void setBorderTitle(final String pTitle) {
169         super.setBorderTitle(pTitle);
170         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
171     }
172 
173     @Override
174     public void addSpacer() {
175         thePanel.add(isHorizontal
176                 ? Box.createHorizontalGlue()
177                 : Box.createVerticalGlue());
178     }
179 
180     @Override
181     public void addStrut() {
182         final JPanel myPanel = new JPanel();
183         setLayout(myPanel);
184         myPanel.add(createStrut());
185         myPanel.add(createStrut());
186         myPanel.add(createStrut());
187         myPanel.add(createStrut());
188         thePanel.add(myPanel);
189     }
190 }