78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 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.ObservableSetTreeContentProvider;
|
|
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.masterdetail.IObservableFactory;
|
|
23 import org.eclipse.core.databinding.observable.set.IObservableSet;
|
|
24 import org.eclipse.core.databinding.observable.set.ISetChangeListener;
|
|
25 import org.eclipse.core.databinding.observable.set.SetChangeEvent;
|
|
26 import org.eclipse.jface.internal.databinding.viewers.ObservableCollectionTreeContentProvider;
|
|
27 import org.eclipse.jface.viewers.AbstractTreeViewer;
|
|
28 import org.eclipse.jface.viewers.ITreeContentProvider;
|
|
29 import org.eclipse.jface.viewers.Viewer;
|
|
30
|
|
31 /**
|
|
32 * An {@link ITreeContentProvider} for use with an {@link AbstractTreeViewer},
|
|
33 * which uses the provided {@link IObservableFactory set factory} to obtain the
|
|
34 * elements of a tree. Objects of this class listen for changes to each
|
|
35 * {@link IObservableSet} created by the factory, and will insert and remove
|
|
36 * viewer elements to reflect the observed changes.
|
|
37 *
|
|
38 * <p>
|
|
39 * This class is not intended to be subclassed by clients.
|
|
40 *
|
|
41 * @since 1.2
|
|
42 */
|
|
43 public class ObservableSetTreeContentProvider : ITreeContentProvider {
|
|
44 private final ObservableCollectionTreeContentProvider impl;
|
|
45
|
|
46 private static class Impl : ObservableCollectionTreeContentProvider {
|
|
47 this(IObservableFactory setFactory,
|
|
48 TreeStructureAdvisor structureAdvisor) {
|
|
49 super(setFactory, structureAdvisor);
|
|
50 }
|
|
51
|
|
52 private class SetChangeListener : ISetChangeListener {
|
|
53 final Object parentElement;
|
|
54
|
|
55 public this(Object parentElement) {
|
|
56 this.parentElement = parentElement;
|
|
57 }
|
|
58
|
|
59 public void handleSetChange(SetChangeEvent event) {
|
|
60 if (isViewerDisposed())
|
|
61 return;
|
|
62
|
|
63 Set removals = event.diff.getRemovals();
|
|
64 if (!removals.isEmpty()) {
|
|
65 viewerUpdater.remove(parentElement, removals.toArray());
|
|
66 for (Iterator iterator = removals.iterator(); iterator
|
|
67 .hasNext();) {
|
|
68 Object child = iterator.next();
|
|
69 TreeNode childNode = getExistingNode(child);
|
|
70 if (childNode !is null)
|
|
71 childNode.removeParent(parentElement);
|
|
72 }
|
|
73 }
|
|
74
|
|
75 Set additions = event.diff.getAdditions();
|
|
76 if (!additions.isEmpty()) {
|
|
77 for (Iterator iterator = additions.iterator(); iterator
|
|
78 .hasNext();) {
|
|
79 Object child = iterator.next();
|
|
80 getOrCreateNode(child).addParent(parentElement);
|
|
81 }
|
|
82 viewerUpdater.add(parentElement, additions.toArray());
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86
|
|
87 protected IObservablesListener createCollectionChangeListener(
|
|
88 Object parentElement) {
|
|
89 return new SetChangeListener(parentElement);
|
|
90 }
|
|
91
|
|
92 protected void addCollectionChangeListener(
|
|
93 IObservableCollection collection, IObservablesListener listener) {
|
|
94 IObservableSet set = cast(IObservableSet) collection;
|
|
95 ISetChangeListener setListener = cast(ISetChangeListener) listener;
|
|
96 set.addSetChangeListener(setListener);
|
|
97 }
|
|
98
|
|
99 protected void removeCollectionChangeListener(
|
|
100 IObservableCollection collection, IObservablesListener listener) {
|
|
101 IObservableSet set = cast(IObservableSet) collection;
|
|
102 ISetChangeListener setListener = cast(ISetChangeListener) listener;
|
|
103 set.removeSetChangeListener(setListener);
|
|
104 }
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Constructs an ObservableListTreeContentProvider using the given list
|
|
109 * factory.
|
|
110 *
|
|
111 * @param setFactory
|
|
112 * observable factory that produces an IObservableSet of children
|
|
113 * for a given parent element. Observable sets created by this
|
|
114 * factory must be on the realm of the current display.
|
|
115 * @param structureAdvisor
|
|
116 * an advisor that will be consulted from the implementations of
|
|
117 * the {@link #getParent(Object)} and
|
|
118 * {@link #hasChildren(Object)} methods, or <code>null</code>
|
|
119 * if no advisor is available. It is recommended that clients
|
|
120 * pass a non-null advisor if they can provide additional
|
|
121 * structural information about the tree.
|
|
122 */
|
|
123 public this(IObservableFactory setFactory,
|
|
124 TreeStructureAdvisor structureAdvisor) {
|
|
125 impl = new Impl(setFactory, structureAdvisor);
|
|
126 }
|
|
127
|
|
128 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
|
129 impl.inputChanged(viewer, oldInput, newInput);
|
|
130 }
|
|
131
|
|
132 public Object[] getElements(Object inputElement) {
|
|
133 return impl.getElements(inputElement);
|
|
134 }
|
|
135
|
|
136 public bool hasChildren(Object element) {
|
|
137 return impl.hasChildren(element);
|
|
138 }
|
|
139
|
|
140 public Object[] getChildren(Object parentElement) {
|
|
141 return impl.getChildren(parentElement);
|
|
142 }
|
|
143
|
|
144 public Object getParent(Object element) {
|
|
145 return impl.getParent(element);
|
|
146 }
|
|
147
|
|
148 public void dispose() {
|
|
149 impl.dispose();
|
|
150 }
|
|
151
|
|
152 /**
|
|
153 * Returns the set of elements known to this content provider. Label
|
|
154 * providers may track this set if they need to be notified about additions
|
|
155 * before the viewer sees the added element, and notified about removals
|
|
156 * after the element was removed from the viewer. This is intended for use
|
|
157 * by label providers, as it will always return the items that need labels.
|
|
158 *
|
|
159 * @return readableSet of items that will need labels
|
|
160 */
|
|
161 public IObservableSet getKnownElements() {
|
|
162 return impl.getKnownElements();
|
|
163 }
|
|
164 }
|