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. 60 getTextField().setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT); 61 } 62 63 @Override public WebColorField getSkinnable() { 64 return (WebColorField) control; 65 } 66 67 @Override public Node getNode() { 68 return getTextField(); 69 } 70 71 /** 72 * Called by a Skinnable when the Skin is replaced on the Skinnable. This method 73 * allows a Skin to implement any logic necessary to clean up itself after 74 * the Skin is no longer needed. It may be used to release native resources. 75 * The methods {@link #getSkinnable()} and {@link #getNode()} 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 }