]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/eclipse/ui/jcr/AsyncUiEventListener.java
Working UUID factory
[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.argeo.api.cms.CmsLog;
12 import org.argeo.eclipse.ui.EclipseUiException;
13 import org.eclipse.swt.widgets.Display;
14
15 /**
16 * {@link EventListener} which simplifies running actions within the UI thread.
17 */
18 public abstract class AsyncUiEventListener implements EventListener {
19 // private final static Log logSuper = LogFactory
20 // .getLog(AsyncUiEventListener.class);
21 private final CmsLog logThis = CmsLog.getLog(getClass());
22
23 private final Display display;
24
25 public AsyncUiEventListener(Display display) {
26 super();
27 this.display = display;
28 }
29
30 /** Called asynchronously in the UI thread. */
31 protected abstract void onEventInUiThread(List<Event> events) 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) throws RepositoryException {
38 return true;
39 }
40
41 protected CmsLog getLog() {
42 return logThis;
43 }
44
45 public final void onEvent(final EventIterator eventIterator) {
46 final List<Event> events = new ArrayList<Event>();
47 while (eventIterator.hasNext())
48 events.add(eventIterator.nextEvent());
49
50 if (logThis.isTraceEnabled())
51 logThis.trace("Received " + events.size() + " events");
52
53 try {
54 if (!willProcessInUiThread(events))
55 return;
56 } catch (RepositoryException e) {
57 throw new EclipseUiException("Cannot test skip events " + events, e);
58 }
59
60 // Job job = new Job("JCR Events") {
61 // protected IStatus run(IProgressMonitor monitor) {
62 // if (display.isDisposed()) {
63 // logSuper.warn("Display is disposed cannot update UI");
64 // return Status.CANCEL_STATUS;
65 // }
66
67 if (!display.isDisposed())
68 display.asyncExec(new Runnable() {
69 public void run() {
70 try {
71 onEventInUiThread(events);
72 } catch (RepositoryException e) {
73 throw new EclipseUiException("Cannot process events " + events, e);
74 }
75 }
76 });
77
78 // return Status.OK_STATUS;
79 // }
80 // };
81 // job.schedule();
82 }
83 }