-
Notifications
You must be signed in to change notification settings - Fork 96
/
GalleryTreeViewer.java
505 lines (454 loc) · 14.4 KB
/
GalleryTreeViewer.java
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
503
504
505
/*******************************************************************************
* Copyright (c) 2007-2008 Peter Centgraf.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors :
* Peter Centgraf - initial implementation
*******************************************************************************/
package org.eclipse.nebula.jface.galleryviewer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreePathContentProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.viewers.ViewerRow;
import org.eclipse.nebula.widgets.gallery.DefaultGalleryGroupRenderer;
import org.eclipse.nebula.widgets.gallery.DefaultGalleryItemRenderer;
import org.eclipse.nebula.widgets.gallery.Gallery;
import org.eclipse.nebula.widgets.gallery.GalleryItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Widget;
/**
* A concrete viewer based on a Nebula <code>Gallery</code> control.
* <p>
* This class is not intended to be subclassed outside the viewer framework. It
* is designed to be instantiated with a pre-existing Nebula Gallery control and
* configured with a domain-specific content provider, label provider, element
* filter (optional), and element sorter (optional).
* </p>
*
* <p>
* SWT.VIRTUAL is currently unsupported
* </p>
*
* <p>
* THIS VIEWER SHOULD BE CONSIDERED AS ALPHA
* </p>
*
* @author Peter Centgraf
* @contributor Nicolas Richeton
* @since Dec 5, 2007
*/
public class GalleryTreeViewer extends AbstractTreeViewer {
protected Gallery gallery;
/**
* true if we are inside a preservingSelection() call
*/
protected boolean preservingSelection;
/**
* The row object reused
*/
private GalleryViewerRow cachedRow;
/**
* Creates a gallery viewer on a newly-created gallery control under the
* given parent. The gallery control is created using the SWT style bits
* <code>MULTI, V_SCROLL,</code> and <code>BORDER</code>. The viewer has no
* input, no content provider, a default label provider, no sorter, and no
* filters.
*
* @param parent
* the parent control
*/
public GalleryTreeViewer(Composite parent) {
this(parent, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
}
/**
* Creates a gallery viewer on a newly-created gallery control under the
* given parent. The gallery control is created using the given SWT style
* bits. The viewer has no input, no content provider, a default label
* provider, no sorter, and no filters.
*
* @param parent
* the parent control
* @param style
* the SWT style bits used to create the gallery.
*/
public GalleryTreeViewer(Composite parent, int style) {
gallery = new Gallery(parent, style);
gallery.setGroupRenderer(new DefaultGalleryGroupRenderer());
gallery.setItemRenderer(new DefaultGalleryItemRenderer());
super.setAutoExpandLevel(ALL_LEVELS);
hookControl(gallery);
}
/**
* Creates a gallery viewer on the given gallery control. The viewer has no
* input, no content provider, a default label provider, no sorter, and no
* filters.
*
* @param gallery
* the gallery control
*/
public GalleryTreeViewer(Gallery gallery) {
super();
this.gallery = gallery;
super.setAutoExpandLevel(ALL_LEVELS);
hookControl(gallery);
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#addTreeListener(org.eclipse
* .swt.widgets.Control, org.eclipse.swt.events.TreeListener)
*/
protected void addTreeListener(Control control, TreeListener listener) {
((Gallery) control).addTreeListener(listener);
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getChild(org.eclipse.swt
* .widgets.Widget, int)
*/
protected Item getChild(Widget widget, int index) {
if (widget instanceof GalleryItem) {
return ((GalleryItem) widget).getItem(index);
}
if (widget instanceof Gallery) {
return ((Gallery) widget).getItem(index);
}
return null;
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getChildren(org.eclipse.
* swt.widgets.Widget)
*/
protected Item[] getChildren(Widget widget) {
if (widget instanceof GalleryItem) {
return ((GalleryItem) widget).getItems();
}
if (widget instanceof Gallery) {
return ((Gallery) widget).getItems();
}
return null;
}
protected Widget getColumnViewerOwner(int columnIndex) {
if (columnIndex == 0) {
return getGallery();
}
return null;
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getExpanded(org.eclipse.
* swt.widgets.Item)
*/
protected boolean getExpanded(Item item) {
return ((GalleryItem) item).isExpanded();
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getItemCount(org.eclipse
* .swt.widgets.Control)
*/
protected int getItemCount(Control control) {
return ((Gallery) control).getItemCount();
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getItemCount(org.eclipse
* .swt.widgets.Item)
*/
protected int getItemCount(Item item) {
return ((GalleryItem) item).getItemCount();
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getItems(org.eclipse.swt
* .widgets.Item)
*/
protected Item[] getItems(Item item) {
return ((GalleryItem) item).getItems();
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getParentItem(org.eclipse
* .swt.widgets.Item)
*/
protected Item getParentItem(Item item) {
return ((GalleryItem) item).getParentItem();
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#getSelection(org.eclipse
* .swt.widgets.Control)
*/
protected Item[] getSelection(Control control) {
Item[] selection = ((Gallery) control).getSelection();
if (selection == null) {
return new GalleryItem[0];
}
List<GalleryItem> notDisposed = new ArrayList<>(selection.length);
for (int i = 0; i < selection.length; i++) {
if (!selection[i].isDisposed()) {
notDisposed.add((GalleryItem) selection[i]);
} else {
System.out.println("GalleryItem was disposed (ignoring)");
}
}
selection = notDisposed.toArray(new GalleryItem[notDisposed.size()]);
return selection;
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#newItem(org.eclipse.swt.
* widgets.Widget, int, int)
*/
protected Item newItem(Widget parent, int style, int index) {
GalleryItem item;
if (parent instanceof GalleryItem) {
item = (GalleryItem) createNewRowPart(getViewerRowFromItem(parent),
style, index).getItem();
} else {
item = (GalleryItem) createNewRowPart(null, style, index).getItem();
}
return item;
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#removeAll(org.eclipse.swt
* .widgets.Control)
*/
protected void removeAll(Control control) {
((Gallery) control).removeAll();
}
public void setAutoExpandLevel(int level) {
throw new UnsupportedOperationException(
"Gallery must be fully expanded.");
}
/**
* <p>
* Gallery expects contents to have exactly 2 levels of hierarchy, with
* groups as the root elements and image thumbnails as direct children of
* the groups. This method accepts ITreeContentProvider and
* ITreePathContentProvider as-is, and relies on the providers to return
* contents with the correct structure.
* </p>
* <p>
* This method also accepts IStructuredContentProvider and wraps it in a
* FlatTreeContentProvider with an empty string as the root node. If you
* need a different root node, construct your own FlatTreeContentProvider
* and pass it here. If you want the Gallery to suppress the collapsable
* group header, call
* </p>
* <code>getGallery().setGroupRenderer(new NoGroupRenderer());</code>
*/
public void setContentProvider(IContentProvider provider) {
if (provider instanceof IStructuredContentProvider
&& !(provider instanceof ITreeContentProvider
|| provider instanceof ITreePathContentProvider)) {
// Wrap a table-style contents with a single root node.
super.setContentProvider(new FlatTreeContentProvider(
(IStructuredContentProvider) provider));
} else {
super.setContentProvider(provider);
}
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#setExpanded(org.eclipse.
* swt.widgets.Item, boolean)
*/
protected void setExpanded(Item item, boolean expand) {
((GalleryItem) item).setExpanded(expand);
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#setSelection(java.util.List)
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void setSelection(List items) {
Item[] current = getSelection(getGallery());
// Don't bother resetting the same selection
if (isSameSelection(items, current)) {
return;
}
GalleryItem[] newItems = new GalleryItem[items.size()];
items.toArray(newItems);
getGallery().setSelection(newItems);
}
/**
* @see org.eclipse.jface.viewers.AbstractTreeViewer#showItem(org.eclipse.swt
* .widgets.Item)
*/
protected void showItem(Item item) {
gallery.showItem((GalleryItem) item);
}
/**
* @see org.eclipse.jface.viewers.Viewer#getControl()
*/
public Control getControl() {
return gallery;
}
/**
* Returns this gallery viewer's gallery.
*
* @return the gallery control
*/
public Gallery getGallery() {
return gallery;
}
protected Item getItemAt(Point point) {
return gallery.getItem(point);
}
protected ColumnViewerEditor createViewerEditor() {
// TODO: implement editing support
return null;
}
/**
* For a GalleryViewer with a gallery with the VIRTUAL style bit set, set
* the number of children of the given element or tree path. To set the
* number of children of the invisible root of the gallery, you can pass the
* input object or an empty tree path.
*
* @param elementOrTreePath
* the element, or tree path
* @param count
*
* @since 3.2
*/
public void setChildCount(final Object elementOrTreePath, final int count) {
preservingSelection(() -> {
if (internalIsInputOrEmptyPath(elementOrTreePath)) {
getGallery().setItemCount(count);
return;
}
Widget[] items = internalFindItems(elementOrTreePath);
for (int i = 0; i < items.length; i++) {
GalleryItem galleryItem = (GalleryItem) items[i];
galleryItem.setItemCount(count);
}
});
}
/**
* @see org.eclipse.jface.viewers.ColumnViewer#getRowPartFromItem(org.eclipse
* .swt.widgets.Widget)
*/
protected ViewerRow getViewerRowFromItem(Widget item) {
if (cachedRow == null) {
cachedRow = new GalleryViewerRow((GalleryItem) item);
} else {
cachedRow.setItem((GalleryItem) item);
}
return cachedRow;
}
/**
* Create a new ViewerRow at rowIndex
*
* @param parent
* @param style
* @param rowIndex
* @return ViewerRow
*/
private ViewerRow createNewRowPart(ViewerRow parent, int style,
int rowIndex) {
if (parent == null) {
if (rowIndex >= 0) {
return getViewerRowFromItem(
new GalleryItem(gallery, style, rowIndex));
}
return getViewerRowFromItem(new GalleryItem(gallery, style));
}
if (rowIndex >= 0) {
return getViewerRowFromItem(new GalleryItem(
(GalleryItem) parent.getItem(), SWT.NONE, rowIndex));
}
return getViewerRowFromItem(
new GalleryItem((GalleryItem) parent.getItem(), SWT.NONE));
}
/**
* Removes the element at the specified index of the parent. The selection
* is updated if required.
*
* @param parentOrTreePath
* the parent element, the input element, or a tree path to the
* parent element
* @param index
* child index
* @since 3.3
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void remove(final Object parentOrTreePath, final int index) {
final List<TreeSelection> oldSelection = new LinkedList(
Arrays.asList(((TreeSelection) getSelection()).getPaths()));
preservingSelection(() -> {
TreePath removedPath = null;
if (internalIsInputOrEmptyPath(parentOrTreePath)) {
Gallery gallery = (Gallery) getControl();
if (index < gallery.getItemCount()) {
GalleryItem item = gallery.getItem(index);
if (item.getData() != null) {
removedPath = getTreePathFromItem(item);
disassociate(item);
}
item.dispose();
}
} else {
Widget[] parentItems = internalFindItems(parentOrTreePath);
for (int i = 0; i < parentItems.length; i++) {
GalleryItem parentItem = (GalleryItem) parentItems[i];
if (index < parentItem.getItemCount()) {
GalleryItem item = parentItem.getItem(index);
if (item.getData() != null) {
removedPath = getTreePathFromItem(item);
disassociate(item);
}
item.dispose();
}
}
}
if (removedPath != null) {
boolean removed = false;
for (Iterator it = oldSelection.iterator(); it.hasNext();) {
TreePath path = (TreePath) it.next();
if (path.startsWith(removedPath, getComparer())) {
it.remove();
removed = true;
}
}
if (removed) {
setSelection(new TreeSelection(
(TreePath[]) oldSelection
.toArray(new TreePath[oldSelection.size()]),
getComparer()), false);
}
}
});
}
public void editElement(Object element, int column) {
if (element instanceof TreePath) {
setSelection(new TreeSelection((TreePath) element));
GalleryItem[] items = gallery.getSelection();
if (items.length == 1) {
ViewerRow row = getViewerRowFromItem(items[0]);
if (row != null) {
ViewerCell cell = row.getCell(column);
if (cell != null) {
getControl().setRedraw(false);
triggerEditorActivationEvent(
new ColumnViewerEditorActivationEvent(cell));
getControl().setRedraw(true);
}
}
}
} else {
super.editElement(element, column);
}
}
}