diff options
author | Artur Signell <artur@vaadin.com> | 2013-04-16 13:57:14 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-04-23 11:30:14 +0000 |
commit | 49ac6f4da031d2132035d69105fd3240949d9805 (patch) | |
tree | c660916fb5e848f8b007705353d14d00740ac09a /uitest | |
parent | c844d94b81b2645f2800e17557fe6e300e85de96 (diff) | |
download | vaadin-framework-49ac6f4da031d2132035d69105fd3240949d9805.tar.gz vaadin-framework-49ac6f4da031d2132035d69105fd3240949d9805.zip |
Sources for the "Broadcasting messages to other users" tutorial
Change-Id: I1408d2b4c33bb51393867b2a089035699a5af756
Diffstat (limited to 'uitest')
-rw-r--r-- | uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java | 63 | ||||
-rw-r--r-- | uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java | 58 |
2 files changed, 121 insertions, 0 deletions
diff --git a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java new file mode 100644 index 0000000000..e355cd1dbd --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/Broadcaster.java @@ -0,0 +1,63 @@ +package com.vaadin.tests.minitutorials.broadcastingmessages; +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed 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. + */ + + +import java.util.ArrayList; +import java.util.List; + +public class Broadcaster { + + private static List<BroadcastListener> listeners = new ArrayList<BroadcastListener>(); + + public synchronized static void register(BroadcastListener listener) { + listeners.add(listener); + } + + public synchronized static void unregister(BroadcastListener listener) { + listeners.remove(listener); + } + + private synchronized static List<BroadcastListener> getListeners() { + List<BroadcastListener> listenerCopy = new ArrayList<BroadcastListener>(); + listenerCopy.addAll(listeners); + return listenerCopy; + } + + public static void broadcast(final String message) { + // Make a copy of the listener list while synchronized, can't be + // synchronized while firing the event or we would have to fire each + // event in a separate thread. + final List<BroadcastListener> listenerCopy = getListeners(); + + // We spawn another thread to avoid potential deadlocks with + // multiple UIs locked simultaneously + Thread eventThread = new Thread() { + @Override + public void run() { + for (BroadcastListener listener : listenerCopy) { + listener.receiveBroadcast(message); + } + } + }; + eventThread.start(); + } + + public interface BroadcastListener { + public void receiveBroadcast(String message); + } + +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java new file mode 100644 index 0000000000..80c847250d --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/broadcastingmessages/BroadcasterUI.java @@ -0,0 +1,58 @@ +package com.vaadin.tests.minitutorials.broadcastingmessages; + + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.minitutorials.broadcastingmessages.Broadcaster.BroadcastListener; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Notification.Type; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; + +public class BroadcasterUI extends UI implements BroadcastListener { + + @Override + protected void init(VaadinRequest request) { + final VerticalLayout layout = new VerticalLayout(); + layout.setMargin(true); + setContent(layout); + + final TextArea message = new TextArea("", + "The system is going down for maintenance in 10 minutes"); + layout.addComponent(message); + + final Button button = new Button("Broadcast"); + layout.addComponent(button); + button.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + Broadcaster.broadcast(message.getValue()); + } + }); + + // Register broadcast listener + Broadcaster.register(this); + } + + @Override + public void detach() { + Broadcaster.unregister(this); + super.detach(); + } + + @Override + public void receiveBroadcast(final String message) { + runSafely(new Runnable() { + @Override + public void run() { + Notification n = new Notification("Message received", message, + Type.TRAY_NOTIFICATION); + n.show(getPage()); + } + }); + + } + +}
\ No newline at end of file |