2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.jcr
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import javax
.jcr
.RepositoryException
;
22 import javax
.jcr
.observation
.Event
;
23 import javax
.jcr
.observation
.EventIterator
;
24 import javax
.jcr
.observation
.EventListener
;
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
;
32 * {@link EventListener} which simplifies running actions within the UI thread.
34 public abstract class AsyncUiEventListener
implements EventListener
{
35 // private final static Log logSuper = LogFactory
36 // .getLog(AsyncUiEventListener.class);
37 private final Log logThis
= LogFactory
.getLog(getClass());
39 private final Display display
;
41 public AsyncUiEventListener(Display display
) {
43 this.display
= display
;
46 /** Called asynchronously in the UI thread. */
47 protected abstract void onEventInUiThread(List
<Event
> events
) throws RepositoryException
;
50 * Whether these events should be processed in the UI or skipped with no UI
53 protected Boolean
willProcessInUiThread(List
<Event
> events
) throws RepositoryException
{
57 protected Log
getLog() {
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());
66 if (logThis
.isTraceEnabled())
67 logThis
.trace("Received " + events
.size() + " events");
70 if (!willProcessInUiThread(events
))
72 } catch (RepositoryException e
) {
73 throw new EclipseUiException("Cannot test skip events " + events
, e
);
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;
83 if (!display
.isDisposed())
84 display
.asyncExec(new Runnable() {
87 onEventInUiThread(events
);
88 } catch (RepositoryException e
) {
89 throw new EclipseUiException("Cannot process events " + events
, e
);
94 // return Status.OK_STATUS;