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 javax.swing.JTabbedPane;
20  
21  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
22  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
23  import io.github.tonywasher.joceanus.tethys.core.pane.TethysUICoreTabPaneManager;
24  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
25  
26  /**
27   * Swing Tab Manager.
28   */
29  public class TethysUISwingTabPaneManager
30          extends TethysUICoreTabPaneManager {
31      /**
32       * The Node.
33       */
34      private final TethysUISwingNode theNode;
35  
36      /**
37       * The TabPane.
38       */
39      private final JTabbedPane theTabPane;
40  
41      /**
42       * Constructor.
43       *
44       * @param pFactory the factory
45       */
46      protected TethysUISwingTabPaneManager(final TethysUICoreFactory<?> pFactory) {
47          /* Initialise underlying class */
48          super(pFactory);
49  
50          /* Create the pane */
51          theTabPane = new JTabbedPane();
52          theTabPane.addChangeListener(e -> notifySelection(getSelectedTab()));
53  
54          /* Create the node */
55          theNode = new TethysUISwingNode(theTabPane);
56      }
57  
58      @Override
59      public TethysUISwingNode getNode() {
60          return theNode;
61      }
62  
63      /**
64       * Obtain the tabPane.
65       *
66       * @return the tabPane
67       */
68      JTabbedPane getTabPane() {
69          return theTabPane;
70      }
71  
72      @Override
73      public TethysUISwingTabItem findItemByName(final String pName) {
74          return (TethysUISwingTabItem) super.findItemByName(pName);
75      }
76  
77      @Override
78      public TethysUISwingTabItem findItemByIndex(final int pIndex) {
79          return (TethysUISwingTabItem) super.findItemByIndex(pIndex);
80      }
81  
82      @Override
83      public TethysUISwingTabItem addTabItem(final String pName,
84                                             final TethysUIComponent pItem) {
85          return new TethysUISwingTabItem(this, pName, pItem);
86      }
87  
88      @Override
89      protected void enablePane(final boolean pEnabled) {
90          theTabPane.setEnabled(pEnabled);
91      }
92  
93      @Override
94      public TethysUISwingTabItem getSelectedTab() {
95          return findItemByIndex(theTabPane.getSelectedIndex());
96      }
97  
98      @Override
99      public void setVisible(final boolean pVisible) {
100         theNode.setVisible(pVisible);
101     }
102 
103     @Override
104     public void setPreferredWidth(final Integer pWidth) {
105         theNode.setPreferredWidth(pWidth);
106     }
107 
108     @Override
109     public void setPreferredHeight(final Integer pHeight) {
110         theNode.setPreferredHeight(pHeight);
111     }
112 
113     @Override
114     public void setBorderPadding(final Integer pPadding) {
115         super.setBorderPadding(pPadding);
116         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
117     }
118 
119     @Override
120     public void setBorderTitle(final String pTitle) {
121         super.setBorderTitle(pTitle);
122         theNode.createWrapperPane(getBorderTitle(), getBorderPadding());
123     }
124 
125     /**
126      * TabItem class.
127      */
128     public static class TethysUISwingTabItem
129             extends TethysUICoreTabItem {
130         /**
131          * The component.
132          */
133         private final TethysUIComponent theComponent;
134 
135         /**
136          * Constructor.
137          *
138          * @param pPane the containing pane
139          * @param pName the name of the tab
140          * @param pItem the item
141          */
142         TethysUISwingTabItem(final TethysUISwingTabPaneManager pPane,
143                              final String pName,
144                              final TethysUIComponent pItem) {
145             /* Initialise the underlying class */
146             super(pPane, pName);
147 
148             /* Add to the TabPane */
149             theComponent = pItem;
150             pPane.getTabPane().addTab(pName, TethysUISwingNode.getComponent(theComponent));
151         }
152 
153         @Override
154         public TethysUISwingTabPaneManager getPane() {
155             return (TethysUISwingTabPaneManager) super.getPane();
156         }
157 
158         @Override
159         protected void attachToPane() {
160             final int myIndex = countPreviousVisibleSiblings();
161             getPane().getTabPane().insertTab(getName(), null,
162                     TethysUISwingNode.getComponent(theComponent), null, myIndex);
163         }
164 
165         @Override
166         protected void detachFromPane() {
167             final int myIndex = countPreviousVisibleSiblings();
168             getPane().getTabPane().remove(myIndex);
169         }
170 
171         @Override
172         public void selectItem() {
173             if (isVisible()) {
174                 final int myIndex = countPreviousVisibleSiblings();
175                 getPane().getTabPane().setSelectedIndex(myIndex);
176             }
177         }
178 
179         @Override
180         protected void enableTab(final boolean pEnabled) {
181             final int myIndex = countPreviousVisibleSiblings();
182             getPane().getTabPane().setEnabledAt(myIndex, pEnabled);
183             theComponent.setEnabled(pEnabled);
184         }
185     }
186 }