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.core.button.TethysUICoreDateButtonManager;
22  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
23  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
24  
25  import javax.swing.JButton;
26  import java.awt.Insets;
27  
28  /**
29   * JavaFX DateButton Manager.
30   */
31  public final class TethysUISwingDateButtonManager
32          extends TethysUICoreDateButtonManager {
33      /**
34       * The dialog.
35       */
36      private TethysUISwingDateDialog theDialog;
37  
38      /**
39       * Constructor.
40       *
41       * @param pFactory the GuiFactory
42       */
43      TethysUISwingDateButtonManager(final TethysUICoreFactory<?> pFactory) {
44          /* Initialise the super-class */
45          super(pFactory);
46  
47          /* Set narrow margin */
48          getTheButton().setMargin(new Insets(1, 1, 1, 1));
49      }
50  
51      @Override
52      public TethysUISwingNode getNode() {
53          return (TethysUISwingNode) super.getNode();
54      }
55  
56      /**
57       * Obtain the button.
58       *
59       * @return the button
60       */
61      private JButton getTheButton() {
62          return (JButton) getNode().getNode();
63      }
64  
65      @Override
66      public void setVisible(final boolean pVisible) {
67          getNode().setVisible(pVisible);
68      }
69  
70      /**
71       * Obtain the dialog.
72       *
73       * @return the dialog
74       */
75      public TethysUISwingDateDialog getDialog() {
76          ensureDialog();
77          return theDialog;
78      }
79  
80      /**
81       * Make sure that the dialog is created.
82       */
83      private void ensureDialog() {
84          /* If the dialog does not exist */
85          if (theDialog == null) {
86              /* Create it */
87              theDialog = new TethysUISwingDateDialog(getConfig());
88  
89              /* Add listeners */
90              final OceanusEventRegistrar<TethysUIEvent> myRegistrar = theDialog.getEventRegistrar();
91              myRegistrar.addEventListener(TethysUIEvent.PREPAREDIALOG, e -> handleDialogRequest());
92              myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewValue());
93              myRegistrar.addEventListener(TethysUIEvent.WINDOWCLOSED, e -> handleNewValue());
94          }
95      }
96  
97      @Override
98      protected void showDialog() {
99          /* Make sure that the dialog exists */
100         ensureDialog();
101 
102         /* Show the dialog under the node */
103         theDialog.showDialogUnderNode(getNode().getNode());
104     }
105 
106     @Override
107     public void setPreferredWidth(final Integer pWidth) {
108         getNode().setPreferredWidth(pWidth);
109     }
110 
111     @Override
112     public void setPreferredHeight(final Integer pHeight) {
113         getNode().setPreferredWidth(pHeight);
114     }
115 
116     @Override
117     public void setBorderPadding(final Integer pPadding) {
118         super.setBorderPadding(pPadding);
119         getNode().createWrapperPane(getBorderTitle(), getBorderPadding());
120     }
121 
122     @Override
123     public void setBorderTitle(final String pTitle) {
124         super.setBorderTitle(pTitle);
125         getNode().createWrapperPane(getBorderTitle(), getBorderPadding());
126     }
127 }