diff options
Diffstat (limited to 'bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ContentChangeNotifier.java')
-rw-r--r-- | bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ContentChangeNotifier.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ContentChangeNotifier.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ContentChangeNotifier.java new file mode 100644 index 000000000..9aee9c259 --- /dev/null +++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ContentChangeNotifier.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2006, 2010 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(); + } + +} |