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.IconType;
5
import com.google.gwt.event.dom.client.ClickEvent;
6
import com.google.gwt.event.dom.client.ClickHandler;
7
import com.google.gwt.user.client.ui.FlowPanel;
8
import com.google.gwt.user.client.ui.IsWidget;
9
import com.google.gwt.user.client.ui.Widget;
10
import eu.dnetlib.goldoa.domain.Affiliation;
11
import eu.dnetlib.goldoa.domain.Organization;
12
import eu.dnetlib.goldoa.domain.Person;
13

    
14
import java.util.ArrayList;
15
import java.util.List;
16

    
17
/**
18
 * Created by stefania on 3/11/15.
19
 */
20
public class MultipleAuthorsWidget implements IsWidget {
21

    
22
    private FlowPanel multipleAuthorsPanel = new FlowPanel();
23
    private List<AuthorInfoWidget> authorInfoWidgetList = new ArrayList<>();
24

    
25
    private IconAnchor addMore = new IconAnchor();
26

    
27
    private Organization defaultOrganization = null;
28

    
29
    public MultipleAuthorsWidget() {
30

    
31
        final AuthorInfoWidget authorInfoWidget = new AuthorInfoWidget();
32
        if(defaultOrganization!=null)
33
            loadDefaultAffiliation(defaultOrganization, authorInfoWidget);
34

    
35
        authorInfoWidgetList.add(authorInfoWidget);
36
        multipleAuthorsPanel.add(authorInfoWidget.asWidget());
37
        AuthorInfoWidget.DeleteAuthorListener deleteAuthorListener = new AuthorInfoWidget.DeleteAuthorListener() {
38
            @Override
39
            public void removeAuthor() {
40
                authorInfoWidgetList.remove(authorInfoWidget);
41
                multipleAuthorsPanel.remove(authorInfoWidget.asWidget());
42
            }
43
        };
44
        authorInfoWidget.setDeleteAuthorListener(deleteAuthorListener);
45

    
46
        addMore.setIcon(IconType.PLUS);
47
        addMore.setText("Add another author");
48
        addMore.addClickHandler(new ClickHandler() {
49
            @Override
50
            public void onClick(ClickEvent clickEvent) {
51

    
52
                final AuthorInfoWidget authorInfoWidget = new AuthorInfoWidget();
53
                if(defaultOrganization!=null)
54
                    loadDefaultAffiliation(defaultOrganization, authorInfoWidget);
55

    
56
                authorInfoWidgetList.add(authorInfoWidget);
57
                multipleAuthorsPanel.insert(authorInfoWidget.asWidget(), multipleAuthorsPanel.getWidgetIndex(addMore));
58
                AuthorInfoWidget.DeleteAuthorListener deleteAuthorListener = new AuthorInfoWidget.DeleteAuthorListener() {
59
                    @Override
60
                    public void removeAuthor() {
61
                        authorInfoWidgetList.remove(authorInfoWidget);
62
                        multipleAuthorsPanel.remove(authorInfoWidget.asWidget());
63
                    }
64
                };
65
                authorInfoWidget.setDeleteAuthorListener(deleteAuthorListener);
66
            }
67
        });
68
        multipleAuthorsPanel.add(addMore);
69
    }
70

    
71
    @Override
72
    public Widget asWidget() {
73
        return multipleAuthorsPanel;
74
    }
75

    
76
    public List<Affiliation> getAuthors() {
77

    
78
        List<Affiliation> authorsList = new ArrayList<>();
79

    
80
        for(AuthorInfoWidget authorInfoWidget : authorInfoWidgetList) {
81

    
82
            if(authorInfoWidget.getAuthor()!=null)
83
                authorsList.add(authorInfoWidget.getAuthor());
84

    
85
        }
86

    
87
        return authorsList;
88
    }
89

    
90
    public void clear() {
91

    
92
        multipleAuthorsPanel.clear();
93
        authorInfoWidgetList = new ArrayList<>();
94

    
95
        final AuthorInfoWidget authorInfoWidget = new AuthorInfoWidget();
96
        if(defaultOrganization!=null)
97
            loadDefaultAffiliation(defaultOrganization, authorInfoWidget);
98

    
99
        AuthorInfoWidget.DeleteAuthorListener deleteAuthorListener = new AuthorInfoWidget.DeleteAuthorListener() {
100
            @Override
101
            public void removeAuthor() {
102
                authorInfoWidgetList.remove(authorInfoWidget);
103
                multipleAuthorsPanel.remove(authorInfoWidget.asWidget());
104
            }
105
        };
106
        authorInfoWidget.setDeleteAuthorListener(deleteAuthorListener);
107
        authorInfoWidgetList.add(authorInfoWidget);
108
        multipleAuthorsPanel.add(authorInfoWidget.asWidget());
109
        multipleAuthorsPanel.add(addMore);
110
    }
111

    
112
    public void loadAuthors(List<Affiliation> authorsList) {
113

    
114
        if(!authorsList.isEmpty()) {
115
            multipleAuthorsPanel.clear();
116
            authorInfoWidgetList = new ArrayList<>();
117

    
118
            for (Affiliation affiliation : authorsList) {
119
                final AuthorInfoWidget authorInfoWidget = new AuthorInfoWidget();
120
                authorInfoWidget.loadAuthor(affiliation);
121
                AuthorInfoWidget.DeleteAuthorListener deleteAuthorListener = new AuthorInfoWidget.DeleteAuthorListener() {
122
                    @Override
123
                    public void removeAuthor() {
124
                        authorInfoWidgetList.remove(authorInfoWidget);
125
                        multipleAuthorsPanel.remove(authorInfoWidget.asWidget());
126
                    }
127
                };
128
                authorInfoWidget.setDeleteAuthorListener(deleteAuthorListener);
129
                authorInfoWidgetList.add(authorInfoWidget);
130
                multipleAuthorsPanel.add(authorInfoWidget.asWidget());
131
            }
132
            multipleAuthorsPanel.add(addMore);
133
        }
134
    }
135

    
136
    public boolean hasAtLeastOneEmail() {
137

    
138
        boolean hasAtLeastOneEmail = false;
139

    
140
        for(AuthorInfoWidget authorInfoWidget : authorInfoWidgetList) {
141
            if(authorInfoWidget.hasEmailCompleted())
142
                hasAtLeastOneEmail = true;
143

    
144
        }
145

    
146
        return hasAtLeastOneEmail;
147
    }
148

    
149
    public void setDefaultOrganization(Organization organization) {
150

    
151
        defaultOrganization = organization;
152
        for(AuthorInfoWidget authorInfoWidget : authorInfoWidgetList) {
153
            loadDefaultAffiliation(organization, authorInfoWidget);
154
        }
155

    
156
    }
157

    
158
    private void loadDefaultAffiliation(Organization organization, AuthorInfoWidget authorInfoWidget) {
159
        Affiliation affiliation = new Affiliation();
160
        affiliation.setOrganization(organization);
161
        authorInfoWidget.loadAuthor(affiliation);
162
    }
163
}
(12-12/19)