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.button;
18  
19  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
20  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
21  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIIconId;
22  import io.github.tonywasher.joceanus.tethys.core.button.TethysUICoreScrollButtonManager;
23  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
24  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
25  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingUtils;
26  import io.github.tonywasher.joceanus.tethys.swing.menu.TethysUISwingScrollMenu;
27  
28  import javax.swing.SwingConstants;
29  
30  /**
31   * Swing Button which provides a PopUpMenu selection.
32   *
33   * @param <T> the object type
34   */
35  public final class TethysUISwingScrollButtonManager<T>
36          extends TethysUICoreScrollButtonManager<T> {
37      /**
38       * Constructor.
39       *
40       * @param pFactory the GUI factory
41       * @param pClazz   the value class
42       */
43      public TethysUISwingScrollButtonManager(final TethysUICoreFactory<?> pFactory,
44                                              final Class<T> pClazz) {
45          /* Initialise the underlying class */
46          super(pFactory, pClazz);
47      }
48  
49      @Override
50      public TethysUISwingNode getNode() {
51          return (TethysUISwingNode) super.getNode();
52      }
53  
54      @Override
55      public void setVisible(final boolean pVisible) {
56          getNode().setVisible(pVisible);
57      }
58  
59      @Override
60      protected void registerListeners() {
61          /* Set context menu listener */
62          final OceanusEventRegistrar<TethysUIEvent> myRegistrar = getMenu().getEventRegistrar();
63          myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleMenuClosed());
64          myRegistrar.addEventListener(TethysUIEvent.WINDOWCLOSED, e -> handleMenuClosed());
65      }
66  
67      @Override
68      public TethysUISwingScrollMenu<T> getMenu() {
69          return (TethysUISwingScrollMenu<T>) super.getMenu();
70      }
71  
72      @Override
73      protected void showMenu() {
74          getMenu().showMenuAtPosition(getNode().getNode(), SwingConstants.BOTTOM);
75      }
76  
77      @Override
78      public void setIcon(final TethysUIIconId pId,
79                          final int pWidth) {
80          getButton().setIcon(TethysUISwingUtils.getIconAtSize(pId, pWidth));
81      }
82  
83      @Override
84      public void setPreferredWidth(final Integer pWidth) {
85          getNode().setPreferredWidth(pWidth);
86      }
87  
88      @Override
89      public void setPreferredHeight(final Integer pHeight) {
90          getNode().setPreferredHeight(pHeight);
91      }
92  
93      @Override
94      public void setBorderPadding(final Integer pPadding) {
95          super.setBorderPadding(pPadding);
96          getNode().createWrapperPane(getBorderTitle(), getBorderPadding());
97      }
98  
99      @Override
100     public void setBorderTitle(final String pTitle) {
101         super.setBorderTitle(pTitle);
102         getNode().createWrapperPane(getBorderTitle(), getBorderPadding());
103     }
104 }