functional/ControlsTests/src/javafx/scene/control/test/utils/ptables/TextFieldEventsCounter.java

Print this page




  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 




  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