1
|
package eu.dnetlib.client.widgets;
|
2
|
|
3
|
import com.github.gwtbootstrap.client.ui.base.IconAnchor;
|
4
|
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
|
5
|
import com.google.gwt.core.client.GWT;
|
6
|
import com.google.gwt.event.dom.client.*;
|
7
|
import com.google.gwt.user.client.Timer;
|
8
|
import com.google.gwt.user.client.Window;
|
9
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
10
|
import com.google.gwt.user.client.ui.FlowPanel;
|
11
|
import com.google.gwt.user.client.ui.IsWidget;
|
12
|
import com.google.gwt.user.client.ui.Label;
|
13
|
import com.google.gwt.user.client.ui.Widget;
|
14
|
import com.sencha.gxt.widget.core.client.info.Info;
|
15
|
import eu.dnetlib.goldoa.domain.*;
|
16
|
import eu.dnetlib.goldoa.service.SearchManager;
|
17
|
|
18
|
import java.util.List;
|
19
|
|
20
|
/**
|
21
|
* Created by stefania on 3/2/15.
|
22
|
*/
|
23
|
public class AutoCompleteWidget implements IsWidget {
|
24
|
|
25
|
private FlowPanel contentPanel = new FlowPanel();
|
26
|
|
27
|
private TextBox filter = new TextBox();
|
28
|
private FlowPanel suggestionList = new FlowPanel();
|
29
|
|
30
|
private AutoCompleteServiceAsync autoCompleteVocabularyService = GWT.create(AutoCompleteService.class);
|
31
|
|
32
|
private int minimumNumberOfCharacters = 3;
|
33
|
|
34
|
private AutoCompleteListener autoCompleteListener;
|
35
|
|
36
|
private Timer timer;
|
37
|
|
38
|
public AutoCompleteWidget(final String type, String placeholder) {
|
39
|
|
40
|
contentPanel.addStyleName("autoCompleteWidget");
|
41
|
|
42
|
filter.addStyleName("filterTextBox");
|
43
|
filter.setPlaceholder(placeholder);
|
44
|
filter.setAlternateSize(AlternateSize.XLARGE);
|
45
|
|
46
|
filter.setValueChangeHandler(new ValueChangeHandler() {
|
47
|
@Override
|
48
|
public void handle(final ValueChangeEvent valueChangeEvent) {
|
49
|
|
50
|
suggestionList.clear();
|
51
|
|
52
|
if(valueChangeEvent.getNewValue().length()<minimumNumberOfCharacters) {
|
53
|
|
54
|
Label label = new Label();
|
55
|
label.addStyleName("suggestionInfoItem");
|
56
|
suggestionList.add(label);
|
57
|
|
58
|
if(minimumNumberOfCharacters-valueChangeEvent.getNewValue().length()==1) {
|
59
|
label.setText("Please add 1 more character");
|
60
|
} else {
|
61
|
label.setText("Please add " + (minimumNumberOfCharacters-valueChangeEvent.getNewValue().length()) + " more characters");
|
62
|
}
|
63
|
|
64
|
} else {
|
65
|
|
66
|
if(timer==null) {
|
67
|
|
68
|
timer = new Timer() {
|
69
|
|
70
|
@Override
|
71
|
public void run() {
|
72
|
createSuggestionList(type, valueChangeEvent.getNewValue());
|
73
|
}
|
74
|
};
|
75
|
timer.schedule(300);
|
76
|
|
77
|
} else {
|
78
|
|
79
|
timer.cancel();
|
80
|
timer = new Timer() {
|
81
|
|
82
|
@Override
|
83
|
public void run() {
|
84
|
createSuggestionList(type, valueChangeEvent.getNewValue());
|
85
|
}
|
86
|
};
|
87
|
timer.schedule(300);
|
88
|
}
|
89
|
}
|
90
|
}
|
91
|
});
|
92
|
|
93
|
suggestionList.addStyleName("suggestionList");
|
94
|
|
95
|
contentPanel.add(filter);
|
96
|
|
97
|
filter.addFocusHandler(new FocusHandler() {
|
98
|
@Override
|
99
|
public void onFocus(FocusEvent event) {
|
100
|
|
101
|
suggestionList.clear();
|
102
|
|
103
|
filter.selectAll();
|
104
|
|
105
|
int filterValueLength = filter.getValue().trim().length();
|
106
|
if(filterValueLength < minimumNumberOfCharacters) {
|
107
|
|
108
|
Label label = new Label();
|
109
|
label.addStyleName("suggestionInfoItem");
|
110
|
suggestionList.add(label);
|
111
|
|
112
|
if(minimumNumberOfCharacters-filterValueLength==1) {
|
113
|
label.setText("Please add 1 more character");
|
114
|
} else {
|
115
|
label.setText("Please add " + (minimumNumberOfCharacters-filterValueLength) + " more characters");
|
116
|
}
|
117
|
} else {
|
118
|
createSuggestionList(type, filter.getValue().trim());
|
119
|
}
|
120
|
|
121
|
contentPanel.add(suggestionList);
|
122
|
}
|
123
|
});
|
124
|
|
125
|
filter.addBlurHandler(new BlurHandler() {
|
126
|
@Override
|
127
|
public void onBlur(BlurEvent event) {
|
128
|
Timer timer = new Timer() {
|
129
|
@Override
|
130
|
public void run() {
|
131
|
contentPanel.remove(suggestionList);
|
132
|
}
|
133
|
};
|
134
|
timer.schedule(50);
|
135
|
}
|
136
|
});
|
137
|
}
|
138
|
|
139
|
|
140
|
@Override
|
141
|
public Widget asWidget() {
|
142
|
return contentPanel;
|
143
|
}
|
144
|
|
145
|
public void setValue(String value) {
|
146
|
filter.setValue(value, false);
|
147
|
}
|
148
|
|
149
|
public interface AutoCompleteListener {
|
150
|
public void valueSelected(Vocabulary vocabulary);
|
151
|
}
|
152
|
|
153
|
public void setAutoCompleteListener(AutoCompleteListener autoCompleteListener) {
|
154
|
this.autoCompleteListener = autoCompleteListener;
|
155
|
}
|
156
|
|
157
|
private void createSuggestionList(final String type, String matchString) {
|
158
|
|
159
|
autoCompleteVocabularyService.getAutoCompleteVocabulary(type, matchString, new AsyncCallback<List<Vocabulary>>() {
|
160
|
|
161
|
@Override
|
162
|
public void onFailure(Throwable caught) {
|
163
|
|
164
|
}
|
165
|
|
166
|
@Override
|
167
|
public void onSuccess(List<Vocabulary> vocabularyList) {
|
168
|
|
169
|
FlowPanel suggestionList = new FlowPanel();
|
170
|
|
171
|
if (!vocabularyList.isEmpty()) {
|
172
|
for (final Vocabulary vocabulary:vocabularyList) {
|
173
|
|
174
|
Anchor anchor = new Anchor();
|
175
|
anchor.setText(vocabulary.getName());
|
176
|
anchor.getElement().setId(vocabulary.getId());
|
177
|
anchor.addStyleName("suggestionItem");
|
178
|
anchor.addMouseDownHandler(new MouseDownHandler() {
|
179
|
@Override
|
180
|
public void onMouseDown(MouseDownEvent mouseDownEvent) {
|
181
|
filter.setValue(vocabulary.getName());
|
182
|
if(autoCompleteListener!=null)
|
183
|
autoCompleteListener.valueSelected(vocabulary);
|
184
|
}
|
185
|
});
|
186
|
suggestionList.add(anchor);
|
187
|
}
|
188
|
} else {
|
189
|
Label label = new Label("No results found");
|
190
|
label.addStyleName("suggestionInfoItem");
|
191
|
suggestionList.add(label);
|
192
|
}
|
193
|
|
194
|
suggestionList.addStyleName("suggestionList");
|
195
|
contentPanel.remove(AutoCompleteWidget.this.suggestionList);
|
196
|
contentPanel.add(suggestionList);
|
197
|
AutoCompleteWidget.this.suggestionList = suggestionList;
|
198
|
}
|
199
|
});
|
200
|
}
|
201
|
|
202
|
public void addStyleName(String styleName) {
|
203
|
contentPanel.addStyleName(styleName);
|
204
|
}
|
205
|
|
206
|
public String getValue() {
|
207
|
return filter.getValue().trim();
|
208
|
}
|
209
|
|
210
|
public void setVisible(boolean visible) {
|
211
|
filter.setVisible(visible);
|
212
|
}
|
213
|
}
|