KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > PropertyChangeHandler


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.team.internal.ui;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.jface.util.IPropertyChangeListener;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16
17 /**
18  * Helper class for implementing property change handling
19  */

20 public class PropertyChangeHandler {
21
22     private ListenerList fListeners = new ListenerList(ListenerList.IDENTITY);
23     
24     /**
25      * Notifies listeners of property changes, handling any exceptions
26      */

27     class PropertyNotifier implements ISafeRunnable {
28
29         private IPropertyChangeListener fListener;
30         private PropertyChangeEvent fEvent;
31
32         /**
33          * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
34          */

35         public void handleException(Throwable JavaDoc exception) {
36             TeamUIPlugin.log(IStatus.ERROR, TeamUIMessages.AbstractSynchronizeParticipant_5, exception);
37         }
38
39         /**
40          * @see org.eclipse.core.runtime.ISafeRunnable#run()
41          */

42         public void run() throws Exception JavaDoc {
43             fListener.propertyChange(fEvent);
44         }
45
46         /**
47          * Notifies listeners of the property change
48          *
49          * @param event
50          * the property change event
51          */

52         public void notify(PropertyChangeEvent event) {
53             if (fListeners == null) {
54                 return;
55             }
56             fEvent = event;
57             Object JavaDoc[] copiedListeners = fListeners.getListeners();
58             for (int i = 0; i < copiedListeners.length; i++) {
59                 fListener = (IPropertyChangeListener) copiedListeners[i];
60                 SafeRunner.run(this);
61             }
62             fListener = null;
63         }
64     }
65     
66     public void addPropertyChangeListener(IPropertyChangeListener listener) {
67         fListeners.add(listener);
68     }
69
70     public void removePropertyChangeListener(IPropertyChangeListener listener) {
71         fListeners.remove(listener);
72     }
73     
74     public void firePropertyChange(Object JavaDoc source, String JavaDoc property, Object JavaDoc oldValue, Object JavaDoc newValue) {
75         PropertyNotifier notifier = new PropertyNotifier();
76         notifier.notify(new PropertyChangeEvent(source, property, oldValue, newValue));
77     }
78     
79 }
80
Popular Tags