View | Details | Raw Unified | Return to bug 38782 | Differences between
and this patch

Collapse All | Expand All

(-)AbstractTreeViewer.java (-66 / +61 lines)
Lines 12-24 Link Here
12
package org.eclipse.jface.viewers;
12
package org.eclipse.jface.viewers;
13
13
14
import java.util.ArrayList;
14
import java.util.ArrayList;
15
import java.util.Arrays;
16
import java.util.HashSet;
15
import java.util.HashSet;
17
import java.util.Iterator;
16
import java.util.Iterator;
18
import java.util.List;
17
import java.util.List;
19
18
20
import org.eclipse.core.runtime.Platform;
19
import org.eclipse.core.runtime.Platform;
21
20
import org.eclipse.jface.util.Assert;
21
import org.eclipse.jface.util.ListenerList;
22
import org.eclipse.jface.util.SafeRunnable;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.custom.BusyIndicator;
24
import org.eclipse.swt.custom.BusyIndicator;
24
import org.eclipse.swt.events.SelectionListener;
25
import org.eclipse.swt.events.SelectionListener;
Lines 28-37 Link Here
28
import org.eclipse.swt.widgets.Item;
29
import org.eclipse.swt.widgets.Item;
29
import org.eclipse.swt.widgets.Widget;
30
import org.eclipse.swt.widgets.Widget;
30
31
31
import org.eclipse.jface.util.Assert;
32
import org.eclipse.jface.util.ListenerList;
33
import org.eclipse.jface.util.SafeRunnable;
34
35
/**
32
/**
36
 * Abstract base implementation for tree-structure-oriented viewers
33
 * Abstract base implementation for tree-structure-oriented viewers
37
 * (trees and table trees).
34
 * (trees and table trees).
Lines 103-108 Link Here
103
	protected AbstractTreeViewer() {
100
	protected AbstractTreeViewer() {
104
	}
101
	}
105
	/**
102
	/**
103
		 * Return the current children of the parent element with the
104
		 * newChildren added in. This method
105
		 * does not invoke the content provider but rather asks the
106
		 * widget.
107
		 * @param parentElement
108
		 * @param newChildren Object[]. The children to be added to the
109
		 *  view.
110
		 * @return Collection
111
		 */
112
	private Object[] getMergedChildren(
113
		Widget parentElement,
114
		Object[] newChildren) {
115
		Item[] items = getChildren(parentElement);
116
		Object[] result = new Object[items.length + newChildren.length];
117
		for (int i = 0; i < items.length; i++) {
118
			result[i] = items[i].getData();
119
		}
120
121
		System.arraycopy(
122
			newChildren,
123
			0,
124
			result,
125
			items.length,
126
			newChildren.length);
127
		return result;
128
	}
129
130
	/**
131
	 * Return the index of element in the array elements.
132
	 * @param element
133
	 * @param elements
134
	 */
135
	private int indexOf(Object element, Object[] elements) {
136
		for (int i = 0; i < elements.length; i++) {
137
			if (elements[i].equals(element))
138
				return i;
139
		}
140
		return -1;
141
	}
142
	/**
106
	 * Adds the given child elements to this viewer as children of the given parent element.
143
	 * Adds the given child elements to this viewer as children of the given parent element.
107
	 * If this viewer does not have a sorter, the elements are added at the end of the 
144
	 * If this viewer does not have a sorter, the elements are added at the end of the 
108
	 * parent's list of children in the order given; otherwise, the elements are inserted
145
	 * parent's list of children in the order given; otherwise, the elements are inserted
Lines 124-190 Link Here
124
		if (widget == null)
161
		if (widget == null)
125
			return;
162
			return;
126
163
127
		Control tree = getControl();
164
		Object[] newElements;
165
		//If it is not an item we have no children to worry about
166
		if (widget instanceof Item)
167
			newElements =
168
				filter(getMergedChildren((Item) widget, childElements));
169
		else
170
			newElements = childElements;
171
172
		getSorter().sort(this, newElements);
173
174
		Item[] ch = getChildren(widget);
175
		for (int i = 0; i < childElements.length; i++) {
176
177
			int ix = indexOf(childElements[i], newElements);
178
			if (ix < 0) // child not found: ignore
179
				continue;
128
180
129
		// optimization!
181
			createTreeItem(widget, childElements[i], ix);
130
		// if the widget is not expanded we just invalidate the subtree
182
			continue;
131
		if (widget instanceof Item) {
132
			Item ti = (Item) widget;
133
			if (!getExpanded(ti)) {
134
				boolean needDummy = isExpandable(parentElement);
135
				boolean haveDummy = false;
136
				// remove all children
137
				Item[] items = getItems(ti);
138
				for (int i = 0; i < items.length; i++) {
139
					if (items[i].getData() != null) {
140
						disassociate(items[i]);
141
						items[i].dispose();
142
					} else {
143
						if (needDummy && !haveDummy) {
144
							haveDummy = true;
145
						} else {
146
							items[i].dispose();
147
						}
148
					}
149
				}
150
				// append a dummy if necessary
151
				if (needDummy && !haveDummy) {
152
					newItem(ti, SWT.NULL, -1);
153
				} else {
154
					// XXX: Workaround (PR missing)
155
					tree.redraw();
156
				}
157
158
				return;
159
			}
160
		}
161
162
		if (childElements.length > 0) {
163
			List children = Arrays.asList(getSortedChildren(parentElement));
164
			for (int cc = 0; cc < childElements.length; cc++) {
165
166
				int ix = children.indexOf(childElements[cc]);
167
				if (ix < 0) // child not found: ignore
168
					continue;
169
170
				Item[] ch = getChildren(widget);
171
				if (ch.length + 1 == children.size()) {
172
					createTreeItem(widget, childElements[cc], ix);
173
					// insert child at position
174
					if (ch.length == 0) {
175
						//System.out.println("WORKAROUND setRedraw");
176
						tree.setRedraw(false); // WORKAROUND
177
						tree.setRedraw(true); // WORKAROUND
178
					}
179
					continue;
180
				}
181
182
				// couldn't handle this child:
183
				// skip other children and do general case
184
				// call refresh rather than internalRefresh to preserve selection
185
				refresh(parentElement);
186
				return;
187
			}
188
		}
183
		}
189
	}
184
	}
190
	/**
185
	/**

Return to bug 38782