1
|
package eu.dnetlib.client.widgets;
|
2
|
|
3
|
|
4
|
import java.util.ArrayList;
|
5
|
|
6
|
import com.github.gwtbootstrap.client.ui.Image;
|
7
|
import com.google.gwt.core.client.JsonUtils;
|
8
|
import com.google.gwt.dom.client.Style.Float;
|
9
|
import com.google.gwt.dom.client.Style.TextDecoration;
|
10
|
import com.google.gwt.event.dom.client.ClickEvent;
|
11
|
import com.google.gwt.event.dom.client.ClickHandler;
|
12
|
import com.google.gwt.event.dom.client.KeyUpEvent;
|
13
|
import com.google.gwt.event.dom.client.KeyUpHandler;
|
14
|
import com.google.gwt.http.client.Request;
|
15
|
import com.google.gwt.http.client.RequestBuilder;
|
16
|
import com.google.gwt.http.client.RequestCallback;
|
17
|
import com.google.gwt.http.client.RequestException;
|
18
|
import com.google.gwt.http.client.Response;
|
19
|
import com.google.gwt.user.client.Timer;
|
20
|
import com.google.gwt.user.client.Window;
|
21
|
import com.google.gwt.user.client.ui.Anchor;
|
22
|
import com.google.gwt.user.client.ui.FlowPanel;
|
23
|
import com.google.gwt.user.client.ui.HTML;
|
24
|
import com.google.gwt.user.client.ui.IsWidget;
|
25
|
import com.google.gwt.user.client.ui.Label;
|
26
|
import com.google.gwt.user.client.ui.TextBox;
|
27
|
import com.google.gwt.user.client.ui.Widget;
|
28
|
|
29
|
import eu.dnetlib.shared.FiltersData;
|
30
|
|
31
|
public class AutoCompleteWidget implements IsWidget{
|
32
|
|
33
|
private FlowPanel masterPanel = new FlowPanel();
|
34
|
private FlowPanel suggestionList = new FlowPanel();
|
35
|
public TextBox inputTextBox = new TextBox();
|
36
|
private static String BASE_URL = "http://dl114.madgik.di.uoa.gr/stats/";
|
37
|
private FiltersData filtersData;
|
38
|
private Timer timer;
|
39
|
private String table = new String();
|
40
|
private String field = new String();
|
41
|
private String valueSelected = null;
|
42
|
private HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
|
43
|
private Label loadingLabel = new Label();
|
44
|
private ArrayList<String> blacklist = new ArrayList<String>();
|
45
|
|
46
|
public interface SearchingListener{
|
47
|
public void onSearch();
|
48
|
public void onReturn();
|
49
|
}
|
50
|
|
51
|
private SearchingListener searchingListener;
|
52
|
|
53
|
public void setSearchingListener(SearchingListener searchingListener){
|
54
|
this.searchingListener = searchingListener;
|
55
|
}
|
56
|
|
57
|
public interface AddingListener{
|
58
|
public void onEvent(String value);
|
59
|
}
|
60
|
|
61
|
public TextBox getInputTextBox() {
|
62
|
return inputTextBox;
|
63
|
}
|
64
|
|
65
|
public void setInputTextBox(TextBox inputTextBox) {
|
66
|
this.inputTextBox = inputTextBox;
|
67
|
}
|
68
|
private AddingListener addingListener;
|
69
|
|
70
|
public void setAddingListener(AddingListener addingListener){
|
71
|
this.addingListener=addingListener;
|
72
|
}
|
73
|
|
74
|
public interface DeleteListener{
|
75
|
public void onEvent();
|
76
|
}
|
77
|
|
78
|
public String getTable() {
|
79
|
return table;
|
80
|
}
|
81
|
|
82
|
public void setTable(String table) {
|
83
|
this.table = table;
|
84
|
}
|
85
|
|
86
|
public String getField() {
|
87
|
return field;
|
88
|
}
|
89
|
|
90
|
public void setField(String field) {
|
91
|
this.field = field;
|
92
|
}
|
93
|
|
94
|
public String getValueSelected(){
|
95
|
return this.valueSelected;
|
96
|
}
|
97
|
|
98
|
public void setValue(String value){
|
99
|
inputTextBox.setValue(value);
|
100
|
inputTextBox.setText(value);
|
101
|
inputTextBox.setEnabled(false);
|
102
|
this.valueSelected=value;
|
103
|
}
|
104
|
|
105
|
public void setBlackList(ArrayList<String> blackList){
|
106
|
this.blacklist = blackList;
|
107
|
}
|
108
|
|
109
|
public AutoCompleteWidget(String type, String placeholder,String table, String field,ArrayList<String> blacklist) {
|
110
|
|
111
|
this.blacklist = blacklist;
|
112
|
this.table = table;
|
113
|
this.field = field;
|
114
|
masterPanel.addStyleName("autoCompleteWidget");
|
115
|
masterPanel.add(inputTextBox);
|
116
|
loadingLabel.setText("Loading..");
|
117
|
loadingLabel.addStyleName("suggestionInfoItemLoading");
|
118
|
inputTextBox.addStyleName("filterTextBox");
|
119
|
inputTextBox.getElement().setAttribute("placeholder", placeholder);
|
120
|
this.inputTextBox.setFocus(true);
|
121
|
inputTextBox.addKeyUpHandler(new KeyUpHandler() {
|
122
|
|
123
|
@Override
|
124
|
public void onKeyUp(KeyUpEvent event) {
|
125
|
if(timer==null) {
|
126
|
|
127
|
timer = new Timer() {
|
128
|
|
129
|
@Override
|
130
|
public void run() {
|
131
|
bringRecommendations();
|
132
|
}
|
133
|
};
|
134
|
timer.schedule(750);
|
135
|
|
136
|
} else {
|
137
|
|
138
|
timer.cancel();
|
139
|
timer = new Timer() {
|
140
|
|
141
|
@Override
|
142
|
public void run() {
|
143
|
bringRecommendations();
|
144
|
}
|
145
|
};
|
146
|
timer.schedule(750);
|
147
|
}
|
148
|
}
|
149
|
});
|
150
|
}
|
151
|
|
152
|
public void bringRecommendations(){
|
153
|
if(!inputTextBox.getText().equals("") && inputTextBox.getText().length()>=1){
|
154
|
suggestionList.clear();
|
155
|
masterPanel.add(suggestionList);
|
156
|
suggestionList.addStyleName("suggestionList");
|
157
|
|
158
|
Image tempImage = new Image("./loading_background.gif");
|
159
|
tempImage.addStyleName("loading-image-suggestion");
|
160
|
|
161
|
suggestionList.add(loadingLabel);
|
162
|
suggestionList.add(tempImage);
|
163
|
|
164
|
AjaxRequest(BASE_URL + "getFiltersData.php?table="+this.table+"&field="+this.field+"&input="+inputTextBox.getText()+"&type=final");
|
165
|
if(searchingListener!=null){
|
166
|
searchingListener.onSearch();
|
167
|
}
|
168
|
}else
|
169
|
masterPanel.remove(AutoCompleteWidget.this.suggestionList);
|
170
|
}
|
171
|
|
172
|
private void AjaxRequest (final String URL){
|
173
|
|
174
|
|
175
|
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL);
|
176
|
|
177
|
try {
|
178
|
|
179
|
|
180
|
Request request = builder.sendRequest(null, new RequestCallback() {
|
181
|
|
182
|
@Override
|
183
|
public void onResponseReceived(Request request, Response response) {
|
184
|
|
185
|
|
186
|
if(response.getStatusCode()==200){
|
187
|
suggestionList.clear();
|
188
|
filtersData = JsonUtils.safeEval(response.getText().toString());
|
189
|
if(filtersData.getData()!=null && filtersData.getData().length()!=0){
|
190
|
for(int i=0;i<filtersData.getData().length();i++){
|
191
|
final Anchor anchor = new Anchor();
|
192
|
anchor.setText(filtersData.getData().get(i).toString());
|
193
|
anchor.addStyleName("suggestionItem");
|
194
|
anchor.getElement().setId(filtersData.getData().get(i).toString());
|
195
|
anchor.addClickHandler(new ClickHandler() {
|
196
|
|
197
|
@Override
|
198
|
public void onClick(ClickEvent event) {
|
199
|
blacklist.add(anchor.getText());
|
200
|
inputTextBox.setValue(anchor.getText());
|
201
|
inputTextBox.setText(anchor.getText());
|
202
|
inputTextBox.setEnabled(false);
|
203
|
masterPanel.remove(AutoCompleteWidget.this.suggestionList);
|
204
|
AutoCompleteWidget.this.valueSelected=anchor.getText();
|
205
|
if(addingListener!=null){
|
206
|
addingListener.onEvent(anchor.getText());
|
207
|
}
|
208
|
}
|
209
|
});
|
210
|
boolean found=false;
|
211
|
for(int j=0;j<blacklist.size();j++){
|
212
|
if(blacklist.get(j).equals(filtersData.getData().get(i).toString())){
|
213
|
anchor.getElement().getStyle().setTextDecoration(TextDecoration.LINE_THROUGH);
|
214
|
found=true;
|
215
|
break;
|
216
|
}
|
217
|
|
218
|
}
|
219
|
if(found){
|
220
|
anchor.getElement().getStyle().setTextDecoration(TextDecoration.LINE_THROUGH);
|
221
|
anchor.setText(anchor.getText()+"(added)");
|
222
|
anchor.getElement().getStyle().setProperty("pointer-events", "none");
|
223
|
}else{
|
224
|
|
225
|
}
|
226
|
suggestionList.add(anchor);
|
227
|
|
228
|
}
|
229
|
}else{
|
230
|
Label label = new Label("No result found");
|
231
|
label.addStyleName("suggestionInfoItem");
|
232
|
suggestionList.add(label);
|
233
|
}
|
234
|
if(searchingListener!=null){
|
235
|
searchingListener.onReturn();
|
236
|
}
|
237
|
suggestionList.addStyleName("suggestionList");
|
238
|
masterPanel.remove(AutoCompleteWidget.this.suggestionList);
|
239
|
masterPanel.add(suggestionList);
|
240
|
AutoCompleteWidget.this.suggestionList = suggestionList;
|
241
|
}else{
|
242
|
DisplayError("Could not retrieve JSON");
|
243
|
}
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
public void onError(Request request, Throwable exception) {
|
248
|
DisplayError("Could not retrieve JSON2 ");
|
249
|
}
|
250
|
});
|
251
|
} catch (RequestException e) {
|
252
|
// TODO Auto-generated catch block
|
253
|
e.printStackTrace();
|
254
|
DisplayError("Could not retrieve JSON3");
|
255
|
}
|
256
|
}
|
257
|
|
258
|
|
259
|
public void DisplayError(String msg){
|
260
|
Window.alert(msg);
|
261
|
|
262
|
}
|
263
|
@Override
|
264
|
public Widget asWidget() {
|
265
|
return masterPanel;
|
266
|
}
|
267
|
|
268
|
public void addStyleName(String styleName) {
|
269
|
masterPanel.addStyleName(styleName);
|
270
|
}
|
271
|
}
|