1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2017 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.ui.dialog.selection;
|
11
|
|
12
|
|
13
|
|
14
|
import java.text.Collator;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.Collections;
|
17
|
import java.util.Comparator;
|
18
|
import java.util.List;
|
19
|
|
20
|
import org.eclipse.core.runtime.Assert;
|
21
|
import org.eclipse.core.runtime.CoreException;
|
22
|
import org.eclipse.core.runtime.IProgressMonitor;
|
23
|
import org.eclipse.core.runtime.IStatus;
|
24
|
import org.eclipse.core.runtime.ListenerList;
|
25
|
import org.eclipse.core.runtime.Status;
|
26
|
import org.eclipse.core.runtime.jobs.Job;
|
27
|
import org.eclipse.jface.action.LegacyActionTools;
|
28
|
import org.eclipse.jface.dialogs.Dialog;
|
29
|
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
|
30
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
31
|
import org.eclipse.jface.viewers.IColorProvider;
|
32
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
33
|
import org.eclipse.jface.viewers.IFontProvider;
|
34
|
import org.eclipse.jface.viewers.ILabelProvider;
|
35
|
import org.eclipse.jface.viewers.ILabelProviderListener;
|
36
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
37
|
import org.eclipse.jface.viewers.IStructuredContentProvider;
|
38
|
import org.eclipse.jface.viewers.LabelProvider;
|
39
|
import org.eclipse.jface.viewers.LabelProviderChangedEvent;
|
40
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
41
|
import org.eclipse.jface.viewers.StructuredSelection;
|
42
|
import org.eclipse.jface.viewers.StyledCellLabelProvider;
|
43
|
import org.eclipse.jface.viewers.StyledString;
|
44
|
import org.eclipse.jface.viewers.TableViewer;
|
45
|
import org.eclipse.jface.viewers.Viewer;
|
46
|
import org.eclipse.jface.viewers.ViewerCell;
|
47
|
import org.eclipse.swt.SWT;
|
48
|
import org.eclipse.swt.accessibility.AccessibleAdapter;
|
49
|
import org.eclipse.swt.accessibility.AccessibleEvent;
|
50
|
import org.eclipse.swt.events.KeyAdapter;
|
51
|
import org.eclipse.swt.events.KeyEvent;
|
52
|
import org.eclipse.swt.events.ModifyEvent;
|
53
|
import org.eclipse.swt.events.ModifyListener;
|
54
|
import org.eclipse.swt.events.TraverseEvent;
|
55
|
import org.eclipse.swt.events.TraverseListener;
|
56
|
import org.eclipse.swt.graphics.Color;
|
57
|
import org.eclipse.swt.graphics.Font;
|
58
|
import org.eclipse.swt.graphics.GC;
|
59
|
import org.eclipse.swt.graphics.Point;
|
60
|
import org.eclipse.swt.graphics.Rectangle;
|
61
|
import org.eclipse.swt.layout.GridData;
|
62
|
import org.eclipse.swt.layout.GridLayout;
|
63
|
import org.eclipse.swt.widgets.Button;
|
64
|
import org.eclipse.swt.widgets.Composite;
|
65
|
import org.eclipse.swt.widgets.Control;
|
66
|
import org.eclipse.swt.widgets.Display;
|
67
|
import org.eclipse.swt.widgets.Event;
|
68
|
import org.eclipse.swt.widgets.Label;
|
69
|
import org.eclipse.swt.widgets.Shell;
|
70
|
import org.eclipse.swt.widgets.Table;
|
71
|
import org.eclipse.swt.widgets.Text;
|
72
|
import org.eclipse.ui.IWorkbenchPreferenceConstants;
|
73
|
import org.eclipse.ui.PlatformUI;
|
74
|
import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
|
75
|
import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
|
76
|
import org.eclipse.ui.internal.WorkbenchImages;
|
77
|
import org.eclipse.ui.internal.WorkbenchMessages;
|
78
|
import org.eclipse.ui.internal.WorkbenchPlugin;
|
79
|
|
80
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
81
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
82
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
83
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.CdmFilteredItemsSelectionDialog.ItemsListSeparator;
|
84
|
|
85
|
/**
|
86
|
* @author k.luther
|
87
|
* @date 15.11.2017
|
88
|
*
|
89
|
*/
|
90
|
public abstract class SearchDialog<T extends ICdmBase> extends Dialog{// implements IConversationEnabled{
|
91
|
|
92
|
private TableViewer list;
|
93
|
private Text searchField;
|
94
|
private String title;
|
95
|
protected Button newButton1;
|
96
|
|
97
|
|
98
|
protected Button newButton2;
|
99
|
protected Button filterButton;
|
100
|
protected Button btnCheckButton;
|
101
|
private StructuredSelection currentSelection;
|
102
|
|
103
|
// message to show user
|
104
|
private String message = ""; //$NON-NLS-1$
|
105
|
|
106
|
protected final ContentProvider contentProvider;
|
107
|
ItemsListLabelProvider itemsListLabelProvider;
|
108
|
|
109
|
private final RefreshCacheJob refreshCacheJob;
|
110
|
|
111
|
protected Object preferenceID;
|
112
|
|
113
|
protected final int new_id = 4;
|
114
|
protected final int new_id2 = 5;
|
115
|
protected final int space_id = 6;
|
116
|
// Need to keep our own list of listeners
|
117
|
private final ListenerList listeners = new ListenerList();
|
118
|
|
119
|
|
120
|
private static final String EMPTY_STRING = ""; //$NON-NLS-1$
|
121
|
private GridData gd_1;
|
122
|
protected boolean useIdentifier;
|
123
|
|
124
|
public SearchDialog(Shell parent, String title) {
|
125
|
super(parent);
|
126
|
this.title = title;
|
127
|
contentProvider = new ContentProvider();
|
128
|
refreshCacheJob = new RefreshCacheJob();
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
protected void configureShell(Shell shell) {
|
133
|
super.configureShell(shell);
|
134
|
shell.setText(title);
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
protected Control createDialogArea(Composite parent) {
|
139
|
Composite container = (Composite) super.createDialogArea(parent);
|
140
|
|
141
|
GridData gd = new GridData(GridData.FILL_BOTH);
|
142
|
container.setLayoutData(gd);
|
143
|
|
144
|
GridLayout layout = new GridLayout();
|
145
|
layout.numColumns = 1;
|
146
|
layout.marginWidth = 0;
|
147
|
layout.marginHeight = 0;
|
148
|
container.setLayout(layout);
|
149
|
|
150
|
final Label headerLabel = createHeader(container);
|
151
|
|
152
|
Composite searchAndFilter = new Composite(container, container.getStyle());
|
153
|
|
154
|
searchAndFilter.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
|
155
|
GridLayout searchAndFilterLayout = new GridLayout();
|
156
|
searchAndFilterLayout.numColumns = 2;
|
157
|
searchAndFilter.setLayout(searchAndFilterLayout);
|
158
|
searchField = new Text(searchAndFilter, SWT.SINGLE | SWT.BORDER | SWT.SEARCH | SWT.ICON_CANCEL);
|
159
|
searchField.getAccessible().addAccessibleListener(new AccessibleAdapter() {
|
160
|
@Override
|
161
|
public void getName(AccessibleEvent e) {
|
162
|
e.result = LegacyActionTools.removeMnemonics(headerLabel
|
163
|
.getText());
|
164
|
}
|
165
|
});
|
166
|
searchField.addModifyListener(new ModifyListener() {
|
167
|
@Override
|
168
|
public void modifyText(ModifyEvent e) {
|
169
|
search();
|
170
|
// fillContentProvider(null);
|
171
|
|
172
|
}
|
173
|
});
|
174
|
gd_1 = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
|
175
|
gd_1.horizontalIndent = 7;
|
176
|
|
177
|
searchField.setLayoutData(gd_1);
|
178
|
|
179
|
createFilterButton(searchAndFilter);
|
180
|
|
181
|
addIdentifierCheckButton(searchAndFilter);
|
182
|
// new Label(searchAndFilter, SWT.NONE);
|
183
|
setList(new TableViewer(container, SWT.SINGLE
|
184
|
| SWT.BORDER | SWT.V_SCROLL | SWT.VIRTUAL));
|
185
|
|
186
|
getList().setContentProvider(contentProvider);
|
187
|
getList().setLabelProvider(getItemsListLabelProvider());
|
188
|
getList().setInput(new Object[0]);
|
189
|
getList().setItemCount(contentProvider.getNumberOfElements());
|
190
|
getList().addSelectionChangedListener(new ISelectionChangedListener() {
|
191
|
@Override
|
192
|
public void selectionChanged(SelectionChangedEvent event) {
|
193
|
StructuredSelection selection = (StructuredSelection) event
|
194
|
.getSelection();
|
195
|
currentSelection = selection;
|
196
|
}
|
197
|
});
|
198
|
getList().addDoubleClickListener(new IDoubleClickListener() {
|
199
|
@Override
|
200
|
public void doubleClick(DoubleClickEvent event) {
|
201
|
okPressed();
|
202
|
}
|
203
|
});
|
204
|
searchField.addKeyListener(new KeyAdapter() {
|
205
|
@Override
|
206
|
public void keyPressed(KeyEvent e) {
|
207
|
if (e.keyCode == SWT.ARROW_DOWN) {
|
208
|
if (getList().getTable().getItemCount() > 0) {
|
209
|
getList().getTable().setFocus();
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
});
|
214
|
|
215
|
// createExtendedContentArea(container);
|
216
|
new Label(container, SWT.NONE);
|
217
|
search();
|
218
|
|
219
|
return container;
|
220
|
}
|
221
|
|
222
|
|
223
|
protected void addIdentifierCheckButton(Composite searchAndFilter) {
|
224
|
//as default do nothing
|
225
|
}
|
226
|
|
227
|
|
228
|
abstract void createFilterButton(Composite searchAndFilter) ;
|
229
|
|
230
|
protected abstract void search();
|
231
|
|
232
|
|
233
|
/**
|
234
|
* Create a new header which is labelled by headerLabel.
|
235
|
*
|
236
|
* @param parent
|
237
|
* @return Label the label of the header
|
238
|
*/
|
239
|
private Label createHeader(Composite parent) {
|
240
|
Composite header = new Composite(parent, SWT.NONE);
|
241
|
GridData gd_header = new GridData(SWT.CENTER, SWT.CENTER, false, false, 2, 1);
|
242
|
gd_header.horizontalIndent = 5;
|
243
|
gd_header.widthHint = 575;
|
244
|
header.setLayoutData(gd_header);
|
245
|
|
246
|
GridLayout layout = new GridLayout();
|
247
|
layout.marginLeft = 5;
|
248
|
layout.horizontalSpacing = 0;
|
249
|
layout.verticalSpacing = 1;
|
250
|
layout.marginWidth = 0;
|
251
|
layout.marginHeight = 0;
|
252
|
header.setLayout(layout);
|
253
|
new Label(header, SWT.NONE);
|
254
|
Label headerLabel = new Label(header, SWT.NONE);
|
255
|
headerLabel.setText((getMessage() != null && getMessage().trim()
|
256
|
.length() > 0) ? getMessage()
|
257
|
: Messages.SearchDialog_patternLabel);
|
258
|
headerLabel.addTraverseListener(new TraverseListener() {
|
259
|
@Override
|
260
|
public void keyTraversed(TraverseEvent e) {
|
261
|
if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit) {
|
262
|
e.detail = SWT.TRAVERSE_NONE;
|
263
|
searchField.setFocus();
|
264
|
}
|
265
|
}
|
266
|
});
|
267
|
GridData gd_headerLabel = new GridData(GridData.FILL_HORIZONTAL);
|
268
|
gd_headerLabel.horizontalAlignment = SWT.LEFT;
|
269
|
gd_headerLabel.grabExcessHorizontalSpace = false;
|
270
|
gd_headerLabel.verticalAlignment = SWT.BOTTOM;
|
271
|
gd_headerLabel.minimumHeight = 10;
|
272
|
headerLabel.setLayoutData(gd_headerLabel);
|
273
|
return headerLabel;
|
274
|
}
|
275
|
|
276
|
protected String getMessage() {
|
277
|
return message;
|
278
|
}
|
279
|
|
280
|
protected void setMessage(String message){
|
281
|
this.message = message;
|
282
|
}
|
283
|
|
284
|
public Text getSearchField() {
|
285
|
return searchField;
|
286
|
}
|
287
|
|
288
|
|
289
|
public void setSearchField(Text searchField) {
|
290
|
this.searchField = searchField;
|
291
|
}
|
292
|
|
293
|
private ItemsListLabelProvider getItemsListLabelProvider() {
|
294
|
if (itemsListLabelProvider == null) {
|
295
|
itemsListLabelProvider = new ItemsListLabelProvider(
|
296
|
new LabelProvider());
|
297
|
}
|
298
|
return itemsListLabelProvider;
|
299
|
}
|
300
|
|
301
|
|
302
|
public TableViewer getList() {
|
303
|
return list;
|
304
|
}
|
305
|
|
306
|
public void setList(TableViewer list) {
|
307
|
this.list = list;
|
308
|
Table table = list.getTable();
|
309
|
GridData gd_table = new GridData(SWT.CENTER, SWT.CENTER, true, true, 2, 1);
|
310
|
gd_table.heightHint = 231;
|
311
|
gd_table.widthHint = 543;
|
312
|
table.setLayoutData(gd_table);
|
313
|
}
|
314
|
|
315
|
public Button getNewButton1() {
|
316
|
return newButton1;
|
317
|
}
|
318
|
|
319
|
|
320
|
public void setNewButton1(Button newButton1) {
|
321
|
this.newButton1 = newButton1;
|
322
|
}
|
323
|
|
324
|
|
325
|
public Button getNewButton2() {
|
326
|
return newButton2;
|
327
|
}
|
328
|
|
329
|
|
330
|
public void setNewButton2(Button newButton2) {
|
331
|
this.newButton2 = newButton2;
|
332
|
}
|
333
|
|
334
|
|
335
|
public Button getFilterButton() {
|
336
|
return filterButton;
|
337
|
}
|
338
|
|
339
|
|
340
|
public void setFilterButton(Button filterButton) {
|
341
|
this.filterButton = filterButton;
|
342
|
}
|
343
|
|
344
|
/**
|
345
|
* Sets a new label provider for items in the list. If the label provider
|
346
|
* also implements {@link
|
347
|
* org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider
|
348
|
* .IStyledLabelProvider}, the style text labels provided by it will be used
|
349
|
* provided that the corresponding preference is set.
|
350
|
*
|
351
|
* @see IWorkbenchPreferenceConstants#USE_COLORED_LABELS
|
352
|
*
|
353
|
* @param listLabelProvider
|
354
|
* the label provider for items in the list
|
355
|
*/
|
356
|
public void setListLabelProvider(ILabelProvider listLabelProvider) {
|
357
|
getItemsListLabelProvider().setProvider(listLabelProvider);
|
358
|
}
|
359
|
protected Comparator getItemsComparator() {
|
360
|
return new Comparator<UuidAndTitleCache>() {
|
361
|
@Override
|
362
|
public int compare(UuidAndTitleCache entity1,
|
363
|
UuidAndTitleCache entity2) {
|
364
|
Collator collator = Collator.getInstance();
|
365
|
if (entity1.getUuid().equals(entity2.getUuid())){
|
366
|
return 0;
|
367
|
}
|
368
|
int result = collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
|
369
|
if (result == 0){
|
370
|
result = entity1.getUuid().compareTo(entity2.getUuid());
|
371
|
}
|
372
|
return result;
|
373
|
}
|
374
|
};
|
375
|
}
|
376
|
|
377
|
|
378
|
class ContentProvider implements
|
379
|
IStructuredContentProvider {
|
380
|
|
381
|
private List items;
|
382
|
|
383
|
/**
|
384
|
* Creates new instance of <code>ContentProvider</code>.
|
385
|
*/
|
386
|
public ContentProvider() {
|
387
|
this.items = Collections.synchronizedList(new ArrayList(2048));
|
388
|
}
|
389
|
|
390
|
/**
|
391
|
* Removes all content items and resets progress message.
|
392
|
*/
|
393
|
public void reset() {
|
394
|
this.items.clear();
|
395
|
|
396
|
}
|
397
|
|
398
|
public void add(Object item) {
|
399
|
this.items.add(item);
|
400
|
}
|
401
|
|
402
|
/**
|
403
|
* Refresh dialog.
|
404
|
*/
|
405
|
public void refresh() {
|
406
|
scheduleRefresh();
|
407
|
}
|
408
|
|
409
|
/**
|
410
|
* Schedule refresh job.
|
411
|
*/
|
412
|
public void scheduleRefresh() {
|
413
|
refreshCacheJob.cancelAll();
|
414
|
refreshCacheJob.schedule();
|
415
|
}
|
416
|
|
417
|
/*
|
418
|
* (non-Javadoc)
|
419
|
*
|
420
|
* @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
|
421
|
*/
|
422
|
@Override
|
423
|
public Object[] getElements(Object inputElement) {
|
424
|
return items.toArray();
|
425
|
}
|
426
|
|
427
|
public int getNumberOfElements() {
|
428
|
return items.size();
|
429
|
}
|
430
|
|
431
|
/*
|
432
|
* (non-Javadoc)
|
433
|
*
|
434
|
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
|
435
|
*/
|
436
|
@Override
|
437
|
public void dispose() {
|
438
|
}
|
439
|
|
440
|
/*
|
441
|
* (non-Javadoc)
|
442
|
*
|
443
|
* @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
|
444
|
* java.lang.Object, java.lang.Object)
|
445
|
*/
|
446
|
@Override
|
447
|
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
448
|
}
|
449
|
|
450
|
|
451
|
}
|
452
|
|
453
|
|
454
|
public boolean isUseIdentifier() {
|
455
|
return useIdentifier;
|
456
|
}
|
457
|
|
458
|
|
459
|
public StructuredSelection getCurrentSelection() {
|
460
|
return currentSelection;
|
461
|
}
|
462
|
|
463
|
/**
|
464
|
* Fills the content provider with matching items.
|
465
|
*
|
466
|
* @param progressMonitor
|
467
|
* must be used to report search progress. The state of this
|
468
|
* progress monitor reflects the state of the filtering process.
|
469
|
* @throws CoreException
|
470
|
*/
|
471
|
protected abstract void fillContentProvider(IProgressMonitor progressMonitor) ;
|
472
|
|
473
|
|
474
|
/**
|
475
|
* Refreshes the dialog - has to be called in UI thread.
|
476
|
*/
|
477
|
public void refresh() {
|
478
|
if (getList() != null && !getList().getTable().isDisposed()) {
|
479
|
|
480
|
List lastRefreshSelection = ((StructuredSelection) getList()
|
481
|
.getSelection()).toList();
|
482
|
getList().getTable().deselectAll();
|
483
|
|
484
|
getList().setItemCount(contentProvider.getNumberOfElements());
|
485
|
getList().refresh();
|
486
|
|
487
|
if (getList().getTable().getItemCount() > 0) {
|
488
|
// preserve previous selection
|
489
|
if ( lastRefreshSelection != null
|
490
|
&& lastRefreshSelection.size() > 0) {
|
491
|
getList().setSelection(new StructuredSelection(
|
492
|
lastRefreshSelection));
|
493
|
} else {
|
494
|
|
495
|
getList().getTable().setSelection(0);
|
496
|
getList().getTable().notifyListeners(SWT.Selection, new Event());
|
497
|
}
|
498
|
} else {
|
499
|
getList().setSelection(StructuredSelection.EMPTY);
|
500
|
}
|
501
|
|
502
|
}
|
503
|
}
|
504
|
|
505
|
/**
|
506
|
* A job responsible for computing filtered items list presented using
|
507
|
* <code>RefreshJob</code>.
|
508
|
*
|
509
|
* @see FilteredItemsSelectionDialog.RefreshJob
|
510
|
*
|
511
|
*/
|
512
|
private class RefreshCacheJob extends Job {
|
513
|
|
514
|
public RefreshCacheJob() {
|
515
|
super(
|
516
|
WorkbenchMessages.FilteredItemsSelectionDialog_cacheRefreshJob);
|
517
|
setSystem(true);
|
518
|
}
|
519
|
|
520
|
/**
|
521
|
* Stops the job and all sub-jobs.
|
522
|
*/
|
523
|
public void cancelAll() {
|
524
|
cancel();
|
525
|
}
|
526
|
|
527
|
/*
|
528
|
* (non-Javadoc)
|
529
|
*
|
530
|
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
|
531
|
*/
|
532
|
@Override
|
533
|
protected IStatus run(IProgressMonitor monitor) {
|
534
|
if (monitor.isCanceled()) {
|
535
|
return new Status(IStatus.CANCEL, WorkbenchPlugin.PI_WORKBENCH,
|
536
|
IStatus.CANCEL, EMPTY_STRING, null);
|
537
|
}
|
538
|
|
539
|
if (SearchDialog.this != null) {
|
540
|
SearchDialog.this.fillContentProvider(monitor);
|
541
|
}
|
542
|
|
543
|
return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, IStatus.OK,
|
544
|
EMPTY_STRING, null);
|
545
|
|
546
|
}
|
547
|
|
548
|
/*
|
549
|
* (non-Javadoc)
|
550
|
*
|
551
|
* @see org.eclipse.core.runtime.jobs.Job#canceling()
|
552
|
*/
|
553
|
@Override
|
554
|
protected void canceling() {
|
555
|
super.canceling();
|
556
|
}
|
557
|
|
558
|
}
|
559
|
|
560
|
private class ItemsListLabelProvider extends StyledCellLabelProvider
|
561
|
implements ILabelProviderListener {
|
562
|
private ILabelProvider provider;
|
563
|
|
564
|
/**
|
565
|
* Creates a new instance of the class.
|
566
|
*
|
567
|
* @param provider
|
568
|
* the label provider for all items, not <code>null</code>
|
569
|
* @param selectionDecorator
|
570
|
* the decorator for selected items, can be <code>null</code>
|
571
|
*/
|
572
|
public ItemsListLabelProvider(ILabelProvider provider) {
|
573
|
Assert.isNotNull(provider);
|
574
|
this.provider = provider;
|
575
|
this.provider.addListener(this);
|
576
|
}
|
577
|
|
578
|
/**
|
579
|
* Sets new label provider.
|
580
|
*
|
581
|
* @param newProvider
|
582
|
* new label provider for items in the list, not
|
583
|
* <code>null</code>
|
584
|
*/
|
585
|
public void setProvider(ILabelProvider newProvider) {
|
586
|
Assert.isNotNull(newProvider);
|
587
|
provider.removeListener(this);
|
588
|
provider.dispose();
|
589
|
provider = newProvider;
|
590
|
|
591
|
if (provider != null) {
|
592
|
provider.addListener(this);
|
593
|
}
|
594
|
}
|
595
|
|
596
|
|
597
|
|
598
|
private boolean isSelected(Object element) {
|
599
|
if (element != null && getCurrentSelection() != null) {
|
600
|
if (element.equals(getCurrentSelection())) {
|
601
|
return true;
|
602
|
}
|
603
|
}
|
604
|
return false;
|
605
|
}
|
606
|
|
607
|
/*
|
608
|
* (non-Javadoc)
|
609
|
*
|
610
|
* @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
|
611
|
*/
|
612
|
private String getText(Object element) {
|
613
|
if (element instanceof ItemsListSeparator) {
|
614
|
return getSeparatorLabel(((ItemsListSeparator) element)
|
615
|
.getName());
|
616
|
}
|
617
|
String str = provider.getText(element);
|
618
|
return str;
|
619
|
}
|
620
|
|
621
|
private StyledString getStyledText(Object element,
|
622
|
IStyledLabelProvider provider) {
|
623
|
StyledString string = provider.getStyledText(element);
|
624
|
|
625
|
return string;
|
626
|
}
|
627
|
|
628
|
@Override
|
629
|
public void update(ViewerCell cell) {
|
630
|
Object element = cell.getElement();
|
631
|
|
632
|
if (!(element instanceof ItemsListSeparator)
|
633
|
&& provider instanceof IStyledLabelProvider) {
|
634
|
IStyledLabelProvider styledLabelProvider = (IStyledLabelProvider) provider;
|
635
|
StyledString styledString = getStyledText(element,
|
636
|
styledLabelProvider);
|
637
|
|
638
|
cell.setText(styledString.getString());
|
639
|
cell.setStyleRanges(styledString.getStyleRanges());
|
640
|
cell.setImage(styledLabelProvider.getImage(element));
|
641
|
} else {
|
642
|
cell.setText(getText(element));
|
643
|
|
644
|
}
|
645
|
cell.setFont(getFont(element));
|
646
|
cell.setForeground(getForeground(element));
|
647
|
cell.setBackground(getBackground(element));
|
648
|
|
649
|
super.update(cell);
|
650
|
}
|
651
|
|
652
|
private String getSeparatorLabel(String separatorLabel) {
|
653
|
Rectangle rect = getList().getTable().getBounds();
|
654
|
|
655
|
int borderWidth = getList().getTable().computeTrim(0, 0, 0, 0).width;
|
656
|
|
657
|
int imageWidth = WorkbenchImages.getImage(
|
658
|
IWorkbenchGraphicConstants.IMG_OBJ_SEPARATOR).getBounds().width;
|
659
|
|
660
|
int width = rect.width - borderWidth - imageWidth;
|
661
|
|
662
|
GC gc = new GC(getList().getTable());
|
663
|
gc.setFont(getList().getTable().getFont());
|
664
|
|
665
|
int fSeparatorWidth = gc.getAdvanceWidth('-');
|
666
|
int fMessageLength = gc.textExtent(separatorLabel).x;
|
667
|
|
668
|
gc.dispose();
|
669
|
|
670
|
StringBuffer dashes = new StringBuffer();
|
671
|
int chars = (((width - fMessageLength) / fSeparatorWidth) / 2) - 2;
|
672
|
for (int i = 0; i < chars; i++) {
|
673
|
dashes.append('-');
|
674
|
}
|
675
|
|
676
|
StringBuffer result = new StringBuffer();
|
677
|
result.append(dashes);
|
678
|
result.append(" " + separatorLabel + " "); //$NON-NLS-1$//$NON-NLS-2$
|
679
|
result.append(dashes);
|
680
|
return result.toString().trim();
|
681
|
}
|
682
|
|
683
|
/*
|
684
|
* (non-Javadoc)
|
685
|
*
|
686
|
* @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener)
|
687
|
*/
|
688
|
@Override
|
689
|
public void addListener(ILabelProviderListener listener) {
|
690
|
listeners.add(listener);
|
691
|
}
|
692
|
|
693
|
/*
|
694
|
* (non-Javadoc)
|
695
|
*
|
696
|
* @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
|
697
|
*/
|
698
|
@Override
|
699
|
public void dispose() {
|
700
|
provider.removeListener(this);
|
701
|
provider.dispose();
|
702
|
super.dispose();
|
703
|
}
|
704
|
|
705
|
/*
|
706
|
* (non-Javadoc)
|
707
|
*
|
708
|
* @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object,
|
709
|
* java.lang.String)
|
710
|
*/
|
711
|
@Override
|
712
|
public boolean isLabelProperty(Object element, String property) {
|
713
|
if (provider.isLabelProperty(element, property)) {
|
714
|
return true;
|
715
|
}
|
716
|
return false;
|
717
|
}
|
718
|
|
719
|
/*
|
720
|
* (non-Javadoc)
|
721
|
*
|
722
|
* @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener)
|
723
|
*/
|
724
|
@Override
|
725
|
public void removeListener(ILabelProviderListener listener) {
|
726
|
listeners.remove(listener);
|
727
|
}
|
728
|
|
729
|
private Color getBackground(Object element) {
|
730
|
if (element instanceof ItemsListSeparator) {
|
731
|
return null;
|
732
|
}
|
733
|
if (provider instanceof IColorProvider) {
|
734
|
return ((IColorProvider) provider).getBackground(element);
|
735
|
}
|
736
|
return null;
|
737
|
}
|
738
|
|
739
|
private Color getForeground(Object element) {
|
740
|
if (element instanceof ItemsListSeparator) {
|
741
|
return Display.getCurrent().getSystemColor(
|
742
|
SWT.COLOR_WIDGET_NORMAL_SHADOW);
|
743
|
}
|
744
|
if (provider instanceof IColorProvider) {
|
745
|
return ((IColorProvider) provider).getForeground(element);
|
746
|
}
|
747
|
return null;
|
748
|
}
|
749
|
|
750
|
private Font getFont(Object element) {
|
751
|
if (element instanceof ItemsListSeparator) {
|
752
|
return null;
|
753
|
}
|
754
|
if (provider instanceof IFontProvider) {
|
755
|
return ((IFontProvider) provider).getFont(element);
|
756
|
}
|
757
|
return null;
|
758
|
}
|
759
|
|
760
|
/*
|
761
|
* (non-Javadoc)
|
762
|
*
|
763
|
* @see org.eclipse.jface.viewers.ILabelProviderListener#labelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
|
764
|
*/
|
765
|
@Override
|
766
|
public void labelProviderChanged(LabelProviderChangedEvent event) {
|
767
|
Object[] l = listeners.getListeners();
|
768
|
for (int i = 0; i < listeners.size(); i++) {
|
769
|
((ILabelProviderListener) l[i]).labelProviderChanged(event);
|
770
|
}
|
771
|
}
|
772
|
}
|
773
|
|
774
|
@Override
|
775
|
protected Point getInitialSize() {
|
776
|
return new Point(593, 399);
|
777
|
}
|
778
|
|
779
|
|
780
|
|
781
|
}
|
782
|
|
783
|
|