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.internal.databinding.viewers.TreeViewerUpdater;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17 import org.eclipse.jface.util.Util;
|
|
18 import org.eclipse.jface.viewers.AbstractTreeViewer;
|
|
19 import org.eclipse.jface.viewers.IElementComparer;
|
|
20 import org.eclipse.jface.viewers.ITreeSelection;
|
|
21 import org.eclipse.jface.viewers.TreePath;
|
|
22 import org.eclipse.jface.viewers.TreeViewer;
|
|
23
|
|
24 /**
|
|
25 * NON-API - An interface for sending updates to an {@link AbstractTreeViewer}.
|
|
26 *
|
|
27 * @since 1.2
|
|
28 */
|
|
29 public class TreeViewerUpdater {
|
|
30 private final AbstractTreeViewer viewer;
|
|
31 private final TreeViewer treeViewer;
|
|
32
|
|
33 /**
|
|
34 * Constructs an ITreeViewerUpdater for updating the given viewer.
|
|
35 *
|
|
36 * @param viewer
|
|
37 * the viewer that will be updated
|
|
38 */
|
|
39 public this(AbstractTreeViewer viewer) {
|
|
40 this.viewer = viewer;
|
|
41 if (null !is cast(TreeViewer)viewer)
|
|
42 treeViewer = cast(TreeViewer) viewer;
|
|
43 else
|
|
44 treeViewer = null;
|
|
45 }
|
|
46
|
|
47 /**
|
|
48 * Insert the element into the viewer as a child of the specified parent
|
|
49 * element, at the specified position.
|
|
50 *
|
|
51 * @param parent
|
|
52 * the parent of the element being inserted
|
|
53 * @param element
|
|
54 * the element to insert
|
|
55 * @param position
|
|
56 * the position where the element is inserted
|
|
57 */
|
|
58 public void insert(Object parent, Object element, int position) {
|
|
59 viewer.insert(parent, element, position);
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Replaces the specified element whenever it appears as a child of the
|
|
64 * specified parent element, at the given position with the new element.
|
|
65 *
|
|
66 * @param parent
|
|
67 * the parent of the element being replaced
|
|
68 * @param oldElement
|
|
69 * the element being replaced
|
|
70 * @param newElement
|
|
71 * the element that replaces <code>oldElement</code>
|
|
72 * @param position
|
|
73 * the position of the element being replaced.
|
|
74 */
|
|
75 public void replace(Object parent, Object oldElement, Object newElement,
|
|
76 int position) {
|
|
77 if (treeViewer !is null && viewer.getComparator() is null
|
|
78 && viewer.getFilters().length is 0) {
|
|
79 treeViewer.replace(parent, position, newElement);
|
|
80 } else {
|
|
81 remove(parent, oldElement, position);
|
|
82 insert(parent, newElement, position);
|
|
83 }
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * Moves the specified element from the specified old position to the
|
|
88 * specified new position, whenever it appears as a child of the specified
|
|
89 * parent element. No action is taken if the viewer has a sorter or
|
|
90 * filter(s).
|
|
91 *
|
|
92 * @param parent
|
|
93 * the parent of the element being moved
|
|
94 * @param element
|
|
95 * the element being moved
|
|
96 * @param oldPosition
|
|
97 * the position of the element before it is moved
|
|
98 * @param newPosition
|
|
99 * the position of the element after it is moved
|
|
100 */
|
|
101 public void move(Object parent, Object element, int oldPosition,
|
|
102 int newPosition) {
|
|
103 if (viewer.getComparator() is null && viewer.getFilters().length is 0) {
|
|
104
|
|
105 ITreeSelection selection = cast(ITreeSelection) viewer.getSelection();
|
|
106
|
|
107 remove(parent, element, oldPosition);
|
|
108 insert(parent, element, newPosition);
|
|
109
|
|
110 // Preserve selection
|
|
111 if (!selection.isEmpty()) {
|
|
112 // If the moved element is selected (or an ancestor of a
|
|
113 // selected element), restore the selection.
|
|
114 IElementComparer comparer = viewer.getComparer();
|
|
115 TreePath[] paths = selection.getPaths();
|
|
116 outer: for (int i = 0; i < paths.length; i++) {
|
|
117 TreePath path = paths[i];
|
|
118 for (int j = 0; j < path.getSegmentCount(); j++) {
|
|
119 Object pathElement = path.getSegment(j);
|
|
120 if (comparer is null ? Util
|
|
121 .equals(element, pathElement) : comparer
|
|
122 .equals(element, pathElement)) {
|
|
123 viewer.setSelection(selection);
|
|
124 break outer;
|
|
125 }
|
|
126 }
|
|
127 }
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131
|
|
132 /**
|
|
133 * Removes the element from the from whenever it appears as a child of the
|
|
134 * specified parent element, at the specified position.
|
|
135 *
|
|
136 * @param parent
|
|
137 * the parent of the element being removed
|
|
138 * @param element
|
|
139 * the element to remove
|
|
140 * @param position
|
|
141 * the position where the element is located
|
|
142 */
|
|
143 public void remove(Object parent, Object element, int position) {
|
|
144 if (treeViewer !is null && viewer.getComparator() is null
|
|
145 && viewer.getFilters().length is 0) {
|
|
146 // Only TreeViewer has a remove-by-index method.
|
|
147 treeViewer.remove(parent, position);
|
|
148 } else {
|
|
149 viewer.remove(parent, [ element ]);
|
|
150 }
|
|
151 }
|
|
152
|
|
153 /**
|
|
154 * Add the elements into the viewer as children of the specified parent
|
|
155 * element.
|
|
156 *
|
|
157 * @param parent
|
|
158 * the parent of the element being inserted
|
|
159 * @param elements
|
|
160 * the elements to insert
|
|
161 */
|
|
162 public void add(Object parent, Object[] elements) {
|
|
163 viewer.add(parent, elements);
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * Remove the elements from the viewer wherever they appear as children of
|
|
168 * the specified parent element.
|
|
169 *
|
|
170 * @param parent
|
|
171 * the parent of the elements being removed
|
|
172 * @param elements
|
|
173 * the elements to remove
|
|
174 */
|
|
175 public void remove(Object parent, Object[] elements) {
|
|
176 viewer.remove(parent, elements);
|
|
177 }
|
|
178 }
|