Project

General

Profile

1
package eu.dnetlib.client.widgets;
2

    
3
import com.github.gwtbootstrap.client.ui.*;
4
import com.github.gwtbootstrap.client.ui.constants.AlertType;
5
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
6
import com.github.gwtbootstrap.client.ui.constants.BackdropType;
7
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
8
import com.google.gwt.event.dom.client.ChangeEvent;
9
import com.google.gwt.event.dom.client.ChangeHandler;
10
import com.google.gwt.event.dom.client.ClickEvent;
11
import com.google.gwt.event.dom.client.ClickHandler;
12
import com.google.gwt.user.client.ui.FlowPanel;
13
import eu.dnetlib.goldoa.domain.CommentTemplate;
14

    
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18

    
19
/**
20
 * Created by stefania on 11/17/15.
21
 */
22
public class ConfirmActionModal {
23

    
24
    private Modal actionConfirmationModal = new Modal();
25

    
26
    private FlowPanel actionConfirmationPanel = new FlowPanel();
27

    
28
    private Alert errorLabel = new Alert();
29

    
30
    private Form commentsForm = new Form();
31
    private ListBox commentTemplatesListBox = new ListBox();
32
    private TextArea comments = new TextArea();
33

    
34
    private ModalFooter modalFooter = new ModalFooter();
35
    private FlowPanel actionButtons = new FlowPanel();
36
    private Button cancelButton = new Button();
37
    private Button okButton = new Button();
38

    
39
    private Map<String, CommentTemplate> commentTemplatesMap = new HashMap<>();
40

    
41
    private ActionConfirmedListener actionConfirmedListener;
42

    
43
    public ConfirmActionModal(String action, final List<CommentTemplate> commentTemplates) {
44

    
45
        actionConfirmationModal.setTitle(action + " Confirmation");
46

    
47
        actionConfirmationModal.add(actionConfirmationPanel);
48

    
49
        errorLabel.setType(AlertType.ERROR);
50
        errorLabel.setVisible(false);
51
        errorLabel.setClose(false);
52

    
53
        actionConfirmationPanel.add(errorLabel);
54

    
55
        actionConfirmationPanel.add(commentsForm);
56

    
57
        if(commentTemplates!=null && !commentTemplates.isEmpty()) {
58

    
59
            for(CommentTemplate commentTemplate : commentTemplates) {
60
                commentTemplatesListBox.addItem(commentTemplate.getName(), commentTemplate.getId());
61
                commentTemplatesMap.put(commentTemplate.getId(), commentTemplate);
62
            }
63

    
64
            commentTemplatesListBox.setAlternateSize(AlternateSize.XXLARGE);
65
            commentsForm.add(new FormFieldSet("Reason (*)", commentTemplatesListBox));
66

    
67
            commentTemplatesListBox.addChangeHandler(new ChangeHandler() {
68

    
69
                @Override
70
                public void onChange(ChangeEvent event) {
71
                    comments.setValue(commentTemplatesMap.get(commentTemplatesListBox.getSelectedValue()).getComment());
72
                }
73
            });
74

    
75
            comments.setValue(commentTemplatesMap.get(commentTemplatesListBox.getValue(0)).getComment());
76
        }
77

    
78
        comments.setAlternateSize(AlternateSize.XXLARGE);
79
        commentsForm.add(new FormFieldSet("Comment (*)", comments));
80

    
81
        actionButtons.addStyleName("confirmationModalButtons");
82

    
83
        modalFooter.add(actionButtons);
84
        actionConfirmationModal.add(modalFooter);
85

    
86
        cancelButton.setText("Cancel");
87
        cancelButton.setType(ButtonType.DEFAULT);
88
        cancelButton.addClickHandler(new ClickHandler() {
89
            @Override
90
            public void onClick(ClickEvent event) {
91
                hide();
92
            }
93
        });
94
        actionButtons.add(cancelButton);
95

    
96

    
97
        okButton.setText("Ok");
98
        okButton.setType(ButtonType.PRIMARY);
99
        okButton.addClickHandler(new ClickHandler() {
100
            @Override
101
            public void onClick(ClickEvent event) {
102

    
103
                errorLabel.setVisible(false);
104

    
105
                if(comments.getValue()==null || comments.getValue().trim().isEmpty()) {
106
                    errorLabel.setVisible(true);
107
                    errorLabel.setText("Providing a comment is mandatory");
108
                } else {
109
                    if(actionConfirmedListener!=null) {
110
                        if(commentTemplates!=null && !commentTemplates.isEmpty())
111
                            actionConfirmedListener.actionConfirmed(comments.getValue().trim(), commentTemplatesListBox.getSelectedValue());
112
                        else
113
                            actionConfirmedListener.actionConfirmed(comments.getValue().trim(), null);
114
                    }
115
                    hide();
116
                }
117
            }
118
        });
119
        actionButtons.add(okButton);
120

    
121
        actionConfirmationModal.addStyleName("confirmationModal");
122
        actionConfirmationModal.setAnimation(true);
123
        actionConfirmationModal.setBackdrop(BackdropType.STATIC);
124
    }
125

    
126
    public void show() {
127
        actionConfirmationModal.show();
128
    }
129

    
130
    public void hide() {
131
        actionConfirmationModal.hide();
132
        actionConfirmationModal.removeFromParent();
133
    }
134

    
135
    public interface ActionConfirmedListener {
136
        void actionConfirmed(String comment, String templateId);
137
    }
138

    
139
    public void setActionConfirmedListener(ActionConfirmedListener actionConfirmedListener) {
140
        this.actionConfirmedListener = actionConfirmedListener;
141
    }
142
}
(9-9/21)