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