1 /*
   2  * Copyright (c) 2012, 2013, 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 com.sun.javafx.scene.control;
  27 
  28 import javafx.beans.property.*;
  29 import javafx.event.ActionEvent;
  30 import javafx.event.EventHandler;
  31 import javafx.scene.control.Control;
  32 
  33 /**
  34  *
  35  */
  36 public abstract class InputField extends Control {
  37     /**
  38      * The default value for {@link #prefColumnCount}.
  39      */
  40     public static final int DEFAULT_PREF_COLUMN_COUNT = 12;
  41 
  42     /**
  43      * Indicates whether this InputField can be edited by the user. If true, the
  44      * "readonly" pseudo class will be false, but if false, the "readonly"
  45      * pseudo class will be true.
  46      */
  47     private BooleanProperty editable = new SimpleBooleanProperty(this, "editable", true);
  48     public final boolean isEditable() { return editable.getValue(); }
  49     public final void setEditable(boolean value) { editable.setValue(value); }
  50     public final BooleanProperty editableProperty() { return editable; }
  51 
  52     /**
  53      * The {@code InputField}'s prompt text to display, or
  54      * <tt>null</tt> if no prompt text is displayed.
  55      */
  56     private StringProperty promptText = new StringPropertyBase("") {
  57         @Override protected void invalidated() {
  58             // Strip out newlines
  59             String txt = get();
  60             if (txt != null && txt.contains("\n")) {
  61                 txt = txt.replace("\n", "");
  62                 set(txt);
  63             }
  64         }
  65 
  66         @Override public Object getBean() { return InputField.this; }
  67         @Override public String getName() { return "promptText"; }
  68     };
  69     public final StringProperty promptTextProperty() { return promptText; }
  70     public final String getPromptText() { return promptText.get(); }
  71     public final void setPromptText(String value) { promptText.set(value); }
  72 
  73 
  74     /**
  75      * The preferred number of text columns. This is used for
  76      * calculating the {@code InputField}'s preferred width.
  77      */
  78     private IntegerProperty prefColumnCount = new IntegerPropertyBase(DEFAULT_PREF_COLUMN_COUNT) {
  79         private int oldValue = get();
  80         
  81         @Override
  82         protected void invalidated() {
  83             int value = get();
  84             
  85             if (value < 0) {
  86                 if (isBound()) {
  87                     unbind();
  88                 }
  89                 set(oldValue);
  90                 throw new IllegalArgumentException("value cannot be negative.");
  91             }
  92             
  93             oldValue = value;
  94         }
  95 
  96         @Override public Object getBean() { return InputField.this; }
  97         @Override public String getName() { return "prefColumnCount"; }
  98     };
  99     public final IntegerProperty prefColumnCountProperty() { return prefColumnCount; }
 100     public final int getPrefColumnCount() { return prefColumnCount.getValue(); }
 101     public final void setPrefColumnCount(int value) { prefColumnCount.setValue(value); }
 102 
 103     /**
 104      * The action handler associated with this InputField, or
 105      * <tt>null</tt> if no action handler is assigned.
 106      *
 107      * The action handler is normally called when the user types the ENTER key.
 108      */
 109     private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
 110         @Override protected void invalidated() {
 111             setEventHandler(ActionEvent.ACTION, get());
 112         }
 113 
 114         @Override public Object getBean() { return InputField.this; }
 115         @Override public String getName() { return "onAction"; }
 116     };
 117     public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; }
 118     public final EventHandler<ActionEvent> getOnAction() { return onActionProperty().get(); }
 119     public final void setOnAction(EventHandler<ActionEvent> value) { onActionProperty().set(value); }
 120 
 121     /**
 122      * Creates a new InputField. The style class is set to "money-field".
 123      */
 124     public InputField() {
 125         getStyleClass().setAll("input-field");
 126     }
 127 
 128     //    @Override protected String getUserAgentStylesheet() {
 129 //        return getClass().getResource("InputField.css").toExternalForm();
 130 //    }
 131 }