Project

General

Profile

Download (4.61 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.view.registration;
10

    
11
import java.util.Collection;
12
import java.util.List;
13

    
14
import org.apache.commons.lang3.StringUtils;
15
import org.springframework.context.annotation.Scope;
16
import org.springframework.security.core.GrantedAuthority;
17

    
18
import com.vaadin.server.FontAwesome;
19
import com.vaadin.shared.ui.label.ContentMode;
20
import com.vaadin.spring.annotation.SpringComponent;
21
import com.vaadin.ui.Alignment;
22
import com.vaadin.ui.Button;
23
import com.vaadin.ui.HorizontalLayout;
24
import com.vaadin.ui.Label;
25
import com.vaadin.ui.Panel;
26
import com.vaadin.ui.TextArea;
27
import com.vaadin.ui.VerticalLayout;
28

    
29
import eu.etaxonomy.cdm.ext.registration.messages.Message;
30
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
31
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
32
import eu.etaxonomy.vaadin.mvp.AbstractPopupView;
33

    
34
@SpringComponent
35
@Scope("prototype")
36
public class RegistrationMessagesPopup extends AbstractPopupView<RegistrationMessagesPresenter>
37
    implements RegistrationMessagesView, AccessRestrictedView {
38

    
39
    private static final long serialVersionUID = 713522519903334889L;
40

    
41
    Panel messagesPanel;
42

    
43
    TextArea newMessageField;
44

    
45
    Button sendMessageButton;
46

    
47
    private VerticalLayout mainLayout;
48

    
49
    private DelegatingErrorHandler errrorHandler = new DelegatingErrorHandler();
50

    
51
    public RegistrationMessagesPopup() {
52

    
53
        mainLayout = new VerticalLayout();
54
        // IMPORTANT: mainLayout must be set to full size otherwise the
55
        // popup window may have problems with automatic resizing of its
56
        // content.
57
        mainLayout.setSizeFull();
58
        setCompositionRoot(mainLayout);
59
    }
60

    
61
    /**
62
     * {@inheritDoc}
63
     */
64
    @Override
65
    protected void initContent() {
66

    
67
        messagesPanel = new Panel();
68

    
69
        newMessageField = new TextArea();
70
        newMessageField.setNullRepresentation("");
71
        newMessageField.addTextChangeListener(e -> {
72
            sendMessageButton.setEnabled(StringUtils.isNoneBlank(e.getText()));
73
        });
74

    
75
        sendMessageButton = new Button(FontAwesome.SEND);
76
        sendMessageButton.addClickListener(e -> postNewMessage());
77

    
78
        HorizontalLayout sendMessagebar = new HorizontalLayout(newMessageField, sendMessageButton);
79
        sendMessagebar.setComponentAlignment(sendMessageButton, Alignment.MIDDLE_RIGHT);
80
        sendMessagebar.setExpandRatio(newMessageField, 1f);
81

    
82
        mainLayout.addComponents(messagesPanel, sendMessagebar);
83

    
84
        mainLayout.setErrorHandler(errrorHandler);
85
    }
86

    
87
    /**
88
     * @return
89
     */
90
    private void postNewMessage() {
91
        // quick and dirty implementation, better send an event
92
        String text = newMessageField.getValue();
93
        if(StringUtils.isNotBlank(text)){
94
            getPresenter().postMessage(text);
95
            newMessageField.setValue(null);
96
        }
97
    }
98

    
99
    /**
100
     * {@inheritDoc}
101
     */
102
    @Override
103
    public String getWindowCaption() {
104
        return "Messages";
105
    }
106

    
107
    /**
108
     * {@inheritDoc}
109
     */
110
    @Override
111
    public void focusFirst() {
112
        // none
113
    }
114

    
115

    
116
    /**
117
     * {@inheritDoc}
118
     */
119
    @Override
120
    public boolean allowAnonymousAccess() {
121
        return false;
122
    }
123

    
124
    /**
125
     * {@inheritDoc}
126
     */
127
    @Override
128
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
129
        return null;
130
    }
131

    
132
    /**
133
     * {@inheritDoc}
134
     */
135
    @Override
136
    public void cancel() {
137
        // not needed
138

    
139
    }
140

    
141
    /**
142
     * @param identifier
143
     */
144
    public void loadMessagesFor(Integer registrationEntityId) {
145
        getPresenter().loadMessagesFor(registrationEntityId);
146

    
147
    }
148

    
149
    /**
150
     * {@inheritDoc}
151
     */
152
    @Override
153
    public void showMessages(List<Message> messages) {
154

    
155
        VerticalLayout messagesList = new VerticalLayout();
156

    
157
        for(Message message : messages){
158
            Label item = new Label("<span class=\"date-time\">(" +  message.getId() + ")</span> <span class=\"user-name\"><span class=\"user-name\">" + message.getFrom().getUsername() + "</span>: "  + message.getText());
159
            item.setStyleName("message-item");
160
            item.setContentMode(ContentMode.HTML);
161
            messagesList.addComponent(item);
162

    
163
        }
164
        messagesPanel.setContent(messagesList);
165

    
166
    }
167

    
168
    // TODO move into AbstractPopupView?
169
    @Override
170
    public DelegatingErrorHandler getErrrorHandler(){
171
        return errrorHandler;
172
    }
173

    
174

    
175
}
(8-8/23)