KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
15
16 import org.eclipse.jface.internal.databinding.provisional.observable.IObservableCollection;
17 import org.eclipse.jface.internal.databinding.provisional.observable.list.IListChangeListener;
18 import org.eclipse.jface.internal.databinding.provisional.observable.list.IObservableList;
19 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiff;
20 import org.eclipse.jface.internal.databinding.provisional.observable.list.ListDiffEntry;
21 import org.eclipse.jface.internal.databinding.provisional.observable.set.IObservableSet;
22 import org.eclipse.jface.internal.databinding.provisional.observable.set.ISetChangeListener;
23 import org.eclipse.jface.internal.databinding.provisional.observable.set.SetDiff;
24
25 /**
26  * @since 1.0
27  *
28  */

29 abstract public class ObservableMultiMappingWithDomain extends
30         AbstractObservableMultiMapping implements
31         IObservableMultiMappingWithDomain {
32
33     private ISetChangeListener listener = new ISetChangeListener() {
34         public void handleSetChange(IObservableSet source, SetDiff diff) {
35             for (Iterator JavaDoc it = diff.getAdditions().iterator(); it.hasNext();) {
36                 addListenerTo(it.next());
37             }
38             for (Iterator JavaDoc it = diff.getRemovals().iterator(); it.hasNext();) {
39                 removeListenerFrom(it.next());
40             }
41         }
42     };
43
44     private IListChangeListener listListener = new IListChangeListener() {
45
46         public void handleListChange(IObservableList source, ListDiff diff) {
47             ListDiffEntry[] entries = diff.getDifferences();
48             for (int i = 0; i < entries.length; i++) {
49                 ListDiffEntry entry = entries[i];
50                 if (entry.isAddition()) {
51                     addListenerTo(entry.getElement());
52                 } else {
53                     removeListenerFrom(entry.getElement());
54                 }
55             }
56         }
57     };
58
59     private IObservableCollection domain;
60
61     /**
62      *
63      */

64     public ObservableMultiMappingWithDomain() {
65     }
66
67     /**
68      * @param domain
69      */

70     protected void initDomain(IObservableCollection domain) {
71         this.domain = domain;
72         if (domain instanceof IObservableList) {
73             IObservableList listDomain = (IObservableList) domain;
74             listDomain.addListChangeListener(listListener);
75         } else {
76             ((IObservableSet) domain).addSetChangeListener(listener);
77         }
78         for (Iterator JavaDoc it = getDomainIterator(domain); it.hasNext();) {
79             addListenerTo(it.next());
80         }
81     }
82
83     private Iterator JavaDoc getDomainIterator(IObservableCollection domain) {
84         Iterator JavaDoc it;
85         if (domain instanceof IObservableList) {
86             IObservableList listDomain = (IObservableList) domain;
87             it = listDomain.iterator();
88         } else {
89             IObservableSet setDomain = (IObservableSet) domain;
90             it = setDomain.iterator();
91         }
92         return it;
93     }
94
95     /**
96      * @return Returns the domain.
97      */

98     public IObservableCollection getDomain() {
99         return domain;
100     }
101
102     /**
103      * @param domainElement
104      */

105     protected abstract void addListenerTo(Object JavaDoc domainElement);
106
107     /**
108      * @param domainElement
109      */

110     protected abstract void removeListenerFrom(Object JavaDoc domainElement);
111
112     public void dispose() {
113         for (Iterator JavaDoc iter = getDomainIterator(domain); iter.hasNext();) {
114             removeListenerFrom(iter.next());
115         }
116         if (domain instanceof IObservableList) {
117             IObservableList listDomain = (IObservableList) domain;
118             listDomain.removeListChangeListener(listListener);
119         } else {
120             IObservableSet setDomain = (IObservableSet) domain;
121             setDomain.removeSetChangeListener(listener);
122         }
123         super.dispose();
124     }
125
126 }
127
Popular Tags