78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2007 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 module org.eclipse.jface.databinding.viewers.ListeningLabelProvider;
|
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.Iterator;
|
|
17
|
|
18 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
19 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
20 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
21 import org.eclipse.jface.internal.databinding.provisional.viewers.ViewerLabelProvider;
|
|
22
|
|
23 /**
|
|
24 * @since 1.1
|
|
25 *
|
|
26 */
|
|
27 public abstract class ListeningLabelProvider : ViewerLabelProvider {
|
|
28
|
|
29 private ISetChangeListener listener = new class() ISetChangeListener {
|
|
30 public void handleSetChange(SetChangeEvent event) {
|
|
31 for (Iterator it = event.diff.getAdditions().iterator(); it.hasNext();) {
|
|
32 addListenerTo(it.next());
|
|
33 }
|
|
34 for (Iterator it = event.diff.getRemovals().iterator(); it.hasNext();) {
|
|
35 removeListenerFrom(it.next());
|
|
36 }
|
|
37 }
|
|
38 };
|
|
39
|
|
40 private IObservableSet items;
|
|
41
|
|
42 /**
|
|
43 * @param itemsThatNeedLabels
|
|
44 */
|
|
45 public this(IObservableSet itemsThatNeedLabels) {
|
|
46 this.items = itemsThatNeedLabels;
|
|
47 items.addSetChangeListener(listener);
|
|
48 for (Iterator it = items.iterator(); it.hasNext();) {
|
|
49 addListenerTo(it.next());
|
|
50 }
|
|
51 }
|
|
52
|
|
53 /**
|
|
54 * @param next
|
|
55 */
|
|
56 protected abstract void removeListenerFrom(Object next);
|
|
57
|
|
58 /**
|
|
59 * @param next
|
|
60 */
|
|
61 protected abstract void addListenerTo(Object next);
|
|
62
|
|
63 public void dispose() {
|
|
64 for (Iterator iter = items.iterator(); iter.hasNext();) {
|
|
65 removeListenerFrom(iter.next());
|
|
66 }
|
|
67 items.removeSetChangeListener(listener);
|
|
68 super.dispose();
|
|
69 }
|
|
70 }
|