KickJava   Java API By Example, From Geeks To Geeks.

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


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.set.IObservableSet;
17 import org.eclipse.jface.internal.databinding.provisional.observable.set.ISetChangeListener;
18 import org.eclipse.jface.internal.databinding.provisional.observable.set.SetDiff;
19
20 /**
21  * @since 1.0
22  *
23  */

24 abstract public class ObservableMappingWithDomain extends AbstractObservableMapping implements IObservableMappingWithDomain {
25
26     private ISetChangeListener listener = new ISetChangeListener() {
27         public void handleSetChange(IObservableSet source, SetDiff diff) {
28             for (Iterator JavaDoc it = diff.getAdditions().iterator(); it.hasNext();) {
29                 addListenerTo(it.next());
30             }
31             for (Iterator JavaDoc it = diff.getRemovals().iterator(); it.hasNext();) {
32                 removeListenerFrom(it.next());
33             }
34         }
35     };
36
37     private IObservableSet domain;
38
39     /**
40      *
41      */

42     public ObservableMappingWithDomain() {
43     }
44     
45     /**
46      * @param domain
47      */

48     protected void initDomain(IObservableSet domain) {
49         this.domain = domain;
50         domain.addSetChangeListener(listener);
51         for (Iterator JavaDoc it = domain.iterator(); it.hasNext();) {
52             addListenerTo(it.next());
53         }
54     }
55
56     /**
57      * @return Returns the domain.
58      */

59     public IObservableSet getDomain() {
60         return domain;
61     }
62
63     /**
64      * @param domainElement
65      */

66     protected abstract void addListenerTo(Object JavaDoc domainElement);
67
68     /**
69      * @param domainElement
70      */

71     protected abstract void removeListenerFrom(Object JavaDoc domainElement);
72
73     public void dispose() {
74         for (Iterator JavaDoc iter = domain.iterator(); iter.hasNext();) {
75             removeListenerFrom(iter.next());
76         }
77         domain.removeSetChangeListener(listener);
78         super.dispose();
79     }
80
81 }
82
Popular Tags