KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > ContentChangeNotifier


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import org.eclipse.compare.IContentChangeListener;
14 import org.eclipse.compare.IContentChangeNotifier;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.swt.widgets.Display;
17
18 /**
19  * A helper class for managing content change notification.
20  */

21 public class ContentChangeNotifier implements IContentChangeNotifier {
22
23     private ListenerList fListenerList;
24     private final IContentChangeNotifier element;
25     
26     public ContentChangeNotifier(IContentChangeNotifier element) {
27         this.element = element;
28     }
29
30     /* (non-Javadoc)
31      * see IContentChangeNotifier.addChangeListener
32      */

33     public void addContentChangeListener(IContentChangeListener listener) {
34         if (fListenerList == null)
35             fListenerList= new ListenerList();
36         fListenerList.add(listener);
37     }
38     
39     /* (non-Javadoc)
40      * see IContentChangeNotifier.removeChangeListener
41      */

42     public void removeContentChangeListener(IContentChangeListener listener) {
43         if (fListenerList != null) {
44             fListenerList.remove(listener);
45             if (fListenerList.isEmpty())
46                 fListenerList= null;
47         }
48     }
49     
50     /**
51      * Notifies all registered <code>IContentChangeListener</code>s of a content change.
52      */

53     public void fireContentChanged() {
54         if (isEmpty()) {
55             return;
56         }
57         // Legacy listeners may expect to be notified in the UI thread.
58
Runnable JavaDoc runnable = new Runnable JavaDoc() {
59             public void run() {
60                 Object JavaDoc[] listeners= fListenerList.getListeners();
61                 for (int i= 0; i < listeners.length; i++) {
62                     final IContentChangeListener contentChangeListener = (IContentChangeListener)listeners[i];
63                     SafeRunner.run(new ISafeRunnable() {
64                         public void run() throws Exception JavaDoc {
65                             (contentChangeListener).contentChanged(element);
66                         }
67                         public void handleException(Throwable JavaDoc exception) {
68                             // Logged by safe runner
69
}
70                     });
71                 }
72             }
73         };
74         if (Display.getCurrent() == null) {
75             Display.getDefault().syncExec(runnable);
76         } else {
77             runnable.run();
78         }
79     }
80
81     /**
82      * Return whether this notifier is empty (i.e. has no listeners).
83      * @return whether this notifier is empty
84      */

85     public boolean isEmpty() {
86         return fListenerList == null || fListenerList.isEmpty();
87     }
88
89 }
90
Popular Tags