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