changeset 7653:7ec4725320d0

RT-25980 Add Window setUserData and user data to other non-Node classes Reviewed by: kcr, snorthov Contributed by: tom.schindl@bestsolution.at
author Martin Sladecek <martin.sladecek@oracle.com>
date Tue, 12 Aug 2014 09:55:18 +0200
parents 44b6e89d00ba
children 42903ea10084
files modules/controls/src/main/java/javafx/scene/control/ToggleGroup.java modules/controls/src/test/java/javafx/scene/control/ToggleGroupTest.java modules/graphics/src/main/java/javafx/scene/Scene.java modules/graphics/src/main/java/javafx/stage/Window.java modules/graphics/src/test/java/javafx/scene/SceneTest.java modules/graphics/src/test/java/javafx/stage/WindowTest.java
diffstat 6 files changed, 258 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/controls/src/main/java/javafx/scene/control/ToggleGroup.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/controls/src/main/java/javafx/scene/control/ToggleGroup.java	Tue Aug 12 09:55:18 2014 +0200
@@ -25,11 +25,16 @@
 
 package javafx.scene.control;
 
+import java.util.HashMap;
 import java.util.List;
+
 import javafx.beans.property.ReadOnlyObjectProperty;
 import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.collections.ListChangeListener.Change;
+import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
+import javafx.collections.ObservableMap;
+
 import com.sun.javafx.collections.VetoableListDecorator;
 import com.sun.javafx.collections.TrackableObservableList;
 
@@ -175,4 +180,60 @@
         }
         selectedToggle.set(null);
     }
+    
+    /*************************************************************************
+    *                                                                        *
+    *                                                                        *
+    *                                                                        *
+    *************************************************************************/
+
+    private static final Object USER_DATA_KEY = new Object();
+    // A map containing a set of properties for this scene
+    private ObservableMap<Object, Object> properties;
+
+    /**
+      * Returns an observable map of properties on this node for use primarily
+      * by application developers.
+      *
+      * @return an observable map of properties on this node for use primarily
+      * by application developers
+      */
+     public final ObservableMap<Object, Object> getProperties() {
+        if (properties == null) {
+            properties = FXCollections.observableMap(new HashMap<Object, Object>());
+        }
+        return properties;
+    }
+
+    /**
+     * Tests if ToggleGroup has properties.
+     * @return true if node has properties.
+     */
+     public boolean hasProperties() {
+        return properties != null && !properties.isEmpty();
+    }
+
+    /**
+     * Convenience method for setting a single Object property that can be
+     * retrieved at a later date. This is functionally equivalent to calling
+     * the getProperties().put(Object key, Object value) method. This can later
+     * be retrieved by calling {@link ToggleGroup#getUserData()}.
+     *
+     * @param value The value to be stored - this can later be retrieved by calling
+     *          {@link ToggleGroup#getUserData()}.
+     */
+    public void setUserData(Object value) {
+        getProperties().put(USER_DATA_KEY, value);
+    }
+
+    /**
+     * Returns a previously set Object property, or null if no such property
+     * has been set using the {@link ToggleGroup#setUserData(java.lang.Object)} method.
+     *
+     * @return The Object that was previously set, or null if no property
+     *          has been set or if null was set.
+     */
+    public Object getUserData() {
+        return getProperties().get(USER_DATA_KEY);
+    }
 }
--- a/modules/controls/src/test/java/javafx/scene/control/ToggleGroupTest.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/controls/src/test/java/javafx/scene/control/ToggleGroupTest.java	Tue Aug 12 09:55:18 2014 +0200
@@ -27,15 +27,19 @@
 
 import com.sun.javafx.event.EventUtil;
 import com.sun.javafx.pgstub.StubScene;
+
 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.SimpleBooleanProperty;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.input.KeyCode;
 import javafx.scene.input.KeyEvent;
+
 import org.junit.Before;
 import org.junit.Test;
 
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
 import static org.junit.Assert.*;
 
 public class ToggleGroupTest {
@@ -520,4 +524,27 @@
         assertFalse(b2.isSelected());
         assertEquals(g1.getSelectedToggle(), b1);
     }
+    
+    @Test public void testProperties() {
+        javafx.collections.ObservableMap<Object, Object> properties = g1.getProperties();
+
+        /* If we ask for it, we should get it.
+         */
+        assertNotNull(properties);
+
+        /* What we put in, we should get out.
+         */
+        properties.put("MyKey", "MyValue");
+        assertEquals("MyValue", properties.get("MyKey"));
+
+        /* If we ask for it again, we should get the same thing.
+         */
+        javafx.collections.ObservableMap<Object, Object> properties2 = g1.getProperties();
+        assertEquals(properties2, properties);
+
+        /* What we put in to the other one, we should get out of this one because
+         * they should be the same thing.
+         */
+        assertEquals("MyValue", properties2.get("MyKey"));
+    }
 }
--- a/modules/graphics/src/main/java/javafx/scene/Scene.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/graphics/src/main/java/javafx/scene/Scene.java	Tue Aug 12 09:55:18 2014 +0200
@@ -55,6 +55,7 @@
 import com.sun.javafx.sg.prism.NGLightBase;
 import com.sun.javafx.tk.*;
 import com.sun.prism.impl.PrismSettings;
+
 import javafx.animation.KeyFrame;
 import javafx.animation.Timeline;
 import javafx.application.ConditionalFeature;
@@ -90,6 +91,7 @@
 import java.util.*;
 
 import com.sun.javafx.logging.PulseLogger;
+
 import static com.sun.javafx.logging.PulseLogger.PULSE_LOGGING_ENABLED;
 
 /**
@@ -6008,6 +6010,62 @@
             scene = s;
         }
     }
+    
+    /*************************************************************************
+    *                                                                        *
+    *                                                                        *
+    *                                                                        *
+    *************************************************************************/
+
+    private static final Object USER_DATA_KEY = new Object();
+    // A map containing a set of properties for this scene
+    private ObservableMap<Object, Object> properties;
+
+    /**
+      * Returns an observable map of properties on this node for use primarily
+      * by application developers.
+      *
+      * @return an observable map of properties on this node for use primarily
+      * by application developers
+      */
+     public final ObservableMap<Object, Object> getProperties() {
+        if (properties == null) {
+            properties = FXCollections.observableMap(new HashMap<Object, Object>());
+        }
+        return properties;
+    }
+
+    /**
+     * Tests if Scene has properties.
+     * @return true if node has properties.
+     */
+     public boolean hasProperties() {
+        return properties != null && !properties.isEmpty();
+    }
+
+    /**
+     * Convenience method for setting a single Object property that can be
+     * retrieved at a later date. This is functionally equivalent to calling
+     * the getProperties().put(Object key, Object value) method. This can later
+     * be retrieved by calling {@link Scene#getUserData()}.
+     *
+     * @param value The value to be stored - this can later be retrieved by calling
+     *          {@link Scene#getUserData()}.
+     */
+    public void setUserData(Object value) {
+        getProperties().put(USER_DATA_KEY, value);
+    }
+
+    /**
+     * Returns a previously set Object property, or null if no such property
+     * has been set using the {@link Scene#setUserData(java.lang.Object)} method.
+     *
+     * @return The Object that was previously set, or null if no property
+     *          has been set or if null was set.
+     */
+    public Object getUserData() {
+        return getProperties().get(USER_DATA_KEY);
+    }
 
     /***************************************************************************
      *                                                                         *
--- a/modules/graphics/src/main/java/javafx/stage/Window.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/graphics/src/main/java/javafx/stage/Window.java	Tue Aug 12 09:55:18 2014 +0200
@@ -28,6 +28,7 @@
 import java.security.AllPermission;
 import java.security.AccessControlContext;
 import java.security.AccessController;
+import java.util.HashMap;
 import java.util.Iterator;
 
 import javafx.beans.property.DoubleProperty;
@@ -41,6 +42,8 @@
 import javafx.beans.property.ReadOnlyDoubleProperty;
 import javafx.beans.property.ReadOnlyDoubleWrapper;
 import javafx.beans.property.SimpleObjectProperty;
+import javafx.collections.FXCollections;
+import javafx.collections.ObservableMap;
 import javafx.event.Event;
 import javafx.event.EventDispatchChain;
 import javafx.event.EventDispatcher;
@@ -434,6 +437,62 @@
     public final boolean isFocused() { return focused.get(); }
     public final ReadOnlyBooleanProperty focusedProperty() { return focused.getReadOnlyProperty(); }
 
+    /*************************************************************************
+    *                                                                        *
+    *                                                                        *
+    *                                                                        *
+    *************************************************************************/
+
+    private static final Object USER_DATA_KEY = new Object();
+    // A map containing a set of properties for this window
+    private ObservableMap<Object, Object> properties;
+
+    /**
+      * Returns an observable map of properties on this node for use primarily
+      * by application developers.
+      *
+      * @return an observable map of properties on this node for use primarily
+      * by application developers
+      */
+     public final ObservableMap<Object, Object> getProperties() {
+        if (properties == null) {
+            properties = FXCollections.observableMap(new HashMap<Object, Object>());
+        }
+        return properties;
+    }
+
+    /**
+     * Tests if Window has properties.
+     * @return true if node has properties.
+     */
+     public boolean hasProperties() {
+        return properties != null && !properties.isEmpty();
+    }
+
+    /**
+     * Convenience method for setting a single Object property that can be
+     * retrieved at a later date. This is functionally equivalent to calling
+     * the getProperties().put(Object key, Object value) method. This can later
+     * be retrieved by calling {@link Window#getUserData()}.
+     *
+     * @param value The value to be stored - this can later be retrieved by calling
+     *          {@link Window#getUserData()}.
+     */
+    public void setUserData(Object value) {
+        getProperties().put(USER_DATA_KEY, value);
+    }
+
+    /**
+     * Returns a previously set Object property, or null if no such property
+     * has been set using the {@link Window#setUserData(java.lang.Object)} method.
+     *
+     * @return The Object that was previously set, or null if no property
+     *          has been set or if null was set.
+     */
+    public Object getUserData() {
+        return getProperties().get(USER_DATA_KEY);
+    }
+    
     /**
      * The {@code Scene} to be rendered on this {@code Stage}. There can only
      * be one {@code Scene} on the {@code Stage} at a time, and a {@code Scene}
--- a/modules/graphics/src/test/java/javafx/scene/SceneTest.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/graphics/src/test/java/javafx/scene/SceneTest.java	Tue Aug 12 09:55:18 2014 +0200
@@ -35,21 +35,26 @@
 import javafx.scene.transform.Scale;
 import javafx.stage.PopupWindow;
 import javafx.stage.Stage;
+
 import com.sun.javafx.pgstub.StubScene;
 import com.sun.javafx.pgstub.StubToolkit;
 import com.sun.javafx.sg.prism.NGCamera;
 import com.sun.javafx.test.MouseEventGenerator;
 import com.sun.javafx.tk.Toolkit;
+
 import javafx.application.Platform;
 import javafx.beans.InvalidationListener;
 import javafx.beans.Observable;
 import javafx.scene.input.MouseEvent;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.util.concurrent.ExecutionException;
 
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
 import static org.junit.Assert.*;
 
 /**
@@ -816,4 +821,28 @@
                    ((StubScene) scene.impl_getPeer()).getCursor());
     }
 
+    @Test public void testProperties() {
+        final Scene scene = new Scene(new Group(), 300, 200);
+        
+        javafx.collections.ObservableMap<Object, Object> properties = scene.getProperties();
+
+        /* If we ask for it, we should get it.
+         */
+        assertNotNull(properties);
+
+        /* What we put in, we should get out.
+         */
+        properties.put("MyKey", "MyValue");
+        assertEquals("MyValue", properties.get("MyKey"));
+
+        /* If we ask for it again, we should get the same thing.
+         */
+        javafx.collections.ObservableMap<Object, Object> properties2 = scene.getProperties();
+        assertEquals(properties2, properties);
+
+        /* What we put in to the other one, we should get out of this one because
+         * they should be the same thing.
+         */
+        assertEquals("MyValue", properties2.get("MyKey"));
+    }
 }
--- a/modules/graphics/src/test/java/javafx/stage/WindowTest.java	Mon Aug 11 14:23:41 2014 -0700
+++ b/modules/graphics/src/test/java/javafx/stage/WindowTest.java	Tue Aug 12 09:55:18 2014 +0200
@@ -27,6 +27,7 @@
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.assertNotNull;
 import javafx.beans.property.DoubleProperty;
 import javafx.beans.property.SimpleDoubleProperty;
 
@@ -65,6 +66,29 @@
 
         assertEquals(1.0f, peer.opacity);
     }
+    
+    @Test public void testProperties() {
+        javafx.collections.ObservableMap<Object, Object> properties = testWindow.getProperties();
+
+        /* If we ask for it, we should get it.
+         */
+        assertNotNull(properties);
+
+        /* What we put in, we should get out.
+         */
+        properties.put("MyKey", "MyValue");
+        assertEquals("MyValue", properties.get("MyKey"));
+
+        /* If we ask for it again, we should get the same thing.
+         */
+        javafx.collections.ObservableMap<Object, Object> properties2 = testWindow.getProperties();
+        assertEquals(properties2, properties);
+
+        /* What we put in to the other one, we should get out of this one because
+         * they should be the same thing.
+         */
+        assertEquals("MyValue", properties2.get("MyKey"));
+    }
 
     private static StubStage getPeer(final Window window) {
         final TKStage unkPeer = window.impl_getPeer();