]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AsyncUiEventListener.java
0e8a12cbee7a5d6f4d0d01d210eac5a070d2ba98
[lgpl/argeo-commons.git] / AsyncUiEventListener.java
1 package org.argeo.eclipse.ui.jcr;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.RepositoryException;
7 import javax.jcr.observation.Event;
8 import javax.jcr.observation.EventIterator;
9 import javax.jcr.observation.EventListener;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.ArgeoException;
14 import org.eclipse.swt.widgets.Display;
15
16 /** {@link EventListener} which simplifies running actions within the UI thread. */
17 public abstract class AsyncUiEventListener implements EventListener {
18 // private final static Log logSuper = LogFactory
19 // .getLog(AsyncUiEventListener.class);
20 private final Log logThis = LogFactory.getLog(getClass());
21
22 private final Display display;
23
24 public AsyncUiEventListener(Display display) {
25 super();
26 this.display = display;
27 }
28
29 /** Called asynchronously in the UI thread. */
30 protected abstract void onEventInUiThread(List<Event> events)
31 throws RepositoryException;
32
33 /**
34 * Whether these events should be processed in the UI or skipped with no UI
35 * job created.
36 */
37 protected Boolean willProcessInUiThread(List<Event> events)
38 throws RepositoryException {
39 return true;
40 }
41
42 protected Log getLog() {
43 return logThis;
44 }
45
46 public final void onEvent(final EventIterator eventIterator) {
47 final List<Event> events = new ArrayList<Event>();
48 while (eventIterator.hasNext())
49 events.add(eventIterator.nextEvent());
50
51 if (logThis.isTraceEnabled())
52 logThis.trace("Received " + events.size() + " events");
53
54 try {
55 if (!willProcessInUiThread(events))
56 return;
57 } catch (RepositoryException e) {
58 throw new ArgeoException("Cannot test skip events " + events, e);
59 }
60
61 // Job job = new Job("JCR Events") {
62 // protected IStatus run(IProgressMonitor monitor) {
63 // if (display.isDisposed()) {
64 // logSuper.warn("Display is disposed cannot update UI");
65 // return Status.CANCEL_STATUS;
66 // }
67
68 display.asyncExec(new Runnable() {
69 public void run() {
70 try {
71 onEventInUiThread(events);
72 } catch (RepositoryException e) {
73 throw new ArgeoException("Cannot process events "
74 + events, e);
75 }
76 }
77 });
78
79 // return Status.OK_STATUS;
80 // }
81 // };
82 // job.schedule();
83 }
84 }