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