1 /*
   2  * Copyright (c) 2014, 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 com.sun.javafx.scene.control.skin.resources.ControlResources;
  28 import javafx.application.Platform;
  29 import javafx.beans.NamedArg;
  30 import javafx.geometry.Pos;
  31 import javafx.scene.control.ButtonBar.ButtonData;
  32 import javafx.scene.layout.GridPane;
  33 import javafx.scene.layout.Priority;
  34 import javafx.scene.layout.Region;
  35 
  36 /**
  37  * A dialog that shows a text input control to the user.
  38  *
  39  * @see Dialog
  40  * @since JavaFX 8u40
  41  */
  42 public class TextInputDialog extends Dialog<String> {
  43 
  44     /**************************************************************************
  45      *
  46      * Fields
  47      *
  48      **************************************************************************/
  49 
  50     private final GridPane grid;
  51     private final Label label;
  52     private final TextField textField;
  53     private final String defaultValue;
  54 
  55 
  56 
  57     /**************************************************************************
  58      *
  59      * Constructors
  60      *
  61      **************************************************************************/
  62 
  63     /**
  64      * Creates a new TextInputDialog without a default value entered into the
  65      * dialog {@link TextField}.
  66      */
  67     public TextInputDialog() {
  68         this("");
  69     }
  70 
  71     /**
  72      * Creates a new TextInputDialog with the default value entered into the
  73      * dialog {@link TextField}.
  74      * @param defaultValue the default value entered into the dialog
  75      */
  76     public TextInputDialog(@NamedArg("defaultValue") String defaultValue) {
  77         final DialogPane dialogPane = getDialogPane();
  78 
  79         // -- textfield
  80         this.textField = new TextField(defaultValue);
  81         this.textField.setMaxWidth(Double.MAX_VALUE);
  82         GridPane.setHgrow(textField, Priority.ALWAYS);
  83         GridPane.setFillWidth(textField, true);
  84 
  85         // -- label
  86         label = DialogPane.createContentLabel(dialogPane.getContentText());
  87         label.setPrefWidth(Region.USE_COMPUTED_SIZE);
  88         label.textProperty().bind(dialogPane.contentTextProperty());
  89 
  90         this.defaultValue = defaultValue;
  91 
  92         this.grid = new GridPane();
  93         this.grid.setHgap(10);
  94         this.grid.setMaxWidth(Double.MAX_VALUE);
  95         this.grid.setAlignment(Pos.CENTER_LEFT);
  96 
  97         dialogPane.contentTextProperty().addListener(o -> updateGrid());
  98 
  99         setTitle(ControlResources.getString("Dialog.confirm.title"));
 100         dialogPane.setHeaderText(ControlResources.getString("Dialog.confirm.header"));
 101         dialogPane.getStyleClass().add("text-input-dialog");
 102         dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
 103 
 104         updateGrid();
 105 
 106         setResultConverter((dialogButton) -> {
 107             ButtonData data = dialogButton == null ? null : dialogButton.getButtonData();
 108             return data == ButtonData.OK_DONE ? textField.getText() : null;
 109         });
 110     }
 111 
 112 
 113 
 114     /**************************************************************************
 115      *
 116      * Public API
 117      *
 118      **************************************************************************/
 119 
 120     /**
 121      * Returns the {@link TextField} used within this dialog.
 122      * @return the {@link TextField} used within this dialog
 123      */
 124     public final TextField getEditor() {
 125         return textField;
 126     }
 127 
 128     /**
 129      * Returns the default value that was specified in the constructor.
 130      * @return the default value that was specified in the constructor
 131      */
 132     public final String getDefaultValue() {
 133         return defaultValue;
 134     }
 135 
 136 
 137 
 138     /**************************************************************************
 139      *
 140      * Private Implementation
 141      *
 142      **************************************************************************/
 143 
 144     private void updateGrid() {
 145         grid.getChildren().clear();
 146 
 147         grid.add(label, 0, 0);
 148         grid.add(textField, 1, 0);
 149         getDialogPane().setContent(grid);
 150 
 151         Platform.runLater(() -> textField.requestFocus());
 152     }
 153 }