diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
commit | 2da71a8a85486a494343f1662d64fb5ae5a2a44e (patch) | |
tree | 23992f9f2a00bac23b345e5c2cc9c1194efc25be /src/com/isode/stroke/eventloop | |
download | stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.zip stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.tar.bz2 |
Initial import
Diffstat (limited to 'src/com/isode/stroke/eventloop')
-rw-r--r-- | src/com/isode/stroke/eventloop/Event.java | 42 | ||||
-rw-r--r-- | src/com/isode/stroke/eventloop/EventLoop.java | 90 | ||||
-rw-r--r-- | src/com/isode/stroke/eventloop/EventOwner.java | 14 | ||||
-rw-r--r-- | src/com/isode/stroke/eventloop/SimpleEventLoop.java | 17 |
4 files changed, 163 insertions, 0 deletions
diff --git a/src/com/isode/stroke/eventloop/Event.java b/src/com/isode/stroke/eventloop/Event.java new file mode 100644 index 0000000..b3c8c00 --- /dev/null +++ b/src/com/isode/stroke/eventloop/Event.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2010 Remko Tron¨on + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ +/* + * Copyright (c) 2010, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.eventloop; + +public class Event { + + public interface Callback { + + void run(); + } + + Event(EventOwner owner, Callback callback, int id) { + this.owner = owner; + this.callback = callback; + this.id = id; + } + + @Override + public boolean equals(Object other) { + if (other instanceof Event) { + return id == ((Event) other).id; + } + return false; + } + + @Override + public int hashCode() { + int hash = 7; + hash = 23 * hash + this.id; + return hash; + } + final int id; + final EventOwner owner; + public final Callback callback; +} diff --git a/src/com/isode/stroke/eventloop/EventLoop.java b/src/com/isode/stroke/eventloop/EventLoop.java new file mode 100644 index 0000000..9399d9a --- /dev/null +++ b/src/com/isode/stroke/eventloop/EventLoop.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2010 Remko Tron¨on + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ +/* + * Copyright (c) 2010, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.eventloop; + +import java.util.ArrayList; +import java.util.Vector; + +public abstract class EventLoop { + + public EventLoop() { + } + + public void postEvent(Event.Callback callback) { + postEvent(callback, null); + } + + public void postEvent(Event.Callback callback, EventOwner owner) { + Event event; + synchronized (eventsMutex_) { + event = new Event(owner, callback, nextEventID_); + nextEventID_++; + events_.add(event); + } + post(event); + } + + public void removeEventsFromOwner(EventOwner owner) { + synchronized (eventsMutex_) { + ArrayList<Event> matches = new ArrayList<Event>(); + for (Event event : events_) { + if (event.owner == owner) { + matches.add(event); + } + } + events_.removeAll(matches); + } + } + + /** + * Reimplement this to call handleEvent(event) from the thread in which + * the event loop is residing. + */ + protected abstract void post(Event event); + + protected void handleEvent(Event event) { + if (handlingEvents_) { + // We're being called recursively. Push in the list of events to + // handle in the parent handleEvent() + eventsToHandle_.add(event); + return; + } + + boolean doCallback = false; + synchronized (eventsMutex_) { + doCallback = events_.contains(event); + if (doCallback) { + events_.remove(event); + } + } + if (doCallback) { + handlingEvents_ = true; + event.callback.run(); + // Process events that were passed to handleEvent during the callback + // (i.e. through recursive calls of handleEvent) + while (!eventsToHandle_.isEmpty()) { + Event nextEvent = eventsToHandle_.firstElement(); + eventsToHandle_.remove(0); + nextEvent.callback.run(); + } + handlingEvents_ = false; + } + } + // struct HasOwner { + // HasOwner(boost::shared_ptr<EventOwner> owner) : owner(owner) {} + // bool operator()(const Event& event) { return event.owner == owner; } + // boost::shared_ptr<EventOwner> owner; + // }; + private final Object eventsMutex_ = new Object(); + private int nextEventID_ = 0; + private Vector<Event> events_ = new Vector<Event>(); + boolean handlingEvents_ = false; + private Vector<Event> eventsToHandle_ = new Vector<Event>(); +} diff --git a/src/com/isode/stroke/eventloop/EventOwner.java b/src/com/isode/stroke/eventloop/EventOwner.java new file mode 100644 index 0000000..305ab1a --- /dev/null +++ b/src/com/isode/stroke/eventloop/EventOwner.java @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2010 Remko Tron¨on + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ +/* + * Copyright (c) 2010, Isode Limited, London, England. + * All rights reserved. + */ +package com.isode.stroke.eventloop; + +public interface EventOwner { + +} diff --git a/src/com/isode/stroke/eventloop/SimpleEventLoop.java b/src/com/isode/stroke/eventloop/SimpleEventLoop.java new file mode 100644 index 0000000..003fc98 --- /dev/null +++ b/src/com/isode/stroke/eventloop/SimpleEventLoop.java @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2010, 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 SimpleEventLoop extends EventLoop { + @Override + protected void post(Event event) { + event.callback.run(); + } +} |