]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/eclipse/ui/jcr/AsyncUiEventListener.java
Move JCR utilities from API to CMS JCR
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / eclipse / ui / 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.eclipse.ui.EclipseUiException;
14 import org.eclipse.swt.widgets.Display;
15
16 /**
17 * {@link EventListener} which simplifies running actions within the UI thread.
18 */
19 public abstract class AsyncUiEventListener implements EventListener {
20 // private final static Log logSuper = LogFactory
21 // .getLog(AsyncUiEventListener.class);
22 private final Log logThis = LogFactory.getLog(getClass());
23
24 private final Display display;
25
26 public AsyncUiEventListener(Display display) {
27 super();
28 this.display = display;
29 }
30
31 /** Called asynchronously in the UI thread. */
32 protected abstract void onEventInUiThread(List<Event> events) throws RepositoryException;
33
34 /**
35 * Whether these events should be processed in the UI or skipped with no UI
36 * job created.
37 */
38 protected Boolean willProcessInUiThread(List<Event> events) 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 EclipseUiException("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 if (!display.isDisposed())
69 display.asyncExec(new Runnable() {
70 public void run() {
71 try {
72 onEventInUiThread(events);
73 } catch (RepositoryException e) {
74 throw new EclipseUiException("Cannot process events " + events, e);
75 }
76 }
77 });
78
79 // return Status.OK_STATUS;
80 // }
81 // };
82 // job.schedule();
83 }
84 }