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 javafx.application.Platform; 29 import javafx.beans.InvalidationListener; 30 import javafx.scene.Node; 31 32 /** 33 */ 34 public class DoubleFieldSkin extends InputFieldSkin { 35 private InvalidationListener doubleFieldValueListener; 36 37 /** 38 * Create a new DoubleFieldSkin. 39 * @param control The DoubleField 40 */ 41 public DoubleFieldSkin(final DoubleField control) { 42 super(control); 43 44 // Whenever the value changes on the control, we need to update the text 45 // in the TextField. The only time this is not the case is when the update 46 // to the control happened as a result of an update in the text textField. 47 control.valueProperty().addListener(doubleFieldValueListener = observable -> { 48 updateText(); 49 }); 50 } 51 52 @Override public DoubleField getSkinnable() { 53 return (DoubleField) control; 54 } 55 56 @Override public Node getNode() { 57 return getTextField(); 58 } 59 60 /** 61 * Called by a Skinnable when the Skin is replaced on the Skinnable. This method 62 * allows a Skin to implement any logic necessary to clean up itself after 63 * the Skin is no longer needed. It may be used to release native resources. 64 * The methods {@link #getSkinnable()} and {@link #getNode()} 65 * should return null following a call to dispose. Calling dispose twice 66 * has no effect. 67 */ 68 @Override 69 public void dispose() { 70 ((DoubleField) control).valueProperty().removeListener(doubleFieldValueListener); 71 super.dispose(); 72 } 73 74 protected boolean accept(String text) { 75 if (text.length() == 0) return true; 76 if (text.matches("[0-9\\.]*")) { 77 try { 78 Double.parseDouble(text); 79 return true; 80 } catch (NumberFormatException ex) { } 81 } 82 return false; 83 } 84 85 protected void updateText() { 86 getTextField().setText("" + ((DoubleField) control).getValue()); 87 } 88 89 protected void updateValue() { 90 double value = ((DoubleField) control).getValue(); 91 double newValue; 92 String text = getTextField().getText() == null ? "" : getTextField().getText().trim(); 93 try { 94 newValue = Double.parseDouble(text); 95 if (newValue != value) { 96 ((DoubleField) control).setValue(newValue); 97 } 98 } catch (NumberFormatException ex) { 99 // Empty string most likely 100 ((DoubleField) control).setValue(0); 101 Platform.runLater(() -> { 102 getTextField().positionCaret(1); 103 }); 104 } 105 } 106 }