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.base;
18
19 import java.awt.Dimension;
20 import javax.swing.JComponent;
21 import javax.swing.JLabel;
22 import javax.swing.JPanel;
23
24 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
25 import io.github.tonywasher.joceanus.tethys.api.base.TethysUINode;
26
27 /**
28 * Swing Node instance.
29 */
30 public class TethysUISwingNode
31 implements TethysUINode {
32 /**
33 * The underlying node.
34 */
35 private final JComponent theUnderlying;
36
37 /**
38 * The node.
39 */
40 private JComponent theNode;
41
42 /**
43 * Constructor.
44 *
45 * @param pComponent the component
46 */
47 public TethysUISwingNode(final JComponent pComponent) {
48 theUnderlying = pComponent;
49 theNode = theUnderlying;
50 }
51
52 /**
53 * Obtain the underlying node.
54 *
55 * @return the icon
56 */
57 public JComponent getNode() {
58 return theNode;
59 }
60
61 /**
62 * Obtain the child component.
63 *
64 * @param pChild the Tethys child component
65 * @return the Swing component.
66 */
67 public static JComponent getComponent(final TethysUIComponent pChild) {
68 return pChild == null
69 ? null
70 : ((TethysUISwingNode) pChild.getNode()).getNode();
71 }
72
73 /**
74 * Is the node visible?
75 *
76 * @return true/false
77 */
78 public boolean isVisible() {
79 return theNode.isVisible();
80 }
81
82 /**
83 * Set visibility of node.
84 *
85 * @param pVisible set node visible true/false?
86 */
87 public void setVisible(final boolean pVisible) {
88 theNode.setVisible(pVisible);
89 }
90
91 /**
92 * Set the preferred width of the underlying node.
93 *
94 * @param pWidth the preferred width
95 */
96 public void setPreferredWidth(final Integer pWidth) {
97 Dimension myDim = theUnderlying.getPreferredSize();
98 myDim = new Dimension(pWidth, myDim.height);
99 theUnderlying.setPreferredSize(myDim);
100 }
101
102 /**
103 * Set the preferred height of the underlying node.
104 *
105 * @param pHeight the preferred height
106 */
107 public void setPreferredHeight(final Integer pHeight) {
108 Dimension myDim = theUnderlying.getPreferredSize();
109 myDim = new Dimension(myDim.width, pHeight);
110 theUnderlying.setPreferredSize(myDim);
111 }
112
113 /**
114 * create wrapper pane.
115 *
116 * @param pTitle the title
117 * @param pPadding the padding
118 */
119 public void createWrapperPane(final String pTitle,
120 final Integer pPadding) {
121 /* If the underlying node is a JPanel/JLabel */
122 if (theUnderlying instanceof JPanel
123 || theUnderlying instanceof JLabel) {
124 /* Set the panel border */
125 TethysUISwingUtils.setPanelBorder(pTitle, pPadding, theUnderlying);
126
127 /* Else add a bordered panel */
128 } else {
129 theNode = TethysUISwingUtils.addPanelBorder(pTitle, pPadding, theUnderlying);
130 }
131 }
132 }