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 IntegerFieldSkin extends InputFieldSkin {
  35     private InvalidationListener integerFieldValueListener;
  36 
  37     /**
  38      * Create a new IntegerFieldSkin.
  39      * @param control The IntegerField
  40      */
  41     public IntegerFieldSkin(final IntegerField 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(integerFieldValueListener = observable -> {
  48             updateText();
  49         });
  50     }
  51 
  52     @Override public IntegerField getSkinnable() {
  53         return (IntegerField) 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         ((IntegerField) control).valueProperty().removeListener(integerFieldValueListener);
  71         super.dispose();
  72     }
  73 
  74     @Override protected boolean accept(String text) {
  75         if (text.length() == 0) return true;
  76         if (text.matches("[0-9]*")) {
  77             try {
  78                 Integer.parseInt(text);
  79                 int value = Integer.parseInt(text);
  80                 int maxValue = ((IntegerField) control).getMaxValue();
  81                 return (maxValue != -1) ? (value <= maxValue )  : true;
  82             } catch (NumberFormatException ex) { }
  83         }
  84         return false;
  85     }
  86 
  87     @Override protected void updateText() {
  88         getTextField().setText("" + ((IntegerField) control).getValue());
  89     }
  90 
  91     @Override protected void updateValue() {
  92         int value = ((IntegerField) control).getValue();
  93         int newValue;
  94         String text = getTextField().getText() == null ? "" : getTextField().getText().trim();
  95         try {
  96             newValue = Integer.parseInt(text);
  97             if (newValue != value) {
  98                 ((IntegerField) control).setValue(newValue);
  99             }
 100         } catch (NumberFormatException ex) {
 101             // Empty string most likely
 102             ((IntegerField) control).setValue(0);
 103             Platform.runLater(() -> {
 104                 getTextField().positionCaret(1);
 105             });
 106         }
 107     }
 108 }