Project

General

Profile

Download (5.57 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

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

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

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

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

    
42
    private static final long serialVersionUID = 713522519903334889L;
43

    
44
    Panel messagesPanel;
45

    
46
    TextArea newMessageField;
47

    
48
    Button sendMessageButton;
49

    
50
    private VerticalLayout mainLayout;
51

    
52
    private DelegatingErrorHandler errrorHandler = new DelegatingErrorHandler();
53

    
54
    public RegistrationMessagesPopup() {
55

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

    
62
        setCompositionRoot(mainLayout);
63
    }
64

    
65

    
66
    @Override
67
    protected void initContent() {
68

    
69
        messagesPanel = new Panel();
70
        messagesPanel.setStyleName(EditValoTheme.PANEL_CONTENT_PADDING_LEFT);
71

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

    
80
        sendMessageButton = new Button(FontAwesome.SEND);
81
        sendMessageButton.addClickListener(e -> postNewMessage());
82
        sendMessageButton.setStyleName(ValoTheme.BUTTON_HUGE + " " +ValoTheme.BUTTON_PRIMARY);
83

    
84
        HorizontalLayout sendMessagebar = new HorizontalLayout(newMessageField, sendMessageButton);
85
        sendMessagebar.setComponentAlignment(sendMessageButton, Alignment.MIDDLE_RIGHT);
86
        sendMessagebar.setExpandRatio(newMessageField, 1f);
87
        sendMessagebar.setWidth("100%");
88

    
89
        mainLayout.addComponents(messagesPanel, sendMessagebar);
90

    
91
        mainLayout.setErrorHandler(errrorHandler);
92
        mainLayout.setComponentAlignment(sendMessagebar, Alignment.BOTTOM_CENTER);
93
    }
94

    
95

    
96
    @Override
97
    public int getWindowHeight() {
98
        // undefined
99
        return -1;
100
    }
101

    
102
    @Override
103
    public boolean isClosable() {
104
        return true;
105
    }
106

    
107
    @Override
108
    public boolean isResizable() {
109
        return true;
110
    }
111

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

    
124
    /**
125
     * {@inheritDoc}
126
     */
127
    @Override
128
    public String getWindowCaption() {
129
        return "Messages";
130
    }
131

    
132
    /**
133
     * {@inheritDoc}
134
     */
135
    @Override
136
    public void focusFirst() {
137
        // none
138
    }
139

    
140

    
141
    /**
142
     * {@inheritDoc}
143
     */
144
    @Override
145
    public boolean allowAnonymousAccess() {
146
        return false;
147
    }
148

    
149
    /**
150
     * {@inheritDoc}
151
     */
152
    @Override
153
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
154
        return null;
155
    }
156

    
157
    /**
158
     * {@inheritDoc}
159
     */
160
    @Override
161
    public void cancel() {
162
        // not needed
163

    
164
    }
165

    
166
    /**
167
     * @param identifier
168
     */
169
    public void loadMessagesFor(Integer registrationEntityId) {
170
        getPresenter().loadMessagesFor(registrationEntityId);
171

    
172
    }
173

    
174
    /**
175
     * {@inheritDoc}
176
     */
177
    @Override
178
    public void showMessages(String registrationLabel, List<Message> messages) {
179

    
180
        VerticalLayout messagesList = new VerticalLayout();
181

    
182
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
183

    
184
        for(Message message : messages){
185
            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>");
186
            item.setStyleName("message-item");
187
            item.setContentMode(ContentMode.HTML);
188
            messagesList.addComponent(item);
189

    
190
        }
191
        messagesPanel.setCaption(registrationLabel);
192
        messagesPanel.setContent(messagesList);
193

    
194
    }
195

    
196
    // TODO move into AbstractPopupView?
197
    @Override
198
    public DelegatingErrorHandler getErrrorHandler(){
199
        return errrorHandler;
200
    }
201

    
202

    
203
}
(8-8/23)