2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.ArgeoException
;
29 import org
.eclipse
.swt
.widgets
.Display
;
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());
37 private final Display display
;
39 public AsyncUiEventListener(Display display
) {
41 this.display
= display
;
44 /** Called asynchronously in the UI thread. */
45 protected abstract void onEventInUiThread(List
<Event
> events
)
46 throws RepositoryException
;
49 * Whether these events should be processed in the UI or skipped with no UI
52 protected Boolean
willProcessInUiThread(List
<Event
> events
)
53 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 ArgeoException("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 display
.asyncExec(new Runnable() {
86 onEventInUiThread(events
);
87 } catch (RepositoryException e
) {
88 throw new ArgeoException("Cannot process events "
94 // return Status.OK_STATUS;