modules/controls/src/main/java/com/sun/javafx/scene/control/skin/WebColorFieldSkin.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization
   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 java.util.Locale;
  29 

  30 import javafx.beans.InvalidationListener;
  31 import javafx.geometry.NodeOrientation;
  32 import javafx.scene.Node;

  33 import javafx.scene.paint.Color;
  34 
  35 /**
  36  */
  37 class WebColorFieldSkin extends InputFieldSkin {
  38     private InvalidationListener integerFieldValueListener;
  39     private boolean noChangeInValue = false;
  40 
  41     /**
  42      * Create a new WebColorFieldSkin.
  43      * @param control The WebColorField
  44      */
  45     public WebColorFieldSkin(final WebColorField control) {
  46         super(control);
  47 
  48         // Whenever the value changes on the control, we need to update the text
  49         // in the TextField. The only time this is not the case is when the update
  50         // to the control happened as a result of an update in the text textField.
  51         control.valueProperty().addListener(integerFieldValueListener = observable -> {
  52             updateText();
  53         });
  54 
  55         // RT-37494: Force the major text direction to LTR, so that '#' is always
  56         // on the left side of the text. A special style is used in CSS to keep
  57         // the text right-aligned when in RTL mode.


  74      * should return null following a call to dispose. Calling dispose twice
  75      * has no effect.
  76      */
  77     @Override public void dispose() {
  78         ((WebColorField) control).valueProperty().removeListener(integerFieldValueListener);
  79         super.dispose();
  80     }
  81 
  82     //  "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
  83     protected boolean accept(String text) {
  84         if (text.length() == 0) return true;
  85         if (text.matches("#[a-fA-F0-9]{0,6}") || text.matches("[a-fA-F0-9]{0,6}")) {
  86             return true;
  87         }
  88         return false;
  89     }
  90 
  91     protected void updateText() {
  92         Color color = ((WebColorField) control).getValue();
  93         if (color == null) color = Color.BLACK;
  94         getTextField().setText(ColorPickerSkin.formatHexString(color));
  95     }
  96 
  97     protected void updateValue() {
  98         if (noChangeInValue) return;
  99         Color value = ((WebColorField) control).getValue();
 100         String text = getTextField().getText() == null ? "" : getTextField().getText().trim().toUpperCase(Locale.ROOT);
 101         if (text.matches("#[A-F0-9]{6}") || text.matches("[A-F0-9]{6}")) {
 102             try {
 103                 Color newValue = (text.charAt(0) == '#')? Color.web(text) : Color.web("#"+text);
 104                 if (!newValue.equals(value)) {
 105                     ((WebColorField) control).setValue(newValue);
 106                 } else {
 107                     // calling setText results in updateValue - so we set this flag to true
 108                     // so that when this is true updateValue simply returns.
 109                     noChangeInValue = true; 
 110                     getTextField().setText(ColorPickerSkin.formatHexString(newValue));
 111                     noChangeInValue = false;
 112                 }
 113             } catch (java.lang.IllegalArgumentException ex) {
 114                 System.out.println("Failed to parse ["+text+"]");
 115             }
 116         } 
 117     }
 118 }
   1 /*
   2  * Copyright (c) 2012, 2015, 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 java.util.Locale;
  29 
  30 import com.sun.javafx.scene.control.WebColorField;
  31 import javafx.beans.InvalidationListener;
  32 import javafx.geometry.NodeOrientation;
  33 import javafx.scene.Node;
  34 import javafx.scene.control.skin.ColorPickerSkin;
  35 import javafx.scene.paint.Color;
  36 
  37 /**
  38  */
  39 public class WebColorFieldSkin extends InputFieldSkin {
  40     private InvalidationListener integerFieldValueListener;
  41     private boolean noChangeInValue = false;
  42 
  43     /**
  44      * Create a new WebColorFieldSkin.
  45      * @param control The WebColorField
  46      */
  47     public WebColorFieldSkin(final WebColorField control) {
  48         super(control);
  49 
  50         // Whenever the value changes on the control, we need to update the text
  51         // in the TextField. The only time this is not the case is when the update
  52         // to the control happened as a result of an update in the text textField.
  53         control.valueProperty().addListener(integerFieldValueListener = observable -> {
  54             updateText();
  55         });
  56 
  57         // RT-37494: Force the major text direction to LTR, so that '#' is always
  58         // on the left side of the text. A special style is used in CSS to keep
  59         // the text right-aligned when in RTL mode.


  76      * should return null following a call to dispose. Calling dispose twice
  77      * has no effect.
  78      */
  79     @Override public void dispose() {
  80         ((WebColorField) control).valueProperty().removeListener(integerFieldValueListener);
  81         super.dispose();
  82     }
  83 
  84     //  "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
  85     protected boolean accept(String text) {
  86         if (text.length() == 0) return true;
  87         if (text.matches("#[a-fA-F0-9]{0,6}") || text.matches("[a-fA-F0-9]{0,6}")) {
  88             return true;
  89         }
  90         return false;
  91     }
  92 
  93     protected void updateText() {
  94         Color color = ((WebColorField) control).getValue();
  95         if (color == null) color = Color.BLACK;
  96         getTextField().setText(Utils.formatHexString(color));
  97     }
  98 
  99     protected void updateValue() {
 100         if (noChangeInValue) return;
 101         Color value = ((WebColorField) control).getValue();
 102         String text = getTextField().getText() == null ? "" : getTextField().getText().trim().toUpperCase(Locale.ROOT);
 103         if (text.matches("#[A-F0-9]{6}") || text.matches("[A-F0-9]{6}")) {
 104             try {
 105                 Color newValue = (text.charAt(0) == '#')? Color.web(text) : Color.web("#"+text);
 106                 if (!newValue.equals(value)) {
 107                     ((WebColorField) control).setValue(newValue);
 108                 } else {
 109                     // calling setText results in updateValue - so we set this flag to true
 110                     // so that when this is true updateValue simply returns.
 111                     noChangeInValue = true; 
 112                     getTextField().setText(Utils.formatHexString(newValue));
 113                     noChangeInValue = false;
 114                 }
 115             } catch (java.lang.IllegalArgumentException ex) {
 116                 System.out.println("Failed to parse ["+text+"]");
 117             }
 118         } 
 119     }
 120 }