1 /*
   2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.language.stringbinding;
  33 
  34 import java.text.SimpleDateFormat;
  35 import java.util.Calendar;
  36 import java.util.Date;
  37 import javafx.application.Application;
  38 import javafx.beans.binding.StringBinding;
  39 import javafx.geometry.Insets;
  40 import javafx.geometry.Pos;
  41 import javafx.scene.Parent;
  42 import javafx.scene.Scene;
  43 import javafx.scene.control.Label;
  44 import javafx.scene.control.TextField;
  45 import javafx.scene.layout.VBox;
  46 import javafx.stage.Stage;
  47 
  48 /**
  49  * A sample that demonstrates how to bind text properties so the
  50  * value of the bound property is updated automatically when the value
  51  * of the original property is changed.
  52  *
  53  * @sampleName String Binding
  54  * @preview preview.png
  55  * @see javafx.beans.binding.StringBinding
  56  * @see javafx.scene.control.TextField
  57  * @see javafx.scene.control.Label
  58  * @embedded
  59  */
  60 public class StringBindingApp extends Application {
  61 
  62     public Parent createContent() {
  63         final SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
  64         format.setLenient(false);
  65         final TextField dateField = new TextField();
  66         dateField.setPromptText("Enter a birth date");
  67         dateField.setMaxHeight(TextField.USE_PREF_SIZE);
  68         dateField.setMaxWidth(TextField.USE_PREF_SIZE);
  69 
  70         Label label = new Label();
  71         label.setWrapText(true);
  72         label.textProperty().bind(new StringBinding() {
  73             {
  74                 bind(dateField.textProperty());
  75             }
  76 
  77             @Override
  78             protected String computeValue() {
  79                 try {
  80                     Date date = format.parse(dateField.getText());
  81                     Calendar c = Calendar.getInstance();
  82                     c.setTime(date);
  83 
  84                     Date today = new Date();
  85                     Calendar c2 = Calendar.getInstance();
  86                     c2.setTime(today);
  87                     
  88                     if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR) - 1
  89                             && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
  90                         return "You were born yesterday";
  91                     } else if (c.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR)
  92                                && c.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {
  93                         return "You were born today";
  94                     } else {
  95                         return "You were born " + format.format(date);
  96                     }
  97                 } catch (Exception e) {
  98                     return "Enter your valid birth date (mm/dd/yyyy)";
  99                 }
 100             }
 101         });
 102 
 103         VBox vBox = new VBox(7);
 104         vBox.setPadding(new Insets(12));
 105         vBox.getChildren().addAll(label, dateField);
 106         vBox.setAlignment(Pos.CENTER);
 107         return vBox;
 108     }
 109 
 110     @Override
 111     public void start(Stage primaryStage) throws Exception {
 112         primaryStage.setScene(new Scene(createContent()));
 113         primaryStage.show();
 114     }
 115 
 116     /**
 117      * Java main for when running without JavaFX launcher
 118      * @param args command line arguments
 119      */
 120     public static void main(String[] args) {
 121         launch(args);
 122     }
 123 }