Revision d147dfd6
Added by Katja Luther about 4 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/ListComponent.java | ||
---|---|---|
10 | 10 |
|
11 | 11 |
|
12 | 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; |
|
13 | 17 |
import java.net.URL; |
18 |
import java.util.ArrayList; |
|
14 | 19 |
|
15 | 20 |
import javax.swing.event.DocumentEvent; |
16 | 21 |
|
17 | 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; |
|
18 | 28 |
import org.eclipse.jface.resource.JFaceResources; |
19 | 29 |
import org.eclipse.swt.SWT; |
20 | 30 |
import org.eclipse.swt.custom.CLabel; |
... | ... | |
25 | 35 |
import org.eclipse.swt.graphics.Rectangle; |
26 | 36 |
import org.eclipse.swt.layout.GridData; |
27 | 37 |
import org.eclipse.swt.widgets.Button; |
38 |
import org.eclipse.swt.widgets.Combo; |
|
28 | 39 |
import org.eclipse.swt.widgets.Composite; |
29 | 40 |
import org.eclipse.swt.widgets.Display; |
30 | 41 |
import org.eclipse.swt.widgets.Label; |
31 | 42 |
import org.eclipse.swt.widgets.List; |
32 | 43 |
import org.eclipse.swt.widgets.Text; |
44 |
import org.json.JSONArray; |
|
45 |
import org.json.JSONException; |
|
46 |
import org.json.JSONObject; |
|
33 | 47 |
|
34 | 48 |
import eu.etaxonomy.cdm.model.metadata.CdmPreference; |
35 | 49 |
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate; |
... | ... | |
51 | 65 |
private static final String noProvider = Messages.ListComponent_NO_PROVIDER_AVAILABLE; |
52 | 66 |
private static final String serverSidePreference = "Server Side Preference:"; |
53 | 67 |
private Button removeButton; |
54 |
private Text providerURI;
|
|
68 |
private Combo providerURI;
|
|
55 | 69 |
protected Label labelException; |
56 | 70 |
boolean isAdmin = false; |
57 | 71 |
CdmPreferencePage preferencePage; |
58 | 72 |
CdmPreference providerListPreference; |
59 | 73 |
String providerList; |
74 |
String[] items; |
|
60 | 75 |
|
61 | 76 |
|
62 | 77 |
public CdmPreference getProviderListPreference() { |
... | ... | |
129 | 144 |
list.setLayoutData(dataList); |
130 | 145 |
|
131 | 146 |
|
132 |
providerURI = new Text(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION); |
|
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 |
|
|
133 | 199 |
GridData dataProviderUri = new GridData(); |
134 | 200 |
dataProviderUri.horizontalAlignment = GridData.FILL; |
135 | 201 |
dataProviderUri.horizontalSpan = 3; |
136 | 202 |
providerURI.setLayoutData(dataProviderUri); |
137 |
providerURI.addModifyListener(this); |
|
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); |
|
138 | 233 |
labelException = new Label(parent, SWT.NULL); |
139 | 234 |
labelException.setText(""); |
140 | 235 |
|
... | ... | |
152 | 247 |
} |
153 | 248 |
|
154 | 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 |
|
|
155 | 313 |
public boolean isAdmin() { |
156 | 314 |
return isAdmin; |
157 | 315 |
} |
... | ... | |
276 | 434 |
//User didn't type in a unique name... |
277 | 435 |
if (name.equals("") || alreadyInList(name) || !StringUtils.isBlank(labelException.getText())) { //$NON-NLS-1$ |
278 | 436 |
Toolkit.getDefaultToolkit().beep(); |
279 |
providerURI.selectAll(); |
|
437 |
//providerURI.selectAll();
|
|
280 | 438 |
return; |
281 | 439 |
} |
282 | 440 |
|
... | ... | |
312 | 470 |
for (String item: list.getItems()){ |
313 | 471 |
if (item.equals(noProvider) || item.equals(serverSidePreference)){ |
314 | 472 |
//do nothing |
315 |
}else if (first || allProviderString == null || allProviderString == ""){ //$NON-NLS-1$
|
|
473 |
}else if (first || StringUtils.isBlank(allProviderString)){ //$NON-NLS-1$
|
|
316 | 474 |
allProviderString = item.trim(); |
317 | 475 |
first = false; |
318 | 476 |
}else { |
Also available in: Unified diff
ref #8562: continue work on preference pages