1 /*
   2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.stage;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import javafx.application.Platform;
  32 import javafx.beans.property.BooleanProperty;
  33 import javafx.beans.property.SimpleBooleanProperty;
  34 import javafx.beans.property.StringProperty;
  35 import javafx.beans.property.StringPropertyBase;
  36 import javafx.collections.ListChangeListener.Change;
  37 import javafx.collections.ObservableList;
  38 import javafx.geometry.NodeOrientation;
  39 import javafx.scene.Scene;
  40 import javafx.scene.image.Image;
  41 import javafx.scene.input.KeyCombination;
  42 
  43 import com.sun.javafx.collections.VetoableListDecorator;
  44 import com.sun.javafx.collections.TrackableObservableList;
  45 import com.sun.javafx.scene.SceneHelper;
  46 import com.sun.javafx.stage.StageHelper;
  47 import com.sun.javafx.stage.StagePeerListener;
  48 import com.sun.javafx.tk.TKStage;
  49 import com.sun.javafx.tk.Toolkit;
  50 import static com.sun.javafx.FXPermissions.CREATE_TRANSPARENT_WINDOW_PERMISSION;
  51 import javafx.beans.NamedArg;
  52 import javafx.beans.property.DoubleProperty;
  53 import javafx.beans.property.DoublePropertyBase;
  54 import javafx.beans.property.ObjectProperty;
  55 import javafx.beans.property.ReadOnlyBooleanProperty;
  56 import javafx.beans.property.ReadOnlyBooleanWrapper;
  57 import javafx.beans.property.SimpleObjectProperty;
  58 import javafx.beans.value.ObservableValue;
  59 
  60 /**
  61  * The JavaFX {@code Stage} class is the top level JavaFX container.
  62  * The primary Stage is constructed by the platform. Additional Stage
  63  * objects may be constructed by the application.
  64  *
  65  * <p>
  66  * Stage objects must be constructed and modified on the
  67  * JavaFX Application Thread.
  68  * </p>
  69  * <p>
  70  * The JavaFX Application Thread is created as part of the startup process for
  71  * the JavaFX runtime. See the {@link javafx.application.Application} class and
  72  * the {@link Platform#startup(Runnable)} method for more information.
  73  * </p>
  74  * <p>
  75  * Many of the {@code Stage} properties are read only because they can
  76  * be changed externally by the underlying platform and therefore must
  77  * not be bindable.
  78  * </p>
  79  *
  80  * <p><b>Style</b></p>
  81  * <p>
  82  * A stage has one of the following styles:
  83  * <ul>
  84  * <li>{@link StageStyle#DECORATED} - a stage with a solid white background and
  85  * platform decorations.</li>
  86  * <li>{@link StageStyle#UNDECORATED} - a stage with a solid white background
  87  * and no decorations.</li>
  88  * <li>{@link StageStyle#TRANSPARENT} - a stage with a transparent background
  89  * and no decorations.</li>
  90  * <li>{@link StageStyle#UTILITY} - a stage with a solid white background and
  91  * minimal platform decorations.</li>
  92  * </ul>
  93  * <p>The style must be initialized before the stage is made visible.</p>
  94  * <p>On some platforms decorations might not be available. For example, on
  95  * some mobile or embedded devices. In these cases a request for a DECORATED or
  96  * UTILITY window will be accepted, but no decorations will be shown. </p>
  97  *
  98  * <p><b>Owner</b></p>
  99  * <p>
 100  * A stage can optionally have an owner Window.
 101  * When a window is a stage's owner, it is said to be the parent of that stage.
 102  * <p>
 103  * Owned Stages are tied to the parent Window.
 104  * An owned stage will always be on top of its parent window.
 105  * When a parent window is closed or iconified, then all owned windows will be affected as well.
 106  * Owned Stages cannot be independantly iconified.
 107  * <p>
 108  * The owner must be initialized before the stage is made visible.
 109  *
 110  * <p><b>Modality</b></p>
 111  * <p>
 112  * A stage has one of the following modalities:
 113  * <ul>
 114  * <li>{@link Modality#NONE} - a stage that does not block any other window.</li>
 115  * <li>{@link Modality#WINDOW_MODAL} - a stage that blocks input events from
 116  * being delivered to all windows from its owner (parent) to its root.
 117  * Its root is the closest ancestor window without an owner.</li>
 118  * <li>{@link Modality#APPLICATION_MODAL} - a stage that blocks input events from
 119  * being delivered to all windows from the same application, except for those
 120  * from its child hierarchy.</li>
 121  * </ul>
 122  *
 123  * <p>When a window is blocked by a modal stage its Z-order relative to its ancestors
 124  * is preserved, and it receives no input events and no window activation events,
 125  * but continues to animate and render normally.
 126  * Note that showing a modal stage does not necessarily block the caller. The
 127  * {@link #show} method returns immediately regardless of the modality of the stage.
 128  * Use the {@link #showAndWait} method if you need to block the caller until
 129  * the modal stage is hidden (closed).
 130  * The modality must be initialized before the stage is made visible.</p>
 131  *
 132  * <p><b>Example:</b></p>
 133  *
 134  *
 135 <pre><code>
 136 import javafx.application.Application;
 137 import javafx.scene.Group;
 138 import javafx.scene.Scene;
 139 import javafx.scene.text.Font;
 140 import javafx.scene.text.Text;
 141 import javafx.stage.Stage;
 142 
 143 public class HelloWorld extends Application {
 144 
 145     {@literal @Override} public void start(Stage stage) {
 146         Text text = new Text(10, 40, "Hello World!");
 147         text.setFont(new Font(40));
 148         Scene scene = new Scene(new Group(text));
 149 
 150         stage.setTitle("Welcome to JavaFX!");
 151         stage.setScene(scene);
 152         stage.sizeToScene();
 153         stage.show();
 154     }
 155 
 156     public static void main(String[] args) {
 157         Application.launch(args);
 158     }
 159 }
 160 
 161  * </code></pre>
 162  * <p>produces the following on Windows:</p>
 163  * <p><img src="doc-files/Stage-win.png" alt="A visual rendering
 164      * of a JavaFX Stage on Windows"></p>
 165  *
 166  * <p>produces the following on Mac OSX:</p>
 167  * <p><img src="doc-files/Stage-mac.png" alt="A visual rendering
 168      * of a JavaFX Stage on Mac OSX"></p>
 169  *
 170  * <p>produces the following on Linux:</p>
 171  * <p><img src="doc-files/Stage-linux.png" alt="A visual rendering
 172      * of a JavaFX Stage on Linux"></p>
 173  * @since JavaFX 2.0
 174  */
 175 public class Stage extends Window {
 176 
 177     private boolean inNestedEventLoop = false;
 178 
 179     static {
 180         StageHelper.setStageAccessor(new StageHelper.StageAccessor() {
 181             @Override public void doVisibleChanging(Window window, boolean visible) {
 182                 ((Stage) window).doVisibleChanging(visible);
 183             }
 184 
 185             @Override public void doVisibleChanged(Window window, boolean visible) {
 186                 ((Stage) window).doVisibleChanged(visible);
 187             }
 188 
 189             @Override public void initSecurityDialog(Stage stage, boolean securityDialog) {
 190                 stage.initSecurityDialog(securityDialog);
 191             }
 192 
 193             @Override
 194             public void setPrimary(Stage stage, boolean primary) {
 195                 stage.setPrimary(primary);
 196             }
 197 
 198             @Override
 199             public void setImportant(Stage stage, boolean important) {
 200                 stage.setImportant(important);
 201             }
 202         });
 203     }
 204 
 205     private static final StagePeerListener.StageAccessor STAGE_ACCESSOR = new StagePeerListener.StageAccessor() {
 206 
 207         @Override
 208         public void setIconified(Stage stage, boolean iconified) {
 209             stage.iconifiedPropertyImpl().set(iconified);
 210         }
 211 
 212         @Override
 213         public void setMaximized(Stage stage, boolean maximized) {
 214             stage.maximizedPropertyImpl().set(maximized);
 215         }
 216 
 217         @Override
 218         public void setResizable(Stage stage, boolean resizable) {
 219             ((ResizableProperty)stage.resizableProperty()).setNoInvalidate(resizable);
 220         }
 221 
 222         @Override
 223         public void setFullScreen(Stage stage, boolean fs) {
 224             stage.fullScreenPropertyImpl().set(fs);
 225         }
 226 
 227         @Override
 228         public void setAlwaysOnTop(Stage stage, boolean aot) {
 229             stage.alwaysOnTopPropertyImpl().set(aot);
 230         }
 231     };
 232 
 233     /**
 234      * Creates a new instance of decorated {@code Stage}.
 235      *
 236      * @throws IllegalStateException if this constructor is called on a thread
 237      * other than the JavaFX Application Thread.
 238      */
 239     public Stage() {
 240         this(StageStyle.DECORATED);
 241     }
 242 
 243     /**
 244      * Creates a new instance of {@code Stage}.
 245      *
 246      * @param style The style of the {@code Stage}
 247      *
 248      * @throws IllegalStateException if this constructor is called on a thread
 249      * other than the JavaFX Application Thread.
 250      */
 251     public Stage(@NamedArg(value="style", defaultValue="DECORATED") StageStyle style) {
 252         super();
 253 
 254         Toolkit.getToolkit().checkFxUserThread();
 255 
 256         // Set the style
 257         initStyle(style);
 258         StageHelper.initHelper(this);
 259     }
 260 
 261     /**
 262      * Specify the scene to be used on this stage.
 263      */
 264     @Override final public void setScene(Scene value) {
 265         Toolkit.getToolkit().checkFxUserThread();
 266         super.setScene(value);
 267     }
 268 
 269     /**
 270      * {@inheritDoc}
 271      */
 272     @Override public final void show() {
 273         super.show();
 274     }
 275 
 276     private boolean primary = false;
 277 
 278     ////////////////////////////////////////////////////////////////////
 279 
 280     // Flag indicating that this stage is being used to show a security dialog
 281     private boolean securityDialog = false;
 282 
 283     /**
 284      * Sets a flag indicating that this stage is used for a security dialog and
 285      * must always be on top. If set, this will cause the window to be always
 286      * on top, regardless of the setting of the alwaysOnTop property, and
 287      * whether or not permissions are granted when the dialog is shown.
 288      * NOTE: this flag must be set prior to showing the stage the first time.
 289      *
 290      * @param securityDialog flag indicating that this Stage is being used to
 291      * show a security dialog that should be always-on-top
 292      *
 293      * @throws IllegalStateException if this property is set after the stage
 294      * has ever been made visible.
 295      *
 296      * @defaultValue false
 297      */
 298     final void initSecurityDialog(boolean securityDialog) {
 299         if (hasBeenVisible) {
 300             throw new IllegalStateException("Cannot set securityDialog once stage has been set visible");
 301         }
 302 
 303         this.securityDialog = securityDialog;
 304     }
 305 
 306     /**
 307      * Returns the state of the securityDialog flag.
 308      *
 309      * @return a flag indicating whether or not this is a security dialog
 310      */
 311     final boolean isSecurityDialog() {
 312         return securityDialog;
 313     }
 314 
 315     /*
 316      * Sets this stage to be the primary stage.
 317      */
 318     void setPrimary(boolean primary) {
 319         this.primary = primary;
 320     }
 321 
 322     /*
 323      * Returns whether this stage is the primary stage.
 324      *
 325      * @return true if this stage is the primary stage for the application.
 326      */
 327     boolean isPrimary() {
 328         return primary;
 329     }
 330 
 331     private boolean important = true;
 332 
 333     /*
 334      * Sets a flag indicating whether this stage is an "important" window for
 335      * the purpose of determining whether the application is idle and should
 336      * exit. The application is considered finished when the last important
 337      * window is closed.
 338      */
 339     void setImportant(boolean important) {
 340         this.important = important;
 341     }
 342 
 343     private boolean isImportant() {
 344         return important;
 345     }
 346 
 347     /**
 348      * Shows this stage and waits for it to be hidden (closed) before returning
 349      * to the caller. This method temporarily blocks processing of the current
 350      * event, and starts a nested event loop to handle other events.
 351      * This method must be called on the FX Application thread.
 352      * <p>
 353      * A Stage is hidden (closed) by one of the following means:
 354      * <ul>
 355      * <li>the application calls the {@link #hide} or {@link #close} method on
 356      * this stage</li>
 357      * <li>this stage has a non-null owner window, and its owner is closed</li>
 358      * <li>the user closes the window via the window system (for example,
 359      * by pressing the close button in the window decoration)</li>
 360      * </ul>
 361      *
 362      * <p>
 363      * After the Stage is hidden, and the application has returned from the
 364      * event handler to the event loop, the nested event loop terminates
 365      * and this method returns to the caller.
 366      * </p>
 367      * <p>
 368      * For example, consider the following sequence of operations for different
 369      * event handlers, assumed to execute in the order shown below:
 370      * <pre>void evtHander1(...) {
 371      *     stage1.showAndWait();
 372      *     doSomethingAfterStage1Closed(...)
 373      * }
 374      *
 375      * void evtHander2(...) {
 376      *     stage1.hide();
 377      *     doSomethingElseHere(...)
 378      * }</pre>
 379      * evtHandler1 will block at the call to showAndWait. It will resume execution
 380      * after stage1 is hidden and the current event handler, in this case evtHandler2,
 381      * returns to the event loop. This means that doSomethingElseHere will
 382      * execute before doSomethingAfterStage1Closed.
 383      *
 384      * <p>
 385      * More than one stage may be shown with showAndWait. Each call
 386      * will start a new nested event loop. The stages may be hidden in any order,
 387      * but a particular nested event loop (and thus the showAndWait method
 388      * for the associated stage) will only terminate after all inner event loops
 389      * have also terminated.
 390      * </p>
 391      * <p>
 392      * For example, consider the following sequence of operations for different
 393      * event handlers, assumed to execute in the order shown below:
 394      * <pre>void evtHander1() {
 395      *     stage1.showAndWait();
 396      *     doSomethingAfterStage1Closed(...)
 397      * }
 398      *
 399      * void evtHander2() {
 400      *     stage2.showAndWait();
 401      *     doSomethingAfterStage2Closed(...)
 402      * }
 403      *
 404      * void evtHander3() {
 405      *     stage1.hide();
 406      *     doSomethingElseHere(...)
 407      * }
 408      *
 409      * void evtHander4() {
 410      *     stage2.hide();
 411      *     doSomethingElseHereToo(...)
 412      * }</pre>
 413      * evtHandler1 will block at the call to stage1.showAndWait, starting up
 414      * a nested event loop just like in the previous example. evtHandler2 will
 415      * then block at the call to stage2.showAndWait, starting up another (inner)
 416      * nested event loop. The first call to stage1.showAndWait will resume execution
 417      * after stage1 is hidden, but only after the inner nested event loop started
 418      * by stage2.showAndWait has terminated. This means that the call to
 419      * stage1.showAndWait won't return until after evtHandler2 has returned.
 420      * The order of execution is: stage1.showAndWait, stage2.showAndWait,
 421      * stage1.hide, doSomethingElseHere, stage2.hide, doSomethingElseHereToo,
 422      * doSomethingAfterStage2Closed, doSomethingAfterStage1Closed.
 423      *
 424      * <p>
 425      * This method must not be called on the primary stage or on a stage that
 426      * is already visible.
 427      * Additionally, it must either be called from an input event handler or
 428      * from the run method of a Runnable passed to
 429      * {@link javafx.application.Platform#runLater Platform.runLater}.
 430      * It must not be called during animation or layout processing.
 431      * </p>
 432      *
 433      * @throws IllegalStateException if this method is called on a thread
 434      *     other than the JavaFX Application Thread.
 435      * @throws IllegalStateException if this method is called during
 436      *     animation or layout processing.
 437      * @throws IllegalStateException if this method is called on the
 438      *     primary stage.
 439      * @throws IllegalStateException if this stage is already showing.
 440      * @since JavaFX 2.2
 441      */
 442     public void showAndWait() {
 443 
 444         Toolkit.getToolkit().checkFxUserThread();
 445 
 446         if (isPrimary()) {
 447             throw new IllegalStateException("Cannot call this method on primary stage");
 448         }
 449 
 450         if (isShowing()) {
 451             throw new IllegalStateException("Stage already visible");
 452         }
 453 
 454         if (!Toolkit.getToolkit().canStartNestedEventLoop()) {
 455             throw new IllegalStateException("showAndWait is not allowed during animation or layout processing");
 456         }
 457 
 458         // TODO: file a new bug; the following assertion can fail if this
 459         // method is called from an event handler that is listening to a
 460         // WindowEvent.WINDOW_HIDING event.
 461         assert !inNestedEventLoop;
 462 
 463         show();
 464         inNestedEventLoop = true;
 465         Toolkit.getToolkit().enterNestedEventLoop(this);
 466     }
 467 
 468     private StageStyle style; // default is set in constructor
 469 
 470     /**
 471      * Specifies the style for this stage. This must be done prior to making
 472      * the stage visible. The style is one of: StageStyle.DECORATED,
 473      * StageStyle.UNDECORATED, StageStyle.TRANSPARENT, or StageStyle.UTILITY.
 474      *
 475      * @param style the style for this stage.
 476      *
 477      * @throws IllegalStateException if this property is set after the stage
 478      * has ever been made visible.
 479      *
 480      * @defaultValue StageStyle.DECORATED
 481      */
 482     public final void initStyle(StageStyle style) {
 483         if (hasBeenVisible) {
 484             throw new IllegalStateException("Cannot set style once stage has been set visible");
 485         }
 486         this.style = style;
 487     }
 488 
 489     /**
 490      * Retrieves the style attribute for this stage.
 491      *
 492      * @return the stage style.
 493      */
 494     public final StageStyle getStyle() {
 495         return style;
 496     }
 497 
 498     private Modality modality = Modality.NONE;
 499 
 500     /**
 501      * Specifies the modality for this stage. This must be done prior to making
 502      * the stage visible. The modality is one of: Modality.NONE,
 503      * Modality.WINDOW_MODAL, or Modality.APPLICATION_MODAL.
 504      *
 505      * @param modality the modality for this stage.
 506      *
 507      * @throws IllegalStateException if this property is set after the stage
 508      * has ever been made visible.
 509      *
 510      * @throws IllegalStateException if this stage is the primary stage.
 511      *
 512      * @defaultValue Modality.NONE
 513      */
 514     public final void initModality(Modality modality) {
 515         if (hasBeenVisible) {
 516             throw new IllegalStateException("Cannot set modality once stage has been set visible");
 517         }
 518 
 519         if (isPrimary()) {
 520             throw new IllegalStateException("Cannot set modality for the primary stage");
 521         }
 522 
 523         this.modality = modality;
 524     }
 525 
 526     /**
 527      * Retrieves the modality attribute for this stage.
 528      *
 529      * @return the modality.
 530      */
 531     public final Modality getModality() {
 532         return modality;
 533     }
 534 
 535     private Window owner = null;
 536 
 537     /**
 538      * Specifies the owner Window for this stage, or null for a top-level,
 539      * unowned stage. This must be done prior to making the stage visible.
 540      *
 541      * @param owner the owner for this stage.
 542      *
 543      * @throws IllegalStateException if this property is set after the stage
 544      * has ever been made visible.
 545      *
 546      * @throws IllegalStateException if this stage is the primary stage.
 547      *
 548      * @defaultValue null
 549      */
 550     public final void initOwner(Window owner) {
 551         if (hasBeenVisible) {
 552             throw new IllegalStateException("Cannot set owner once stage has been set visible");
 553         }
 554 
 555         if (isPrimary()) {
 556             throw new IllegalStateException("Cannot set owner for the primary stage");
 557         }
 558 
 559         this.owner = owner;
 560 
 561         final Scene sceneValue = getScene();
 562         if (sceneValue != null) {
 563             SceneHelper.parentEffectiveOrientationInvalidated(sceneValue);
 564         }
 565     }
 566 
 567     /**
 568      * Retrieves the owner Window for this stage, or null for an unowned stage.
 569      *
 570      * @return the owner Window.
 571      */
 572     public final Window getOwner() {
 573         return owner;
 574     }
 575 
 576     /**
 577      * Specifies whether this {@code Stage} should be a full-screen,
 578      * undecorated window.
 579      * <p>
 580      * The implementation of full-screen mode is platform and profile-dependent.
 581      * </p>
 582      * <p>
 583      * When set to {@code true}, the {@code Stage} will attempt to enter
 584      * full-screen mode when visible. Set to {@code false} to return {@code Stage}
 585      * to windowed mode.
 586      * An {@link IllegalStateException} is thrown if this property is set
 587      * on a thread other than the JavaFX Application Thread.
 588      * </p>
 589      * <p>
 590      * The full-screen mode will be exited (and the {@code fullScreen} attribute
 591      * will be set to {@code false}) if the full-screen
 592      * {@code Stage} loses focus or if another {@code Stage} enters
 593      * full-screen mode on the same {@link Screen}. Note that a {@code Stage}
 594      * in full-screen mode can become invisible without losing its
 595      * full-screen status and will again enter full-screen mode when the
 596      * {@code Stage} becomes visible.
 597      * </p>
 598      * If the platform supports multiple screens an application can control
 599      * which {@code Screen} the Stage will enter full-screen mode on by
 600      * setting its position to be within the bounds of that {@code Screen}
 601      * prior to entering full-screen mode.
 602      * <p>
 603      * However once in full-screen mode, {@code Stage}'s {@code x}, {@code y},
 604      * {@code width}, and {@code height} variables will continue to represent
 605      * the non-full-screen position and size of the window; the same for
 606      * {@code iconified}, {@code resizable}, {@code style}, and {@code
 607      * opacity}. If changes are made to any of these attributes while in
 608      * full-screen mode, upon exiting full-screen mode the {@code Stage} will
 609      * assume those attributes.
 610      * </p>
 611      * <p>
 612      * In case that more {@code Stage} modes are set simultaneously their order
 613      * of importance is {@code iconified}, fullScreen, {@code maximized} (from
 614      * strongest to weakest).
 615      * </p>
 616      * <p>
 617      * The property is read only because it can be changed externally
 618      * by the underlying platform and therefore must not be bindable.
 619      * </p>
 620      *
 621      * The user can unconditionally exit full-screen mode
 622      * at any time by pressing {@code ESC}.
 623      * <p>
 624      * If a security manager is present, the application must have the
 625      * {@link javafx.util.FXPermission} "unrestrictedFullScreen" in order
 626      * to enter full-screen mode with no restrictions. Applications without
 627      * permission will have the following restrictions:
 628      * </p>
 629      * <ul>
 630      *  <li>Applications can only enter full-screen mode in response
 631      *   to user input. More specifically, entering is allowed from mouse
 632      *   ({@code Node.mousePressed/mouseReleased/mouseClicked}) or keyboard
 633      *   ({@code Node.keyPressed/keyReleased/keyTyped}) event handlers. It is
 634      *   not allowed to enter full-screen mode in response to {@code ESC}
 635      *   key. Attempting to enter full-screen mode from any other context will
 636      *   be ignored.
 637      *   <p>
 638      *   If {@code Stage} was constructed as full-screen but not visible
 639      *   it will enter full-screen mode upon becoming visible, with the same
 640      *   limitations to when this is allowed to happen as when setting
 641      *   {@code fullScreen} to {@code true}.
 642      *   </p>
 643      *  </li>
 644      *  <li> If the application was allowed to enter full-screen mode
 645      *   it will have limited keyboard input. It will only receive KEY_PRESSED
 646      *   and KEY_RELEASED events from the following keys:
 647      *   {@code UP, DOWN, LEFT, RIGHT, SPACE, TAB, PAGE_UP, PAGE_DOWN, HOME, END, ENTER}
 648      *  </li>
 649      * </ul>
 650      * @defaultValue false
 651      */
 652     private ReadOnlyBooleanWrapper fullScreen;
 653 
 654     public final void setFullScreen(boolean value) {
 655         Toolkit.getToolkit().checkFxUserThread();
 656         fullScreenPropertyImpl().set(value);
 657         if (getPeer() != null)
 658             getPeer().setFullScreen(value);
 659     }
 660 
 661     public final boolean isFullScreen() {
 662         return fullScreen == null ? false : fullScreen.get();
 663     }
 664 
 665     public final ReadOnlyBooleanProperty fullScreenProperty() {
 666         return fullScreenPropertyImpl().getReadOnlyProperty();
 667     }
 668 
 669     private ReadOnlyBooleanWrapper fullScreenPropertyImpl () {
 670         if (fullScreen == null) {
 671             fullScreen = new ReadOnlyBooleanWrapper(Stage.this, "fullScreen");
 672         }
 673         return fullScreen;
 674     }
 675 
 676     /**
 677      * Defines the icon images to be used in the window decorations and when
 678      * minimized. The images should be different sizes of the same image and
 679      * the best size will be chosen, eg. 16x16, 32,32.
 680      *
 681      * @defaultValue empty
 682      */
 683     private ObservableList<Image> icons = new VetoableListDecorator<Image>(new TrackableObservableList<Image>() {
 684         @Override protected void onChanged(Change<Image> c) {
 685             List<Object> platformImages = new ArrayList<Object>();
 686             for (Image icon : icons) {
 687                 platformImages.add(Toolkit.getImageAccessor().getPlatformImage(icon));
 688             }
 689             if (getPeer() != null) {
 690                 getPeer().setIcons(platformImages);
 691             }
 692         }
 693     }) {
 694         @Override protected void onProposedChange(
 695                 final List<Image> toBeAddedIcons, int[] indices) {
 696             for (Image icon : toBeAddedIcons) {
 697                 if (icon == null) {
 698                     throw new NullPointerException("icon can not be null.");
 699                 }
 700             }
 701         }
 702     };
 703 
 704     /**
 705      * Gets the icon images to be used in the window decorations and when
 706      * minimized. The images should be different sizes of the same image and
 707      * the best size will be chosen, eg. 16x16, 32,32.
 708      * @return An observable list of icons of this window
 709      */
 710     public final ObservableList<Image> getIcons() {
 711         return icons;
 712     }
 713 
 714     /**
 715      * Defines the title of the {@code Stage}.
 716      *
 717      * @defaultValue empty string
 718      */
 719     private StringProperty title;
 720 
 721     public final void setTitle(String value) {
 722         titleProperty().set(value);
 723     }
 724 
 725     public final String getTitle() {
 726         return title == null ? null : title.get();
 727     }
 728 
 729     public final StringProperty titleProperty() {
 730         if (title == null) {
 731             title = new StringPropertyBase() {
 732 
 733                 @Override
 734                 protected void invalidated() {
 735                     if (getPeer() != null) {
 736                         getPeer().setTitle(get());
 737                     }
 738                 }
 739 
 740                 @Override
 741                 public Object getBean() {
 742                     return Stage.this;
 743                 }
 744 
 745                 @Override
 746                 public String getName() {
 747                     return "title";
 748                 }
 749             };
 750         }
 751         return title;
 752     }
 753 
 754     /**
 755      * Defines whether the {@code Stage} is iconified or not.
 756      * <p>
 757      * In case that more {@code Stage} modes are set simultaneously their order
 758      * of importance is iconified} {@code fullScreen}, {@code maximized} (from
 759      * strongest to weakest).
 760      * </p>
 761      * <p>
 762      * On some mobile and embedded platforms setting this property to true will
 763      * hide the {@code Stage} but not show an icon for it.
 764      * </p>
 765      * <p>
 766      * The property is read only because it can be changed externally
 767      * by the underlying platform and therefore must not be bindable.
 768      * </p>
 769      *
 770      * @defaultValue false
 771      */
 772     private ReadOnlyBooleanWrapper iconified;
 773 
 774     public final void setIconified(boolean value) {
 775         iconifiedPropertyImpl().set(value);
 776         if (getPeer() != null)
 777             getPeer().setIconified(value);
 778     }
 779 
 780     public final boolean isIconified() {
 781         return iconified == null ? false : iconified.get();
 782     }
 783 
 784     public final ReadOnlyBooleanProperty iconifiedProperty() {
 785         return iconifiedPropertyImpl().getReadOnlyProperty();
 786     }
 787 
 788     private final ReadOnlyBooleanWrapper iconifiedPropertyImpl() {
 789         if (iconified == null) {
 790             iconified = new ReadOnlyBooleanWrapper(Stage.this, "iconified");
 791         }
 792         return iconified;
 793     }
 794 
 795     /**
 796      * Defines whether the {@code Stage} is maximized or not.
 797      * <p>
 798      * In case that more {@code Stage} modes are set simultaneously their order
 799      * of importance is {@code iconified}, {@code fullScreen}, maximized (from
 800      * strongest to weakest).
 801      * </p>
 802      * <p>
 803      * The property is read only because it can be changed externally
 804      * by the underlying platform and therefore must not be bindable.
 805      * </p>
 806      *
 807      * @defaultValue false
 808      * @since JavaFX 8.0
 809      */
 810     private ReadOnlyBooleanWrapper maximized;
 811 
 812     public final void setMaximized(boolean value) {
 813         maximizedPropertyImpl().set(value);
 814         if (getPeer() != null) {
 815             getPeer().setMaximized(value);
 816         }
 817     }
 818 
 819     public final boolean isMaximized() {
 820         return maximized == null ? false : maximized.get();
 821     }
 822 
 823     public final ReadOnlyBooleanProperty maximizedProperty() {
 824         return maximizedPropertyImpl().getReadOnlyProperty();
 825     }
 826 
 827     private final ReadOnlyBooleanWrapper maximizedPropertyImpl() {
 828         if (maximized == null) {
 829             maximized = new ReadOnlyBooleanWrapper(Stage.this, "maximized");
 830         }
 831         return maximized;
 832     }
 833 
 834     /**
 835      * Defines whether this {@code Stage} is kept on top of other windows.
 836      * <p>
 837      * If some other window is already always-on-top then the
 838      * relative order between these windows is unspecified (depends on
 839      * platform).
 840      * </p>
 841      * <p>
 842      * If a security manager is present, the application must have the
 843      * {@link javafx.util.FXPermission} "setWindowAlwaysOnTop" in order for
 844      * this property to have any effect. If the application does not have
 845      * permission, attempting to set this property will be ignored
 846      * and the property value will be restored to {@code false}.
 847      * </p>
 848      * <p>
 849      * The property is read only because it can be changed externally
 850      * by the underlying platform and therefore must not be bindable.
 851      * </p>
 852      *
 853      * @defaultValue false
 854      * @since JavaFX 8u20
 855      */
 856     private ReadOnlyBooleanWrapper alwaysOnTop;
 857 
 858     public final void setAlwaysOnTop(boolean value) {
 859         alwaysOnTopPropertyImpl().set(value);
 860         if (getPeer() != null) {
 861             getPeer().setAlwaysOnTop(value);
 862         }
 863     }
 864 
 865     public final boolean isAlwaysOnTop() {
 866         return alwaysOnTop == null ? false : alwaysOnTop.get();
 867     }
 868 
 869     public final ReadOnlyBooleanProperty alwaysOnTopProperty() {
 870         return alwaysOnTopPropertyImpl().getReadOnlyProperty();
 871     }
 872 
 873     private ReadOnlyBooleanWrapper alwaysOnTopPropertyImpl() {
 874         if (alwaysOnTop == null) {
 875             alwaysOnTop = new ReadOnlyBooleanWrapper(Stage.this, "alwaysOnTop");
 876         }
 877         return alwaysOnTop;
 878     }
 879 
 880     /**
 881      * Defines whether the {@code Stage} is resizable or not by the user.
 882      * Programatically you may still change the size of the Stage. This is
 883      * a hint which allows the implementation to optionally make the Stage
 884      * resizable by the user.
 885      * <p>
 886      * <b>Warning:</b> Since 8.0 the property cannot be bound and will throw
 887      * {@code RuntimeException} on an attempt to do so. This is because
 888      * the setting of resizable is asynchronous on some systems or generally
 889      * might be set by the system / window manager.
 890      * <br>
 891      * Bidirectional binds are still allowed, as they don't block setting of the
 892      * property by the system.
 893      *
 894      * @defaultValue true
 895      */
 896     private BooleanProperty resizable;
 897 
 898     public final void setResizable(boolean value) {
 899         resizableProperty().set(value);
 900     }
 901 
 902     public final boolean isResizable() {
 903         return resizable == null ? true : resizable.get();
 904     }
 905 
 906     public final BooleanProperty resizableProperty() {
 907         if (resizable == null) {
 908             resizable = new ResizableProperty();
 909         }
 910         return resizable;
 911     }
 912 
 913     //We cannot return ReadOnlyProperty in resizable, as this would be
 914     // backward incompatible. All we can do is to create this custom property
 915     // implementation that disallows binds
 916     private class ResizableProperty extends SimpleBooleanProperty {
 917         private boolean noInvalidate;
 918 
 919         public ResizableProperty() {
 920             super(Stage.this, "resizable", true);
 921         }
 922 
 923         void setNoInvalidate(boolean value) {
 924             noInvalidate = true;
 925             set(value);
 926             noInvalidate = false;
 927         }
 928 
 929         @Override
 930         protected void invalidated() {
 931             if (noInvalidate) {
 932                 return;
 933             }
 934             if (getPeer() != null) {
 935                 applyBounds();
 936                 getPeer().setResizable(get());
 937             }
 938         }
 939 
 940         @Override
 941         public void bind(ObservableValue<? extends Boolean> rawObservable) {
 942             throw new RuntimeException("Resizable property cannot be bound");
 943         }
 944 
 945     }
 946 
 947     /**
 948      * Defines the minimum width of this {@code Stage}.
 949      *
 950      * @defaultValue 0
 951      * @since JavaFX 2.1
 952      */
 953     private DoubleProperty minWidth;
 954 
 955     public final void setMinWidth(double value) {
 956         minWidthProperty().set(value);
 957     }
 958 
 959     public final double getMinWidth() {
 960         return minWidth == null ? 0 : minWidth.get();
 961     }
 962 
 963     public final DoubleProperty minWidthProperty() {
 964         if (minWidth == null) {
 965             minWidth = new DoublePropertyBase(0) {
 966 
 967                 @Override
 968                 protected void invalidated() {
 969                     if (getPeer() != null) {
 970                         getPeer().setMinimumSize((int) Math.ceil(get()),
 971                                 (int) Math.ceil(getMinHeight()));
 972                     }
 973                     if (getWidth() < getMinWidth()) {
 974                         setWidth(getMinWidth());
 975                     }
 976                 }
 977 
 978                 @Override
 979                 public Object getBean() {
 980                     return Stage.this;
 981                 }
 982 
 983                 @Override
 984                 public String getName() {
 985                     return "minWidth";
 986                 }
 987             };
 988         }
 989         return minWidth;
 990     }
 991 
 992     /**
 993      * Defines the minimum height of this {@code Stage}.
 994      *
 995      * @defaultValue 0
 996      * @since JavaFX 2.1
 997      */
 998     private DoubleProperty minHeight;
 999 
1000     public final void setMinHeight(double value) {
1001         minHeightProperty().set(value);
1002     }
1003 
1004     public final double getMinHeight() {
1005         return minHeight == null ? 0 : minHeight.get();
1006     }
1007 
1008     public final DoubleProperty minHeightProperty() {
1009         if (minHeight == null) {
1010             minHeight = new DoublePropertyBase(0) {
1011 
1012                 @Override
1013                 protected void invalidated() {
1014                     if (getPeer() != null) {
1015                         getPeer().setMinimumSize(
1016                                 (int) Math.ceil(getMinWidth()),
1017                                 (int) Math.ceil(get()));
1018                     }
1019                     if (getHeight() < getMinHeight()) {
1020                         setHeight(getMinHeight());
1021                     }
1022                 }
1023 
1024                 @Override
1025                 public Object getBean() {
1026                     return Stage.this;
1027                 }
1028 
1029                 @Override
1030                 public String getName() {
1031                     return "minHeight";
1032                 }
1033             };
1034         }
1035         return minHeight;
1036     }
1037 
1038     /**
1039      * Defines the maximum width of this {@code Stage}.
1040      *
1041      * @defaultValue Double.MAX_VALUE
1042      * @since JavaFX 2.1
1043      */
1044     private DoubleProperty maxWidth;
1045 
1046     public final void setMaxWidth(double value) {
1047         maxWidthProperty().set(value);
1048     }
1049 
1050     public final double getMaxWidth() {
1051         return maxWidth == null ? Double.MAX_VALUE : maxWidth.get();
1052     }
1053 
1054     public final DoubleProperty maxWidthProperty() {
1055         if (maxWidth == null) {
1056             maxWidth = new DoublePropertyBase(Double.MAX_VALUE) {
1057 
1058                 @Override
1059                 protected void invalidated() {
1060                     if (getPeer() != null) {
1061                         getPeer().setMaximumSize((int) Math.floor(get()),
1062                                 (int) Math.floor(getMaxHeight()));
1063                     }
1064                     if (getWidth() > getMaxWidth()) {
1065                         setWidth(getMaxWidth());
1066                     }
1067                 }
1068 
1069                 @Override
1070                 public Object getBean() {
1071                     return Stage.this;
1072                 }
1073 
1074                 @Override
1075                 public String getName() {
1076                     return "maxWidth";
1077                 }
1078             };
1079         }
1080         return maxWidth;
1081     }
1082 
1083     /**
1084      * Defines the maximum height of this {@code Stage}.
1085      *
1086      * @defaultValue Double.MAX_VALUE
1087      * @since JavaFX 2.1
1088      */
1089     private DoubleProperty maxHeight;
1090 
1091     public final void setMaxHeight(double value) {
1092         maxHeightProperty().set(value);
1093     }
1094 
1095     public final double getMaxHeight() {
1096         return maxHeight == null ? Double.MAX_VALUE : maxHeight.get();
1097     }
1098 
1099     public final DoubleProperty maxHeightProperty() {
1100         if (maxHeight == null) {
1101             maxHeight = new DoublePropertyBase(Double.MAX_VALUE) {
1102 
1103                 @Override
1104                 protected void invalidated() {
1105                     if (getPeer() != null) {
1106                         getPeer().setMaximumSize(
1107                                 (int) Math.floor(getMaxWidth()),
1108                                 (int) Math.floor(get()));
1109                     }
1110                     if (getHeight() > getMaxHeight()) {
1111                         setHeight(getMaxHeight());
1112                     }
1113                 }
1114 
1115                 @Override
1116                 public Object getBean() {
1117                     return Stage.this;
1118                 }
1119 
1120                 @Override
1121                 public String getName() {
1122                     return "maxHeight";
1123                 }
1124             };
1125         }
1126         return maxHeight;
1127     }
1128 
1129     /*
1130      * This can be replaced by listening for the onShowing/onHiding events
1131      * Note: This method MUST only be called via its accessor method.
1132      */
1133     private void doVisibleChanging(boolean value) {
1134         Toolkit toolkit = Toolkit.getToolkit();
1135         if (value && (getPeer() == null)) {
1136             // Setup the peer
1137             Window window = getOwner();
1138             TKStage tkStage = (window == null ? null : window.getPeer());
1139             Scene scene = getScene();
1140             boolean rtl = scene != null && scene.getEffectiveNodeOrientation() == NodeOrientation.RIGHT_TO_LEFT;
1141 
1142             StageStyle stageStyle = getStyle();
1143             if (stageStyle == StageStyle.TRANSPARENT) {
1144                 final SecurityManager securityManager =
1145                         System.getSecurityManager();
1146                 if (securityManager != null) {
1147                     try {
1148                         securityManager.checkPermission(CREATE_TRANSPARENT_WINDOW_PERMISSION);
1149                     } catch (final SecurityException e) {
1150                         stageStyle = StageStyle.UNDECORATED;
1151                     }
1152                 }
1153             }
1154             setPeer(toolkit.createTKStage(this, isSecurityDialog(),
1155                     stageStyle, isPrimary(), getModality(), tkStage, rtl, acc));
1156             getPeer().setMinimumSize((int) Math.ceil(getMinWidth()),
1157                     (int) Math.ceil(getMinHeight()));
1158             getPeer().setMaximumSize((int) Math.floor(getMaxWidth()),
1159                     (int) Math.floor(getMaxHeight()));
1160             setPeerListener(new StagePeerListener(this, STAGE_ACCESSOR));
1161         }
1162     }
1163 
1164 
1165     /*
1166      * This can be replaced by listening for the onShown/onHidden events
1167      * Note: This method MUST only be called via its accessor method.
1168      */
1169     private void doVisibleChanged(boolean value) {
1170         if (value) {
1171             // Finish initialization
1172             TKStage peer = getPeer();
1173             peer.setImportant(isImportant());
1174             peer.setResizable(isResizable());
1175             peer.setFullScreen(isFullScreen());
1176             peer.setAlwaysOnTop(isAlwaysOnTop());
1177             peer.setIconified(isIconified());
1178             peer.setMaximized(isMaximized());
1179             peer.setTitle(getTitle());
1180 
1181             List<Object> platformImages = new ArrayList<Object>();
1182             for (Image icon : icons) {
1183                 platformImages.add(Toolkit.getImageAccessor().getPlatformImage(icon));
1184             }
1185             if (peer != null) {
1186                 peer.setIcons(platformImages);
1187             }
1188         }
1189 
1190         if (!value && inNestedEventLoop) {
1191             inNestedEventLoop = false;
1192             Toolkit.getToolkit().exitNestedEventLoop(this, null);
1193         }
1194     }
1195 
1196     /**
1197      * Bring the {@code Window} to the foreground.  If the {@code Window} is
1198      * already in the foreground there is no visible difference.
1199      */
1200     public void toFront() {
1201         if (getPeer() != null) {
1202             getPeer().toFront();
1203         }
1204     }
1205 
1206     /**
1207      * Send the {@code Window} to the background.  If the {@code Window} is
1208      * already in the background there is no visible difference.  This action
1209      * places this {@code Window} at the bottom of the stacking order on
1210      * platforms that support stacking.
1211      */
1212     public void toBack() {
1213         if (getPeer() != null) {
1214             getPeer().toBack();
1215         }
1216     }
1217 
1218     /**
1219      * Closes this {@code Stage}.
1220      * This call is equivalent to {@code hide()}.
1221      */
1222     public void close() {
1223         hide();
1224     }
1225 
1226     @Override
1227     Window getWindowOwner() {
1228         return getOwner();
1229     }
1230 
1231 
1232     private final ObjectProperty<KeyCombination> fullScreenExitCombination =
1233             new SimpleObjectProperty<KeyCombination>(this, "fullScreenExitCombination", null);
1234 
1235     /**
1236      * Specifies the KeyCombination that will allow the user to exit full screen
1237      * mode. A value of KeyCombination.NO_MATCH will not match any KeyEvent and
1238      * will make it so the user is not able to escape from Full Screen mode.
1239      * A value of null indicates that the default platform specific key combination
1240      * should be used.
1241      * <p>
1242      * An internal copy of this value is made when entering full-screen mode and will be
1243      * used to trigger the exit from the mode.
1244      * If a security manager is present, the application must have the
1245      * {@link javafx.util.FXPermission} "unrestrictedFullScreen" to modify the
1246      * exit key combination. If the application does not have permission, the
1247      * value of this property will be ignored, in which case the
1248      * default key combination will be used.
1249      * </p>
1250      * @param keyCombination the key combination to exit on
1251      * @since JavaFX 8.0
1252      */
1253     public final void setFullScreenExitKeyCombination(KeyCombination keyCombination) {
1254         fullScreenExitCombination.set(keyCombination);
1255     }
1256 
1257     /**
1258      * Get the current sequence used to exit Full Screen mode.
1259      * @return the current setting (null for system default)
1260      * @since JavaFX 8.0
1261      */
1262     public final KeyCombination getFullScreenExitKeyCombination() {
1263         return fullScreenExitCombination.get();
1264     }
1265 
1266     /**
1267      * Get the property for the Full Screen exit key combination.
1268      * @return the property.
1269      * @since JavaFX 8.0
1270      */
1271     public final ObjectProperty<KeyCombination> fullScreenExitKeyProperty() {
1272         return fullScreenExitCombination;
1273     }
1274 
1275     private final ObjectProperty<String> fullScreenExitHint =
1276             new SimpleObjectProperty<String>(this, "fullScreenExitHint", null);
1277 
1278     /**
1279      * Specifies the text to show when a user enters full screen mode, usually
1280      * used to indicate the way a user should go about exiting out of full
1281      * screen mode. A value of null will result in the default per-locale
1282      * message being displayed.
1283      * If set to the empty string, then no message will be displayed.
1284      * <p>
1285      * If a security manager is present, the application must have the
1286      * {@link javafx.util.FXPermission} "unrestrictedFullScreen" to modify the
1287      * exit hint. If the application does not have permission, the
1288      * value of this property will be ignored, in which case the
1289      * default message will be displayed.
1290      * </p>
1291      * @param value the string to be displayed.
1292      * @since JavaFX 8.0
1293      */
1294     public final void setFullScreenExitHint(String value) {
1295         fullScreenExitHint.set(value);
1296     }
1297 
1298     public final String getFullScreenExitHint() {
1299         return fullScreenExitHint.get();
1300     }
1301 
1302     public final ObjectProperty<String> fullScreenExitHintProperty() {
1303         return fullScreenExitHint;
1304     }
1305 }