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