1 /*
   2  * Copyright (c) 2012, 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 
  26 package com.sun.javafx.scene.control.skin;
  27 
  28 import com.sun.javafx.event.EventDispatchChainImpl;
  29 import com.sun.javafx.scene.control.InputField;
  30 import javafx.beans.InvalidationListener;
  31 import javafx.event.EventDispatchChain;
  32 import javafx.scene.Node;
  33 import javafx.scene.control.Skin;
  34 import javafx.scene.control.TextField;
  35 
  36 /**
  37  */
  38 public abstract class InputFieldSkin implements Skin<InputField> {
  39     /**
  40      * The {@code Control} that is referencing this Skin. There is a
  41      * one-to-one relationship between a {@code Skin} and a {@code Control}.
  42      * When a {@code Skin} is set on a {@code Control}, this variable is
  43      * automatically updated.
  44      */
  45     protected InputField control;
  46 
  47     /**
  48      * This textField is used to represent the InputField.
  49      */
  50     private InnerTextField textField;
  51 
  52     private InvalidationListener InputFieldFocusListener;
  53     private InvalidationListener InputFieldStyleClassListener;
  54 
  55     /**
  56      * Create a new InputFieldSkin.
  57      * @param control The InputField
  58      */
  59     public InputFieldSkin(final InputField control) {
  60         this.control = control;
  61 
  62         // Create the TextField that we are going to use to represent this InputFieldSkin.
  63         // The textField restricts input so that only valid digits that contribute to the
  64         // Money can be input.
  65         textField = new InnerTextField() {
  66             @Override public void replaceText(int start, int end, String text) {
  67                 String t = textField.getText() == null ? "" : textField.getText();
  68                 t = t.substring(0, start) + text + t.substring(end);
  69                 if (accept(t)) {
  70                     super.replaceText(start, end, text);
  71                 }
  72 //                if (!text.matches("[a-z]")) {
  73 //                    super.replaceText(start, end, text);
  74 //                }
  75             }
  76 
  77             @Override public void replaceSelection(String text) {
  78                 String t = textField.getText() == null ? "" : textField.getText();
  79                 int start = Math.min(textField.getAnchor(), textField.getCaretPosition());
  80                 int end = Math.max(textField.getAnchor(), textField.getCaretPosition());
  81                 t = t.substring(0, start) + text + t.substring(end);
  82                 if (accept(t)) {
  83                     super.replaceSelection(text);
  84                 }
  85 //                
  86 //                if (!text.matches("[a-z]")) {
  87 //                    super.replaceSelection(text);
  88 //                }
  89             }
  90         };
  91 
  92         textField.setId("input-text-field");
  93         textField.setFocusTraversable(false);
  94         control.getStyleClass().addAll(textField.getStyleClass());
  95         textField.getStyleClass().setAll(control.getStyleClass());
  96         control.getStyleClass().addListener(InputFieldStyleClassListener = observable -> {
  97             textField.getStyleClass().setAll(control.getStyleClass());
  98         });
  99 
 100 //        // Align the text to the right
 101 //        textField.setAlignment(Pos.BASELINE_RIGHT);
 102         textField.promptTextProperty().bind(control.promptTextProperty());
 103 //        textField.editableProperty().bind(control.editableProperty());
 104         textField.prefColumnCountProperty().bind(control.prefColumnCountProperty());
 105 
 106         // Whenever the text of the textField changes, we may need to
 107         // update the value.
 108         textField.textProperty().addListener(observable -> {
 109             updateValue();
 110         });
 111 
 112         // Right now there is some funny business regarding focus in JavaFX. So
 113         // we will just make sure the TextField gets focus whenever somebody tries
 114         // to give it to the InputField. This isn't right, but we need to fix
 115         // this in JavaFX, I don't think I can hack around it
 116 //        textField.setFocusTraversable(false);
 117         control.focusedProperty().addListener(InputFieldFocusListener = observable -> {
 118             textField.handleFocus(control.isFocused());
 119         });
 120         // getting an exception with this...
 121 //        control.addEventFilter(InputEvent.ANY, new EventHandler<InputEvent>() {
 122 //            @Override public void handle(InputEvent t) {
 123 //                if (textField == null) return;
 124 //                textField.fireEvent(t);
 125 //            }
 126 //        });
 127 
 128 //        textField.setOnAction(new EventHandler<ActionEvent>() {
 129 //            @Override public void handle(ActionEvent actionEvent) {
 130 //                // Because TextFieldBehavior fires an action event on the parent of the TextField
 131 //                // (maybe a misfeature?) I don't need to do this. But I think this is
 132 //                // a bug, because having to add an empty event handler to get an
 133 //                // event on the control is odd to say the least!
 134 //                control.fireEvent(new ActionEvent(textField, textField));
 135 //            }
 136 //        });
 137 
 138         updateText();
 139     }
 140 
 141     @Override public InputField getSkinnable() {
 142         return control;
 143     }
 144 
 145     @Override public Node getNode() {
 146         return textField;
 147     }
 148     
 149     /**
 150      * Called by a Skinnable when the Skin is replaced on the Skinnable. This method
 151      * allows a Skin to implement any logic necessary to clean up itself after
 152      * the Skin is no longer needed. It may be used to release native resources.
 153      * The methods {@link #getSkinnable()} and {@link #getNode()}
 154      * should return null following a call to dispose. Calling dispose twice
 155      * has no effect.
 156      */
 157     @Override
 158     public void dispose() {
 159         control.getStyleClass().removeListener(InputFieldStyleClassListener);
 160         control.focusedProperty().removeListener(InputFieldFocusListener);
 161         textField = null;
 162     }
 163 
 164     protected abstract boolean accept(String text);
 165     protected abstract void updateText();
 166     protected abstract void updateValue();
 167 
 168     protected TextField getTextField() {
 169         return textField;
 170     }
 171 
 172     private class InnerTextField extends TextField {
 173         public void handleFocus(boolean b) {
 174             setFocused(b);
 175         }
 176 
 177         @Override public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) {
 178             EventDispatchChain chain = new EventDispatchChainImpl();
 179             chain.append(textField.getEventDispatcher());
 180             return chain;
 181         }
 182    }
 183 }