1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.eclipse.ui.jcr;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.jcr.RepositoryException;
22 import javax.jcr.observation.Event;
23 import javax.jcr.observation.EventIterator;
24 import javax.jcr.observation.EventListener;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.eclipse.ui.EclipseUiException;
29 import org.eclipse.swt.widgets.Display;
30
31
32
33
34 public abstract class AsyncUiEventListener implements EventListener {
35
36
37 private final Log logThis = LogFactory.getLog(getClass());
38
39 private final Display display;
40
41 public AsyncUiEventListener(Display display) {
42 super();
43 this.display = display;
44 }
45
46
47 protected abstract void onEventInUiThread(List<Event> events) throws RepositoryException;
48
49
50
51
52
53 protected Boolean willProcessInUiThread(List<Event> events) throws RepositoryException {
54 return true;
55 }
56
57 protected Log getLog() {
58 return logThis;
59 }
60
61 public final void onEvent(final EventIterator eventIterator) {
62 final List<Event> events = new ArrayList<Event>();
63 while (eventIterator.hasNext())
64 events.add(eventIterator.nextEvent());
65
66 if (logThis.isTraceEnabled())
67 logThis.trace("Received " + events.size() + " events");
68
69 try {
70 if (!willProcessInUiThread(events))
71 return;
72 } catch (RepositoryException e) {
73 throw new EclipseUiException("Cannot test skip events " + events, e);
74 }
75
76
77
78
79
80
81
82
83 if (!display.isDisposed())
84 display.asyncExec(new Runnable() {
85 public void run() {
86 try {
87 onEventInUiThread(events);
88 } catch (RepositoryException e) {
89 throw new EclipseUiException("Cannot process events " + events, e);
90 }
91 }
92 });
93
94
95
96
97
98 }
99 }