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