< prev index next >

modules/javafx.controls/src/main/java/javafx/scene/control/Dialog.java

Print this page




 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);


 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     }


 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 




 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         @Override
 525         protected void invalidated() {
 526             DialogPane oldDialogPane = dialogPaneRef.get();
 527             if (oldDialogPane != null) {
 528                 // clean up
 529                 oldDialogPane.expandedProperty().removeListener(expandedListener);
 530                 oldDialogPane.headerProperty().removeListener(headerListener);
 531                 oldDialogPane.headerTextProperty().removeListener(headerListener);
 532                 oldDialogPane.setDialog(null);
 533             }
 534 
 535             final DialogPane newDialogPane = getDialogPane();
 536 
 537             if (newDialogPane != null) {
 538                 newDialogPane.setDialog(Dialog.this);
 539 
 540                 // if the buttons change, we dynamically update the dialog
 541                 newDialogPane.getButtonTypes().addListener((ListChangeListener<ButtonType>) c -> {
 542                     newDialogPane.requestLayout();
 543                 });
 544                 newDialogPane.expandedProperty().addListener(expandedListener);


 558 
 559     public final ObjectProperty<DialogPane> dialogPaneProperty() {
 560         return dialogPane;
 561     }
 562 
 563     public final DialogPane getDialogPane() {
 564         return dialogPane.get();
 565     }
 566 
 567     public final void setDialogPane(DialogPane value) {
 568         dialogPane.set(value);
 569     }
 570 
 571 
 572     // --- content text (forwarded from DialogPane)
 573     /**
 574      * A property representing the content text for the dialog pane. The content text
 575      * is lower precedence than the {@link DialogPane#contentProperty() content node}, meaning
 576      * that if both the content node and the contentText properties are set, the
 577      * content text will not be displayed in a default DialogPane instance.
 578      * @return the property representing the content text for the dialog pane
 579      */
 580     public final StringProperty contentTextProperty() {
 581         return getDialogPane().contentTextProperty();
 582     }
 583 
 584     /**
 585      * Returns the currently-set content text for this DialogPane.
 586      * @return the currently-set content text for this DialogPane
 587      */
 588     public final String getContentText() {
 589         return getDialogPane().getContentText();
 590     }
 591 
 592     /**
 593      * Sets the string to show in the dialog content area. Note that the content text
 594      * is lower precedence than the {@link DialogPane#contentProperty() content node}, meaning
 595      * that if both the content node and the contentText properties are set, the
 596      * content text will not be displayed in a default DialogPane instance.
 597      * @param contentText the string to show in the dialog content area
 598      */
 599     public final void setContentText(String contentText) {
 600         getDialogPane().setContentText(contentText);
 601     }
 602 
 603 
 604     // --- header text (forwarded from DialogPane)
 605     /**
 606      * A property representing the header text for the dialog pane. The header text
 607      * is lower precedence than the {@link DialogPane#headerProperty() header node}, meaning
 608      * that if both the header node and the headerText properties are set, the
 609      * header text will not be displayed in a default DialogPane instance.
 610      * @return a property representing the header text for the dialog pane
 611      */
 612     public final StringProperty headerTextProperty() {
 613         return getDialogPane().headerTextProperty();
 614     }
 615 
 616     /**
 617      * Returns the currently-set header text for this DialogPane.
 618      * @return the currently-set header text for this DialogPane
 619      */
 620     public final String getHeaderText() {
 621         return getDialogPane().getHeaderText();
 622     }
 623 
 624     /**
 625      * Sets the string to show in the dialog header area. Note that the header text
 626      * is lower precedence than the {@link DialogPane#headerProperty() header node}, meaning
 627      * that if both the header node and the headerText properties are set, the
 628      * header text will not be displayed in a default DialogPane instance.
 629      * @param headerText the string to show in the dialog header area
 630      */
 631     public final void setHeaderText(String headerText) {
 632         getDialogPane().setHeaderText(headerText);
 633     }
 634 
 635 
 636     // --- graphic (forwarded from DialogPane)
 637     /**
 638      * The dialog graphic, presented either in the header, if one is showing, or
 639      * to the left of the {@link DialogPane#contentProperty() content}.
 640      *
 641      * @return An ObjectProperty wrapping the current graphic.
 642      */
 643     public final ObjectProperty<Node> graphicProperty() {
 644         return getDialogPane().graphicProperty();
 645     }
 646 
 647     public final Node getGraphic() {
 648         return getDialogPane().getGraphic();
 649     }


 656      *            The new dialog graphic, or null if no graphic should be shown.
 657      */
 658     public final void setGraphic(Node graphic) {
 659         getDialogPane().setGraphic(graphic);
 660     }
 661 
 662 
 663     // --- result
 664     private final ObjectProperty<R> resultProperty = new SimpleObjectProperty<R>() {
 665         protected void invalidated() {
 666             close();
 667         }
 668     };
 669 
 670     /**
 671      * A property representing what has been returned from the dialog. A result
 672      * is generated through the {@link #resultConverterProperty() result converter},
 673      * which is intended to convert from the {@link ButtonType} that the user
 674      * clicked on into a value of type R. Refer to the {@link Dialog} class
 675      * JavaDoc for more details.
 676      * @return a property representing what has been returned from the dialog
 677      */
 678     public final ObjectProperty<R> resultProperty() {
 679         return resultProperty;
 680     }
 681 
 682     public final R getResult() {
 683         return resultProperty().get();
 684     }
 685 
 686     public final void setResult(R value) {
 687         this.resultProperty().set(value);
 688     }
 689 
 690 
 691     // --- result converter
 692     private final ObjectProperty<Callback<ButtonType, R>> resultConverterProperty
 693         = new SimpleObjectProperty<>(this, "resultConverter");
 694 
 695     /**
 696      * API to convert the {@link ButtonType} that the user clicked on into a
 697      * result that can be returned via the {@link #resultProperty() result}
 698      * property. This is necessary as {@link ButtonType} represents the visual
 699      * button within the dialog, and do not know how to map themselves to a valid
 700      * result - that is a requirement of the dialog implementation by making use
 701      * of the result converter. In some cases, the result type of a Dialog
 702      * subclass is ButtonType (which means that the result converter can be null),
 703      * but in some cases (where the result type, R, is not ButtonType or Void),
 704      * this callback must be specified.
 705      * @return the API to convert the {@link ButtonType} that the user clicked on
 706      */
 707     public final ObjectProperty<Callback<ButtonType, R>> resultConverterProperty() {
 708         return resultConverterProperty;
 709     }
 710 
 711     public final Callback<ButtonType, R> getResultConverter() {
 712         return resultConverterProperty().get();
 713     }
 714 
 715     public final void setResultConverter(Callback<ButtonType, R> value) {
 716         this.resultConverterProperty().set(value);
 717     }
 718 
 719 
 720     // --- showing
 721     /**
 722      * Represents whether the dialog is currently showing.
 723      * @return the property representing whether the dialog is currently showing
 724      */
 725     public final ReadOnlyBooleanProperty showingProperty() {
 726         return dialog.showingProperty();
 727     }
 728 
 729     /**
 730      * Returns whether or not the dialog is showing.
 731      *
 732      * @return true if dialog is showing.
 733      */
 734     public final boolean isShowing() {
 735         return showingProperty().get();
 736     }
 737 
 738 
 739     // --- resizable
 740     /**
 741      * Represents whether the dialog is resizable.
 742      * @return the property representing whether the dialog is resizable
 743      */
 744     public final BooleanProperty resizableProperty() {
 745         return dialog.resizableProperty();
 746     }
 747 
 748     /**
 749      * Returns whether or not the dialog is resizable.
 750      *
 751      * @return true if dialog is resizable.
 752      */
 753     public final boolean isResizable() {
 754         return resizableProperty().get();
 755     }
 756 
 757     /**
 758      * Sets whether the dialog can be resized by the user.
 759      * Resizable dialogs can also be maximized ( maximize button
 760      * becomes visible)
 761      *
 762      * @param resizable true if dialog should be resizable.
 763      */
 764     public final void setResizable(boolean resizable) {
 765         resizableProperty().set(resizable);
 766     }
 767 
 768 
 769     // --- width
 770     /**
 771      * Property representing the width of the dialog.
 772      * @return the property representing the width of the dialog
 773      */
 774     public final ReadOnlyDoubleProperty widthProperty() {
 775         return dialog.widthProperty();
 776     }
 777 
 778     /**
 779      * Returns the width of the dialog.
 780      * @return the width of the dialog
 781      */
 782     public final double getWidth() {
 783         return widthProperty().get();
 784     }
 785 
 786     /**
 787      * Sets the width of the dialog.
 788      * @param width the width of the dialog
 789      */
 790     public final void setWidth(double width) {
 791         dialog.setWidth(width);
 792     }
 793 
 794 
 795     // --- height
 796     /**
 797      * Property representing the height of the dialog.
 798      * @return the property representing the height of the dialog
 799      */
 800     public final ReadOnlyDoubleProperty heightProperty() {
 801         return dialog.heightProperty();
 802     }
 803 
 804     /**
 805      * Returns the height of the dialog.
 806      * @return the height of the dialog
 807      */
 808     public final double getHeight() {
 809         return heightProperty().get();
 810     }
 811 
 812     /**
 813      * Sets the height of the dialog.
 814      * @param height the height of the dialog
 815      */
 816     public final void setHeight(double height) {
 817         dialog.setHeight(height);
 818     }
 819 
 820 
 821     // --- title
 822     /**
 823      * Return the titleProperty of the dialog.
 824      * @return the titleProperty of the dialog
 825      */
 826     public final StringProperty titleProperty(){
 827         return this.dialog.titleProperty();
 828     }
 829 
 830     /**
 831      * Return the title of the dialog.
 832      * @return the title of the dialog
 833      */
 834     public final String getTitle(){
 835         return this.dialog.titleProperty().get();
 836     }
 837     /**
 838      * Change the Title of the dialog.
 839      * @param title the Title of the dialog
 840      */
 841     public final void setTitle(String title){
 842         this.dialog.titleProperty().set(title);
 843     }
 844 
 845 
 846     // --- x
 847     public final double getX() {
 848         return dialog.getX();
 849     }
 850 
 851     public final void setX(double x) {
 852         dialog.setX(x);
 853     }
 854 
 855     /**
 856      * The horizontal location of this {@code Dialog}. Changing this attribute
 857      * will move the {@code Dialog} horizontally.
 858      * @return the horizontal location of this {@code Dialog}
 859      */
 860     public final ReadOnlyDoubleProperty xProperty() {
 861         return dialog.xProperty();
 862     }
 863 
 864     // --- y
 865     public final double getY() {
 866         return dialog.getY();
 867     }
 868 
 869     public final void setY(double y) {
 870         dialog.setY(y);
 871     }
 872 
 873     /**
 874      * The vertical location of this {@code Dialog}. Changing this attribute
 875      * will move the {@code Dialog} vertically.
 876      * @return the vertical location of this {@code Dialog}
 877      */
 878     public final ReadOnlyDoubleProperty yProperty() {
 879         return dialog.yProperty();
 880     }
 881 
 882 
 883 
 884     /***************************************************************************
 885      *
 886      * Events
 887      *
 888      **************************************************************************/
 889 
 890     private final EventHandlerManager eventHandlerManager = new EventHandlerManager(this);
 891 
 892     /** {@inheritDoc} */
 893     @Override public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) {
 894         return tail.prepend(eventHandlerManager);
 895     }
 896 


< prev index next >