Project

General

Profile

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
import java.util.logging.Level;
20
import java.util.logging.Logger;
21

    
22
/**
23
 * Created by stefania on 3/2/15.
24
 */
25
public class AutoCompleteWidget implements IsWidget {
26

    
27
    private FlowPanel contentPanel = new FlowPanel();
28

    
29
    private TextBox filter = new TextBox();
30
    private FlowPanel suggestionList = new FlowPanel();
31

    
32
    private AutoCompleteServiceAsync autoCompleteVocabularyService = GWT.create(AutoCompleteService.class);
33

    
34
    private int minimumNumberOfCharacters = 3;
35

    
36
    private AutoCompleteListener autoCompleteListener;
37

    
38
    private Timer timer;
39

    
40
    public AutoCompleteWidget(final String type, String placeholder) {
41

    
42
        contentPanel.addStyleName("autoCompleteWidget");
43

    
44
        filter.addStyleName("filterTextBox");
45
        filter.setPlaceholder(placeholder);
46
        filter.setAlternateSize(AlternateSize.XLARGE);
47

    
48
        filter.setValueChangeHandler(new ValueChangeHandler() {
49
            @Override
50
            public void handle(final ValueChangeEvent valueChangeEvent) {
51

    
52
                suggestionList.clear();
53

    
54
                if(valueChangeEvent.getNewValue().length()<minimumNumberOfCharacters) {
55

    
56
                    Label label = new Label();
57
                    label.addStyleName("suggestionInfoItem");
58
                    suggestionList.add(label);
59

    
60
                    if(minimumNumberOfCharacters-valueChangeEvent.getNewValue().length()==1) {
61
                        label.setText("Please add 1 more character");
62
                    } else {
63
                        label.setText("Please add " + (minimumNumberOfCharacters-valueChangeEvent.getNewValue().length()) + " more characters");
64
                    }
65

    
66
                } else {
67

    
68
                    if(timer==null) {
69

    
70
                        timer = new Timer() {
71

    
72
                            @Override
73
                            public void run() {
74
                                createSuggestionList(type, valueChangeEvent.getNewValue());
75
                            }
76
                        };
77
                        timer.schedule(300);
78

    
79
                    } else {
80

    
81
                        timer.cancel();
82
                        timer = new Timer() {
83

    
84
                            @Override
85
                            public void run() {
86
                                createSuggestionList(type, valueChangeEvent.getNewValue());
87
                            }
88
                        };
89
                        timer.schedule(300);
90
                    }
91
                }
92
            }
93
        });
94

    
95
        suggestionList.addStyleName("suggestionList");
96

    
97
        contentPanel.add(filter);
98

    
99
        filter.addFocusHandler(new FocusHandler() {
100
            @Override
101
            public void onFocus(FocusEvent event) {
102

    
103
                suggestionList.clear();
104

    
105
                filter.selectAll();
106

    
107
                int filterValueLength = filter.getValue().trim().length();
108
                if(filterValueLength < minimumNumberOfCharacters) {
109

    
110
                    Label label = new Label();
111
                    label.addStyleName("suggestionInfoItem");
112
                    suggestionList.add(label);
113

    
114
                    if(minimumNumberOfCharacters-filterValueLength==1) {
115
                        label.setText("Please add 1 more character");
116
                    } else {
117
                        label.setText("Please add " + (minimumNumberOfCharacters-filterValueLength) + " more characters");
118
                    }
119
                } else {
120
                    createSuggestionList(type, filter.getValue().trim());
121
                }
122

    
123
                contentPanel.add(suggestionList);
124
            }
125
        });
126

    
127
        filter.addBlurHandler(new BlurHandler() {
128
            @Override
129
            public void onBlur(BlurEvent event) {
130
                Timer timer = new Timer() {
131
                    @Override
132
                    public void run() {
133
                        contentPanel.remove(suggestionList);
134
                    }
135
                };
136
                timer.schedule(50);
137
            }
138
        });
139
    }
140

    
141

    
142
    @Override
143
    public Widget asWidget() {
144
        return contentPanel;
145
    }
146

    
147
    public void setValue(String value) {
148
        filter.setValue(value, false);
149
    }
150

    
151
    public interface AutoCompleteListener {
152
        public void valueSelected(Vocabulary vocabulary);
153
    }
154

    
155
    public void setAutoCompleteListener(AutoCompleteListener autoCompleteListener) {
156
        this.autoCompleteListener = autoCompleteListener;
157
    }
158

    
159
    private void createSuggestionList(final String type, String matchString) {
160

    
161
        autoCompleteVocabularyService.getAutoCompleteVocabulary(type, matchString, new AsyncCallback<List<Vocabulary>>() {
162

    
163
            @Override
164
            public void onFailure(Throwable caught) {
165

    
166
                Window.alert("Error:" + caught.getClass().getName());
167
                Logger.getLogger("gold").log(Level.SEVERE, "error in autocomplete ", caught);
168
            }
169

    
170
            @Override
171
            public void onSuccess(List<Vocabulary> vocabularyList) {
172

    
173
                FlowPanel suggestionList = new FlowPanel();
174

    
175
                if (!vocabularyList.isEmpty()) {
176
                    for (final Vocabulary vocabulary:vocabularyList) {
177

    
178
                        Anchor anchor = new Anchor();
179
                        anchor.setText(vocabulary.getName());
180
                        anchor.getElement().setId(vocabulary.getId());
181
                        anchor.addStyleName("suggestionItem");
182
                        anchor.addMouseDownHandler(new MouseDownHandler() {
183
                            @Override
184
                            public void onMouseDown(MouseDownEvent mouseDownEvent) {
185
                                filter.setValue(vocabulary.getName());
186
                                if(autoCompleteListener!=null)
187
                                    autoCompleteListener.valueSelected(vocabulary);
188
                            }
189
                        });
190
                        suggestionList.add(anchor);
191
                    }
192
                } else {
193
                    Label label = new Label("No results found");
194
                    label.addStyleName("suggestionInfoItem");
195
                    suggestionList.add(label);
196
                }
197

    
198
                suggestionList.addStyleName("suggestionList");
199
                contentPanel.remove(AutoCompleteWidget.this.suggestionList);
200
                contentPanel.add(suggestionList);
201
                AutoCompleteWidget.this.suggestionList = suggestionList;
202
            }
203
        });
204
    }
205

    
206
    public void addStyleName(String styleName) {
207
        contentPanel.addStyleName(styleName);
208
    }
209

    
210
    public String getValue() {
211
        return filter.getValue().trim();
212
    }
213

    
214
    public void setVisible(boolean visible) {
215
        filter.setVisible(visible);
216
    }
217
}
(5-5/21)