View Javadoc
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.eclipse.ui.EclipseUiException;
29  import org.eclipse.swt.widgets.Display;
30  
31  /**
32   * {@link EventListener} which simplifies running actions within the UI thread.
33   */
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());
38  
39  	private final Display display;
40  
41  	public AsyncUiEventListener(Display display) {
42  		super();
43  		this.display = display;
44  	}
45  
46  	/** Called asynchronously in the UI thread. */
47  	protected abstract void onEventInUiThread(List<Event> events) throws RepositoryException;
48  
49  	/**
50  	 * Whether these events should be processed in the UI or skipped with no UI
51  	 * job created.
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  		// 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  		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  		// return Status.OK_STATUS;
95  		// }
96  		// };
97  		// job.schedule();
98  	}
99  }