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.preference;
|
10
|
|
11
|
|
12
|
import java.awt.Toolkit;
|
13
|
import java.net.URL;
|
14
|
|
15
|
import javax.swing.event.DocumentEvent;
|
16
|
|
17
|
import org.apache.commons.lang.StringUtils;
|
18
|
import org.eclipse.jface.resource.JFaceResources;
|
19
|
import org.eclipse.swt.SWT;
|
20
|
import org.eclipse.swt.custom.CLabel;
|
21
|
import org.eclipse.swt.events.ModifyEvent;
|
22
|
import org.eclipse.swt.events.ModifyListener;
|
23
|
import org.eclipse.swt.events.MouseEvent;
|
24
|
import org.eclipse.swt.events.MouseListener;
|
25
|
import org.eclipse.swt.graphics.Rectangle;
|
26
|
import org.eclipse.swt.layout.GridData;
|
27
|
import org.eclipse.swt.widgets.Button;
|
28
|
import org.eclipse.swt.widgets.Composite;
|
29
|
import org.eclipse.swt.widgets.Display;
|
30
|
import org.eclipse.swt.widgets.Label;
|
31
|
import org.eclipse.swt.widgets.List;
|
32
|
import org.eclipse.swt.widgets.Text;
|
33
|
|
34
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
35
|
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
36
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
37
|
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
|
38
|
|
39
|
|
40
|
/**
|
41
|
* @author k.luther
|
42
|
* @date 20.04.2017
|
43
|
*
|
44
|
*/
|
45
|
public class ListComponent implements ModifyListener
|
46
|
{
|
47
|
private List list;
|
48
|
|
49
|
private static final String addString = Messages.ListComponent_ADD_PROVIDER;
|
50
|
private static final String removeString = Messages.ListComponent_REMOVE_PROVIDER;
|
51
|
private static final String noProvider = Messages.ListComponent_NO_PROVIDER_AVAILABLE;
|
52
|
private static final String serverSidePreference = "Server Side Preference:";
|
53
|
private Button removeButton;
|
54
|
private Text providerURI;
|
55
|
protected Label labelException;
|
56
|
boolean isAdmin = false;
|
57
|
CdmPreferencePage preferencePage;
|
58
|
CdmPreference providerListPreference;
|
59
|
|
60
|
|
61
|
public CdmPreference getProviderListPreference() {
|
62
|
return providerListPreference;
|
63
|
}
|
64
|
|
65
|
|
66
|
public void setProviderListPreference(CdmPreference providerListPreference) {
|
67
|
this.providerListPreference = providerListPreference;
|
68
|
}
|
69
|
|
70
|
|
71
|
public ListComponent(Composite parent, int style, boolean isAdmin, CdmPreferencePage parentPage) {
|
72
|
this.isAdmin= isAdmin;
|
73
|
this.preferencePage = parentPage;
|
74
|
providerListPreference = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
|
75
|
if (!isAdmin && !providerListPreference.isAllowOverride()){
|
76
|
Label editingNotAllowed = new Label(parent, style);
|
77
|
editingNotAllowed.setText(Messages.AbcdImportProvider_description_not_available);
|
78
|
return ;
|
79
|
}
|
80
|
final CLabel description = new CLabel(parent, SWT.NULL);
|
81
|
description.setText(Messages.AbcdImportProvider_description);
|
82
|
list = new List(parent, SWT.BORDER | SWT.V_SCROLL);
|
83
|
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 3);
|
84
|
gridData.grabExcessHorizontalSpace = true;
|
85
|
list.setLayoutData(gridData);
|
86
|
|
87
|
Rectangle clientArea = parent.getShell().getClientArea ();
|
88
|
list.setBounds (clientArea.x, clientArea.y, 50, 500);
|
89
|
|
90
|
|
91
|
String allProviderString = ""; //$NON-NLS-1$
|
92
|
String allProviderStringDB = "";
|
93
|
|
94
|
if (!isAdmin && (providerListPreference != null && providerListPreference.isAllowOverride())){
|
95
|
allProviderString = PreferencesUtil.getStringValue(PreferencePredicate.BioCaseProvider.getKey(), true);
|
96
|
|
97
|
}else{
|
98
|
if (providerListPreference != null){
|
99
|
allProviderString = providerListPreference.getValue();
|
100
|
}
|
101
|
}
|
102
|
|
103
|
//the string is structured like this: http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=DNA_Bank;http:...;
|
104
|
String[] providerArray = allProviderString.split(";"); //$NON-NLS-1$
|
105
|
for (String providerString : providerArray){
|
106
|
if (!StringUtils.isBlank(providerString)){
|
107
|
list.add(providerString);
|
108
|
|
109
|
}
|
110
|
}
|
111
|
|
112
|
if (list.getItemCount() == 0){
|
113
|
list.add(noProvider);
|
114
|
}
|
115
|
list.add(""); //$NON-NLS-1$
|
116
|
|
117
|
// list.setSelection(0);
|
118
|
GridData dataList = new GridData();
|
119
|
dataList.horizontalAlignment = GridData.FILL;
|
120
|
dataList.horizontalSpan = 3;
|
121
|
|
122
|
|
123
|
// dataList.grabExcessVerticalSpace = true;
|
124
|
|
125
|
list.setLayoutData(dataList);
|
126
|
|
127
|
|
128
|
providerURI = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
|
129
|
GridData dataProviderUri = new GridData();
|
130
|
dataProviderUri.horizontalAlignment = GridData.FILL;
|
131
|
dataProviderUri.horizontalSpan = 3;
|
132
|
providerURI.setLayoutData(dataProviderUri);
|
133
|
providerURI.addModifyListener(this);
|
134
|
labelException = new Label(parent, SWT.NULL);
|
135
|
labelException.setText("");
|
136
|
|
137
|
|
138
|
labelException.setLayoutData(dataProviderUri);
|
139
|
|
140
|
Button addButton = new Button(parent, SWT.NULL);
|
141
|
AddListener addListener = new AddListener(addButton);
|
142
|
addButton.setText(addString);
|
143
|
addButton.addMouseListener(addListener);
|
144
|
|
145
|
removeButton = new Button(parent, SWT.NULL);
|
146
|
removeButton.setText(removeString);
|
147
|
removeButton.addMouseListener(new RemoveListener());
|
148
|
}
|
149
|
|
150
|
|
151
|
public boolean isAdmin() {
|
152
|
return isAdmin;
|
153
|
}
|
154
|
|
155
|
public void setAdmin(boolean isAdmin) {
|
156
|
this.isAdmin = isAdmin;
|
157
|
}
|
158
|
|
159
|
class RemoveListener implements MouseListener {
|
160
|
@Override
|
161
|
public void mouseUp(MouseEvent e) {
|
162
|
//This method can be called only if
|
163
|
//there's a valid selection
|
164
|
//so go ahead and remove whatever's selected.
|
165
|
int index = list.getSelectionIndex();
|
166
|
String item = list.getItem(index);
|
167
|
if (item.equals(serverSidePreference)) {
|
168
|
return;
|
169
|
}
|
170
|
list.remove(index);
|
171
|
|
172
|
|
173
|
int size = list.getItemCount();
|
174
|
|
175
|
if (size == 0) { //Nothing's left, disable removing.
|
176
|
removeButton.setEnabled(false);
|
177
|
|
178
|
} else { //Select an index.
|
179
|
if (index == size) {
|
180
|
//removed item in last position
|
181
|
index--;
|
182
|
}
|
183
|
|
184
|
list.setSelection(index-1);
|
185
|
preferencePage.setApply(true);
|
186
|
|
187
|
}
|
188
|
}
|
189
|
/**
|
190
|
* {@inheritDoc}
|
191
|
*/
|
192
|
@Override
|
193
|
public void mouseDoubleClick(MouseEvent e) {
|
194
|
// TODO Auto-generated method stub
|
195
|
|
196
|
}
|
197
|
|
198
|
|
199
|
/**
|
200
|
* {@inheritDoc}
|
201
|
*/
|
202
|
@Override
|
203
|
public void mouseDown(MouseEvent e) {
|
204
|
// TODO Auto-generated method stub
|
205
|
|
206
|
}
|
207
|
}
|
208
|
|
209
|
//This listener is shared by the text field and the add button.
|
210
|
class AddListener implements MouseListener {
|
211
|
private boolean alreadyEnabled = false;
|
212
|
private Button button;
|
213
|
|
214
|
public AddListener(Button button) {
|
215
|
this.button = button;
|
216
|
}
|
217
|
|
218
|
|
219
|
protected boolean alreadyInList(String name) {
|
220
|
return list.getData(name) != null;
|
221
|
}
|
222
|
|
223
|
|
224
|
private void enableButton() {
|
225
|
if (!alreadyEnabled) {
|
226
|
button.setEnabled(true);
|
227
|
}
|
228
|
}
|
229
|
|
230
|
private boolean handleEmptyTextField(DocumentEvent e) {
|
231
|
if (e.getDocument().getLength() <= 0) {
|
232
|
button.setEnabled(false);
|
233
|
alreadyEnabled = false;
|
234
|
return true;
|
235
|
}
|
236
|
return false;
|
237
|
}
|
238
|
|
239
|
|
240
|
|
241
|
/**
|
242
|
* {@inheritDoc}
|
243
|
*/
|
244
|
@Override
|
245
|
public void mouseDoubleClick(MouseEvent e) {
|
246
|
// TODO Auto-generated method stub
|
247
|
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* {@inheritDoc}
|
252
|
*/
|
253
|
@Override
|
254
|
public void mouseDown(MouseEvent e) {
|
255
|
// TODO Auto-generated method stub
|
256
|
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* {@inheritDoc}
|
261
|
*/
|
262
|
@Override
|
263
|
public void mouseUp(MouseEvent event) {
|
264
|
String name = providerURI.getText();
|
265
|
|
266
|
//User didn't type in a unique name...
|
267
|
if (name.equals("") || alreadyInList(name) || !StringUtils.isBlank(labelException.getText())) { //$NON-NLS-1$
|
268
|
Toolkit.getDefaultToolkit().beep();
|
269
|
providerURI.selectAll();
|
270
|
return;
|
271
|
}
|
272
|
|
273
|
int index = list.getSelectionIndex(); //get selected index
|
274
|
if (index == -1) { //no selection, so insert at beginning
|
275
|
index = 0;
|
276
|
} else { //add after the selected item
|
277
|
index++;
|
278
|
}
|
279
|
if(list.getItemCount() == 0){
|
280
|
index = 0;
|
281
|
}else if (list.getItem(0).equals(noProvider)){
|
282
|
list.remove(noProvider);
|
283
|
}
|
284
|
int itemCount = list.getItemCount();
|
285
|
|
286
|
|
287
|
list.add(providerURI.getText(), index);
|
288
|
list.setSelection(index);
|
289
|
list.update();
|
290
|
list.redraw();
|
291
|
|
292
|
//Select the new item
|
293
|
|
294
|
providerURI.setText(""); //$NON-NLS-1$
|
295
|
preferencePage.setApply(true);
|
296
|
}
|
297
|
}
|
298
|
|
299
|
public String createAllProviderString(){
|
300
|
String allProviderString = null;
|
301
|
boolean first = true;
|
302
|
for (String item: list.getItems()){
|
303
|
if (item.equals(noProvider) || item.equals(serverSidePreference)){
|
304
|
//do nothing
|
305
|
}else if (first || allProviderString == null || allProviderString == ""){ //$NON-NLS-1$
|
306
|
allProviderString = item.trim();
|
307
|
first = false;
|
308
|
}else {
|
309
|
allProviderString +=";"+ item.trim(); //$NON-NLS-1$
|
310
|
}
|
311
|
}
|
312
|
|
313
|
return allProviderString;
|
314
|
|
315
|
}
|
316
|
|
317
|
|
318
|
@Override
|
319
|
public void modifyText(ModifyEvent e) {
|
320
|
if(e.widget==providerURI){
|
321
|
Text text = (Text) e.widget;
|
322
|
boolean hasControlCharacters = false;
|
323
|
String textString = text.getText();
|
324
|
int stringLength = textString.length();
|
325
|
for (int i = 0; i < stringLength; i++) {
|
326
|
if (Character.isISOControl(textString.charAt(i))) {
|
327
|
hasControlCharacters = true;
|
328
|
break;
|
329
|
}
|
330
|
}
|
331
|
|
332
|
//remove control character such as line breaks etc.
|
333
|
try {
|
334
|
getParsedText();
|
335
|
labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
|
336
|
labelException.setForeground(Display.getCurrent().getSystemColor(SWT.NULL));
|
337
|
labelException.setText("");
|
338
|
} catch (Exception exception) {
|
339
|
labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
|
340
|
labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
341
|
labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+exception.getLocalizedMessage());
|
342
|
}
|
343
|
}
|
344
|
|
345
|
|
346
|
}
|
347
|
|
348
|
|
349
|
protected URL getParsedText() throws Exception {
|
350
|
String uriText = providerURI.getText();
|
351
|
if(!StringUtils.isBlank(uriText)){
|
352
|
return new URL(providerURI.getText());
|
353
|
}
|
354
|
return null;
|
355
|
}
|
356
|
|
357
|
|
358
|
|
359
|
}
|
360
|
|