KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > PropertyEventDispatcher


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.ui.internal.texteditor;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedHashSet JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24
25 public final class PropertyEventDispatcher {
26     private final Map JavaDoc fHandlerMap= new HashMap JavaDoc();
27     private final Map JavaDoc fReverseMap= new HashMap JavaDoc();
28     private final IPreferenceStore fStore;
29     private final IPropertyChangeListener fListener= new IPropertyChangeListener() {
30         public void propertyChange(PropertyChangeEvent event) {
31             firePropertyChange(event);
32         }
33     };
34     public PropertyEventDispatcher(IPreferenceStore store) {
35         Assert.isLegal(store != null);
36         fStore= store;
37     }
38     public void dispose() {
39         if (!fReverseMap.isEmpty())
40             fStore.removePropertyChangeListener(fListener);
41         fReverseMap.clear();
42         fHandlerMap.clear();
43     }
44     private void firePropertyChange(PropertyChangeEvent event) {
45         Object JavaDoc value= fHandlerMap.get(event.getProperty());
46         if (value instanceof IPropertyChangeListener)
47             ((IPropertyChangeListener) value).propertyChange(event);
48         else if (value instanceof Set JavaDoc)
49             for (Iterator JavaDoc it= ((Set JavaDoc) value).iterator(); it.hasNext(); )
50                 ((IPropertyChangeListener) it.next()).propertyChange(event);
51     }
52     public void addPropertyChangeListener(String JavaDoc property, IPropertyChangeListener listener) {
53         Assert.isLegal(property != null);
54         Assert.isLegal(listener != null);
55
56         if (fReverseMap.isEmpty())
57             fStore.addPropertyChangeListener(fListener);
58
59         multiMapPut(fHandlerMap, property, listener);
60         multiMapPut(fReverseMap, listener, property);
61     }
62     private void multiMapPut(Map JavaDoc map, Object JavaDoc key, Object JavaDoc value) {
63         Object JavaDoc mapping= map.get(key);
64         if (mapping == null) {
65             map.put(key, value);
66         } else if (mapping instanceof Set JavaDoc) {
67             ((Set JavaDoc) mapping).add(value);
68         } else {
69             Set JavaDoc set= new LinkedHashSet JavaDoc();
70             set.add(mapping);
71             set.add(value);
72             map.put(key, set);
73         }
74     }
75     private void multiMapRemove(Map JavaDoc map, Object JavaDoc key, Object JavaDoc value) {
76         Object JavaDoc mapping= map.get(key);
77         if (mapping instanceof Set JavaDoc) {
78             ((Set JavaDoc) mapping).remove(value);
79         } else if (mapping != null) {
80             map.remove(key);
81         }
82     }
83     public void removePropertyChangeListener(IPropertyChangeListener listener) {
84         Object JavaDoc value= fReverseMap.get(listener);
85         if (value == null)
86             return;
87         if (value instanceof String JavaDoc) {
88             fReverseMap.remove(listener);
89             multiMapRemove(fHandlerMap, value, listener);
90         } else if (value instanceof Set JavaDoc) {
91             fReverseMap.remove(listener);
92             for (Iterator JavaDoc it= ((Set JavaDoc) value).iterator(); it.hasNext();)
93                 multiMapRemove(fHandlerMap, it.next(), listener);
94         }
95
96         if (fReverseMap.isEmpty())
97             fStore.removePropertyChangeListener(fListener);
98     }
99 }
Popular Tags