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.javafx.dialog;
18  
19  import javafx.scene.Scene;
20  import javafx.scene.layout.Region;
21  import javafx.stage.Modality;
22  import javafx.stage.Stage;
23  import javafx.stage.StageStyle;
24  import io.github.tonywasher.joceanus.tethys.core.dialog.TethysUICoreAboutBox;
25  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
26  import io.github.tonywasher.joceanus.tethys.javafx.base.TethysUIFXNode;
27  
28  /**
29   * javaFX About Box.
30   */
31  public class TethysUIFXAboutBox
32          extends TethysUICoreAboutBox {
33      /**
34       * approximate width.
35       */
36      private static final int APPROX_WIDTH = 200;
37  
38      /**
39       * approximate height.
40       */
41      private static final int APPROX_HEIGHT = 100;
42  
43      /**
44       * The Scene register.
45       */
46      private final TethysUIFXSceneRegister theSceneRegister;
47  
48      /**
49       * The underlying stage.
50       */
51      private final Stage theStage;
52  
53      /**
54       * The dialog panel.
55       */
56      private final Region thePanel;
57  
58      /**
59       * The dialog stage.
60       */
61      private Stage theDialog;
62  
63      /**
64       * Constructor.
65       *
66       * @param pFactory the GUI factory
67       * @param pStage   the stage
68       */
69      TethysUIFXAboutBox(final TethysUICoreFactory<?> pFactory,
70                         final Stage pStage) {
71          /* Initialise underlying class */
72          super(pFactory);
73          if (pStage == null) {
74              throw new IllegalArgumentException("Cannot create Dialog during initialisation");
75          }
76  
77          /* Store parameters */
78          theSceneRegister = (TethysUIFXSceneRegister) pFactory;
79          theStage = pStage;
80          thePanel = (Region) getNode().getNode();
81      }
82  
83      @Override
84      public TethysUIFXNode getNode() {
85          return (TethysUIFXNode) super.getNode();
86      }
87  
88      @Override
89      public void showDialog() {
90          /* If we have not made the dialog yet */
91          if (theDialog == null) {
92              makeDialog();
93          }
94  
95          /* Centre on parent */
96          final double myX = (theStage.getWidth() - APPROX_WIDTH) / 2;
97          final double myY = (theStage.getHeight() - APPROX_HEIGHT) / 2;
98          theDialog.setX(theStage.getX() + myX);
99          theDialog.setY(theStage.getY() + myY);
100 
101         /* Show the dialog */
102         theDialog.show();
103     }
104 
105     /**
106      * Make the dialog.
107      */
108     private void makeDialog() {
109         /* Create the dialog */
110         theDialog = new Stage(StageStyle.UNDECORATED);
111         theDialog.initOwner(theStage);
112         theDialog.initModality(Modality.WINDOW_MODAL);
113 
114         /* Define style of Box */
115         thePanel.getStyleClass().add("-jtethys-about");
116 
117         /* Create the scene and attach it */
118         final Scene myScene = new Scene(thePanel);
119         theSceneRegister.registerScene(myScene);
120         theDialog.setScene(myScene);
121     }
122 
123     @Override
124     public void setPreferredWidth(final Integer pWidth) {
125         thePanel.setPrefWidth(pWidth);
126     }
127 
128     @Override
129     public void setPreferredHeight(final Integer pHeight) {
130         thePanel.setPrefHeight(pHeight);
131     }
132 
133     @Override
134     protected void closeDialog() {
135         theDialog.close();
136     }
137 }