1
|
/**
|
2
|
* Copyright (C) 2014 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.view.search.specimen;
|
10
|
|
11
|
import java.io.BufferedReader;
|
12
|
import java.io.IOException;
|
13
|
import java.io.InputStreamReader;
|
14
|
import java.net.HttpURLConnection;
|
15
|
import java.net.URL;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.Set;
|
19
|
|
20
|
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
|
21
|
import org.eclipse.jface.fieldassist.ComboContentAdapter;
|
22
|
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
|
23
|
import org.eclipse.jface.fieldassist.IContentProposal;
|
24
|
import org.eclipse.jface.fieldassist.IContentProposalProvider;
|
25
|
import org.eclipse.swt.SWT;
|
26
|
import org.eclipse.swt.events.DisposeEvent;
|
27
|
import org.eclipse.swt.events.DisposeListener;
|
28
|
import org.eclipse.swt.widgets.Button;
|
29
|
import org.eclipse.swt.widgets.Combo;
|
30
|
import org.eclipse.swt.widgets.Composite;
|
31
|
import org.eclipse.swt.widgets.Display;
|
32
|
import org.eclipse.swt.widgets.Label;
|
33
|
import org.eclipse.ui.forms.widgets.FormToolkit;
|
34
|
import org.eclipse.ui.forms.widgets.TableWrapData;
|
35
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
36
|
import org.json.JSONArray;
|
37
|
import org.json.JSONException;
|
38
|
import org.json.JSONObject;
|
39
|
|
40
|
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
41
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
42
|
|
43
|
/**
|
44
|
* @author pplitzner
|
45
|
* @date 22.05.2014
|
46
|
*
|
47
|
*/
|
48
|
public class SpecimenProviderSelectionComposite extends Composite {
|
49
|
|
50
|
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
|
51
|
private final Combo txtAccessPoint;
|
52
|
private final Button btnGbif;
|
53
|
private final Button btnBioCaseProvider;
|
54
|
// private final Label lblAccessPointUrl;
|
55
|
|
56
|
private String[] items;
|
57
|
|
58
|
/**
|
59
|
* Create the composite.
|
60
|
* @param parent
|
61
|
* @param style
|
62
|
*/
|
63
|
public SpecimenProviderSelectionComposite(Composite parent, int style) {
|
64
|
super(parent, style);
|
65
|
addDisposeListener(new DisposeListener() {
|
66
|
@Override
|
67
|
public void widgetDisposed(DisposeEvent e) {
|
68
|
toolkit.dispose();
|
69
|
}
|
70
|
});
|
71
|
toolkit.adapt(this);
|
72
|
toolkit.paintBordersFor(this);
|
73
|
{
|
74
|
TableWrapLayout tableWrapLayout = new TableWrapLayout();
|
75
|
tableWrapLayout.numColumns = 2;
|
76
|
setLayout(tableWrapLayout);
|
77
|
}
|
78
|
btnBioCaseProvider = new Button(this, SWT.RADIO);
|
79
|
toolkit.adapt(btnBioCaseProvider, true, true);
|
80
|
btnBioCaseProvider.setText("BioCASE Provider");
|
81
|
btnBioCaseProvider.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 2));
|
82
|
// txtAccessPoint = new Combo(this, SWT.BORDER);
|
83
|
//
|
84
|
Label lblAccessPointUrl = new Label(this, SWT.NONE);
|
85
|
lblAccessPointUrl.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
|
86
|
toolkit.adapt(lblAccessPointUrl, true, true);
|
87
|
lblAccessPointUrl.setText("Access Point URL");
|
88
|
txtAccessPoint = new Combo(this, SWT.BORDER);
|
89
|
txtAccessPoint.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
|
90
|
toolkit.adapt(txtAccessPoint, true, true);
|
91
|
// new Label(this, SWT.NONE);
|
92
|
btnGbif = new Button(this, SWT.RADIO);
|
93
|
toolkit.adapt(btnGbif, true, true);
|
94
|
btnGbif.setText("GBIF");
|
95
|
btnGbif.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 2));
|
96
|
Set<String> itemList = new HashSet<>();
|
97
|
URL url;
|
98
|
String response = null;
|
99
|
try {
|
100
|
url = new URL("https://www.biocase.org/whats_biocase/gbif_biocase.cgi");
|
101
|
|
102
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
103
|
conn.setRequestProperty("Accept", "application/json");
|
104
|
// conn.setConnectTimeout(TIMEOUT*9);
|
105
|
// conn.setReadTimeout(TIMEOUT*9);
|
106
|
|
107
|
if (conn.getResponseCode() != 200) {
|
108
|
//TODO
|
109
|
}
|
110
|
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
|
111
|
|
112
|
StringBuilder builder = new StringBuilder();
|
113
|
String line;
|
114
|
while ((line = br.readLine()) != null) {
|
115
|
builder.append(line);
|
116
|
}
|
117
|
conn.disconnect();
|
118
|
response = builder.toString();
|
119
|
} catch (IOException e) {
|
120
|
// TODO Auto-generated catch block
|
121
|
e.printStackTrace();
|
122
|
}
|
123
|
|
124
|
JSONArray jsonResponseArray;
|
125
|
try {
|
126
|
jsonResponseArray =new JSONArray(response);
|
127
|
|
128
|
int i = 0;
|
129
|
String service_url = null;
|
130
|
JSONArray dataSets = null;
|
131
|
// items = new String[jsonResponseArray.length()];
|
132
|
while (i<jsonResponseArray.length()){
|
133
|
JSONObject object = jsonResponseArray.getJSONObject(i);
|
134
|
service_url = (String) object.get("service_url");
|
135
|
if (service_url != null){
|
136
|
if (itemList.add(service_url)){
|
137
|
dataSets = (JSONArray) object.get("datasets");
|
138
|
if (service_url != null){
|
139
|
int j = 0;
|
140
|
while ( j < dataSets.length()){
|
141
|
String title = ((JSONObject)dataSets.get(j)).getString("title");
|
142
|
itemList.add(title+" - "+ service_url);
|
143
|
txtAccessPoint.add(title+" - "+ service_url);
|
144
|
//create object with title and service url to add as data
|
145
|
txtAccessPoint.setData(title+" - "+ service_url, title);
|
146
|
j++;
|
147
|
}
|
148
|
|
149
|
}
|
150
|
|
151
|
|
152
|
|
153
|
// txtAccessPoint.add(service_url);
|
154
|
}
|
155
|
// items[i] = service_url;
|
156
|
}
|
157
|
i++;
|
158
|
}
|
159
|
|
160
|
} catch (JSONException e) {
|
161
|
// TODO Auto-generated catch block
|
162
|
e.printStackTrace();
|
163
|
}
|
164
|
String biocaseProvider = PreferencesUtil.getStringValue(PreferencePredicate.BioCaseProvider.getKey());
|
165
|
if (biocaseProvider != null){
|
166
|
String items[] = biocaseProvider.split(";");
|
167
|
for (String item: items){
|
168
|
if (itemList.add(item)){
|
169
|
// how to handle biocase provider + different datasets???
|
170
|
txtAccessPoint.add(item);
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
items = new String[itemList.size()];
|
175
|
items = itemList.toArray(items);
|
176
|
|
177
|
// GridData dataProviderUri = new GridData();
|
178
|
// dataProviderUri.horizontalAlignment = GridData.FILL;
|
179
|
// dataProviderUri.horizontalSpan = 3;
|
180
|
// txtAccessPoint.setLayoutData(dataProviderUri);
|
181
|
// providerURI.setVisible(false);
|
182
|
|
183
|
ContentProposalAdapter adapter = new ContentProposalAdapter(txtAccessPoint, new ComboContentAdapter(), getProposalProvider(), null, null);
|
184
|
adapter.setPropagateKeys(true);
|
185
|
adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
|
186
|
// String[] proposals = new String[providerURI.getItems().length];
|
187
|
|
188
|
|
189
|
|
190
|
//
|
191
|
|
192
|
|
193
|
}
|
194
|
|
195
|
public Button getBtnGbif() {
|
196
|
return btnGbif;
|
197
|
}
|
198
|
public Button getBtnBioCaseProvider() {
|
199
|
return btnBioCaseProvider;
|
200
|
}
|
201
|
public Combo getTxtAccessPoint() {
|
202
|
return txtAccessPoint;
|
203
|
}
|
204
|
// public Label getLblAccessPointUrl() {
|
205
|
// return lblAccessPointUrl;
|
206
|
// }
|
207
|
|
208
|
/**
|
209
|
* @return
|
210
|
*/
|
211
|
IContentProposalProvider getProposalProvider() {
|
212
|
return new IContentProposalProvider() {
|
213
|
@Override
|
214
|
public IContentProposal[] getProposals(String contents, int position) {
|
215
|
//String[] items = getStringItems();
|
216
|
if (contents.length() == 0 || items.length == 0) {
|
217
|
return new IContentProposal[0];
|
218
|
}
|
219
|
StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
|
220
|
ArrayList<String> matches = new ArrayList<String>();
|
221
|
for (int i = 0; i < items.length; i++) {
|
222
|
if (matcher.match(items[i])) {
|
223
|
matches.add(items[i]);
|
224
|
}
|
225
|
}
|
226
|
|
227
|
// We don't want to autoactivate if the only proposal exactly matches
|
228
|
// what is in the combo. This prevents the popup from
|
229
|
// opening when the user is merely scrolling through the combo values or
|
230
|
// has accepted a combo value.
|
231
|
if (matches.size() == 1 && matches.get(0).equals(txtAccessPoint.getText())) {
|
232
|
return new IContentProposal[0];
|
233
|
}
|
234
|
|
235
|
if (matches.isEmpty()) {
|
236
|
return new IContentProposal[0];
|
237
|
}
|
238
|
|
239
|
// Make the proposals
|
240
|
IContentProposal[] proposals = new IContentProposal[matches.size()];
|
241
|
for (int i = 0; i < matches.size(); i++) {
|
242
|
final String proposal = matches.get(i);
|
243
|
proposals[i] = new IContentProposal() {
|
244
|
|
245
|
@Override
|
246
|
public String getContent() {
|
247
|
return proposal;
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public int getCursorPosition() {
|
252
|
return proposal.length();
|
253
|
}
|
254
|
|
255
|
@Override
|
256
|
public String getDescription() {
|
257
|
return null;
|
258
|
}
|
259
|
|
260
|
@Override
|
261
|
public String getLabel() {
|
262
|
return null;
|
263
|
}
|
264
|
};
|
265
|
}
|
266
|
return proposals;
|
267
|
}
|
268
|
};
|
269
|
}
|
270
|
}
|