Project

General

Profile

Download (12.2 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.component.registration;
10

    
11
import static eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles.LABEL_NOWRAP;
12

    
13
import java.util.Collection;
14

    
15
import org.joda.time.DateTime;
16
import org.joda.time.format.ISODateTimeFormat;
17

    
18
import com.vaadin.server.ExternalResource;
19
import com.vaadin.server.FontAwesome;
20
import com.vaadin.server.Resource;
21
import com.vaadin.shared.ui.label.ContentMode;
22
import com.vaadin.ui.Alignment;
23
import com.vaadin.ui.Button;
24
import com.vaadin.ui.Button.ClickListener;
25
import com.vaadin.ui.CssLayout;
26
import com.vaadin.ui.GridLayout;
27
import com.vaadin.ui.Label;
28
import com.vaadin.ui.Link;
29
import com.vaadin.ui.themes.ValoTheme;
30

    
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Type;
33
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
34
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
35
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
36
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
37
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
38
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
39
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
40
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorkflowViewBean;
41
import eu.etaxonomy.vaadin.mvp.AbstractView;
42
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
43

    
44
/**
45
 * @author a.kohlbecker
46
 * @since Mar 17, 2017
47
 *
48
 */
49
public class RegistrationItem extends GridLayout {
50

    
51

    
52
    private static final String LABEL_CAPTION_CREATED = "Created";
53

    
54
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
55

    
56
    private static final String LABEL_CAPTION_RELEASED = "Released";
57

    
58
    private static final int GRID_ROWS = 5;
59

    
60
    private static final int GRID_COLS = 3;
61

    
62
    private static final long serialVersionUID = -211003770452173644L;
63

    
64
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
65

    
66
    private AbstractView<?> parentView;
67

    
68
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
69

    
70
    // --------------------------------------------------
71
    private TypeStateLabel typeStateLabel = new TypeStateLabel();
72
    private Link identifierLink = new Link();
73
    private Label citationSummaryLabel = new Label();
74
    private Button blockedByButton = new Button(FontAwesome.WARNING);
75
    private Button messageButton;
76
    private Button openButton = new Button(FontAwesome.COGS);
77
    private Label submitterLabel = new Label();
78
    private Label createdLabel = new Label();
79
    private Label publishedLabel = new Label();
80
    private Label releasedLabel = new Label();
81

    
82
    /**
83
     *
84
     */
85
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
86
        super(GRID_COLS, GRID_ROWS);
87
        init();
88
        setItem(item, parentView);
89
    }
90

    
91
    /**
92
    *
93
    */
94
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
95
       super(GRID_COLS, GRID_ROWS);
96
       init();
97
       setWorkingSet(workingSet, parentView);
98
   }
99

    
100
    public void init() {
101

    
102
        setWidth(100, Unit.PERCENTAGE);
103
        addStyleName("registration-list-item");
104

    
105
        CssLayout stateUserContainer = new CssLayout();
106
        typeStateLabel.setStyleName(LABEL_NOWRAP + " registration-state");
107
        typeStateLabel.setVisible(false);
108

    
109
        submitterLabel.setStyleName(LABEL_NOWRAP + " submitter");
110
        submitterLabel.setIcon(FontAwesome.USER);
111
        submitterLabel.setContentMode(ContentMode.HTML);
112
        submitterLabel.setVisible(false);
113

    
114
        stateUserContainer.addComponents(typeStateLabel, submitterLabel);
115
        addComponent(stateUserContainer, 0, 0);
116
        setComponentAlignment(stateUserContainer, Alignment.TOP_LEFT);
117

    
118
        identifierLink.setVisible(false);
119
        addComponent(identifierLink, 1, 0);
120
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
121
        setColumnExpandRatio(1, 1.0f);
122

    
123
        messageButton = new Button(FontAwesome.COMMENT);
124
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
125
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
126
        blockedByButton.setEnabled(false);
127
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
128
        messageButton.setEnabled(false);
129

    
130
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
131
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
132
        openButton.setVisible(false);
133

    
134
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
135
        addComponent(buttonGroup, 2, 0);
136
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
137

    
138
        citationSummaryLabel.setContentMode(ContentMode.HTML);
139
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
140

    
141
        createdLabel.setStyleName(LABEL_NOWRAP);
142
        createdLabel.setContentMode(ContentMode.HTML);
143
        createdLabel.setWidthUndefined();
144
        addComponent(createdLabel, 2, 1);
145
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
146

    
147
        publishedLabel.setStyleName(LABEL_NOWRAP);
148
        publishedLabel.setContentMode(ContentMode.HTML);
149
        publishedLabel.setWidthUndefined();
150
        publishedLabel.setVisible(false);
151
        addComponent(publishedLabel, 2, 2);
152
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
153

    
154
        releasedLabel.setStyleName(LABEL_NOWRAP);
155
        releasedLabel.setContentMode(ContentMode.HTML);
156
        releasedLabel.setWidthUndefined();
157
        releasedLabel.setVisible(false);
158
        addComponent(releasedLabel, 2, 3);
159
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
160

    
161
    }
162

    
163
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
164
        this.parentView = parentView;
165

    
166
        NavigationEvent navigationEvent = new NavigationEvent(
167
                RegistrationWorkflowViewBean.NAME,
168
                RegistrationWorkflowViewBean.ACTION_EDIT,
169
                Integer.toString(regDto.getId())
170
                );
171

    
172
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
173
                navigationEvent, null, regDto, regDto.getSubmitterUserName());
174
    }
175

    
176
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
177
        this.parentView = parentView;
178

    
179
        ReferenceEditorAction referenceEditorAction;
180
        if(workingSet.getCitationId() != null){
181
            referenceEditorAction = new ReferenceEditorAction(Type.EDIT, workingSet.getCitationId());
182
        } else {
183
            referenceEditorAction = new ReferenceEditorAction(Type.ADD);
184
        }
185
        TimePeriod datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
186
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
187
                referenceEditorAction, FontAwesome.EDIT, null, workingSet.getRegistrationDTOs().get(0).getSubmitterUserName());
188
    }
189

    
190
    /**
191
     * @param submitterUserName TODO
192
     *
193
     */
194
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
195
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto, String submitterUserName) {
196

    
197
        StringBuffer labelMarkup = new StringBuffer();
198
        DateTime registrationDate = null;
199

    
200
        if(messagesCount > 0){
201
            getMessageButton().setEnabled(true);
202
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
203
            getMessageButton().addClickListener(e -> {
204
                ShowDetailsEvent detailsEvent;
205
                if(regDto != null){
206
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
207
                            e,
208
                            RegistrationDTO.class,
209
                            regDto.getId(),
210
                            "messages");
211
                } else {
212
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
213
                            e,
214
                            RegistrationWorkingSet.class,
215
                            null,
216
                            "messages");
217
                }
218
                publishEvent(detailsEvent);
219
                }
220
            );
221
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
222
            getMessageButton().setCaptionAsHtml(true);
223
        }
224

    
225
        labelMarkup.append(citationString);
226

    
227
        if(openButtonEvent != null){
228
            // Buttons
229
            getOpenButton().setVisible(true);
230
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
231
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
232
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
233
        }
234

    
235
        if(openButtonIcon != null){
236
            getOpenButton().setIcon(openButtonIcon);
237
        }
238

    
239
        if(regDto != null){
240
            labelMarkup.append("</br>").append(regDto.getSummary());
241

    
242
            typeStateLabel.setVisible(true);
243
            typeStateLabel.update(regDto.getRegistrationType(), regDto.getStatus());
244
            getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
245
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
246
            getIdentifierLink().setVisible(true);
247
            getIdentifierLink().setCaption(regDto.getIdentifier());
248

    
249
            registrationDate = regDto.getRegistrationDate();
250
        }
251

    
252
        getCitationSummaryLabel().setValue(labelMarkup.toString());
253
        getSubmitterLabel().setValue(submitterUserName);
254
        updateDateLabels(created, datePublished, registrationDate);
255
    }
256

    
257

    
258
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
259
        getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
260
        if(datePublished != null){
261
            getPublishedLabel().setVisible(true);
262
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
263
        }
264
        if(released != null){
265
            getReleasedLabel().setVisible(true);
266
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
267
        }
268
    }
269

    
270

    
271
    private void publishEvent(Object event) {
272
        parentView.getEventBus().publishEvent(event);
273
    }
274

    
275
    /* ====== RegistrationItemDesign Getters ====== */
276
    /**
277
     * @return the typeStateLabel
278
     */
279
    public Label getTypeStateLabel() {
280
        return typeStateLabel;
281
    }
282

    
283
    /**
284
     * @return the identifierLink
285
     */
286
    public Link getIdentifierLink() {
287
        return identifierLink;
288
    }
289

    
290
    /**
291
     * @return the citationSummaryLabel
292
     */
293
    public Label getCitationSummaryLabel() {
294
        return citationSummaryLabel;
295
    }
296

    
297
    /**
298
     * @return the blockedByButton
299
     */
300
    public Button getBlockedByButton() {
301
        return blockedByButton;
302
    }
303

    
304
    /**
305
     * @return the messageButton
306
     */
307
    public Button getMessageButton() {
308
        return messageButton;
309
    }
310

    
311
    /**
312
     * @return the openButton
313
     */
314
    public Button getOpenButton() {
315
        return openButton;
316
    }
317

    
318
    /**
319
     * @return the createdLabel
320
     */
321
    public Label getCreatedLabel() {
322
        return createdLabel;
323
    }
324

    
325
    /**
326
     * @return the publishedLabel
327
     */
328
    public Label getPublishedLabel() {
329
        return publishedLabel;
330
    }
331

    
332

    
333
    /**
334
     * @return
335
     */
336
    public Label getReleasedLabel() {
337
        return releasedLabel;
338
    }
339

    
340
    /**
341
     * @return the submitterLabel
342
     */
343
    public Label getSubmitterLabel() {
344
        return submitterLabel;
345
    }
346

    
347

    
348
   /* --------------------------------------- */
349

    
350
}
(2-2/6)