       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.skin;
      27
      28 import javafx.beans.InvalidationListener;
      29 import javafx.beans.Observable;
      30 import javafx.scene.Node;
      31 import javafx.scene.paint.Color;
      32
      33 /**
      34  */
      35 class WebColorFieldSkin extends InputFieldSkin {
      36     private InvalidationListener integerFieldValueListener;
      37     private boolean noChangeInValue = false;
      38
      39     /**
      40      * Create a new IntegerFieldSkin.
      41      * @param control The IntegerField
      42      */
      43     public WebColorFieldSkin(final WebColorField control) {
      44         super(control);
      45
      46         // Whenever the value changes on the control, we need to update the text
      47         // in the TextField. The only time this is not the case is when the update
      48         // to the control happened as a result of an update in the text textField.
      49         control.valueProperty().addListener(integerFieldValueListener = new InvalidationListener() {
      50             @Override public void invalidated(Observable observable) {
      51                 updateText();
      52             }
      53         });
      54     }
      55
      56     @Override public WebColorField getSkinnable() {
      57         return (WebColorField) control;
      58     }
      59
      60     @Override public Node getNode() {
      61         return getTextField();
      62     }
      63
      64     /**
      65      * Called by a Skinnable when the Skin is replaced on the Skinnable. This method
      66      * allows a Skin to implement any logic necessary to clean up itself after
      67      * the Skin is no longer needed. It may be used to release native resources.
      68      * The methods {@link #getSkinnable()} and {@link #getNode()}
      69      * should return null following a call to dispose. Calling dispose twice
      70      * has no effect.
      71      */
      72     @Override public void dispose() {
      73         ((WebColorField) control).valueProperty().removeListener(integerFieldValueListener);
      74         super.dispose();
      75     }
      76
      77     //  "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$";
      78     protected boolean accept(String text) {
      79         if (text.length() == 0) return true;
      80         if (text.matches("#[a-fA-F0-9]{0,6}") || text.matches("[a-fA-F0-9]{0,6}")) {
      81             return true;
      82         }
      83         return false;
      84     }
      85
      86     protected void updateText() {
      87         Color color = ((WebColorField) control).getValue();
      88         if (color == null) color = Color.BLACK;
      89         getTextField().setText(getWebColor(color));
      90     }
      91
      92     protected void updateValue() {
      93         if (noChangeInValue) return;
      94         Color value = ((WebColorField) control).getValue();
      95         String text = getTextField().getText() == null ? "" : getTextField().getText().trim().toUpperCase();
      96         if (text.matches("#[A-F0-9]{6}") || text.matches("[A-F0-9]{6}")) {
      97             try {
      98                 Color newValue = (text.charAt(0) == '#')? Color.web(text) : Color.web("#"+text);
      99                 if (!newValue.equals(value)) {
     100                     ((WebColorField) control).setValue(newValue);
     101                 } else {
     102                     // calling setText results in updateValue - so we set this flag to true
     103                     // so that when this is true updateValue simply returns.
     104                     noChangeInValue = true;
     105                     getTextField().setText(getWebColor(newValue));
     106                     noChangeInValue = false;
     107                 }
     108             } catch (java.lang.IllegalArgumentException ex) {
     109                 System.out.println("Failed to parse ["+text+"]");
     110             }
     111         }
     112     }
     113
     114
     115     private static String getWebColor(Color color) {
     116         final int red = (int)(color.getRed()*255);
     117         final int green = (int)(color.getGreen()*255);
     118         final int blue = (int)(color.getBlue()*255);
     119         return "#" + String.format("%02X", red) +
     120                           String.format("%02X", green) +
     121                           String.format("%02X", blue);
     122     }
     123 }