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