1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.ui.combo;
|
10
|
|
11
|
import org.eclipse.jface.viewers.ComboViewer;
|
12
|
import org.eclipse.jface.viewers.ILabelProvider;
|
13
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
14
|
import org.eclipse.jface.viewers.IStructuredContentProvider;
|
15
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
16
|
import org.eclipse.swt.SWT;
|
17
|
import org.eclipse.swt.layout.FillLayout;
|
18
|
import org.eclipse.swt.widgets.Combo;
|
19
|
import org.eclipse.swt.widgets.Composite;
|
20
|
|
21
|
/**
|
22
|
*
|
23
|
* Abstract generic combo.
|
24
|
*
|
25
|
* @author pplitzner
|
26
|
* @since Apr 10, 2018
|
27
|
*
|
28
|
* @param <T> the type of the elements to show in the combo
|
29
|
*/
|
30
|
public abstract class AbstractComboComposite<T> extends Composite{
|
31
|
|
32
|
private ComboViewer viewer;
|
33
|
|
34
|
public AbstractComboComposite(Composite parent, int style){
|
35
|
super(parent, style);
|
36
|
setLayout(new FillLayout());
|
37
|
viewer = new ComboViewer(new Combo(this, SWT.READ_ONLY | SWT.SINGLE));
|
38
|
viewer.setContentProvider(getContentProvider());
|
39
|
viewer.setLabelProvider(getLabelProvider());
|
40
|
}
|
41
|
|
42
|
public void setInput(Object input){
|
43
|
viewer.setInput(input);
|
44
|
}
|
45
|
|
46
|
public T getSelection(){
|
47
|
IStructuredSelection structuredSelection = viewer.getStructuredSelection();
|
48
|
if(structuredSelection!=null && !structuredSelection.isEmpty()){
|
49
|
return (T) structuredSelection.getFirstElement();
|
50
|
}
|
51
|
return null;
|
52
|
}
|
53
|
|
54
|
public void setElement(T element){
|
55
|
if(element==null){
|
56
|
viewer.getCombo().deselectAll();
|
57
|
}
|
58
|
int index = viewer.getCombo().indexOf(getElementLabel(element));
|
59
|
viewer.getCombo().select(index);
|
60
|
}
|
61
|
|
62
|
|
63
|
public void addSelectionChangedListener(ISelectionChangedListener listener){
|
64
|
viewer.addSelectionChangedListener(listener);
|
65
|
}
|
66
|
|
67
|
public abstract String getElementLabel(T element);
|
68
|
public abstract IStructuredContentProvider getContentProvider();
|
69
|
public abstract ILabelProvider getLabelProvider();
|
70
|
|
71
|
}
|