78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007-2008 Matthew Hall 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 * Matthew Hall - initial API and implementation (bug 207858)
|
|
10 * Matthew Hall - bug 226765
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module org.eclipse.jface.databinding.viewers.ObservableListTreeContentProvider;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import java.util.Iterator;
|
|
18 import java.util.Set;
|
|
19
|
|
20 import org.eclipse.core.databinding.observable.IObservableCollection;
|
|
21 import org.eclipse.core.databinding.observable.IObservablesListener;
|
|
22 import org.eclipse.core.databinding.observable.list.IListChangeListener;
|
|
23 import org.eclipse.core.databinding.observable.list.IObservableList;
|
|
24 import org.eclipse.core.databinding.observable.list.ListChangeEvent;
|
|
25 import org.eclipse.core.databinding.observable.list.ListDiffVisitor;
|
|
26 import org.eclipse.core.databinding.observable.masterdetail.IObservableFactory;
|
|
27 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
28 import org.eclipse.jface.internal.databinding.viewers.ObservableCollectionTreeContentProvider;
|
|
29 import org.eclipse.jface.internal.databinding.viewers.ViewerElementSet;
|
|
30 import org.eclipse.jface.viewers.AbstractTreeViewer;
|
|
31 import org.eclipse.jface.viewers.ITreeContentProvider;
|
|
32 import org.eclipse.jface.viewers.Viewer;
|
|
33
|
|
34 /**
|
|
35 * An {@link ITreeContentProvider} for use with an {@link AbstractTreeViewer},
|
|
36 * which uses the provided {@link IObservableFactory list factory} to obtain the
|
|
37 * elements of a tree. Object of this class listen for changes to each
|
|
38 * {@link IObservableList} created by the factory, and will insert and remove
|
|
39 * viewer elements to reflect the observed changes.
|
|
40 *
|
|
41 * <p>
|
|
42 * This class is not intended to be subclassed by clients.
|
|
43 *
|
|
44 * @since 1.2
|
|
45 */
|
|
46 public class ObservableListTreeContentProvider : ITreeContentProvider {
|
|
47 private final ObservableCollectionTreeContentProvider impl;
|
|
48
|
|
49 private static class Impl : ObservableCollectionTreeContentProvider {
|
|
50 public this(IObservableFactory listFactory,
|
|
51 TreeStructureAdvisor structureAdvisor) {
|
|
52 super(listFactory, structureAdvisor);
|
|
53 }
|
|
54
|
|
55 private class ListChangeListener : IListChangeListener {
|
|
56 final Object parentElement;
|
|
57
|
|
58 public this(Object parentElement) {
|
|
59 this.parentElement = parentElement;
|
|
60 }
|
|
61
|
|
62 public void handleListChange(ListChangeEvent event) {
|
|
63 if (isViewerDisposed())
|
|
64 return;
|
|
65
|
|
66 final Set removals = ViewerElementSet.withComparer(comparer);
|
|
67 event.diff.accept(new class() ListDiffVisitor {
|
|
68 public void handleAdd(int index, Object child) {
|
|
69 // adds to known elements if new element
|
|
70 getOrCreateNode(child).addParent(parentElement);
|
|
71
|
|
72 viewerUpdater.insert(parentElement, child, index);
|
|
73 }
|
|
74
|
|
75 public void handleRemove(int index, Object child) {
|
|
76 viewerUpdater.remove(parentElement, child, index);
|
|
77
|
|
78 removals.add(child);
|
|
79 }
|
|
80
|
|
81 public void handleReplace(int index, Object oldChild,
|
|
82 Object newChild) {
|
|
83 getOrCreateNode(newChild).addParent(parentElement);
|
|
84
|
|
85 viewerUpdater.replace(parentElement, oldChild,
|
|
86 newChild, index);
|
|
87
|
|
88 removals.add(oldChild);
|
|
89 }
|
|
90
|
|
91 public void handleMove(int oldIndex, int newIndex,
|
|
92 Object child) {
|
|
93 viewerUpdater.move(parentElement, child, oldIndex,
|
|
94 newIndex);
|
|
95 }
|
|
96 });
|
|
97
|
|
98 // For each removed element, do not remove node's parent if the
|
|
99 // element is still present elsewhere in the list.
|
|
100 removals.removeAll(event.getObservableList());
|
|
101 for (Iterator iterator = removals.iterator(); iterator
|
|
102 .hasNext();) {
|
|
103 TreeNode node = getExistingNode(iterator.next());
|
|
104 if (node !is null)
|
|
105 // removes from known elements if last parent
|
|
106 node.removeParent(parentElement);
|
|
107 }
|
|
108 }
|
|
109 }
|
|
110
|
|
111 protected IObservablesListener createCollectionChangeListener(
|
|
112 Object parentElement) {
|
|
113 return new ListChangeListener(parentElement);
|
|
114 }
|
|
115
|
|
116 protected void addCollectionChangeListener(
|
|
117 IObservableCollection collection, IObservablesListener listener) {
|
|
118 IObservableList list = cast(IObservableList) collection;
|
|
119 IListChangeListener listListener = cast(IListChangeListener) listener;
|
|
120 list.addListChangeListener(listListener);
|
|
121 }
|
|
122
|
|
123 protected void removeCollectionChangeListener(
|
|
124 IObservableCollection collection, IObservablesListener listener) {
|
|
125 IObservableList list = cast(IObservableList) collection;
|
|
126 IListChangeListener listListener = cast(IListChangeListener) listener;
|
|
127 list.removeListChangeListener(listListener);
|
|
128 }
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * Constructs an ObservableListTreeContentProvider using the given list
|
|
133 * factory.
|
|
134 *
|
|
135 * @param listFactory
|
|
136 * observable factory that produces an IObservableList of
|
|
137 * children for a given parent element. Observable lists created
|
|
138 * by this factory must be on the realm of the current display.
|
|
139 * @param structureAdvisor
|
|
140 * an advisor that will be consulted from the implementations of
|
|
141 * the {@link #getParent(Object)} and
|
|
142 * {@link #hasChildren(Object)} methods, or <code>null</code>
|
|
143 * if no advisor is available. It is recommended that clients
|
|
144 * pass a non-null advisor if they can provide additional
|
|
145 * structural information about the tree.
|
|
146 */
|
|
147 public this(IObservableFactory listFactory,
|
|
148 TreeStructureAdvisor structureAdvisor) {
|
|
149 impl = new Impl(listFactory, structureAdvisor);
|
|
150 }
|
|
151
|
|
152 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
|
153 impl.inputChanged(viewer, oldInput, newInput);
|
|
154 }
|
|
155
|
|
156 public Object[] getElements(Object inputElement) {
|
|
157 return impl.getElements(inputElement);
|
|
158 }
|
|
159
|
|
160 public bool hasChildren(Object element) {
|
|
161 return impl.hasChildren(element);
|
|
162 }
|
|
163
|
|
164 public Object[] getChildren(Object parentElement) {
|
|
165 return impl.getChildren(parentElement);
|
|
166 }
|
|
167
|
|
168 public Object getParent(Object element) {
|
|
169 return impl.getParent(element);
|
|
170 }
|
|
171
|
|
172 public void dispose() {
|
|
173 impl.dispose();
|
|
174 }
|
|
175
|
|
176 /**
|
|
177 * Returns the set of elements known to this content provider. Label
|
|
178 * providers may track this set if they need to be notified about additions
|
|
179 * before the viewer sees the added element, and notified about removals
|
|
180 * after the element was removed from the viewer. This is intended for use
|
|
181 * by label providers, as it will always return the items that need labels.
|
|
182 *
|
|
183 * @return readableSet of items that will need labels
|
|
184 */
|
|
185 public IObservableSet getKnownElements() {
|
|
186 return impl.getKnownElements();
|
|
187 }
|
|
188 }
|