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.dialog;
18  
19  import io.github.tonywasher.joceanus.tethys.core.dialog.TethysUICoreAboutBox;
20  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
21  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
22  
23  import javax.swing.BorderFactory;
24  import javax.swing.JDialog;
25  import javax.swing.JFrame;
26  import java.awt.Color;
27  import java.awt.Dialog.ModalityType;
28  
29  /**
30   * Swing About Box.
31   */
32  public class TethysUISwingAboutBox
33          extends TethysUICoreAboutBox {
34      /**
35       * The Frame.
36       */
37      private final JFrame theFrame;
38  
39      /**
40       * The dialog stage.
41       */
42      private JDialog theDialog;
43  
44      /**
45       * Constructor.
46       *
47       * @param pFactory the GUI factory
48       * @param pFrame   the frame
49       */
50      TethysUISwingAboutBox(final TethysUICoreFactory<?> pFactory,
51                            final JFrame pFrame) {
52          /* Initialise underlying class */
53          super(pFactory);
54          if (pFrame == null) {
55              throw new IllegalArgumentException("Cannot create Dialog during initialisation");
56          }
57  
58          /* Store parameters */
59          theFrame = pFrame;
60      }
61  
62      @Override
63      public TethysUISwingNode getNode() {
64          return (TethysUISwingNode) super.getNode();
65      }
66  
67      @Override
68      public void setPreferredWidth(final Integer pWidth) {
69          getNode().setPreferredWidth(pWidth);
70      }
71  
72      @Override
73      public void setPreferredHeight(final Integer pHeight) {
74          getNode().setPreferredHeight(pHeight);
75      }
76  
77      @Override
78      public void showDialog() {
79          /* If we have not made the dialog yet */
80          if (theDialog == null) {
81              makeDialog();
82          }
83  
84          /* Show the dialog */
85          theDialog.setVisible(true);
86      }
87  
88      /**
89       * Make the dialog.
90       */
91      private void makeDialog() {
92          /* Create the dialog */
93          theDialog = new JDialog(theFrame);
94          theDialog.setUndecorated(true);
95          theDialog.setModalityType(ModalityType.APPLICATION_MODAL);
96  
97          /* Create simple border */
98          getNode().getNode().setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
99  
100         /* Attach the node to the dialog */
101         theDialog.getContentPane().add(getNode().getNode());
102         theDialog.pack();
103         theDialog.setLocationRelativeTo(theFrame);
104     }
105 
106     @Override
107     protected void closeDialog() {
108         theDialog.setVisible(false);
109     }
110 }