Mercurial > projects > dwt2
comparison org.eclipse.jface.databinding/src/org/eclipse/jface/databinding/viewers/ObservableListContentProvider.d @ 78:0a55d2d5a946
Added file for databinding
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 14 Apr 2009 11:35:29 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
76:f05e6e8b2f2d | 78:0a55d2d5a946 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2006-2008 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 * Tom Schindl<tom.schindl@bestsolution.at> - bugfix in: 214355 | |
11 * Matthew Hall - bugs 215531, 226765 | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.jface.databinding.viewers.ObservableListContentProvider; | |
15 | |
16 import java.lang.all; | |
17 | |
18 import java.util.Set; | |
19 | |
20 import org.eclipse.core.databinding.observable.IObservableCollection; | |
21 import org.eclipse.core.databinding.observable.list.IListChangeListener; | |
22 import org.eclipse.core.databinding.observable.list.IObservableList; | |
23 import org.eclipse.core.databinding.observable.list.ListChangeEvent; | |
24 import org.eclipse.core.databinding.observable.list.ListDiffVisitor; | |
25 import org.eclipse.core.databinding.observable.set.IObservableSet; | |
26 import org.eclipse.core.runtime.Assert; | |
27 import org.eclipse.jface.internal.databinding.viewers.ObservableCollectionContentProvider; | |
28 import org.eclipse.jface.internal.databinding.viewers.ViewerElementSet; | |
29 import org.eclipse.jface.viewers.AbstractListViewer; | |
30 import org.eclipse.jface.viewers.AbstractTableViewer; | |
31 import org.eclipse.jface.viewers.IStructuredContentProvider; | |
32 import org.eclipse.jface.viewers.Viewer; | |
33 | |
34 /** | |
35 * A {@link IStructuredContentProvider content provider} for | |
36 * {@link AbstractTableViewer} or {@link AbstractListViewer} that provides | |
37 * elements of an {@link IObservableList} when set as the viewer's input. | |
38 * Objects of this class listen for changes to the observable list, and will | |
39 * insert and remove viewer elements to reflect observed changes. | |
40 * | |
41 * <p> | |
42 * This class is not intended to be subclassed by clients. | |
43 * | |
44 * @since 1.1 | |
45 */ | |
46 public class ObservableListContentProvider : | |
47 IStructuredContentProvider { | |
48 private ObservableCollectionContentProvider impl; | |
49 | |
50 private static class Impl : ObservableCollectionContentProvider | |
51 , IListChangeListener { | |
52 protected void checkInput(Object input) { | |
53 Assert | |
54 .isTrue(null !is cast(IObservableList)input, | |
55 "This content provider only works with input of type IObservableList"); //$NON-NLS-1$ | |
56 } | |
57 | |
58 protected void addCollectionChangeListener( | |
59 IObservableCollection collection) { | |
60 (cast(IObservableList) collection).addListChangeListener(this); | |
61 } | |
62 | |
63 protected void removeCollectionChangeListener( | |
64 IObservableCollection collection) { | |
65 (cast(IObservableList) collection).removeListChangeListener(this); | |
66 } | |
67 | |
68 public void handleListChange(ListChangeEvent event) { | |
69 if (isViewerDisposed()) | |
70 return; | |
71 final Set removals = ViewerElementSet.withComparer(comparer); | |
72 | |
73 event.diff.accept(new class() ListDiffVisitor { | |
74 public void handleAdd(int index, Object element) { | |
75 knownElements.add(element); | |
76 viewerUpdater.insert(element, index); | |
77 } | |
78 | |
79 public void handleRemove(int index, Object element) { | |
80 viewerUpdater.remove(element, index); | |
81 removals.add(element); | |
82 } | |
83 | |
84 public void handleReplace(int index, Object oldElement, | |
85 Object newElement) { | |
86 knownElements.add(newElement); | |
87 viewerUpdater.replace(oldElement, newElement, index); | |
88 removals.add(oldElement); | |
89 } | |
90 | |
91 public void handleMove(int oldIndex, int newIndex, | |
92 Object element) { | |
93 viewerUpdater.move(element, oldIndex, newIndex); | |
94 } | |
95 }); | |
96 | |
97 // For each removed element, do not remove from known elements if | |
98 // the element is still present elsewhere in the list. | |
99 removals.removeAll(event.getObservableList()); | |
100 knownElements.removeAll(removals); | |
101 } | |
102 } | |
103 | |
104 /** | |
105 * Constructs an ObservableListContentProvider | |
106 */ | |
107 public this() { | |
108 impl = new Impl(); | |
109 } | |
110 | |
111 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { | |
112 impl.inputChanged(viewer, oldInput, newInput); | |
113 } | |
114 | |
115 public Object[] getElements(Object inputElement) { | |
116 return impl.getElements(inputElement); | |
117 } | |
118 | |
119 public void dispose() { | |
120 impl.dispose(); | |
121 } | |
122 | |
123 /** | |
124 * Returns the set of elements known to this content provider. Label | |
125 * providers may track this set if they need to be notified about additions | |
126 * before the viewer sees the added element, and notified about removals | |
127 * after the element was removed from the viewer. This is intended for use | |
128 * by label providers, as it will always return the items that need labels. | |
129 * | |
130 * @return readableSet of items that will need labels | |
131 */ | |
132 public IObservableSet getKnownElements() { | |
133 return impl.getKnownElements(); | |
134 } | |
135 } |