blob: bfcb297f8e6bbec4e7f61c6b25269cb8efd966d3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/*******************************************************************************
* Copyright (c) 2006, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.compare.internal;
import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.swt.widgets.Display;
/**
* A helper class for managing content change notification.
*/
public class ContentChangeNotifier implements IContentChangeNotifier {
private ListenerList fListenerList;
private final IContentChangeNotifier element;
public ContentChangeNotifier(IContentChangeNotifier element) {
this.element = element;
}
/* (non-Javadoc)
* see IContentChangeNotifier.addChangeListener
*/
public void addContentChangeListener(IContentChangeListener listener) {
if (fListenerList == null)
fListenerList= new ListenerList();
fListenerList.add(listener);
}
/* (non-Javadoc)
* see IContentChangeNotifier.removeChangeListener
*/
public void removeContentChangeListener(IContentChangeListener listener) {
if (fListenerList != null) {
fListenerList.remove(listener);
if (fListenerList.isEmpty())
fListenerList= null;
}
}
/**
* Notifies all registered <code>IContentChangeListener</code>s of a content change.
*/
public void fireContentChanged() {
if (isEmpty()) {
return;
}
// Legacy listeners may expect to be notified in the UI thread.
Runnable runnable = new Runnable() {
public void run() {
Object[] listeners= fListenerList.getListeners();
for (int i= 0; i < listeners.length; i++) {
final IContentChangeListener contentChangeListener = (IContentChangeListener)listeners[i];
SafeRunner.run(new ISafeRunnable() {
public void run() throws Exception {
contentChangeListener.contentChanged(element);
}
public void handleException(Throwable exception) {
// Logged by safe runner
}
});
}
}
};
if (Display.getCurrent() == null) {
Display.getDefault().syncExec(runnable);
} else {
runnable.run();
}
}
/**
* Return whether this notifier is empty (i.e. has no listeners).
* @return whether this notifier is empty
*/
public boolean isEmpty() {
return fListenerList == null || fListenerList.isEmpty();
}
}
|