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/06/22 16:13:41 UTC
svn commit: r956903 - in /pivot/trunk: core/src/org/apache/pivot/util/
tests/src/org/apache/pivot/tests/ wtk/src/org/apache/pivot/wtk/
Author: gbrown
Date: Tue Jun 22 14:13:41 2010
New Revision: 956903
URL: http://svn.apache.org/viewvc?rev=956903&view=rev
Log:
Resolve PIVOT-537.
Added:
pivot/trunk/core/src/org/apache/pivot/util/MessageBus.java
pivot/trunk/core/src/org/apache/pivot/util/MessageBusListener.java
pivot/trunk/tests/src/org/apache/pivot/tests/MessageBusTest.java
- copied, changed from r955589, pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java
Removed:
pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java
pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContextMessageListener.java
Modified:
pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java
Added: pivot/trunk/core/src/org/apache/pivot/util/MessageBus.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/MessageBus.java?rev=956903&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/MessageBus.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/util/MessageBus.java Tue Jun 22 14:13:41 2010
@@ -0,0 +1,77 @@
+/*
+ * 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.util;
+
+import org.apache.pivot.collections.HashMap;
+
+public class MessageBus {
+ private static HashMap<Class<?>, ListenerList<MessageBusListener<?>>> messageTopics
+ = new HashMap<Class<?>, ListenerList<MessageBusListener<?>>>();
+
+ /**
+ * Subscribes a listener to a message topic.
+ *
+ * @param topic
+ * @param messageListener
+ */
+ public static <T> void subscribe(Class<? super T> topic, MessageBusListener<T> messageListener) {
+ ListenerList<MessageBusListener<?>> topicListeners = messageTopics.get(topic);
+
+ if (topicListeners == null) {
+ topicListeners = new ListenerList<MessageBusListener<?>>() {};
+ 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, MessageBusListener<T> messageListener) {
+ ListenerList<MessageBusListener<?>> 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<MessageBusListener<?>> topicListeners = messageTopics.get(topic);
+
+ if (topicListeners != null) {
+ for (MessageBusListener<?> listener : topicListeners) {
+ ((MessageBusListener<T>)listener).messageSent(message);
+ }
+ }
+ }
+}
Added: pivot/trunk/core/src/org/apache/pivot/util/MessageBusListener.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/MessageBusListener.java?rev=956903&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/MessageBusListener.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/util/MessageBusListener.java Tue Jun 22 14:13:41 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.util;
+
+/**
+ * Message bus listener interface.
+ */
+public interface MessageBusListener<T> {
+ /**
+ * Called when a message has been sent via {@link MessageBus#sendMessage(Object)}.
+ *
+ * @param message
+ */
+ public void messageSent(T message);
+}
Copied: pivot/trunk/tests/src/org/apache/pivot/tests/MessageBusTest.java (from r955589, pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java)
URL: http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/MessageBusTest.java?p2=pivot/trunk/tests/src/org/apache/pivot/tests/MessageBusTest.java&p1=pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java&r1=955589&r2=956903&rev=956903&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/ApplicationContextMessageTest.java (original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/MessageBusTest.java Tue Jun 22 14:13:41 2010
@@ -16,27 +16,27 @@
*/
package org.apache.pivot.tests;
-import org.apache.pivot.wtk.ApplicationContext;
-import org.apache.pivot.wtk.ApplicationContextMessageListener;
+import org.apache.pivot.util.MessageBus;
+import org.apache.pivot.util.MessageBusListener;
-public class ApplicationContextMessageTest {
+public class MessageBusTest {
public enum TestMessage {
HELLO,
GOODBYE
}
public static void main(String[] args) {
- ApplicationContextMessageListener<TestMessage> testMessageListener =
- new ApplicationContextMessageListener<TestMessage>() {
+ MessageBusListener<TestMessage> testMessageListener =
+ new MessageBusListener<TestMessage>() {
public void messageSent(TestMessage message) {
System.out.println(message);
}
};
- ApplicationContext.subscribe(TestMessage.class, testMessageListener);
- ApplicationContext.sendMessage(TestMessage.HELLO);
+ MessageBus.subscribe(TestMessage.class, testMessageListener);
+ MessageBus.sendMessage(TestMessage.HELLO);
- ApplicationContext.unsubscribe(TestMessage.class, testMessageListener);
- ApplicationContext.sendMessage(TestMessage.GOODBYE);
+ MessageBus.unsubscribe(TestMessage.class, testMessageListener);
+ MessageBus.sendMessage(TestMessage.GOODBYE);
}
}
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=956903&r1=956902&r2=956903&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/ApplicationContext.java Tue Jun 22 14:13:41 2010
@@ -53,7 +53,6 @@ import java.util.TimerTask;
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;
@@ -1479,9 +1478,6 @@ public abstract class ApplicationContext
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>();
@@ -1659,73 +1655,6 @@ public abstract class ApplicationContext
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();
}