Project

General

Profile

Download (8.49 KB) Statistics
| Branch: | Tag: | Revision:
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

    
18
import org.eclipse.equinox.internal.p2.ui.misc.StringMatcher;
19
import org.eclipse.jface.fieldassist.ComboContentAdapter;
20
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
21
import org.eclipse.jface.fieldassist.IContentProposal;
22
import org.eclipse.jface.fieldassist.IContentProposalProvider;
23
import org.eclipse.swt.SWT;
24
import org.eclipse.swt.events.DisposeEvent;
25
import org.eclipse.swt.events.DisposeListener;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.widgets.Button;
28
import org.eclipse.swt.widgets.Combo;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Display;
31
import org.eclipse.swt.widgets.Label;
32
import org.eclipse.ui.forms.widgets.FormToolkit;
33
import org.eclipse.ui.forms.widgets.TableWrapData;
34
import org.eclipse.ui.forms.widgets.TableWrapLayout;
35
import org.json.JSONArray;
36
import org.json.JSONException;
37
import org.json.JSONObject;
38

    
39
/**
40
 * @author pplitzner
41
 * @date 22.05.2014
42
 *
43
 */
44
public class SpecimenProviderSelectionComposite extends Composite {
45

    
46
    private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
47
    private final Combo txtAccessPoint;
48
    private final Button btnGbif;
49
    private final Button btnBioCaseProvider;
50
    private final Label lblAccessPointUrl;
51

    
52
    private String[] items;
53

    
54
    /**
55
     * Create the composite.
56
     * @param parent
57
     * @param style
58
     */
59
    public SpecimenProviderSelectionComposite(Composite parent, int style) {
60
        super(parent, style);
61
        addDisposeListener(new DisposeListener() {
62
            @Override
63
            public void widgetDisposed(DisposeEvent e) {
64
                toolkit.dispose();
65
            }
66
        });
67
        toolkit.adapt(this);
68
        toolkit.paintBordersFor(this);
69
        {
70
            TableWrapLayout tableWrapLayout = new TableWrapLayout();
71
            tableWrapLayout.numColumns = 2;
72
            setLayout(tableWrapLayout);
73
        }
74

    
75
        btnGbif = new Button(this, SWT.RADIO);
76
        toolkit.adapt(btnGbif, true, true);
77
        btnGbif.setText("GBIF");
78
        new Label(this, SWT.NONE);
79

    
80
        btnBioCaseProvider = new Button(this, SWT.RADIO);
81
        toolkit.adapt(btnBioCaseProvider, true, true);
82
        btnBioCaseProvider.setText("BioCASE Provider");
83
//        txtAccessPoint = new Combo(this, SWT.BORDER);
84
//        CdmPreference biocaseProvider = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.BioCaseProvider);
85
//        if (biocaseProvider != null){
86
//            String items[] = biocaseProvider.getValue().split(";");
87
//            txtAccessPoint.setItems(items);
88
//        }
89

    
90
        txtAccessPoint = new Combo(this, SWT.BORDER);
91

    
92
        URL url;
93
        String response = null;
94
        try {
95
            url = new URL("http://www.biocase.org/whats_biocase/gbif_biocase.cgi");
96

    
97
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
98
            conn.setRequestProperty("Accept", "application/json");
99
    //        conn.setConnectTimeout(TIMEOUT*9);
100
    //        conn.setReadTimeout(TIMEOUT*9);
101

    
102
            if (conn.getResponseCode() != 200) {
103
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
104
            }
105
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
106

    
107
            StringBuilder  builder = new StringBuilder();
108
            String line;
109
            while ((line = br.readLine()) != null) {
110
                builder.append(line);
111
            }
112
            conn.disconnect();
113
            response = builder.toString();
114
        } catch (IOException e) {
115
            // TODO Auto-generated catch block
116
            e.printStackTrace();
117
        }
118

    
119
        JSONArray jsonResponseArray;
120
        try {
121
            jsonResponseArray =new JSONArray(response);
122

    
123
            int i = 0;
124
            String service_url = null;
125
            items = new String[jsonResponseArray.length()];
126
            while (i<jsonResponseArray.length()){
127
                JSONObject object = jsonResponseArray.getJSONObject(i);
128
                service_url = (String) object.get("service_url");
129
                if (service_url != null){
130
                    txtAccessPoint.add(service_url);
131
                    items[i] = service_url;
132
                }
133
                i++;
134
            }
135

    
136
        } catch (JSONException e) {
137
            // TODO Auto-generated catch block
138
            e.printStackTrace();
139
        }
140

    
141

    
142
        GridData dataProviderUri = new GridData();
143
        dataProviderUri.horizontalAlignment = GridData.FILL;
144
        dataProviderUri.horizontalSpan = 3;
145
        txtAccessPoint.setLayoutData(dataProviderUri);
146
      //  providerURI.setVisible(false);
147

    
148
        ContentProposalAdapter adapter = new ContentProposalAdapter(txtAccessPoint, new ComboContentAdapter(), getProposalProvider(), null, null);
149
        adapter.setPropagateKeys(true);
150
        adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
151
//        String[] proposals = new String[providerURI.getItems().length];
152

    
153

    
154
        txtAccessPoint.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
155
        toolkit.adapt(txtAccessPoint, true, true);
156

    
157
        lblAccessPointUrl = new Label(this, SWT.NONE);
158
        lblAccessPointUrl.setLayoutData(new TableWrapData(TableWrapData.LEFT, TableWrapData.MIDDLE, 1, 1));
159
        toolkit.adapt(lblAccessPointUrl, true, true);
160
        lblAccessPointUrl.setText("Access Point URL");
161

    
162
    }
163

    
164
    public Button getBtnGbif() {
165
        return btnGbif;
166
    }
167
    public Button getBtnBioCaseProvider() {
168
        return btnBioCaseProvider;
169
    }
170
    public Combo getTxtAccessPoint() {
171
        return txtAccessPoint;
172
    }
173
    public Label getLblAccessPointUrl() {
174
        return lblAccessPointUrl;
175
    }
176

    
177
    /**
178
     * @return
179
     */
180
    IContentProposalProvider getProposalProvider() {
181
        return new IContentProposalProvider() {
182
            @Override
183
            public IContentProposal[] getProposals(String contents, int position) {
184
                //String[] items = getStringItems();
185
                if (contents.length() == 0 || items.length == 0) {
186
                    return new IContentProposal[0];
187
                }
188
                StringMatcher matcher = new StringMatcher("*" + contents + "*", true, false); //$NON-NLS-1$ //$NON-NLS-2$
189
                ArrayList<String> matches = new ArrayList<String>();
190
                for (int i = 0; i < items.length; i++) {
191
                    if (matcher.match(items[i])) {
192
                        matches.add(items[i]);
193
                    }
194
                }
195

    
196
                // We don't want to autoactivate if the only proposal exactly matches
197
                // what is in the combo.  This prevents the popup from
198
                // opening when the user is merely scrolling through the combo values or
199
                // has accepted a combo value.
200
                if (matches.size() == 1 && matches.get(0).equals(txtAccessPoint.getText())) {
201
                    return new IContentProposal[0];
202
                }
203

    
204
                if (matches.isEmpty()) {
205
                    return new IContentProposal[0];
206
                }
207

    
208
                // Make the proposals
209
                IContentProposal[] proposals = new IContentProposal[matches.size()];
210
                for (int i = 0; i < matches.size(); i++) {
211
                    final String proposal = matches.get(i);
212
                    proposals[i] = new IContentProposal() {
213

    
214
                        @Override
215
                        public String getContent() {
216
                            return proposal;
217
                        }
218

    
219
                        @Override
220
                        public int getCursorPosition() {
221
                            return proposal.length();
222
                        }
223

    
224
                        @Override
225
                        public String getDescription() {
226
                            return null;
227
                        }
228

    
229
                        @Override
230
                        public String getLabel() {
231
                            return null;
232
                        }
233
                    };
234
                }
235
                return proposals;
236
            }
237
        };
238
    }
239
}
(1-1/4)