Project

General

Profile

Download (5.88 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.text.SimpleDateFormat;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.UUID;
15

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

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

    
32
import eu.etaxonomy.cdm.ext.registration.messages.Message;
33
import eu.etaxonomy.cdm.vaadin.event.error.DelegatingErrorHandler;
34
import eu.etaxonomy.cdm.vaadin.permission.AccessRestrictedView;
35
import eu.etaxonomy.cdm.vaadin.theme.EditValoTheme;
36
import eu.etaxonomy.vaadin.mvp.AbstractPopupView;
37

    
38
@SpringComponent
39
@Scope("prototype")
40
public class RegistrationMessagesPopup extends AbstractPopupView<RegistrationMessagesPresenter>
41
    implements RegistrationMessagesView, AccessRestrictedView {
42

    
43
    private static final long serialVersionUID = 713522519903334889L;
44

    
45
    Panel messagesPanel;
46

    
47
    TextArea newMessageField;
48

    
49
    Button sendMessageButton;
50

    
51
    private VerticalLayout mainLayout;
52

    
53
    private DelegatingErrorHandler errrorHandler = new DelegatingErrorHandler();
54

    
55
    private String accessDeniedMessage;
56

    
57
    public RegistrationMessagesPopup() {
58

    
59
        mainLayout = new VerticalLayout();
60
        // IMPORTANT: mainLayout must be set to full size otherwise the
61
        // popup window may have problems with automatic resizing of its
62
        // content.
63
        mainLayout.setSizeFull();
64

    
65
        setCompositionRoot(mainLayout);
66
    }
67

    
68

    
69
    @Override
70
    protected void initContent() {
71

    
72
        messagesPanel = new Panel();
73
        messagesPanel.setStyleName(EditValoTheme.PANEL_CONTENT_PADDING_LEFT);
74

    
75
        newMessageField = new TextArea();
76
        newMessageField.setNullRepresentation("");
77
        newMessageField.addTextChangeListener(e -> {
78
            sendMessageButton.setEnabled(StringUtils.isNoneBlank(e.getText()));
79
        });
80
        newMessageField.setHeight("64px"); // height of the Submit button when ValoTheme.BUTTON_HUGE
81
        newMessageField.setWidth("100%");
82

    
83
        sendMessageButton = new Button(FontAwesome.SEND);
84
        sendMessageButton.addClickListener(e -> postNewMessage());
85
        sendMessageButton.setStyleName(ValoTheme.BUTTON_HUGE + " " +ValoTheme.BUTTON_PRIMARY);
86

    
87
        HorizontalLayout sendMessagebar = new HorizontalLayout(newMessageField, sendMessageButton);
88
        sendMessagebar.setComponentAlignment(sendMessageButton, Alignment.MIDDLE_RIGHT);
89
        sendMessagebar.setExpandRatio(newMessageField, 1f);
90
        sendMessagebar.setWidth("100%");
91

    
92
        mainLayout.addComponents(messagesPanel, sendMessagebar);
93

    
94
        mainLayout.setErrorHandler(errrorHandler);
95
        mainLayout.setComponentAlignment(sendMessagebar, Alignment.BOTTOM_CENTER);
96
    }
97

    
98

    
99
    @Override
100
    public int getWindowHeight() {
101
        // undefined
102
        return -1;
103
    }
104

    
105
    @Override
106
    public boolean isClosable() {
107
        return true;
108
    }
109

    
110
    @Override
111
    public boolean isResizable() {
112
        return true;
113
    }
114

    
115
    /**
116
     * @return
117
     */
118
    private void postNewMessage() {
119
        // quick and dirty implementation, better send an event
120
        String text = newMessageField.getValue();
121
        if(StringUtils.isNotBlank(text)){
122
            getPresenter().postMessage(text);
123
            newMessageField.setValue(null);
124
        }
125
    }
126

    
127
    /**
128
     * {@inheritDoc}
129
     */
130
    @Override
131
    public String getWindowCaption() {
132
        return "Messages";
133
    }
134

    
135
    /**
136
     * {@inheritDoc}
137
     */
138
    @Override
139
    public void focusFirst() {
140
        // none
141
    }
142

    
143

    
144
    /**
145
     * {@inheritDoc}
146
     */
147
    @Override
148
    public boolean allowAnonymousAccess() {
149
        return false;
150
    }
151

    
152
    /**
153
     * {@inheritDoc}
154
     */
155
    @Override
156
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
157
        return null;
158
    }
159

    
160
    @Override
161
    public String getAccessDeniedMessage() {
162
        return accessDeniedMessage;
163
    }
164

    
165
    @Override
166
    public void setAccessDeniedMessage(String accessDeniedMessage) {
167
        this.accessDeniedMessage = accessDeniedMessage;
168

    
169
    }
170

    
171
    /**
172
     * {@inheritDoc}
173
     */
174
    @Override
175
    public void cancel() {
176
        // not needed
177

    
178
    }
179

    
180
    /**
181
     * @param identifier
182
     */
183
    public void loadMessagesFor(UUID registrationEntityUuid) {
184
        getPresenter().loadMessagesFor(registrationEntityUuid);
185

    
186
    }
187

    
188
    /**
189
     * {@inheritDoc}
190
     */
191
    @Override
192
    public void showMessages(String registrationLabel, List<Message> messages) {
193

    
194
        VerticalLayout messagesList = new VerticalLayout();
195

    
196
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
197

    
198
        for(Message message : messages){
199
            Label item = new Label("<span class=\"date-time\">(" +  dateFormat.format(message.getCreatedOn()) + ")</span> <span class=\"user-name\">" + message.getFrom().getUsername() + "</span>: <span class=\"message-text\">"  + message.getText() + "</span>");
200
            item.setStyleName("message-item");
201
            item.setContentMode(ContentMode.HTML);
202
            messagesList.addComponent(item);
203

    
204
        }
205
        messagesPanel.setCaption(registrationLabel);
206
        messagesPanel.setContent(messagesList);
207

    
208
    }
209

    
210
    // TODO move into AbstractPopupView?
211
    @Override
212
    public DelegatingErrorHandler getErrrorHandler(){
213
        return errrorHandler;
214
    }
215

    
216

    
217
}
(7-7/21)