1
|
/**
|
2
|
* Copyright (C) 2016 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 java.util.ArrayList;
|
12
|
import java.util.Collections;
|
13
|
|
14
|
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
|
15
|
import org.eclipse.jface.fieldassist.ComboContentAdapter;
|
16
|
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
|
17
|
import org.eclipse.jface.fieldassist.IContentProposal;
|
18
|
import org.eclipse.jface.fieldassist.IContentProposalListener;
|
19
|
import org.eclipse.jface.fieldassist.IContentProposalProvider;
|
20
|
import org.eclipse.swt.SWT;
|
21
|
import org.eclipse.swt.events.DisposeEvent;
|
22
|
import org.eclipse.swt.events.DisposeListener;
|
23
|
import org.eclipse.swt.events.SelectionEvent;
|
24
|
import org.eclipse.swt.events.SelectionListener;
|
25
|
import org.eclipse.swt.graphics.Color;
|
26
|
import org.eclipse.swt.widgets.Combo;
|
27
|
import org.eclipse.swt.widgets.Label;
|
28
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
29
|
|
30
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
31
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
32
|
import eu.etaxonomy.taxeditor.ui.element.AbstractRelevanceFormElement;
|
33
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
34
|
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
|
35
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
36
|
import eu.etaxonomy.taxeditor.ui.element.IEnableableFormElement;
|
37
|
import eu.etaxonomy.taxeditor.ui.element.ISelectable;
|
38
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
39
|
|
40
|
/**
|
41
|
* @author pplitzner
|
42
|
* @date Aug 11, 2016
|
43
|
*/
|
44
|
public abstract class AbstractComboElement<T>
|
45
|
extends AbstractRelevanceFormElement
|
46
|
implements SelectionListener, IEnableableFormElement,
|
47
|
ISelectable, DisposeListener {
|
48
|
|
49
|
protected static final int DEFAULT_VISIBLE_ITEMS = 10;
|
50
|
|
51
|
protected T selection;
|
52
|
|
53
|
protected Label label;
|
54
|
|
55
|
protected Combo combo;
|
56
|
|
57
|
public boolean hasNullValue;
|
58
|
|
59
|
public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement, boolean hasNullValue) {
|
60
|
super(formFactory, formElement);
|
61
|
|
62
|
label = formFactory.createLabel(getLayoutComposite(), "");
|
63
|
addControl(label);
|
64
|
|
65
|
// create combo
|
66
|
combo = new Combo(getLayoutComposite(), SWT.BORDER);
|
67
|
|
68
|
addControl(combo);
|
69
|
TableWrapData fill_HORIZONTALLY = LayoutConstants.FILL_HORIZONTALLY();
|
70
|
combo.setLayoutData(fill_HORIZONTALLY);
|
71
|
fill_HORIZONTALLY.maxWidth = 50;
|
72
|
// combo.setVisibleItemCount(DEFAULT_VISIBLE_ITEMS);
|
73
|
//disable mouse-wheel selection
|
74
|
combo.addListener(SWT.MouseWheel, e->e.doit=false);
|
75
|
this.hasNullValue = hasNullValue;
|
76
|
}
|
77
|
|
78
|
public AbstractComboElement(CdmFormFactory formFactory, ICdmFormElement formElement){
|
79
|
this(formFactory, formElement, false);
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
public void setBackground(Color color) {
|
84
|
if (label != null && !label.isDisposed()) {
|
85
|
label.setBackground(color);
|
86
|
}
|
87
|
}
|
88
|
|
89
|
@Override
|
90
|
public void updateCacheRelevance() {
|
91
|
Color color = cacheRelevance().getColor();
|
92
|
combo.setBackground(color);
|
93
|
}
|
94
|
|
95
|
public void setVisibleItemCount(int count){
|
96
|
combo.setVisibleItemCount(count);
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public void setSelected(boolean selected) {
|
101
|
setBackground(selected ? SELECTED : getPersistentBackground());
|
102
|
}
|
103
|
|
104
|
public T getSelection() {
|
105
|
return selection;
|
106
|
}
|
107
|
|
108
|
public void addSelectionListener(SelectionListener listener) {
|
109
|
combo.addSelectionListener(listener);
|
110
|
}
|
111
|
|
112
|
public void removeSelectionListener(SelectionListener listener) {
|
113
|
combo.removeSelectionListener(listener);
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
public void widgetDisposed(DisposeEvent e) {
|
118
|
PreferencesUtil.getPreferenceStore().removePropertyChangeListener(this);
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public void widgetDefaultSelected(SelectionEvent e) {
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
public boolean isEnabled() {
|
127
|
return combo.isEnabled();
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public void setEnabled(boolean enabled) {
|
132
|
combo.setEnabled(enabled);
|
133
|
}
|
134
|
|
135
|
public abstract void setSelection(T selection);
|
136
|
|
137
|
private AbstractComboElement<T> getComboElement(){
|
138
|
return this;
|
139
|
}
|
140
|
|
141
|
protected void addContentProposalAdapter() {
|
142
|
ContentProposalAdapter adapter;
|
143
|
|
144
|
adapter = new ContentProposalAdapter(combo, new ComboContentAdapter(), getProposalProvider(), null, null);
|
145
|
adapter.setPropagateKeys(true);
|
146
|
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
|
147
|
adapter.addContentProposalListener(new IContentProposalListener() {
|
148
|
@SuppressWarnings("unchecked")
|
149
|
@Override
|
150
|
public void proposalAccepted(IContentProposal proposal) {
|
151
|
setSelection((T)combo.getData(proposal.getContent()));
|
152
|
firePropertyChangeEvent(new CdmPropertyChangeEvent(getComboElement(), null));
|
153
|
}
|
154
|
});
|
155
|
}
|
156
|
|
157
|
IContentProposalProvider getProposalProvider() {
|
158
|
return new IContentProposalProvider() {
|
159
|
@Override
|
160
|
public IContentProposal[] getProposals(String contents, int position) {
|
161
|
String[] items = combo.getItems();
|
162
|
if (contents.length() == 0 || items.length == 0) {
|
163
|
return new IContentProposal[0];
|
164
|
}
|
165
|
StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
|
166
|
ArrayList<String> matches = new ArrayList<>();
|
167
|
for (int i = 0; i < items.length; i++) {
|
168
|
if (matcher.match(items[i])) {
|
169
|
matches.add(items[i]);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
Collections.sort(matches);
|
174
|
|
175
|
// We don't want to autoactivate if the only proposal exactly matches
|
176
|
// what is in the combo. This prevents the popup from
|
177
|
// opening when the user is merely scrolling through the combo values or
|
178
|
// has accepted a combo value.
|
179
|
if (matches.size() == 1 && matches.get(0).equals(combo.getText())) {
|
180
|
return new IContentProposal[0];
|
181
|
}
|
182
|
|
183
|
if (matches.isEmpty()) {
|
184
|
return new IContentProposal[0];
|
185
|
}
|
186
|
|
187
|
// Make the proposals
|
188
|
IContentProposal[] proposals = new IContentProposal[matches.size()];
|
189
|
for (int i = 0; i < matches.size(); i++) {
|
190
|
final String proposal = matches.get(i);
|
191
|
proposals[i] = new IContentProposal() {
|
192
|
|
193
|
@Override
|
194
|
public String getContent() {
|
195
|
return proposal;
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public int getCursorPosition() {
|
200
|
return proposal.length();
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public String getDescription() {
|
205
|
return null;
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public String getLabel() {
|
210
|
return null;
|
211
|
}
|
212
|
};
|
213
|
}
|
214
|
|
215
|
return proposals;
|
216
|
}
|
217
|
};
|
218
|
}
|
219
|
|
220
|
@Override
|
221
|
public String toString() {
|
222
|
if (label != null){
|
223
|
return CdmUtils.concat("", "ComboElement[", label.getText(),"]");
|
224
|
}else{
|
225
|
return super.toString();
|
226
|
}
|
227
|
}
|
228
|
}
|