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.io.BufferedReader;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStreamReader;
|
16
|
import java.net.HttpURLConnection;
|
17
|
import java.net.URL;
|
18
|
import java.util.ArrayList;
|
19
|
|
20
|
import javax.swing.event.DocumentEvent;
|
21
|
|
22
|
import org.apache.commons.lang.StringUtils;
|
23
|
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
|
24
|
import org.eclipse.jface.fieldassist.ComboContentAdapter;
|
25
|
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
|
26
|
import org.eclipse.jface.fieldassist.IContentProposal;
|
27
|
import org.eclipse.jface.fieldassist.IContentProposalProvider;
|
28
|
import org.eclipse.jface.resource.JFaceResources;
|
29
|
import org.eclipse.swt.SWT;
|
30
|
import org.eclipse.swt.custom.CLabel;
|
31
|
import org.eclipse.swt.events.ModifyEvent;
|
32
|
import org.eclipse.swt.events.ModifyListener;
|
33
|
import org.eclipse.swt.events.MouseEvent;
|
34
|
import org.eclipse.swt.events.MouseListener;
|
35
|
import org.eclipse.swt.graphics.Rectangle;
|
36
|
import org.eclipse.swt.layout.GridData;
|
37
|
import org.eclipse.swt.widgets.Button;
|
38
|
import org.eclipse.swt.widgets.Combo;
|
39
|
import org.eclipse.swt.widgets.Composite;
|
40
|
import org.eclipse.swt.widgets.Display;
|
41
|
import org.eclipse.swt.widgets.Label;
|
42
|
import org.eclipse.swt.widgets.List;
|
43
|
import org.eclipse.swt.widgets.Text;
|
44
|
import org.json.JSONArray;
|
45
|
import org.json.JSONException;
|
46
|
import org.json.JSONObject;
|
47
|
|
48
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
49
|
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
50
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
51
|
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
|
52
|
|
53
|
|
54
|
/**
|
55
|
* @author k.luther
|
56
|
* @date 20.04.2017
|
57
|
*
|
58
|
*/
|
59
|
public class ListComponent implements ModifyListener
|
60
|
{
|
61
|
private List list;
|
62
|
|
63
|
private static final String addString = Messages.ListComponent_ADD_PROVIDER;
|
64
|
private static final String removeString = Messages.ListComponent_REMOVE_PROVIDER;
|
65
|
private static final String noProvider = Messages.ListComponent_NO_PROVIDER_AVAILABLE;
|
66
|
private static final String serverSidePreference = "Server Side Preference:";
|
67
|
private Button removeButton;
|
68
|
private Combo providerURI;
|
69
|
protected Label labelException;
|
70
|
boolean isAdmin = false;
|
71
|
CdmPreferencePage preferencePage;
|
72
|
CdmPreference providerListPreference;
|
73
|
String providerList;
|
74
|
String[] items;
|
75
|
|
76
|
|
77
|
public CdmPreference getProviderListPreference() {
|
78
|
return providerListPreference;
|
79
|
}
|
80
|
|
81
|
|
82
|
public void setProviderListPreference(CdmPreference providerListPreference) {
|
83
|
this.providerListPreference = providerListPreference;
|
84
|
}
|
85
|
|
86
|
|
87
|
public ListComponent(Composite parent, int style, boolean isAdmin, CdmPreferencePage parentPage) {
|
88
|
this.isAdmin= isAdmin;
|
89
|
this.preferencePage = parentPage;
|
90
|
|
91
|
providerListPreference = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
|
92
|
if (!isAdmin && providerListPreference != null && !providerListPreference.isAllowOverride()){
|
93
|
Label editingNotAllowed = new Label(parent, style);
|
94
|
editingNotAllowed.setText(Messages.AbcdImportProvider_description_not_available);
|
95
|
return ;
|
96
|
}
|
97
|
final CLabel description = new CLabel(parent, SWT.NULL);
|
98
|
description.setText(Messages.AbcdImportProvider_description);
|
99
|
list = new List(parent, SWT.BORDER | SWT.V_SCROLL);
|
100
|
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 3);
|
101
|
gridData.grabExcessHorizontalSpace = true;
|
102
|
list.setLayoutData(gridData);
|
103
|
|
104
|
Rectangle clientArea = parent.getShell().getClientArea ();
|
105
|
list.setBounds (clientArea.x, clientArea.y, 50, 500);
|
106
|
|
107
|
|
108
|
providerList = null; //$NON-NLS-1$
|
109
|
String allProviderStringDB = "";
|
110
|
|
111
|
if (!isAdmin && ((providerListPreference != null && providerListPreference.isAllowOverride()) || providerListPreference == null)){
|
112
|
providerList = PreferencesUtil.getStringValue(PreferencePredicate.BioCaseProvider.getKey(), true);
|
113
|
|
114
|
}else{
|
115
|
if (providerListPreference != null){
|
116
|
providerList = providerListPreference.getValue();
|
117
|
}
|
118
|
}
|
119
|
|
120
|
//the string is structured like this: http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=DNA_Bank;http:...;
|
121
|
if (providerList != null){
|
122
|
String[] providerArray = providerList.split(";"); //$NON-NLS-1$
|
123
|
for (String providerString : providerArray){
|
124
|
if (!StringUtils.isBlank(providerString)){
|
125
|
list.add(providerString);
|
126
|
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
if (list.getItemCount() == 0){
|
132
|
list.add(noProvider);
|
133
|
}
|
134
|
list.add(""); //$NON-NLS-1$
|
135
|
|
136
|
// list.setSelection(0);
|
137
|
GridData dataList = new GridData();
|
138
|
dataList.horizontalAlignment = GridData.FILL;
|
139
|
dataList.horizontalSpan = 3;
|
140
|
|
141
|
|
142
|
// dataList.grabExcessVerticalSpace = true;
|
143
|
|
144
|
list.setLayoutData(dataList);
|
145
|
|
146
|
|
147
|
providerURI = new Combo(parent, SWT.BORDER);
|
148
|
|
149
|
URL url;
|
150
|
String response = null;
|
151
|
try {
|
152
|
url = new URL("https://www.biocase.org/whats_biocase/gbif_biocase.cgi");
|
153
|
|
154
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
155
|
conn.setRequestProperty("Accept", "application/json");
|
156
|
// conn.setConnectTimeout(TIMEOUT*9);
|
157
|
// conn.setReadTimeout(TIMEOUT*9);
|
158
|
|
159
|
if (conn.getResponseCode() != 200) {
|
160
|
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
|
161
|
}
|
162
|
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
|
163
|
|
164
|
StringBuilder builder = new StringBuilder();
|
165
|
String line;
|
166
|
while ((line = br.readLine()) != null) {
|
167
|
builder.append(line);
|
168
|
}
|
169
|
conn.disconnect();
|
170
|
response = builder.toString();
|
171
|
} catch (IOException e) {
|
172
|
// TODO Auto-generated catch block
|
173
|
e.printStackTrace();
|
174
|
}
|
175
|
|
176
|
JSONArray jsonResponseArray;
|
177
|
try {
|
178
|
jsonResponseArray =new JSONArray(response);
|
179
|
|
180
|
int i = 0;
|
181
|
String service_url = null;
|
182
|
items = new String[jsonResponseArray.length()];
|
183
|
while (i<jsonResponseArray.length()){
|
184
|
JSONObject object = jsonResponseArray.getJSONObject(i);
|
185
|
service_url = (String) object.get("service_url");
|
186
|
if (service_url != null){
|
187
|
providerURI.add(service_url);
|
188
|
items[i] = service_url;
|
189
|
}
|
190
|
i++;
|
191
|
}
|
192
|
|
193
|
} catch (JSONException e) {
|
194
|
// TODO Auto-generated catch block
|
195
|
e.printStackTrace();
|
196
|
}
|
197
|
|
198
|
|
199
|
GridData dataProviderUri = new GridData();
|
200
|
dataProviderUri.horizontalAlignment = GridData.FILL;
|
201
|
dataProviderUri.horizontalSpan = 3;
|
202
|
providerURI.setLayoutData(dataProviderUri);
|
203
|
// providerURI.setVisible(false);
|
204
|
|
205
|
ContentProposalAdapter adapter = new ContentProposalAdapter(providerURI, new ComboContentAdapter(), getProposalProvider(), null, null);
|
206
|
adapter.setPropagateKeys(true);
|
207
|
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
|
208
|
// String[] proposals = new String[providerURI.getItems().length];
|
209
|
//
|
210
|
// for (int i = 0; i < providerURI.getItems().length; i++) {
|
211
|
// proposals[i] = providerURI.getItem(i);
|
212
|
// }
|
213
|
//
|
214
|
// ComboViewer comboViewer = new ComboViewer(parent, SWT.NONE);
|
215
|
// comboViewer.setContentProvider(new ArrayContentProvider());
|
216
|
// comboViewer.setLabelProvider(new LabelProvider(){
|
217
|
// @Override
|
218
|
// public String getText(final Object element){
|
219
|
// if(element instanceof String){
|
220
|
// return (String) element;
|
221
|
// }
|
222
|
// return element.toString();
|
223
|
// }
|
224
|
// });
|
225
|
// comboViewer.setInput(proposals);
|
226
|
// // additionally, configure the comboViewer arbitrary
|
227
|
// AutoCompleteField field = new AutoCompleteField(comboViewer.getCombo(), new ComboContentAdapter(), proposals);
|
228
|
|
229
|
|
230
|
|
231
|
|
232
|
//providerURI.addModifyListener(this);
|
233
|
labelException = new Label(parent, SWT.NULL);
|
234
|
labelException.setText("");
|
235
|
|
236
|
|
237
|
labelException.setLayoutData(dataProviderUri);
|
238
|
|
239
|
Button addButton = new Button(parent, SWT.NULL);
|
240
|
AddListener addListener = new AddListener(addButton);
|
241
|
addButton.setText(addString);
|
242
|
addButton.addMouseListener(addListener);
|
243
|
|
244
|
removeButton = new Button(parent, SWT.NULL);
|
245
|
removeButton.setText(removeString);
|
246
|
removeButton.addMouseListener(new RemoveListener());
|
247
|
}
|
248
|
|
249
|
|
250
|
/**
|
251
|
* @return
|
252
|
*/
|
253
|
IContentProposalProvider getProposalProvider() {
|
254
|
return new IContentProposalProvider() {
|
255
|
@Override
|
256
|
public IContentProposal[] getProposals(String contents, int position) {
|
257
|
//String[] items = getStringItems();
|
258
|
if (contents.length() == 0 || items.length == 0) {
|
259
|
return new IContentProposal[0];
|
260
|
}
|
261
|
StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
|
262
|
ArrayList<String> matches = new ArrayList<String>();
|
263
|
for (int i = 0; i < items.length; i++) {
|
264
|
if (matcher.match(items[i])) {
|
265
|
matches.add(items[i]);
|
266
|
}
|
267
|
}
|
268
|
|
269
|
// We don't want to autoactivate if the only proposal exactly matches
|
270
|
// what is in the combo. This prevents the popup from
|
271
|
// opening when the user is merely scrolling through the combo values or
|
272
|
// has accepted a combo value.
|
273
|
if (matches.size() == 1 && matches.get(0).equals(providerURI.getText())) {
|
274
|
return new IContentProposal[0];
|
275
|
}
|
276
|
|
277
|
if (matches.isEmpty()) {
|
278
|
return new IContentProposal[0];
|
279
|
}
|
280
|
|
281
|
// Make the proposals
|
282
|
IContentProposal[] proposals = new IContentProposal[matches.size()];
|
283
|
for (int i = 0; i < matches.size(); i++) {
|
284
|
final String proposal = matches.get(i);
|
285
|
proposals[i] = new IContentProposal() {
|
286
|
|
287
|
@Override
|
288
|
public String getContent() {
|
289
|
return proposal;
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public int getCursorPosition() {
|
294
|
return proposal.length();
|
295
|
}
|
296
|
|
297
|
@Override
|
298
|
public String getDescription() {
|
299
|
return null;
|
300
|
}
|
301
|
|
302
|
@Override
|
303
|
public String getLabel() {
|
304
|
return null;
|
305
|
}
|
306
|
};
|
307
|
}
|
308
|
return proposals;
|
309
|
}
|
310
|
};
|
311
|
}
|
312
|
|
313
|
public boolean isAdmin() {
|
314
|
return isAdmin;
|
315
|
}
|
316
|
|
317
|
public void setAdmin(boolean isAdmin) {
|
318
|
this.isAdmin = isAdmin;
|
319
|
}
|
320
|
|
321
|
class RemoveListener implements MouseListener {
|
322
|
@Override
|
323
|
public void mouseUp(MouseEvent e) {
|
324
|
//This method can be called only if
|
325
|
//there's a valid selection
|
326
|
//so go ahead and remove whatever's selected.
|
327
|
int index = list.getSelectionIndex();
|
328
|
try{
|
329
|
String item = list.getItem(index);
|
330
|
if (item.equals(serverSidePreference)) {
|
331
|
return;
|
332
|
}
|
333
|
list.remove(index);
|
334
|
|
335
|
|
336
|
int size = list.getItemCount();
|
337
|
|
338
|
if (size == 0) { //Nothing's left, disable removing.
|
339
|
removeButton.setEnabled(false);
|
340
|
|
341
|
} else { //Select an index.
|
342
|
if (index == size) {
|
343
|
//removed item in last position
|
344
|
index--;
|
345
|
}
|
346
|
|
347
|
list.setSelection(index-1);
|
348
|
preferencePage.setApply(true);
|
349
|
|
350
|
}
|
351
|
}catch(IllegalArgumentException iae){
|
352
|
//nothing was selected
|
353
|
return;
|
354
|
}
|
355
|
|
356
|
}
|
357
|
/**
|
358
|
* {@inheritDoc}
|
359
|
*/
|
360
|
@Override
|
361
|
public void mouseDoubleClick(MouseEvent e) {
|
362
|
// TODO Auto-generated method stub
|
363
|
|
364
|
}
|
365
|
|
366
|
|
367
|
/**
|
368
|
* {@inheritDoc}
|
369
|
*/
|
370
|
@Override
|
371
|
public void mouseDown(MouseEvent e) {
|
372
|
// TODO Auto-generated method stub
|
373
|
|
374
|
}
|
375
|
}
|
376
|
|
377
|
//This listener is shared by the text field and the add button.
|
378
|
class AddListener implements MouseListener {
|
379
|
private boolean alreadyEnabled = false;
|
380
|
private Button button;
|
381
|
|
382
|
public AddListener(Button button) {
|
383
|
this.button = button;
|
384
|
}
|
385
|
|
386
|
|
387
|
protected boolean alreadyInList(String name) {
|
388
|
return list.getData(name) != null;
|
389
|
}
|
390
|
|
391
|
|
392
|
private void enableButton() {
|
393
|
if (!alreadyEnabled) {
|
394
|
button.setEnabled(true);
|
395
|
}
|
396
|
}
|
397
|
|
398
|
private boolean handleEmptyTextField(DocumentEvent e) {
|
399
|
if (e.getDocument().getLength() <= 0) {
|
400
|
button.setEnabled(false);
|
401
|
alreadyEnabled = false;
|
402
|
return true;
|
403
|
}
|
404
|
return false;
|
405
|
}
|
406
|
|
407
|
|
408
|
|
409
|
/**
|
410
|
* {@inheritDoc}
|
411
|
*/
|
412
|
@Override
|
413
|
public void mouseDoubleClick(MouseEvent e) {
|
414
|
// TODO Auto-generated method stub
|
415
|
|
416
|
}
|
417
|
|
418
|
/**
|
419
|
* {@inheritDoc}
|
420
|
*/
|
421
|
@Override
|
422
|
public void mouseDown(MouseEvent e) {
|
423
|
// TODO Auto-generated method stub
|
424
|
|
425
|
}
|
426
|
|
427
|
/**
|
428
|
* {@inheritDoc}
|
429
|
*/
|
430
|
@Override
|
431
|
public void mouseUp(MouseEvent event) {
|
432
|
String name = providerURI.getText();
|
433
|
|
434
|
//User didn't type in a unique name...
|
435
|
if (name.equals("") || alreadyInList(name) || !StringUtils.isBlank(labelException.getText())) { //$NON-NLS-1$
|
436
|
Toolkit.getDefaultToolkit().beep();
|
437
|
//providerURI.selectAll();
|
438
|
return;
|
439
|
}
|
440
|
|
441
|
int index = list.getSelectionIndex(); //get selected index
|
442
|
if (index == -1) { //no selection, so insert at beginning
|
443
|
index = 0;
|
444
|
} else { //add after the selected item
|
445
|
index++;
|
446
|
}
|
447
|
if(list.getItemCount() == 0){
|
448
|
index = 0;
|
449
|
}else if (list.getItem(0).equals(noProvider)){
|
450
|
list.remove(noProvider);
|
451
|
}
|
452
|
int itemCount = list.getItemCount();
|
453
|
|
454
|
|
455
|
list.add(providerURI.getText(), index);
|
456
|
list.setSelection(index);
|
457
|
list.update();
|
458
|
list.redraw();
|
459
|
|
460
|
//Select the new item
|
461
|
|
462
|
providerURI.setText(""); //$NON-NLS-1$
|
463
|
preferencePage.setApply(true);
|
464
|
}
|
465
|
}
|
466
|
|
467
|
public String createAllProviderString(){
|
468
|
String allProviderString = null;
|
469
|
boolean first = true;
|
470
|
for (String item: list.getItems()){
|
471
|
if (item.equals(noProvider) || item.equals(serverSidePreference)){
|
472
|
//do nothing
|
473
|
}else if (first || StringUtils.isBlank(allProviderString)){ //$NON-NLS-1$
|
474
|
allProviderString = item.trim();
|
475
|
first = false;
|
476
|
}else {
|
477
|
allProviderString +=";"+ item.trim(); //$NON-NLS-1$
|
478
|
}
|
479
|
}
|
480
|
|
481
|
return allProviderString;
|
482
|
|
483
|
}
|
484
|
|
485
|
|
486
|
@Override
|
487
|
public void modifyText(ModifyEvent e) {
|
488
|
if(e.widget==providerURI){
|
489
|
Text text = (Text) e.widget;
|
490
|
boolean hasControlCharacters = false;
|
491
|
String textString = text.getText();
|
492
|
int stringLength = textString.length();
|
493
|
for (int i = 0; i < stringLength; i++) {
|
494
|
if (Character.isISOControl(textString.charAt(i))) {
|
495
|
hasControlCharacters = true;
|
496
|
break;
|
497
|
}
|
498
|
}
|
499
|
|
500
|
//remove control character such as line breaks etc.
|
501
|
try {
|
502
|
getParsedText();
|
503
|
labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
|
504
|
labelException.setForeground(Display.getCurrent().getSystemColor(SWT.NULL));
|
505
|
labelException.setText("");
|
506
|
} catch (Exception exception) {
|
507
|
labelException.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
|
508
|
labelException.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
|
509
|
labelException.setText(Messages.UriWithLabelElement_URL_NOT_SAVED+exception.getLocalizedMessage());
|
510
|
}
|
511
|
}
|
512
|
|
513
|
|
514
|
}
|
515
|
|
516
|
|
517
|
protected URL getParsedText() throws Exception {
|
518
|
String uriText = providerURI.getText();
|
519
|
if(!StringUtils.isBlank(uriText)){
|
520
|
return new URL(providerURI.getText());
|
521
|
}
|
522
|
return null;
|
523
|
}
|
524
|
|
525
|
|
526
|
|
527
|
}
|
528
|
|