]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/jcr/AsyncUiEventListener.java
Add some utilities to eclipse UI Utils to ease UI implementation.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / jcr / AsyncUiEventListener.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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.ArgeoException;
29 import org.eclipse.swt.widgets.Display;
30
31 /** {@link EventListener} which simplifies running actions within the UI thread. */
32 public abstract class AsyncUiEventListener implements EventListener {
33 // private final static Log logSuper = LogFactory
34 // .getLog(AsyncUiEventListener.class);
35 private final Log logThis = LogFactory.getLog(getClass());
36
37 private final Display display;
38
39 public AsyncUiEventListener(Display display) {
40 super();
41 this.display = display;
42 }
43
44 /** Called asynchronously in the UI thread. */
45 protected abstract void onEventInUiThread(List<Event> events)
46 throws RepositoryException;
47
48 /**
49 * Whether these events should be processed in the UI or skipped with no UI
50 * job created.
51 */
52 protected Boolean willProcessInUiThread(List<Event> events)
53 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 ArgeoException("Cannot test skip events " + events, e);
74 }
75
76 // Job job = new Job("JCR Events") {
77 // protected IStatus run(IProgressMonitor monitor) {
78 // if (display.isDisposed()) {
79 // logSuper.warn("Display is disposed cannot update UI");
80 // return Status.CANCEL_STATUS;
81 // }
82
83 display.asyncExec(new Runnable() {
84 public void run() {
85 try {
86 onEventInUiThread(events);
87 } catch (RepositoryException e) {
88 throw new ArgeoException("Cannot process events "
89 + events, e);
90 }
91 }
92 });
93
94 // return Status.OK_STATUS;
95 // }
96 // };
97 // job.schedule();
98 }
99 }