Skip to main content
summaryrefslogtreecommitdiffstats
blob: c68ac0ede736a6142663e7b3e588932403ff35dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*******************************************************************************
 * Copyright (c) 2005, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.internal.p2.ui.discovery.util;

import java.util.*;
import java.util.List;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

/**
 * Based on {@link org.eclipse.ui.internal.progress.DetailedProgressViewer}.
 * 
 * @author Steffen Pingel
 */
@SuppressWarnings("restriction")
public abstract class ControlListViewer extends StructuredViewer {

	Composite control;

	private final ScrolledComposite scrolled;

	private final Composite noEntryArea;

	protected boolean hasFocus;

	/**
	 * Create a new instance of the receiver with a control that is a child of parent with style style.
	 * 
	 * @param parent
	 * @param style
	 */
	public ControlListViewer(Composite parent, int style) {
		scrolled = new ScrolledComposite(parent, style | SWT.VERTICAL);
		int height = JFaceResources.getDefaultFont().getFontData()[0].getHeight();
		scrolled.getVerticalBar().setIncrement(height * 2);
		scrolled.setExpandHorizontal(true);
		scrolled.setExpandVertical(true);
		// bug 311276: can cause unintended scrolling of viewer
		//scrolled.setShowFocusedControl(true);

		control = new Composite(scrolled, SWT.NONE) {
			//			@Override
			//			public boolean setFocus() {
			//				forceFocus();
			//				return true;
			//			}

			@Override
			public void setVisible(boolean visible) {
				super.setVisible(visible);
				if (visible) {
					updateSize(control);
				}
			}
		};
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		control.setLayout(layout);
		control.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		control.setBackgroundMode(SWT.INHERIT_FORCE);
		control.addControlListener(new ControlListener() {
			public void controlMoved(ControlEvent e) {
				updateVisibleItems();
			}

			public void controlResized(ControlEvent e) {
				updateVisibleItems();
			}
		});

		scrolled.setContent(control);
		hookControl(control);

		noEntryArea = new Composite(scrolled, SWT.NONE);
		doCreateNoEntryArea(noEntryArea);

		scrolled.setExpandHorizontal(true);
		scrolled.setExpandVertical(true);
		scrolled.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				updateSize(scrolled.getContent());
			}
		});
		control.addTraverseListener(new TraverseListener() {
			private boolean handleEvent = true;

			public void keyTraversed(TraverseEvent event) {
				if (!handleEvent) {
					return;
				}
				switch (event.detail) {
					case SWT.TRAVERSE_ARROW_PREVIOUS : {
						Control[] children = control.getChildren();
						if (children.length > 0) {
							boolean selected = false;
							for (int i = 0; i < children.length; i++) {
								ControlListItem<?> item = (ControlListItem<?>) children[i];
								if (item.isSelected()) {
									selected = true;
									if (i > 0) {
										setSelection(new StructuredSelection(children[i - 1].getData()), true);
									}
									break;
								}
							}
							if (!selected) {
								setSelection(new StructuredSelection(children[children.length - 1].getData()), true);
							}
						}
						break;
					}
					case SWT.TRAVERSE_ARROW_NEXT : {
						Control[] children = control.getChildren();
						if (children.length > 0) {
							boolean selected = false;
							for (int i = 0; i < children.length; i++) {
								ControlListItem<?> item = (ControlListItem<?>) children[i];
								if (item.isSelected()) {
									selected = true;
									if (i < children.length - 1) {
										setSelection(new StructuredSelection(children[i + 1].getData()), true);
									}
									break;
								}
							}
							if (!selected) {
								setSelection(new StructuredSelection(children[0].getData()), true);
							}
						}
						break;
					}
					default :
						handleEvent = false;
						event.doit = true;
						Control control = ControlListViewer.this.control;
						Shell shell = control.getShell();
						while (control != null) {
							if (control.traverse(event.detail)) {
								break;
							}
							if (!event.doit || control == shell) {
								break;
							}
							control = control.getParent();
						}
						handleEvent = true;
						break;
				}
			}
		});
	}

	protected void doCreateNoEntryArea(Composite parent) {
	}

	public void add(Object[] elements) {
		ViewerComparator sorter = getComparator();

		// Use a Set in case we are getting something added that exists
		Set<Object> newItems = new HashSet<Object>(elements.length);

		Control[] existingChildren = control.getChildren();
		for (Control element : existingChildren) {
			if (element.getData() != null) {
				newItems.add(element.getData());
			}
		}

		for (Object element : elements) {
			if (element != null) {
				newItems.add(element);
			}
		}

		Object[] infos = new Object[newItems.size()];
		newItems.toArray(infos);

		if (sorter != null) {
			sorter.sort(this, infos);
		}

		// Update with the new elements to prevent flash
		for (Control element : existingChildren) {
			((ControlListItem<?>) element).dispose();
		}

		for (int i = 0; i < infos.length; i++) {
			ControlListItem<?> item = createNewItem(infos[i]);
			item.updateColors(i);
		}

		control.layout(true);
		doUpdateContent();
	}

	private void updateSize(Control c) {
		if (c == null) {
			return;
		}
		// XXX need a small offset in case the list has a scroll bar
		Point size = c.computeSize(scrolled.getClientArea().width - 20, SWT.DEFAULT, true);
		c.setSize(size);
		scrolled.setMinSize(size);
	}

	protected void doUpdateContent() {
		if (control.getChildren().length > 0) {
			updateSize(control);
			scrolled.setContent(control);
		} else {
			updateSize(noEntryArea);
			scrolled.setContent(noEntryArea);
		}
	}

	/**
	 * Create a new item for info.
	 * 
	 * @param element
	 * @return ControlListItem
	 */
	private ControlListItem<?> createNewItem(Object element) {
		final ControlListItem<?> item = doCreateItem(control, element);
		//		item.getChildren()[0].addPaintListener(new PaintListener() {
		//			public void paintControl(PaintEvent e) {
		//				if (hasFocus && item.isSelected()) {
		//					Point size = item.getSize();
		//					e.gc.setForeground(e.gc.getDevice().getSystemColor(SWT.COLOR_DARK_GRAY));
		//					e.gc.setLineDash(new int[] { 1, 2 });
		//					e.gc.drawRoundRectangle(0, 0, size.x - 1, size.y - 1, 5, 5);
		//				}
		//			}
		//		});
		item.setIndexListener(new ControlListItem.IndexListener() {
			public void selectNext() {
				Control[] children = control.getChildren();
				for (int i = 0; i < children.length; i++) {
					if (item == children[i]) {
						if (i < children.length - 1) {
							setSelection(new StructuredSelection(children[i + 1].getData()));
						}
						break;
					}
				}
			}

			public void selectPrevious() {
				Control[] children = control.getChildren();
				for (int i = 0; i < children.length; i++) {
					if (item == children[i]) {
						if (i > 0) {
							setSelection(new StructuredSelection(children[i - 1].getData()));
						}
						break;
					}
				}
			}

			public void select() {
				setSelection(new StructuredSelection(item.getData()));
				setFocus();
			}

			public void open() {
				handleOpen();
			}
		});
		GridDataFactory.fillDefaults().grab(true, false).applyTo(item);
		// Refresh to populate with the current tasks
		item.refresh();
		return item;
	}

	protected abstract ControlListItem<?> doCreateItem(Composite parent, Object element);

	@Override
	protected ControlListItem<?> doFindInputItem(Object element) {
		return null;
	}

	@Override
	protected ControlListItem<?> doFindItem(Object element) {
		Control[] children = control.getChildren();
		for (Control child : children) {
			if (child.isDisposed() || child.getData() == null) {
				continue;
			}
			if (child.getData().equals(element)) {
				return (ControlListItem<?>) child;
			}
		}
		return null;
	}

	@Override
	protected void doUpdateItem(Widget item, Object element, boolean fullMap) {
		if (usingElementMap()) {
			unmapElement(item);
		}
		item.dispose();
		add(new Object[] {element});
	}

	@Override
	public ScrolledComposite getControl() {
		return scrolled;
	}

	@Override
	protected List<?> getSelectionFromWidget() {
		Control[] children = control.getChildren();
		ArrayList<Object> selection = new ArrayList<Object>(children.length);
		for (Control child : children) {
			ControlListItem<?> item = (ControlListItem<?>) child;
			if (item.isSelected() && item.getData() != null) {
				selection.add(item.getData());
			}
		}
		return selection;
	}

	protected void handleOpen() {
		Control c = getControl();
		if (c != null && !c.isDisposed()) {
			ISelection selection = getSelection();
			fireOpen(new OpenEvent(this, selection));
		}
	}

	@Override
	protected void inputChanged(Object input, Object oldInput) {
		super.inputChanged(input, oldInput);
		refreshAll();
		doUpdateContent();
	}

	@Override
	protected void internalRefresh(Object element) {
		if (element == null) {
			return;
		}

		if (element.equals(getRoot())) {
			refreshAll();
			return;
		}
		Widget widget = findItem(element);
		if (widget == null) {
			add(new Object[] {element});
			return;
		}
		((ControlListItem<?>) widget).refresh();

		updateSize(control);
	}

	public void remove(Object[] elements) {
		for (Object element : elements) {
			Widget item = doFindItem(element);
			if (item != null) {
				unmapElement(element);
				item.dispose();
			}
		}

		Control[] existingChildren = control.getChildren();
		for (int i = 0; i < existingChildren.length; i++) {
			ControlListItem<?> item = (ControlListItem<?>) existingChildren[i];
			item.updateColors(i);
		}
		control.layout(true);
		doUpdateContent();
	}

	@Override
	public void reveal(Object element) {
		Control control = doFindItem(element);
		if (control != null) {
			revealControl(control);
		}
	}

	private void revealControl(Control control) {
		Rectangle clientArea = scrolled.getClientArea();
		Point origin = scrolled.getOrigin();
		Point location = control.getLocation();
		Point size = control.getSize();
		if (location.y + size.y > origin.y + clientArea.height) {
			scrolled.setOrigin(origin.x, location.y + size.y - clientArea.height);
		}
		if (location.y < origin.y) {
			scrolled.setOrigin(origin.x, location.y);
		}
	}

	@SuppressWarnings({"rawtypes"})
	@Override
	protected void setSelectionToWidget(List list, boolean reveal) {
		if (list != null) {
			//see bug 423628. This should be suppressed as:
			//1. it will cause warning at build time.
			//2. should be possible to fix once swt/jface enables generics for good.
			HashSet<Object> elements = new HashSet<Object>(list);
			Control[] children = control.getChildren();
			for (Control c : children) {
				ControlListItem child = (ControlListItem) c;
				boolean selected = elements.contains(child.getData());
				if (selected != child.isSelected()) {
					child.setSelected(selected);
				}
				if (reveal && selected) {
					revealControl(child);
					reveal = false;
				}
			}
		} else {
			Control[] children = control.getChildren();
			for (Control c : children) {
				ControlListItem child = (ControlListItem) c;
				if (child.isSelected()) {
					child.setSelected(false);
				}
			}
		}
	}

	/**
	 * Set focus on the current selection.
	 */
	public void setFocus() {
		Control[] children = control.getChildren();
		if (children.length > 0) {
			// causes the item's tool bar to get focus when clicked which is undesirable 
			//			for (Control element : children) {
			//				ControlListItem item = (ControlListItem) element;
			//				if (item.isSelected()) {
			//					if (item.setFocus()) {
			//						return;
			//					}
			//				}
			//			}
			control.forceFocus();
		} else {
			noEntryArea.setFocus();
		}
	}

	/**
	 * Refresh everything as the root is being refreshed.
	 */
	private void refreshAll() {
		Object[] infos = getSortedChildren(getRoot());
		Control[] existingChildren = control.getChildren();

		for (Control element : existingChildren) {
			element.dispose();
		}

		for (int i = 0; i < infos.length; i++) {
			ControlListItem<?> item = createNewItem(infos[i]);
			item.updateColors(i);
		}

		control.layout(true);
		doUpdateContent();
	}

	/**
	 * Set the virtual items to be visible or not depending on the displayed area.
	 */
	private void updateVisibleItems() {
		Control[] children = control.getChildren();
		int top = scrolled.getOrigin().y;
		int bottom = top + scrolled.getParent().getBounds().height;
		for (Control element : children) {
			ControlListItem<?> item = (ControlListItem<?>) element;
			item.setDisplayed(top, bottom);
		}
	}

}

Back to the top