Project

General

Profile

1
package eu.dnetlib.client.user;
2

    
3
import com.github.gwtbootstrap.client.ui.Alert;
4
import com.github.gwtbootstrap.client.ui.Form;
5
import com.github.gwtbootstrap.client.ui.ListBox;
6
import com.github.gwtbootstrap.client.ui.constants.AlertType;
7
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
8
import com.github.gwtbootstrap.client.ui.constants.FormType;
9
import com.google.gwt.event.dom.client.ChangeEvent;
10
import com.google.gwt.event.dom.client.ChangeHandler;
11
import com.google.gwt.i18n.client.DateTimeFormat;
12
import com.google.gwt.user.client.ui.FlowPanel;
13
import com.google.gwt.user.client.ui.HTML;
14
import com.google.gwt.user.client.ui.IsWidget;
15
import com.google.gwt.user.client.ui.Widget;
16
import eu.dnetlib.client.widgets.FormFieldSet;
17
import eu.dnetlib.client.widgets.TextBox;
18
import eu.dnetlib.client.widgets.ValueChangeEvent;
19
import eu.dnetlib.client.widgets.ValueChangeHandler;
20
import eu.dnetlib.goldoa.domain.Project;
21

    
22
import java.util.ArrayList;
23
import java.util.Collections;
24
import java.util.Comparator;
25
import java.util.List;
26

    
27
/**
28
 * Created by stefania on 4/8/15.
29
 */
30
public class FutureEligibleProjectsInfoElement implements IsWidget {
31

    
32
    private FlowPanel futureEligibleProjectsPanel = new FlowPanel();
33

    
34
    private TextBox search = new TextBox();
35
    private Form sortByForm = new Form();
36
    private ListBox sortByListForm = new ListBox();
37

    
38
    private FlowPanel futureEligibleProjectsInfoPanel = new FlowPanel();
39
    private HTML futureEligibleProjectsInfo = new HTML();
40
    private String futureEligibleProjectsInfoContents = "";
41
    private Alert futureEligibleProjectsWarningLabel = new Alert();
42

    
43
    private DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy/MM/dd");
44

    
45
    private List<Project> allProjects = new ArrayList<>();
46
    private List<Project> matchingProjects = new ArrayList<>();
47

    
48
    public FutureEligibleProjectsInfoElement(List<Project> projectList) {
49

    
50
        allProjects.addAll(projectList);
51
        matchingProjects.addAll(projectList);
52

    
53
        sortRequestsByAcronym();
54

    
55
        futureEligibleProjectsWarningLabel.setText("No projects matched your criteria");
56
        futureEligibleProjectsWarningLabel.addStyleName("alertLabel");
57
        futureEligibleProjectsWarningLabel.setType(AlertType.WARNING);
58
        futureEligibleProjectsWarningLabel.setClose(false);
59
        futureEligibleProjectsWarningLabel.setVisible(false);
60

    
61
        search.addStyleName("searchTextBox");
62
        search.setPlaceholder("Filter...");
63
        search.setValueChangeHandler(new ValueChangeHandler() {
64
            @Override
65
            public void handle(ValueChangeEvent valueChangeEvent) {
66

    
67
                matchingProjects.clear();
68
                futureEligibleProjectsWarningLabel.setVisible(false);
69

    
70
                if(valueChangeEvent.getNewValue()!=null && !valueChangeEvent.getNewValue().trim().equals("")) {
71
                    for (Project project : allProjects) {
72
                        if (project.getAcronym().toLowerCase().contains(valueChangeEvent.getNewValue().toLowerCase())
73
                                || dtf.format(project.getEndDate()).contains(valueChangeEvent.getNewValue().toLowerCase())
74
                                || project.getGrant().toLowerCase().contains(valueChangeEvent.getNewValue().toLowerCase())) {
75

    
76
                            matchingProjects.add(project);
77
                        }
78
                    }
79
                    redrawProjectList();
80
                    if(matchingProjects.size()==0) {
81
                        futureEligibleProjectsWarningLabel.setVisible(true);
82
                        futureEligibleProjectsInfoPanel.clear();
83
                    }
84
                } else {
85
                    matchingProjects.addAll(allProjects);
86
                    redrawProjectList();
87
                }
88
            }
89
        });
90

    
91
        sortByListForm.addItem("Acronym", "acronym");
92
        sortByListForm.addItem("Name", "name");
93
        sortByListForm.addItem("Grant", "ga");
94
        sortByListForm.addItem("Date", "date");
95
        sortByListForm.setAlternateSize(AlternateSize.SMALL);
96
        sortByListForm.addChangeHandler(new ChangeHandler() {
97
            @Override
98
            public void onChange(ChangeEvent event) {
99
                if(sortByListForm.getValue().equals("date")) {
100
                    sortRequestsByDate();
101
                } else if(sortByListForm.getValue().equals("acronym")){
102
                    sortRequestsByAcronym();
103
                } else if(sortByListForm.getValue().equals("name")) {
104
                    sortRequestsByName();
105
                } else {
106
                    sortRequestsByGA();
107
                }
108
            }
109
        });
110
        sortByForm.setType(FormType.HORIZONTAL);
111
        sortByForm.add(new FormFieldSet("Sort by", sortByListForm));
112
        sortByForm.addStyleName("float-right");
113

    
114
        futureEligibleProjectsPanel.add(sortByForm);
115
        futureEligibleProjectsPanel.add(search);
116
        futureEligibleProjectsPanel.add(futureEligibleProjectsWarningLabel);
117
        futureEligibleProjectsPanel.add(futureEligibleProjectsInfoPanel);
118

    
119
        if(projectList.isEmpty()) {
120
            futureEligibleProjectsWarningLabel.setVisible(true);
121
        }
122
    }
123

    
124
    @Override
125
    public Widget asWidget() {
126
        return futureEligibleProjectsPanel;
127
    }
128

    
129
    public void addStyleName(String styleName) {
130
        futureEligibleProjectsPanel.addStyleName(styleName);
131
    }
132

    
133
    private void redrawProjectList() {
134

    
135
        futureEligibleProjectsInfoPanel.clear();
136
        futureEligibleProjectsInfoContents = "";
137

    
138
        for(int i=0; i<matchingProjects.size(); i++) {
139
            if(((i+1)%2)==1)
140
                futureEligibleProjectsInfoContents += "<dd class=\"odd\">";
141
            else
142
                futureEligibleProjectsInfoContents += "<dd>";
143

    
144
            if (matchingProjects.get(i).getAcronym() != null)
145
                futureEligibleProjectsInfoContents += "<span class=\"strong\">" + matchingProjects.get(i).getAcronym() + "</span>";
146

    
147
            if (matchingProjects.get(i).getGrant() !=null)
148
                futureEligibleProjectsInfoContents += "<span class=\"strong\"> - " + matchingProjects.get(i).getGrant() + "</span>";
149

    
150
            if (matchingProjects.get(i).getTitle() != null)
151
                futureEligibleProjectsInfoContents += " \"" + matchingProjects.get(i).getTitle() + "\" ";
152

    
153
            if (matchingProjects.get(i).getEndDate()!=null)
154
                futureEligibleProjectsInfoContents += " (ends " + dtf.format(matchingProjects.get(i).getEndDate()) + ")";
155

    
156
            futureEligibleProjectsInfoContents += "</dd>";
157
        }
158
        futureEligibleProjectsInfo.setHTML(futureEligibleProjectsInfoContents);
159

    
160
        futureEligibleProjectsInfoPanel.add(futureEligibleProjectsInfo);
161
    }
162

    
163
    private void sortRequestsByDate() {
164

    
165
        Collections.sort(matchingProjects, new Comparator<Project>() {
166

    
167
            public int compare(Project p1, Project p2) {
168
                return p1.getEndDate().compareTo(p2.getEndDate());
169
            }
170
        });
171
        Collections.sort(allProjects, new Comparator<Project>() {
172

    
173
            public int compare(Project p1, Project p2) {
174
                return p1.getEndDate().compareTo(p2.getEndDate());
175
            }
176
        });
177
        redrawProjectList();
178
    }
179

    
180
    private void sortRequestsByAcronym() {
181

    
182
        Collections.sort(matchingProjects, new Comparator<Project>() {
183

    
184
            public int compare(Project p1, Project p2) {
185
                return p1.getAcronym().toLowerCase().compareTo(p2.getAcronym().toLowerCase());
186
            }
187
        });
188
        Collections.sort(allProjects, new Comparator<Project>() {
189

    
190
            public int compare(Project p1, Project p2) {
191
                return p1.getAcronym().toLowerCase().compareTo(p2.getAcronym().toLowerCase());
192
            }
193
        });
194

    
195
        redrawProjectList();
196
    }
197

    
198
    private void sortRequestsByName() {
199

    
200
        Collections.sort(matchingProjects, new Comparator<Project>() {
201

    
202
            public int compare(Project p1, Project p2) {
203
                return p1.getTitle().toLowerCase().compareTo(p2.getTitle().toLowerCase());
204
            }
205
        });
206
        Collections.sort(allProjects, new Comparator<Project>() {
207

    
208
            public int compare(Project p1, Project p2) {
209
                return p1.getTitle().toLowerCase().compareTo(p2.getTitle().toLowerCase());
210
            }
211
        });
212
        redrawProjectList();
213
    }
214

    
215
    private void sortRequestsByGA() {
216

    
217
        Collections.sort(matchingProjects, new Comparator<Project>() {
218

    
219
            public int compare(Project p1, Project p2) {
220
                return p1.getGrant().toLowerCase().compareTo(p2.getGrant().toLowerCase());
221
            }
222
        });
223
        Collections.sort(allProjects, new Comparator<Project>() {
224

    
225
            public int compare(Project p1, Project p2) {
226
                return p1.getGrant().toLowerCase().compareTo(p2.getGrant().toLowerCase());
227
            }
228
        });
229
        redrawProjectList();
230
    }
231
}
(4-4/9)