1 /* 2 * Copyright (c) 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 package javafx.scene.control.test.utils.ptables; 26 27 import javafx.beans.property.IntegerProperty; 28 import javafx.beans.property.SimpleIntegerProperty; 29 import javafx.beans.value.ChangeListener; 30 import javafx.beans.value.ObservableValue; 31 import javafx.scene.Node; 32 import javafx.scene.control.Label; 33 import javafx.scene.control.LabelBuilder; 34 import javafx.scene.control.TextField; 35 import javafx.scene.control.TextFieldBuilder; 36 import static javafx.scene.control.test.utils.ptables.StaticLogger.*; 37 import javafx.scene.control.test.utils.ptables.TextFieldEventsCounter.Count; 38 import javafx.scene.layout.HBox; 39 import javafx.scene.text.Text; 40 41 /** 42 * @author Alexander Kirov 43 */ 44 public class TextFieldEventsCounter extends HBox implements AbstractEventsCounter { 45 46 final private IntegerProperty counter = new SimpleIntegerProperty(0); 47 public static final String COUNTER_SUFFIX = "_COUNTER_TEXT_FIELD_ID"; 48 private Count rememberedCount; 49 private String counterName; 50 51 public TextFieldEventsCounter(final String counterName) { 52 if (counterName == null) { 53 throw new IllegalArgumentException("Counter name cannot be null."); 54 } 55 try { 56 this.counterName = counterName; 57 Label label = LabelBuilder.create().text(counterName + " : ").prefWidth((new Text(counterName + " : ")).getBoundsInParent().getWidth() + 30).build(); 58 final TextField tf = TextFieldBuilder.create().text("0").prefWidth(50).id(counterName.toUpperCase() + COUNTER_SUFFIX).build(); 59 counter.addListener(new ChangeListener<Number>() { 60 public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) { 61 log("Counter " + counterName + ": new value : <" + t1 + ">."); 62 tf.setText(t1.toString()); 63 } 64 }); 65 getChildren().addAll(label, tf); 66 } catch (Throwable ex) { 67 log(ex); 68 } 69 } 70 71 public int getCurrentValue() { 72 return counter.getValue(); 73 } 74 75 public void increment() { 76 increment(1); 77 } 78 79 public void increment(int increment) { 80 try { 81 counter.setValue(counter.getValue() + increment); 82 } catch (Throwable ex) { 83 log(ex); 84 } 85 } 86 87 public Node getVisualRepresentation() { 88 return this; 89 } 90 91 public void refresh() { 92 try { 93 counter.setValue(0); 94 } catch (Throwable ex) { 95 log(ex); 96 } 97 } 98 99 public void rememberCurrentState() { 100 this.rememberedCount = new Count(getCurrentValue()); 101 } 102 103 public void checkCurrentStateEquality() throws StateChangedException { 104 if (rememberedCount == null) { 105 throw new IllegalStateException("Before comparation, some state must be remembered"); 106 } 107 rememberedCount.compareOnEquality(new Count(getCurrentValue())); 108 } 109 110 public String getName() { 111 return counterName; 112 } 113 114 public class Count { 115 116 private final int count; 117 118 public Count(int count) { 119 this.count = count; 120 } 121 122 public void compareOnEquality(Count newCount) throws StateChangedException { 123 if (newCount.count != this.count) { 124 throw new StateChangedException("Counter comparation error : expect to find <" + count + ">, but has found : <" + newCount.count + ">."); 125 } 126 } 127 } 128 }