diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/isode/stroke/eventloop/ImmediateEventLoop.java | 17 | ||||
-rw-r--r-- | src/com/isode/stroke/eventloop/SimpleEventLoop.java | 104 |
2 files changed, 114 insertions, 7 deletions
diff --git a/src/com/isode/stroke/eventloop/ImmediateEventLoop.java b/src/com/isode/stroke/eventloop/ImmediateEventLoop.java new file mode 100644 index 0000000..19bb42d --- /dev/null +++ b/src/com/isode/stroke/eventloop/ImmediateEventLoop.java @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2010-14, Isode Limited, London, England. + * All rights reserved. + */ + +package com.isode.stroke.eventloop; + +/** + * Don't use this, it simply runs the callback in the same thread. + * It is useful for unit testing, but will break GUIs. + */ +public class ImmediateEventLoop extends EventLoop { + @Override + protected void post(Event event) { + handleEvent(event); + } +} diff --git a/src/com/isode/stroke/eventloop/SimpleEventLoop.java b/src/com/isode/stroke/eventloop/SimpleEventLoop.java index 423321e..a471457 100644 --- a/src/com/isode/stroke/eventloop/SimpleEventLoop.java +++ b/src/com/isode/stroke/eventloop/SimpleEventLoop.java @@ -1,17 +1,107 @@ /* - * Copyright (c) 2010, Isode Limited, London, England. - * All rights reserved. +* Copyright (c) 2014, Isode Limited, London, England. +* All rights reserved. +*/ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. */ package com.isode.stroke.eventloop; -/** - * Don't use this, it simply runs the callback in the same thread. - * It is useful for unit testing, but will break GUIs. - */ +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + public class SimpleEventLoop extends EventLoop { + + private boolean isRunning_; + private final List<Event> events_ = new ArrayList<Event>(); + private final Object eventsMutex_ = new Object(); + + public SimpleEventLoop() { + isRunning_ = true; + } + + @Override + protected void finalize() throws Throwable { + synchronized (eventsMutex_) { + if (!events_.isEmpty()) { + System.err.println("Warning: Pending events in SimpleEventLoop at finalize time"); + } + } + } + + public void run() { + doRun(false); + } + + public void runUntilEvents() { + doRun(true); + } + + private void doRun(boolean breakAfterEvents) { + while (isRunning_) { + List<Event> events = new ArrayList<Event>(); + synchronized (eventsMutex_) { + while (events_.isEmpty()) { + try { + eventsMutex_.wait(); + } catch (InterruptedException e) { + // Ignore + } + } + swapCollectionContents(events, events_); + } + for (Event event : events) { + handleEvent(event); + } + if (breakAfterEvents) { + return; + } + } + } + + public void runOnce() { + List<Event> events = new ArrayList<Event>(); + synchronized (eventsMutex_) { + swapCollectionContents(events, events_); + } + for (Event event : events) { + handleEvent(event); + } + } + + public void stop() { + postEvent(new Event.Callback() { + + @Override + public void run() { + doStop(); + } + + }); + } + + private void doStop() { + isRunning_ = false; + } + + static <T> void swapCollectionContents(Collection<T> coll1, Collection<T> coll2) { + Collection<T> temp = new ArrayList<T>(coll1); + coll1.clear(); + coll1.addAll(coll2); + coll2.clear(); + coll2.addAll(temp); + } + @Override protected void post(Event event) { - handleEvent(event); + synchronized (eventsMutex_) { + events_.add(event); + eventsMutex_.notifyAll(); + } } + } |