1 /* 2 * Copyright (c) 2014, 2016, 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 package javafx.scene.control; 26 27 import java.lang.ref.WeakReference; 28 import java.util.Optional; 29 30 import javafx.beans.InvalidationListener; 31 import javafx.beans.property.BooleanProperty; 32 import javafx.beans.property.ObjectProperty; 33 import javafx.beans.property.ReadOnlyBooleanProperty; 34 import javafx.beans.property.ReadOnlyDoubleProperty; 35 import javafx.beans.property.SimpleObjectProperty; 36 import javafx.beans.property.StringProperty; 37 import javafx.collections.ListChangeListener; 38 import javafx.css.PseudoClass; 39 import javafx.event.Event; 40 import javafx.event.EventDispatchChain; 41 import javafx.event.EventHandler; 42 import javafx.event.EventTarget; 43 import javafx.scene.Node; 44 import javafx.scene.control.ButtonBar.ButtonData; 45 import javafx.stage.Modality; 46 import javafx.stage.Stage; 47 import javafx.stage.StageStyle; 48 import javafx.stage.Window; 49 import javafx.util.Callback; 50 51 import com.sun.javafx.event.EventHandlerManager; 52 import com.sun.javafx.tk.Toolkit; 53 54 /** 55 * A Dialog in JavaFX wraps a {@link DialogPane} and provides the necessary API 56 * to present it to end users. In JavaFX 8u40, this essentially means that the 57 * {@link DialogPane} is shown to users inside a {@link Stage}, but future releases 58 * may offer alternative options (such as 'lightweight' or 'internal' dialogs). 59 * This API therefore is intentionally ignorant of the underlying implementation, 60 * and attempts to present a common API for all possible implementations. 61 * 62 * <p>The Dialog class has a single generic type, R, which is used to represent 63 * the type of the {@link #resultProperty() result} property (and also, how to 64 * convert from {@link ButtonType} to R, through the use of the 65 * {@link #resultConverterProperty() result converter} {@link Callback}). 66 * 67 * <p><strong>Critical note:</strong> It is critical that all developers who choose 68 * to create their own dialogs by extending the Dialog class understand the 69 * importance of the {@link #resultConverterProperty() result converter} property. 70 * A result converter must always be set, whenever the R type is not 71 * {@link Void} or {@link ButtonType}. If this is not heeded, developers will find 72 * that they get ClassCastExceptions in their code, for failure to convert from 73 * {@link ButtonType} via the {@link #resultConverterProperty() result converter}. 74 * 75 * <p>It is likely that most developers would be better served using either the 76 * {@link Alert} class (for pre-defined, notification-style alerts), or either of 77 * the two pre-built dialogs ({@link TextInputDialog} and {@link ChoiceDialog}), 78 * depending on their needs. 79 * 80 * <p>Once a Dialog is instantiated, the next step is to configure it. Almost 81 * all properties on Dialog are not related to the content of the Dialog, the 82 * only exceptions are {@link #contentTextProperty()}, 83 * {@link #headerTextProperty()}, and {@link #graphicProperty()}, and these 84 * properties are simply forwarding API onto the respective properties on the 85 * {@link DialogPane} stored in the {@link #dialogPaneProperty() dialog pane} 86 * property. These three properties are forwarded from DialogPane for developer 87 * convenience. For developers wanting to configure their dialog, they will in many 88 * cases be required to use code along the lines of 89 * {@code dialog.getDialogPane().setExpandableContent(node)}. 90 * 91 * <p>After configuring these properties, all that remains is to consider whether 92 * the buttons (created using {@link ButtonType} and the 93 * {@link DialogPane#createButton(ButtonType)} method) are fully configured. 94 * Developers will quickly find that the amount of configurability offered 95 * via the {@link ButtonType} class is minimal. This is intentional, but does not 96 * mean that developers can not modify the buttons created by the {@link ButtonType} 97 * that have been specified. To do this, developers simply call the 98 * {@link DialogPane#lookupButton(ButtonType)} method with the ButtonType 99 * (assuming it has already been set in the {@link DialogPane#getButtonTypes()} 100 * list. The returned Node is typically of type {@link Button}, but this depends 101 * on if the {@link DialogPane#createButton(ButtonType)} method has been overridden. A 102 * typical approach is therefore along the following lines: 103 * 104 * <pre>{@code ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE); 105 * Dialog<String> dialog = new Dialog<>(); 106 * dialog.getDialogPane().getButtonTypes().add(loginButtonType); 107 * boolean disabled = false; // computed based on content of text fields, for example 108 * dialog.getDialogPane().lookupButton(loginButtonType).setDisable(disabled);}</pre> 109 * 110 * <p>Once a Dialog is instantiated and fully configured, the next step is to 111 * show it. More often than not, dialogs are shown in a modal and blocking 112 * fashion. 'Modal' means that the dialog prevents user interaction with the 113 * owning application whilst it is showing, and 'blocking' means that code 114 * execution stops at the point in which the dialog is shown. This means that 115 * you can show a dialog, await the user response, and then continue running the 116 * code that directly follows the show call, giving developers the ability to 117 * immediately deal with the user input from the dialog (if relevant). 118 * 119 * <p>JavaFX dialogs are modal by default (you can change this via the 120 * {@link #initModality(javafx.stage.Modality)} API). To specify whether you want 121 * blocking or non-blocking dialogs, developers simply choose to call 122 * {@link #showAndWait()} or {@link #show()} (respectively). By default most 123 * developers should choose to use {@link #showAndWait()}, given the ease of 124 * coding in these situations. Shown below is three code snippets, showing three 125 * equally valid ways of showing a dialog: 126 * 127 * <p><strong>Option 1: The 'traditional' approach</strong> 128 * <pre>{@code Optional<ButtonType> result = dialog.showAndWait(); 129 * if (result.isPresent() && result.get() == ButtonType.OK) { 130 * formatSystem(); 131 * }}</pre> 132 * 133 * <p><strong>Option 2: The traditional + Optional approach</strong> 134 * <pre>{@code dialog.showAndWait().ifPresent(response -> { 135 * if (response == ButtonType.OK) { 136 * formatSystem(); 137 * } 138 * });}</pre> 139 * 140 * <p><strong>Option 3: The fully lambda approach</strong> 141 * <pre>{@code dialog.showAndWait() 142 * .filter(response -> response == ButtonType.OK) 143 * .ifPresent(response -> formatSystem());}</pre> 144 * 145 * <p>There is no better or worse option of the three listed above, so developers 146 * are encouraged to work to their own style preferences. The purpose of showing 147 * the above is to help introduce developers to the {@link Optional} API, which 148 * is new in Java 8 and may be foreign to many developers. 149 * 150 * <h3>Dialog Validation / Intercepting Button Actions</h3> 151 * 152 * <p>In some circumstances it is desirable to prevent a dialog from closing 153 * until some aspect of the dialog becomes internally consistent (e.g. a form 154 * inside the dialog has all fields in a valid state). To do this, users of the 155 * dialogs API should become familiar with the 156 * {@link DialogPane#lookupButton(ButtonType)} method. By passing in a 157 * {@link javafx.scene.control.ButtonType ButtonType} (that has already been set 158 * in the {@link DialogPane#getButtonTypes() button types} list), users will be 159 * returned a Node that is typically of type {@link Button} (but this depends 160 * on if the {@link DialogPane#createButton(ButtonType)} method has been 161 * overridden). With this button, users may add an event filter that is called 162 * before the button does its usual event handling, and as such users may 163 * prevent the event handling by {@code consuming} the event. Here's a simplified 164 * example: 165 * 166 * <pre>{@code final Button btOk = (Button) dlg.getDialogPane().lookupButton(ButtonType.OK); 167 * btOk.addEventFilter(ActionEvent.ACTION, event -> { 168 * if (!validateAndStore()) { 169 * event.consume(); 170 * } 171 * });}</pre> 172 * 173 * <h3>Dialog Closing Rules</h3> 174 * 175 * <p>It is important to understand what happens when a Dialog is closed, and 176 * also how a Dialog can be closed, especially in abnormal closing situations 177 * (such as when the 'X' button is clicked in a dialogs title bar, or when 178 * operating system specific keyboard shortcuts (such as alt-F4 on Windows) 179 * are entered). Fortunately, the outcome is well-defined in these situations, 180 * and can be best summarised in the following bullet points: 181 * 182 * <ul> 183 * <li>JavaFX dialogs can only be closed 'abnormally' (as defined above) in 184 * two situations: 185 * <ol> 186 * <li>When the dialog only has one button, or 187 * <li>When the dialog has multiple buttons, as long as one of them meets 188 * one of the following requirements: 189 * <ol> 190 * <li>The button has a {@link ButtonType} whose {@link ButtonData} is of type 191 * {@link ButtonData#CANCEL_CLOSE}.</li> 192 * <li>The button has a {@link ButtonType} whose {@link ButtonData} returns true 193 * when {@link ButtonData#isCancelButton()} is called.</li> 194 * </ol> 195 * </ol> 196 * <li>In all other situations, the dialog will refuse to respond to all 197 * close requests, remaining open until the user clicks on one of the available 198 * buttons in the {@link DialogPane} area of the dialog. 199 * <li>If a dialog is closed abnormally, and if the dialog contains a button 200 * which meets one of the two criteria above, the dialog will attempt to set 201 * the {@link #resultProperty() result} property to whatever value is returned 202 * from calling the {@link #resultConverterProperty() result converter} with 203 * the first matching {@link ButtonType}. 204 * <li>If for any reason the result converter returns null, or if the dialog 205 * is closed when only one non-cancel button is present, the 206 * {@link #resultProperty() result} property will be null, and the 207 * {@link #showAndWait()} method will return {@link Optional#empty()}. This 208 * later point means that, if you use either of option 2 or option 3 (as 209 * presented earlier in this class documentation), the 210 * {@link Optional#ifPresent(java.util.function.Consumer)} lambda will never 211 * be called, and code will continue executing as if the dialog had not 212 * returned any value at all. 213 * </ul> 214 * 215 * @param <R> The return type of the dialog, via the 216 * {@link #resultProperty() result} property. 217 * @see Alert 218 * @see TextInputDialog 219 * @see ChoiceDialog 220 * @since JavaFX 8u40 221 */ 222 public class Dialog<R> implements EventTarget { 223 224 /************************************************************************** 225 * 226 * Static fields 227 * 228 **************************************************************************/ 229 230 231 232 233 /************************************************************************** 234 * 235 * Static methods 236 * 237 **************************************************************************/ 238 239 240 241 /************************************************************************** 242 * 243 * Private fields 244 * 245 **************************************************************************/ 246 247 final FXDialog dialog; 248 249 private boolean isClosing; 250 251 252 253 /************************************************************************** 254 * 255 * Constructors 256 * 257 **************************************************************************/ 258 259 /** 260 * Creates a dialog without a specified owner. 261 */ 262 public Dialog() { 263 this.dialog = new HeavyweightDialog(this); 264 setDialogPane(new DialogPane()); 265 initModality(Modality.APPLICATION_MODAL); 266 } 267 268 269 270 /************************************************************************** 271 * 272 * Abstract methods 273 * 274 **************************************************************************/ 275 276 277 278 279 /************************************************************************** 280 * 281 * Public API 282 * 283 **************************************************************************/ 284 285 /** 286 * Shows the dialog but does not wait for a user response (in other words, 287 * this brings up a non-blocking dialog). Users of this API must either 288 * poll the {@link #resultProperty() result property}, or else add a listener 289 * to the result property to be informed of when it is set. 290 * @throws IllegalStateException if this method is called on a thread 291 * other than the JavaFX Application Thread. 292 */ 293 public final void show() { 294 Toolkit.getToolkit().checkFxUserThread(); 295 296 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_SHOWING)); 297 if (Double.isNaN(getWidth()) && Double.isNaN(getHeight())) { 298 dialog.sizeToScene(); 299 } 300 301 dialog.show(); 302 303 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_SHOWN)); 304 } 305 306 /** 307 * Shows the dialog and waits for the user response (in other words, brings 308 * up a blocking dialog, with the returned value the users input). 309 * <p> 310 * This method must be called on the JavaFX Application thread. 311 * Additionally, it must either be called from an input event handler or 312 * from the run method of a Runnable passed to 313 * {@link javafx.application.Platform#runLater Platform.runLater}. 314 * It must not be called during animation or layout processing. 315 * </p> 316 * 317 * @return An {@link Optional} that contains the {@link #resultProperty() result}. 318 * Refer to the {@link Dialog} class documentation for more detail. 319 * @throws IllegalStateException if this method is called on a thread 320 * other than the JavaFX Application Thread. 321 * @throws IllegalStateException if this method is called during 322 * animation or layout processing. 323 */ 324 public final Optional<R> showAndWait() { 325 Toolkit.getToolkit().checkFxUserThread(); 326 327 if (!Toolkit.getToolkit().canStartNestedEventLoop()) { 328 throw new IllegalStateException("showAndWait is not allowed during animation or layout processing"); 329 } 330 331 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_SHOWING)); 332 if (Double.isNaN(getWidth()) && Double.isNaN(getHeight())) { 333 dialog.sizeToScene(); 334 } 335 336 337 // this is slightly odd - we fire the SHOWN event before the show() 338 // call, so that users get the event before the dialog blocks 339 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_SHOWN)); 340 341 dialog.showAndWait(); 342 343 return Optional.ofNullable(getResult()); 344 } 345 346 /** 347 * Hides the dialog. 348 */ 349 public final void close() { 350 if (isClosing) return; 351 isClosing = true; 352 353 final R result = getResult(); 354 355 // if the result is null and we do not have permission to close the 356 // dialog, then we cancel the close request before any events are 357 // even fired 358 if (result == null && ! dialog.requestPermissionToClose(this)) { 359 isClosing = false; 360 return; 361 } 362 363 // if we are here we have permission to close the dialog. However, we 364 // may not have a result set to return to the user. Therefore, we need 365 // to handle that before the dialog closes (especially in case the 366 // dialog is blocking, in which case having a null result is really going 367 // to mess up users). 368 // 369 // In cases where the result is null, and where the dialog has a cancel 370 // button, we call into the result converter to see what to do. This is 371 // used primarily to handle the requirement that the X button has the 372 // same result as clicking the cancel button. 373 // 374 // A 'cancel button' can mean two different things (although they may 375 // be the same thing): 376 // 1) A button whose ButtonData is of type CANCEL_CLOSE. 377 // 2) A button whose ButtonData returns true for isCancelButton(). 378 if (result == null) { 379 ButtonType cancelButton = null; 380 381 // we do two things here. We are primarily looking for a button with 382 // ButtonData.CANCEL_CLOSE. If we find one, we use it as the result. 383 // However, if we don't find one, we can also use any button that 384 // is a cancel button. 385 for (ButtonType button : getDialogPane().getButtonTypes()) { 386 ButtonData buttonData = button.getButtonData(); 387 if (buttonData == null) continue; 388 389 if (buttonData == ButtonData.CANCEL_CLOSE) { 390 cancelButton = button; 391 break; 392 } 393 if (buttonData.isCancelButton()) { 394 cancelButton = button; 395 } 396 } 397 398 setResultAndClose(cancelButton, false); 399 } 400 401 // start normal closing process 402 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_HIDING)); 403 404 DialogEvent closeRequestEvent = new DialogEvent(this, DialogEvent.DIALOG_CLOSE_REQUEST); 405 Event.fireEvent(this, closeRequestEvent); 406 if (closeRequestEvent.isConsumed()) { 407 isClosing = false; 408 return; 409 } 410 411 dialog.close(); 412 413 Event.fireEvent(this, new DialogEvent(this, DialogEvent.DIALOG_HIDDEN)); 414 415 isClosing = false; 416 } 417 418 /** 419 * closes the dialog. 420 */ 421 public final void hide() { 422 close(); 423 } 424 425 /** 426 * Specifies the modality for this dialog. This must be done prior to making 427 * the dialog visible. The modality is one of: Modality.NONE, 428 * Modality.WINDOW_MODAL, or Modality.APPLICATION_MODAL. 429 * 430 * @param modality the modality for this dialog. 431 * 432 * @throws IllegalStateException if this property is set after the dialog 433 * has ever been made visible. 434 * 435 * @defaultValue Modality.APPLICATION_MODAL 436 */ 437 public final void initModality(Modality modality) { 438 dialog.initModality(modality); 439 } 440 441 /** 442 * Retrieves the modality attribute for this dialog. 443 * 444 * @return the modality. 445 */ 446 public final Modality getModality() { 447 return dialog.getModality(); 448 } 449 450 /** 451 * Specifies the style for this dialog. This must be done prior to making 452 * the dialog visible. The style is one of: StageStyle.DECORATED, 453 * StageStyle.UNDECORATED, StageStyle.TRANSPARENT, StageStyle.UTILITY, 454 * or StageStyle.UNIFIED. 455 * 456 * @param style the style for this dialog. 457 * 458 * @throws IllegalStateException if this property is set after the dialog 459 * has ever been made visible. 460 * 461 * @defaultValue StageStyle.DECORATED 462 */ 463 public final void initStyle(StageStyle style) { 464 dialog.initStyle(style); 465 } 466 467 /** 468 * Specifies the owner {@link Window} for this dialog, or null for a top-level, 469 * unowned dialog. This must be done prior to making the dialog visible. 470 * 471 * @param window the owner {@link Window} for this dialog. 472 * 473 * @throws IllegalStateException if this property is set after the dialog 474 * has ever been made visible. 475 * 476 * @defaultValue null 477 */ 478 public final void initOwner(Window window) { 479 dialog.initOwner(window); 480 } 481 482 /** 483 * Retrieves the owner Window for this dialog, or null for an unowned dialog. 484 * 485 * @return the owner Window. 486 */ 487 public final Window getOwner() { 488 return dialog.getOwner(); 489 } 490 491 492 493 /************************************************************************** 494 * 495 * Properties 496 * 497 **************************************************************************/ 498 499 // --- dialog Pane 500 /** 501 * The root node of the dialog, the {@link DialogPane} contains all visual 502 * elements shown in the dialog. As such, it is possible to completely adjust 503 * the display of the dialog by modifying the existing dialog pane or creating 504 * a new one. 505 */ 506 private ObjectProperty<DialogPane> dialogPane = new SimpleObjectProperty<DialogPane>(this, "dialogPane", new DialogPane()) { 507 final InvalidationListener expandedListener = o -> { 508 DialogPane dialogPane = getDialogPane(); 509 if (dialogPane == null) return; 510 511 final Node content = dialogPane.getExpandableContent(); 512 final boolean isExpanded = content == null ? false : content.isVisible(); 513 setResizable(isExpanded); 514 515 Dialog.this.dialog.sizeToScene(); 516 }; 517 518 final InvalidationListener headerListener = o -> { 519 updatePseudoClassState(); 520 }; 521 522 WeakReference<DialogPane> dialogPaneRef = new WeakReference<>(null); 523 524 protected void invalidated() { 525 DialogPane oldDialogPane = dialogPaneRef.get(); 526 if (oldDialogPane != null) { 527 // clean up 528 oldDialogPane.expandedProperty().removeListener(expandedListener); 529 oldDialogPane.headerProperty().removeListener(headerListener); 530 oldDialogPane.headerTextProperty().removeListener(headerListener); 531 oldDialogPane.setDialog(null); 532 } 533 534 final DialogPane newDialogPane = getDialogPane(); 535 536 if (newDialogPane != null) { 537 newDialogPane.setDialog(Dialog.this); 538 539 // if the buttons change, we dynamically update the dialog 540 newDialogPane.getButtonTypes().addListener((ListChangeListener<ButtonType>) c -> { 541 newDialogPane.requestLayout(); 542 }); 543 newDialogPane.expandedProperty().addListener(expandedListener); 544 newDialogPane.headerProperty().addListener(headerListener); 545 newDialogPane.headerTextProperty().addListener(headerListener); 546 547 updatePseudoClassState(); 548 newDialogPane.requestLayout(); 549 } 550 551 // push the new dialog down into the implementation for rendering 552 dialog.setDialogPane(newDialogPane); 553 554 dialogPaneRef = new WeakReference<DialogPane>(newDialogPane); 555 } 556 }; 557 558 public final ObjectProperty<DialogPane> dialogPaneProperty() { 559 return dialogPane; 560 } 561 562 public final DialogPane getDialogPane() { 563 return dialogPane.get(); 564 } 565 566 public final void setDialogPane(DialogPane value) { 567 dialogPane.set(value); 568 } 569 570 571 // --- content text (forwarded from DialogPane) 572 /** 573 * A property representing the content text for the dialog pane. The content text 574 * is lower precedence than the {@link DialogPane#contentProperty() content node}, meaning 575 * that if both the content node and the contentText properties are set, the 576 * content text will not be displayed in a default DialogPane instance. 577 */ 578 public final StringProperty contentTextProperty() { 579 return getDialogPane().contentTextProperty(); 580 } 581 582 /** 583 * Returns the currently-set content text for this DialogPane. 584 */ 585 public final String getContentText() { 586 return getDialogPane().getContentText(); 587 } 588 589 /** 590 * Sets the string to show in the dialog content area. Note that the content text 591 * is lower precedence than the {@link DialogPane#contentProperty() content node}, meaning 592 * that if both the content node and the contentText properties are set, the 593 * content text will not be displayed in a default DialogPane instance. 594 */ 595 public final void setContentText(String contentText) { 596 getDialogPane().setContentText(contentText); 597 } 598 599 600 // --- header text (forwarded from DialogPane) 601 /** 602 * A property representing the header text for the dialog pane. The header text 603 * is lower precedence than the {@link DialogPane#headerProperty() header node}, meaning 604 * that if both the header node and the headerText properties are set, the 605 * header text will not be displayed in a default DialogPane instance. 606 */ 607 public final StringProperty headerTextProperty() { 608 return getDialogPane().headerTextProperty(); 609 } 610 611 /** 612 * Returns the currently-set header text for this DialogPane. 613 */ 614 public final String getHeaderText() { 615 return getDialogPane().getHeaderText(); 616 } 617 618 /** 619 * Sets the string to show in the dialog header area. Note that the header text 620 * is lower precedence than the {@link DialogPane#headerProperty() header node}, meaning 621 * that if both the header node and the headerText properties are set, the 622 * header text will not be displayed in a default DialogPane instance. 623 */ 624 public final void setHeaderText(String headerText) { 625 getDialogPane().setHeaderText(headerText); 626 } 627 628 629 // --- graphic (forwarded from DialogPane) 630 /** 631 * The dialog graphic, presented either in the header, if one is showing, or 632 * to the left of the {@link DialogPane#contentProperty() content}. 633 * 634 * @return An ObjectProperty wrapping the current graphic. 635 */ 636 public final ObjectProperty<Node> graphicProperty() { 637 return getDialogPane().graphicProperty(); 638 } 639 640 public final Node getGraphic() { 641 return getDialogPane().getGraphic(); 642 } 643 644 /** 645 * Sets the dialog graphic, which will be displayed either in the header, if 646 * one is showing, or to the left of the {@link DialogPane#contentProperty() content}. 647 * 648 * @param graphic 649 * The new dialog graphic, or null if no graphic should be shown. 650 */ 651 public final void setGraphic(Node graphic) { 652 getDialogPane().setGraphic(graphic); 653 } 654 655 656 // --- result 657 private final ObjectProperty<R> resultProperty = new SimpleObjectProperty<R>() { 658 protected void invalidated() { 659 close(); 660 } 661 }; 662 663 /** 664 * A property representing what has been returned from the dialog. A result 665 * is generated through the {@link #resultConverterProperty() result converter}, 666 * which is intended to convert from the {@link ButtonType} that the user 667 * clicked on into a value of type R. Refer to the {@link Dialog} class 668 * JavaDoc for more details. 669 */ 670 public final ObjectProperty<R> resultProperty() { 671 return resultProperty; 672 } 673 674 public final R getResult() { 675 return resultProperty().get(); 676 } 677 678 public final void setResult(R value) { 679 this.resultProperty().set(value); 680 } 681 682 683 // --- result converter 684 private final ObjectProperty<Callback<ButtonType, R>> resultConverterProperty 685 = new SimpleObjectProperty<>(this, "resultConverter"); 686 687 /** 688 * API to convert the {@link ButtonType} that the user clicked on into a 689 * result that can be returned via the {@link #resultProperty() result} 690 * property. This is necessary as {@link ButtonType} represents the visual 691 * button within the dialog, and do not know how to map themselves to a valid 692 * result - that is a requirement of the dialog implementation by making use 693 * of the result converter. In some cases, the result type of a Dialog 694 * subclass is ButtonType (which means that the result converter can be null), 695 * but in some cases (where the result type, R, is not ButtonType or Void), 696 * this callback must be specified. 697 */ 698 public final ObjectProperty<Callback<ButtonType, R>> resultConverterProperty() { 699 return resultConverterProperty; 700 } 701 702 public final Callback<ButtonType, R> getResultConverter() { 703 return resultConverterProperty().get(); 704 } 705 706 public final void setResultConverter(Callback<ButtonType, R> value) { 707 this.resultConverterProperty().set(value); 708 } 709 710 711 // --- showing 712 /** 713 * Represents whether the dialog is currently showing. 714 */ 715 public final ReadOnlyBooleanProperty showingProperty() { 716 return dialog.showingProperty(); 717 } 718 719 /** 720 * Returns whether or not the dialog is showing. 721 * 722 * @return true if dialog is showing. 723 */ 724 public final boolean isShowing() { 725 return showingProperty().get(); 726 } 727 728 729 // --- resizable 730 /** 731 * Represents whether the dialog is resizable. 732 */ 733 public final BooleanProperty resizableProperty() { 734 return dialog.resizableProperty(); 735 } 736 737 /** 738 * Returns whether or not the dialog is resizable. 739 * 740 * @return true if dialog is resizable. 741 */ 742 public final boolean isResizable() { 743 return resizableProperty().get(); 744 } 745 746 /** 747 * Sets whether the dialog can be resized by the user. 748 * Resizable dialogs can also be maximized ( maximize button 749 * becomes visible) 750 * 751 * @param resizable true if dialog should be resizable. 752 */ 753 public final void setResizable(boolean resizable) { 754 resizableProperty().set(resizable); 755 } 756 757 758 // --- width 759 /** 760 * Property representing the width of the dialog. 761 */ 762 public final ReadOnlyDoubleProperty widthProperty() { 763 return dialog.widthProperty(); 764 } 765 766 /** 767 * Returns the width of the dialog. 768 */ 769 public final double getWidth() { 770 return widthProperty().get(); 771 } 772 773 /** 774 * Sets the width of the dialog. 775 */ 776 public final void setWidth(double width) { 777 dialog.setWidth(width); 778 } 779 780 781 // --- height 782 /** 783 * Property representing the height of the dialog. 784 */ 785 public final ReadOnlyDoubleProperty heightProperty() { 786 return dialog.heightProperty(); 787 } 788 789 /** 790 * Returns the height of the dialog. 791 */ 792 public final double getHeight() { 793 return heightProperty().get(); 794 } 795 796 /** 797 * Sets the height of the dialog. 798 */ 799 public final void setHeight(double height) { 800 dialog.setHeight(height); 801 } 802 803 804 // --- title 805 /** 806 * Return the titleProperty of the dialog. 807 */ 808 public final StringProperty titleProperty(){ 809 return this.dialog.titleProperty(); 810 } 811 812 /** 813 * Return the title of the dialog. 814 */ 815 public final String getTitle(){ 816 return this.dialog.titleProperty().get(); 817 } 818 /** 819 * Change the Title of the dialog. 820 * @param title 821 */ 822 public final void setTitle(String title){ 823 this.dialog.titleProperty().set(title); 824 } 825 826 827 // --- x 828 public final double getX() { 829 return dialog.getX(); 830 } 831 832 public final void setX(double x) { 833 dialog.setX(x); 834 } 835 836 /** 837 * The horizontal location of this {@code Dialog}. Changing this attribute 838 * will move the {@code Dialog} horizontally. 839 */ 840 public final ReadOnlyDoubleProperty xProperty() { 841 return dialog.xProperty(); 842 } 843 844 // --- y 845 public final double getY() { 846 return dialog.getY(); 847 } 848 849 public final void setY(double y) { 850 dialog.setY(y); 851 } 852 853 /** 854 * The vertical location of this {@code Dialog}. Changing this attribute 855 * will move the {@code Dialog} vertically. 856 */ 857 public final ReadOnlyDoubleProperty yProperty() { 858 return dialog.yProperty(); 859 } 860 861 862 863 /*************************************************************************** 864 * 865 * Events 866 * 867 **************************************************************************/ 868 869 private final EventHandlerManager eventHandlerManager = new EventHandlerManager(this); 870 871 /** {@inheritDoc} */ 872 @Override public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) { 873 return tail.prepend(eventHandlerManager); 874 } 875 876 /** 877 * Called just prior to the Dialog being shown. 878 */ 879 private ObjectProperty<EventHandler<DialogEvent>> onShowing; 880 public final void setOnShowing(EventHandler<DialogEvent> value) { onShowingProperty().set(value); } 881 public final EventHandler<DialogEvent> getOnShowing() { 882 return onShowing == null ? null : onShowing.get(); 883 } 884 public final ObjectProperty<EventHandler<DialogEvent>> onShowingProperty() { 885 if (onShowing == null) { 886 onShowing = new SimpleObjectProperty<EventHandler<DialogEvent>>(this, "onShowing") { 887 @Override protected void invalidated() { 888 eventHandlerManager.setEventHandler(DialogEvent.DIALOG_SHOWING, get()); 889 } 890 }; 891 } 892 return onShowing; 893 } 894 895 /** 896 * Called just after the Dialog is shown. 897 */ 898 private ObjectProperty<EventHandler<DialogEvent>> onShown; 899 public final void setOnShown(EventHandler<DialogEvent> value) { onShownProperty().set(value); } 900 public final EventHandler<DialogEvent> getOnShown() { 901 return onShown == null ? null : onShown.get(); 902 } 903 public final ObjectProperty<EventHandler<DialogEvent>> onShownProperty() { 904 if (onShown == null) { 905 onShown = new SimpleObjectProperty<EventHandler<DialogEvent>>(this, "onShown") { 906 @Override protected void invalidated() { 907 eventHandlerManager.setEventHandler(DialogEvent.DIALOG_SHOWN, get()); 908 } 909 }; 910 } 911 return onShown; 912 } 913 914 /** 915 * Called just prior to the Dialog being hidden. 916 */ 917 private ObjectProperty<EventHandler<DialogEvent>> onHiding; 918 public final void setOnHiding(EventHandler<DialogEvent> value) { onHidingProperty().set(value); } 919 public final EventHandler<DialogEvent> getOnHiding() { 920 return onHiding == null ? null : onHiding.get(); 921 } 922 public final ObjectProperty<EventHandler<DialogEvent>> onHidingProperty() { 923 if (onHiding == null) { 924 onHiding = new SimpleObjectProperty<EventHandler<DialogEvent>>(this, "onHiding") { 925 @Override protected void invalidated() { 926 eventHandlerManager.setEventHandler(DialogEvent.DIALOG_HIDING, get()); 927 } 928 }; 929 } 930 return onHiding; 931 } 932 933 /** 934 * Called just after the Dialog has been hidden. 935 * When the {@code Dialog} is hidden, this event handler is invoked allowing 936 * the developer to clean up resources or perform other tasks when the 937 * {@link Alert} is closed. 938 */ 939 private ObjectProperty<EventHandler<DialogEvent>> onHidden; 940 public final void setOnHidden(EventHandler<DialogEvent> value) { onHiddenProperty().set(value); } 941 public final EventHandler<DialogEvent> getOnHidden() { 942 return onHidden == null ? null : onHidden.get(); 943 } 944 public final ObjectProperty<EventHandler<DialogEvent>> onHiddenProperty() { 945 if (onHidden == null) { 946 onHidden = new SimpleObjectProperty<EventHandler<DialogEvent>>(this, "onHidden") { 947 @Override protected void invalidated() { 948 eventHandlerManager.setEventHandler(DialogEvent.DIALOG_HIDDEN, get()); 949 } 950 }; 951 } 952 return onHidden; 953 } 954 955 /** 956 * Called when there is an external request to close this {@code Dialog}. 957 * The installed event handler can prevent dialog closing by consuming the 958 * received event. 959 */ 960 private ObjectProperty<EventHandler<DialogEvent>> onCloseRequest; 961 public final void setOnCloseRequest(EventHandler<DialogEvent> value) { 962 onCloseRequestProperty().set(value); 963 } 964 public final EventHandler<DialogEvent> getOnCloseRequest() { 965 return (onCloseRequest != null) ? onCloseRequest.get() : null; 966 } 967 public final ObjectProperty<EventHandler<DialogEvent>> 968 onCloseRequestProperty() { 969 if (onCloseRequest == null) { 970 onCloseRequest = new SimpleObjectProperty<EventHandler<DialogEvent>>(this, "onCloseRequest") { 971 @Override protected void invalidated() { 972 eventHandlerManager.setEventHandler(DialogEvent.DIALOG_CLOSE_REQUEST, get()); 973 } 974 }; 975 } 976 return onCloseRequest; 977 } 978 979 980 981 /*************************************************************************** 982 * 983 * Private implementation 984 * 985 **************************************************************************/ 986 987 // This code is called both in the normal and in the abnormal case (i.e. 988 // both when a button is clicked and when the user forces a window closed 989 // with keyboard OS-specific shortchuts or OS-native titlebar buttons). 990 @SuppressWarnings("unchecked") 991 void setResultAndClose(ButtonType cmd, boolean close) { 992 Callback<ButtonType, R> resultConverter = getResultConverter(); 993 994 R priorResultValue = getResult(); 995 R newResultValue = null; 996 997 if (resultConverter == null) { 998 // The choice to cast cmd to R here was a conscious decision, taking 999 // into account the choices available to us. Firstly, to summarise the 1000 // issue, at this point here we have a null result converter, and no 1001 // idea how to convert the given ButtonType to R. Our options are: 1002 // 1003 // 1) We could throw an exception here, but this requires that all 1004 // developers who create a dialog set a result converter (at least 1005 // setResultConverter(buttonType -> (R) buttonType)). This is 1006 // non-intuitive and depends on the developer reading documentation. 1007 // 1008 // 2) We could set a default result converter in the resultConverter 1009 // property that does the identity conversion. This saves people from 1010 // having to set a default result converter, but it is a little odd 1011 // that the result converter is non-null by default. 1012 // 1013 // 3) We can cast the button type here, which is what we do. This means 1014 // that the result converter is null by default. 1015 // 1016 // In the case of option 1), developers will receive a NPE when the 1017 // dialog is closed, regardless of how it was closed. In the case of 1018 // option 2) and 3), the user unfortunately receives a ClassCastException 1019 // in their code. This is unfortunate as it is not immediately obvious 1020 // why the ClassCastException occurred, and how to resolve it. However, 1021 // we decided to take this later approach as it prevents the issue of 1022 // requiring all custom dialog developers from having to supply their 1023 // own result converters. 1024 newResultValue = (R) cmd; 1025 } else { 1026 newResultValue = resultConverter.call(cmd); 1027 } 1028 1029 setResult(newResultValue); 1030 1031 // fix for the case where we set the same result as what 1032 // was already set. We should still close the dialog, but 1033 // we need to special-case it here, as the result property 1034 // won't fire any event if the value won't change. 1035 if (close && priorResultValue == newResultValue) { 1036 close(); 1037 } 1038 } 1039 1040 1041 1042 1043 /*************************************************************************** 1044 * 1045 * Stylesheet Handling 1046 * 1047 **************************************************************************/ 1048 private static final PseudoClass HEADER_PSEUDO_CLASS = 1049 PseudoClass.getPseudoClass("header"); //$NON-NLS-1$ 1050 private static final PseudoClass NO_HEADER_PSEUDO_CLASS = 1051 PseudoClass.getPseudoClass("no-header"); //$NON-NLS-1$ 1052 1053 private void updatePseudoClassState() { 1054 DialogPane dialogPane = getDialogPane(); 1055 if (dialogPane != null) { 1056 final boolean hasHeader = getDialogPane().hasHeader(); 1057 dialogPane.pseudoClassStateChanged(HEADER_PSEUDO_CLASS, hasHeader); 1058 dialogPane.pseudoClassStateChanged(NO_HEADER_PSEUDO_CLASS, !hasHeader); 1059 } 1060 } 1061 }