changeset 10912:299108465d55 jdk-11+11

8167096: Add APIs to customize step repeat timing for Spinner control Reviewed-by: kcr, aghaisas
author pkbalakr
date Mon, 23 Apr 2018 14:02:12 +0530
parents 40be25ced32b
children b3413dca3c70
files modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/behavior/SpinnerBehavior.java modules/javafx.controls/src/main/java/javafx/scene/control/Spinner.java modules/javafx.controls/src/test/java/test/javafx/scene/control/SpinnerTest.java modules/javafx.graphics/src/main/docs/javafx/scene/doc-files/cssref.html
diffstat 4 files changed, 172 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/behavior/SpinnerBehavior.java	Mon Apr 23 09:08:17 2018 +0200
+++ b/modules/javafx.controls/src/main/java/com/sun/javafx/scene/control/behavior/SpinnerBehavior.java	Mon Apr 23 14:02:12 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -128,10 +128,12 @@
         }
         timeline = new Timeline();
         timeline.setCycleCount(Timeline.INDEFINITE);
-        final KeyFrame kf = new KeyFrame(Duration.millis(INITIAL_DURATION_MS), spinningKeyFrameEventHandler);
-        timeline.getKeyFrames().setAll(kf);
+        timeline.setDelay(getNode().getInitialDelay());
+        final KeyFrame start = new KeyFrame(Duration.ZERO, spinningKeyFrameEventHandler);
+        final KeyFrame repeat = new KeyFrame(getNode().getRepeatDelay());
+        timeline.getKeyFrames().setAll(start, repeat);
         timeline.playFromStart();
-        timeline.play();
+
         spinningKeyFrameEventHandler.handle(null);
     }
 
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/Spinner.java	Mon Apr 23 09:08:17 2018 +0200
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/Spinner.java	Mon Apr 23 14:02:12 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,11 +34,13 @@
 import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.beans.property.SimpleBooleanProperty;
 import javafx.beans.property.SimpleObjectProperty;
+import javafx.beans.value.WritableValue;
 import javafx.collections.MapChangeListener;
 import javafx.collections.ObservableList;
 import javafx.scene.AccessibleAction;
 import javafx.scene.AccessibleAttribute;
 import javafx.scene.AccessibleRole;
+import javafx.util.Duration;
 import javafx.util.StringConverter;
 
 import java.math.BigDecimal;
@@ -46,6 +48,17 @@
 import java.time.LocalTime;
 import java.time.temporal.TemporalUnit;
 
+import javafx.css.CssMetaData;
+import javafx.css.converter.DurationConverter;
+import javafx.css.Styleable;
+import javafx.css.StyleableObjectProperty;
+import javafx.css.StyleableProperty;
+import javafx.css.SimpleStyleableObjectProperty;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 /**
  * A single line text field that lets the user select a number or an object
  * value from an ordered sequence. Spinners typically provide a pair of tiny
@@ -598,7 +611,122 @@
     public final String getPromptText() { return getEditor().getPromptText(); }
     public final void setPromptText(String value) { getEditor().setPromptText(value); }
 
+    private final ObjectProperty<Duration> initialDelay =
+                            new SimpleStyleableObjectProperty<>(INITIAL_DELAY,
+                            this, "initialDelay", new Duration(300));
 
+    /**
+     * The duration that the mouse has to be pressed on an arrow button
+     * before the next value steps. Successive step duration is set using
+     * {@link #repeatDelayProperty() repeat delay}.
+     *
+     * @return inital delay property
+     * @since 11
+     * @defaultValue 300ms
+     */
+    public final ObjectProperty<Duration> initialDelayProperty() {
+        return initialDelay;
+    }
+
+    public final void setInitialDelay(Duration value) {
+        if (value != null) {
+            initialDelay.set(value);
+        }
+    }
+
+    public final Duration getInitialDelay() {
+        return initialDelay.get();
+    }
+
+    private final ObjectProperty<Duration> repeatDelay =
+                            new SimpleStyleableObjectProperty<>(REPEAT_DELAY,
+                            this, "repeatDelay", new Duration(60));
+
+    /**
+     * The duration that the mouse has to be pressed for each successive step
+     * after the first value steps. Initial step duration is set using
+     * {@link #initialDelayProperty() initial delay}.
+     *
+     * @return repeat delay property
+     * @since 11
+     * @defaultValue 60ms
+     */
+    public final ObjectProperty<Duration> repeatDelayProperty() {
+        return repeatDelay;
+    }
+
+    public final void setRepeatDelay(Duration value) {
+        if (value != null) {
+            repeatDelay.set(value);
+        }
+    }
+
+    public final Duration getRepeatDelay() {
+        return repeatDelay.get();
+    }
+
+    /***************************************************************************
+     *                                                                         *
+     * Stylesheet Handling                                                     *
+     *                                                                         *
+     **************************************************************************/
+
+    private static final CssMetaData<Spinner<?>,Duration> INITIAL_DELAY =
+                                    new CssMetaData<Spinner<?>,Duration>("-fx-initial-delay",
+                                        DurationConverter.getInstance(), new Duration(300)) {
+
+        @Override
+        public boolean isSettable(Spinner<?> spinner) {
+            return !spinner.initialDelayProperty().isBound();
+        }
+
+        @Override
+        public StyleableProperty<Duration> getStyleableProperty(Spinner<?> spinner) {
+            return (StyleableProperty<Duration>)(WritableValue<Duration>)spinner.initialDelayProperty();
+        }
+    };
+
+    private static final CssMetaData<Spinner<?>,Duration> REPEAT_DELAY =
+                                   new CssMetaData<Spinner<?>,Duration>("-fx-repeat-delay",
+                                        DurationConverter.getInstance(), new Duration(60)) {
+
+        @Override
+        public boolean isSettable(Spinner<?> spinner) {
+            return !spinner.repeatDelayProperty().isBound();
+        }
+
+        @Override
+        public StyleableProperty<Duration> getStyleableProperty(Spinner<?> spinner) {
+            return (StyleableProperty<Duration>)(WritableValue<Duration>)spinner.repeatDelayProperty();
+        }
+    };
+
+    private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
+    static {
+        final List<CssMetaData<? extends Styleable, ?>> styleables =
+            new ArrayList<>(Control.getClassCssMetaData());
+        styleables.add(INITIAL_DELAY);
+        styleables.add(REPEAT_DELAY);
+        STYLEABLES = Collections.unmodifiableList(styleables);
+    }
+
+    /*
+     * @return The CssMetaData associated with this class, which may include the
+     * CssMetaData of its superclasses.
+     * @since 11
+     */
+    public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
+        return STYLEABLES;
+    }
+
+    /*
+     * {@inheritDoc}
+     * @since 11
+     */
+    @Override
+    public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
+        return getClassCssMetaData();
+    }
 
     /***************************************************************************
      *                                                                         *
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/SpinnerTest.java	Mon Apr 23 09:08:17 2018 +0200
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/SpinnerTest.java	Mon Apr 23 14:02:12 2018 +0530
@@ -42,9 +42,11 @@
 import javafx.scene.control.SpinnerValueFactory;
 import javafx.scene.control.SpinnerValueFactoryShim;
 import javafx.scene.input.KeyCode;
+import javafx.scene.layout.FlowPane;
 import javafx.scene.layout.VBox;
 import javafx.scene.Scene;
 import javafx.stage.Stage;
+import javafx.util.Duration;
 
 import test.com.sun.javafx.pgstub.StubToolkit;
 import test.com.sun.javafx.scene.control.infrastructure.KeyEventFirer;
@@ -1404,4 +1406,27 @@
         assertTrue(defMsg, enterDefaultPass);
         assertTrue(canMsg, escapeCancelPass);
     }
+
+    @Test public void spinnerDelayTest() {
+        Spinner<Double> spinner = new Spinner<>(-100, 100, 2.5, 0.5);
+
+        spinner.setInitialDelay(null);
+        assertEquals(300.0, spinner.getInitialDelay().toMillis(), 0.001);
+        spinner.setInitialDelay(new Duration(500));
+        assertEquals(500.0, spinner.getInitialDelay().toMillis(), 0.001);
+
+        spinner.setRepeatDelay(null);
+        assertEquals(60.0, spinner.getRepeatDelay().toMillis(), 0.001);
+        spinner.setRepeatDelay(new Duration(200));
+        assertEquals(200.0, spinner.getRepeatDelay().toMillis(), 0.001);
+
+        spinner.setStyle("-fx-initial-delay: 400ms; -fx-repeat-delay: 100ms");
+        FlowPane root = new FlowPane();
+        root.getChildren().addAll(spinner);
+        Scene scene = new Scene(root, 150, 150);
+        scene.getRoot().applyCss();
+
+        assertEquals(400.0, spinner.getInitialDelay().toMillis(), 0.001);
+        assertEquals(100.0, spinner.getRepeatDelay().toMillis(), 0.001);
+    }
 }
--- a/modules/javafx.graphics/src/main/docs/javafx/scene/doc-files/cssref.html	Mon Apr 23 09:08:17 2018 +0200
+++ b/modules/javafx.graphics/src/main/docs/javafx/scene/doc-files/cssref.html	Mon Apr 23 14:02:12 2018 +0530
@@ -4391,6 +4391,18 @@
       </thead>
       <tbody>
         <tr>
+        <th scope="row" class="propertyname">-fx-initial-delay</th>
+            <td class="value"><a href="#typeduration" class="typelink">&lt;duration&gt;</a></td>
+            <td>300ms</td>
+            <td>&nbsp;</td>
+        </tr>
+        <tr>
+        <th scope="row" class="propertyname" >-fx-repeat-delay</th>
+            <td class="value"><a href="#typeduration" class="typelink">&lt;duration&gt;</a></td>
+            <td>60ms</td>
+            <td>&nbsp;</td>
+        </tr>
+        <tr>
         <th colspan="4" class="parents" scope="row">Also has all properties of <a href="#control">Control</a></th>
         </tr>
       </tbody>