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