1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36 public class TethysUISwingBoxPaneManager
37 extends TethysUICoreBoxPaneManager {
38
39
40
41 private final TethysUISwingNode theNode;
42
43
44
45
46 private final JPanel thePanel;
47
48
49
50
51 private final Map<Integer, JPanel> theNodeMap;
52
53
54
55
56 private final boolean isHorizontal;
57
58
59
60
61
62
63
64 protected TethysUISwingBoxPaneManager(final TethysUICoreFactory<?> pFactory,
65 final boolean pHorizontal) {
66 super(pFactory);
67 isHorizontal = pHorizontal;
68 theNodeMap = new HashMap<>();
69
70
71 thePanel = new JPanel();
72 setLayout(thePanel);
73
74
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
98
99
100
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
118
119
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
130
131
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
143 final JPanel myPanel = theNodeMap.get(pChild.getId());
144
145
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 }