KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > observable > mapping > BaseObservableMapping


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
12 package org.eclipse.jface.internal.databinding.provisional.observable.mapping;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashSet JavaDoc;
17
18 import org.eclipse.jface.internal.databinding.provisional.observable.AbstractObservable;
19 import org.eclipse.jface.internal.databinding.provisional.observable.IChangeListener;
20
21 /**
22  * @since 3.2
23  *
24  */

25 public abstract class BaseObservableMapping extends AbstractObservable {
26
27     /**
28      * Points to an instance of IChangeListener or a Collection of
29      * IMappingChangeListener
30      */

31     protected Object JavaDoc mappingChangeListeners = null;
32
33     public void addMappingChangeListener(IMappingChangeListener listener) {
34         if (mappingChangeListeners == null) {
35             boolean hadListeners = hasListeners();
36             mappingChangeListeners = listener;
37             if (!hadListeners) {
38                 firstListenerAdded();
39             }
40             return;
41         }
42     
43         Collection JavaDoc listenerList;
44         if (mappingChangeListeners instanceof IMappingChangeListener) {
45             IChangeListener l = (IChangeListener) mappingChangeListeners;
46     
47             listenerList = new ArrayList JavaDoc();
48             listenerList.add(l);
49         } else {
50             listenerList = (Collection JavaDoc) mappingChangeListeners;
51         }
52     
53         if (listenerList.size() > 16) {
54             HashSet JavaDoc listenerSet = new HashSet JavaDoc();
55             listenerSet.addAll(listenerList);
56             mappingChangeListeners = listenerList;
57         }
58     
59         listenerList.add(listener);
60     }
61
62     public void removeMappingChangeListener(IMappingChangeListener listener) {
63         if (mappingChangeListeners == listener) {
64             mappingChangeListeners = null;
65             if (!hasListeners()) {
66                 lastListenerRemoved();
67             }
68             return;
69         }
70     
71         if (mappingChangeListeners instanceof Collection JavaDoc) {
72             Collection JavaDoc listenerList = (Collection JavaDoc) mappingChangeListeners;
73             listenerList.remove(listener);
74             if (listenerList.size() == 0) {
75                 mappingChangeListeners = null;
76                 if (!hasListeners()) {
77                     lastListenerRemoved();
78                 }
79             }
80         }
81     }
82
83     protected void fireMappingValueChange(MappingDiff diff) {
84         if (mappingChangeListeners == null) {
85             return;
86         }
87     
88         if (mappingChangeListeners instanceof IMappingChangeListener) {
89             ((IMappingChangeListener) mappingChangeListeners).handleMappingValueChange(this, diff);
90             return;
91         }
92     
93         Collection JavaDoc changeListenerCollection = (Collection JavaDoc) mappingChangeListeners;
94     
95         IMappingChangeListener[] listeners = (IMappingChangeListener[]) (changeListenerCollection)
96                 .toArray(new IMappingChangeListener[changeListenerCollection.size()]);
97         for (int i = 0; i < listeners.length; i++) {
98             listeners[i].handleMappingValueChange(this, diff);
99         }
100     }
101
102 }
103
Popular Tags