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