You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2010/01/28 15:46:46 UTC

svn commit: r904106 - in /pivot/trunk: core/src/org/apache/pivot/util/ tests/src/org/apache/pivot/tests/ tutorials/src/org/apache/pivot/tutorials/stocktracker/ wtk/src/org/apache/pivot/wtk/

Author: gbrown
Date: Thu Jan 28 14:46:45 2010
New Revision: 904106

URL: http://svn.apache.org/viewvc?rev=904106&view=rev
Log:
Add support for decoupled, intra-application messaging.

Added:
    pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java
Modified:
    pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
    pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/stocktracker.wtkx
    pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java

Modified: pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java?rev=904106&r1=904105&r2=904106&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/ListenerList.java Thu Jan 28 14:46:45 2010
@@ -140,6 +140,10 @@
         }
     }
 
+    public boolean isEmpty() {
+        return (first == null);
+    }
+
     @Override
     public Iterator<T> iterator() {
         return new NodeIterator();

Added: pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java?rev=904106&view=auto
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java (added)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java Thu Jan 28 14:46:45 2010
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.tests;
+
+import org.apache.pivot.wtk.ApplicationContext;
+import org.apache.pivot.wtk.ApplicationContextMessageListener;
+
+public class ApplicationContextMessageTest {
+    public enum TestMessage {
+        HELLO,
+        GOODBYE
+    }
+
+    public static void main(String[] args) {
+        ApplicationContextMessageListener<TestMessage> testMessageListener =
+            new ApplicationContextMessageListener<TestMessage>() {
+                public void messageSent(TestMessage message) {
+                    System.out.println(message);
+                }
+            };
+
+        ApplicationContext.subscribe(TestMessage.class, testMessageListener);
+        ApplicationContext.sendMessage(TestMessage.HELLO);
+
+        ApplicationContext.unsubscribe(TestMessage.class, testMessageListener);
+        ApplicationContext.sendMessage(TestMessage.GOODBYE);
+    }
+}

Modified: pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/stocktracker.wtkx
URL: http://svn.apache.org/viewvc/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/stocktracker.wtkx?rev=904106&r1=904105&r2=904106&view=diff
==============================================================================
--- pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/stocktracker.wtkx (original)
+++ pivot/trunk/tutorials/src/org/apache/pivot/tutorials/stocktracker/stocktracker.wtkx Thu Jan 28 14:46:45 2010
@@ -72,7 +72,7 @@
                         <right>
                             <Border styles="{padding:6, color:10}">
                                 <content>
-                                    <wtkx:include wtkx:id="detail" src="stocktracker.detail.wtkx" />
+                                    <wtkx:include wtkx:id="detail" src="stocktracker.detail.wtkx"/>
                                 </content>
                             </Border>
                         </right>

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java?rev=904106&r1=904105&r2=904106&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java Thu Jan 28 14:46:45 2010
@@ -53,6 +53,7 @@
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.util.ListenerList;
 import org.apache.pivot.util.Version;
 import org.apache.pivot.wtk.Component.DecoratorSequence;
 import org.apache.pivot.wtk.effects.Decorator;
@@ -1484,6 +1485,9 @@
     private static HashMap<URI, Object> resourceCache = new HashMap<URI, Object>();
     private static ResourceCacheDictionary resourceCacheDictionary = new ResourceCacheDictionary();
 
+    private static HashMap<Class<?>, ListenerList<ApplicationContextMessageListener<?>>> messageTopics
+        = new HashMap<Class<?>, ListenerList<ApplicationContextMessageListener<?>>>();
+
     private static Timer timer = null;
     private static ArrayList<Display> displays = new ArrayList<Display>();
     private static ArrayList<Application> applications = new ArrayList<Application>();
@@ -1661,6 +1665,73 @@
         return queuedCallback;
     }
 
+    /**
+     * Subscribes a listener to a message topic.
+     *
+     * @param topic
+     * @param messageListener
+     */
+    public static <T> void subscribe(Class<? super T> topic, ApplicationContextMessageListener<T> messageListener) {
+        ListenerList<ApplicationContextMessageListener<?>> topicListeners = messageTopics.get(topic);
+
+        if (topicListeners == null) {
+            topicListeners = new ListenerList<ApplicationContextMessageListener<?>>() {};
+            messageTopics.put(topic, topicListeners);
+        }
+
+        topicListeners.add(messageListener);
+    }
+
+    /**
+     * Unsubscribes a listener from a message topic.
+     *
+     * @param topic
+     * @param messageListener
+     */
+    public static <T> void unsubscribe(Class<? super T> topic, ApplicationContextMessageListener<T> messageListener) {
+        ListenerList<ApplicationContextMessageListener<?>> topicListeners = messageTopics.get(topic);
+
+        if (topicListeners == null) {
+            throw new IllegalArgumentException(topic.getName() + " does not exist.");
+        }
+
+        topicListeners.remove(messageListener);
+        if (topicListeners.isEmpty()) {
+            messageTopics.remove(topic);
+        }
+    }
+
+    /**
+     * Sends a message to subscribed topic listeners.
+     *
+     * @param message
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> void sendMessage(T message) {
+        Class<?> topic = message.getClass();
+        ListenerList<ApplicationContextMessageListener<?>> topicListeners = messageTopics.get(topic);
+
+        if (topicListeners != null) {
+            for (ApplicationContextMessageListener<?> listener : topicListeners) {
+                ((ApplicationContextMessageListener<T>)listener).messageSent(message);
+            }
+        }
+    }
+
+    /**
+     * Queues a callback to send a message via {@link #sendMessage(Object)}.
+     *
+     * @param message
+     */
+    public static <T> void queueMessage(final T message) {
+        queueCallback(new Runnable() {
+            @Override
+            public void run() {
+                sendMessage(message);
+            }
+        });
+    }
+
     protected static void createTimer() {
         timer = new Timer();
     }

Added: pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java?rev=904106&view=auto
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java (added)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java Thu Jan 28 14:46:45 2010
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.wtk;
+
+/**
+ * Application context message listener interface.
+ */
+public interface ApplicationContextMessageListener<T> {
+    /**
+     * Called when a message has been sent via {@link ApplicationContext#sendMessage(Object)}.
+     *
+     * @param message
+     */
+    public void messageSent(T message);
+}