Project

General

Profile

Download (11.3 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 org.joda.time.DateTime;
14
import org.joda.time.format.ISODateTimeFormat;
15

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

    
28
import eu.etaxonomy.cdm.model.common.TimePeriod;
29
import eu.etaxonomy.cdm.vaadin.event.EntityEventType;
30
import eu.etaxonomy.cdm.vaadin.event.ReferenceEvent;
31
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
32
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
33
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
34
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
35
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorkflowViewBean;
36
import eu.etaxonomy.vaadin.mvp.AbstractView;
37
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
38

    
39
/**
40
 * @author a.kohlbecker
41
 * @since Mar 17, 2017
42
 *
43
 */
44
public class RegistrationItem extends GridLayout {
45

    
46

    
47
    private static final String LABEL_CAPTION_CREATED = "Created";
48

    
49
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
50

    
51
    private static final String LABEL_CAPTION_RELEASED = "Released";
52

    
53
    private static final int GRID_ROWS = 4;
54

    
55
    private static final int GRID_COLS = 3;
56

    
57
    private static final long serialVersionUID = -211003770452173644L;
58

    
59
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
60

    
61
    private AbstractView<?> parentView;
62

    
63
    // --------------------------------------------------
64
    private TypeStateLabel typeStateLabel = new TypeStateLabel();
65
    private Link identifierLink = new Link();
66
    private Label citationSummaryLabel = new Label();
67
    private Button blockedByButton = new Button(FontAwesome.WARNING);
68
    private Button messageButton;
69
    private Button openButton = new Button(FontAwesome.COGS);
70
    private Label createdLabel = new Label();
71
    private Label publishedLabel = new Label();
72
    private Label releasedLabel = new Label();
73

    
74
    private String citationString;
75
    // --------------------------------------------------
76

    
77
    /**
78
     *
79
     */
80
    public RegistrationItem(RegistrationDTO item, AbstractView<?> parentView) {
81
        super(GRID_COLS, GRID_ROWS);
82
        init();
83
        setItem(item, parentView);
84
    }
85

    
86
    /**
87
    *
88
    */
89
   public RegistrationItem(RegistrationWorkingSet workingSet, AbstractView<?> parentView) {
90
       super(GRID_COLS, GRID_ROWS);
91
       init();
92
       setWorkingSet(workingSet, parentView);
93
   }
94

    
95
    public void init() {
96

    
97
        setWidth(100, Unit.PERCENTAGE);
98
        addStyleName("registration-list-item");
99

    
100
        typeStateLabel.setStyleName(LABEL_NOWRAP);
101
        typeStateLabel.setVisible(false);
102
        addComponent(typeStateLabel, 0, 0);
103
        setComponentAlignment(typeStateLabel, Alignment.TOP_LEFT);
104

    
105
        identifierLink.setVisible(false);
106
        addComponent(identifierLink, 1, 0);
107
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
108
        setColumnExpandRatio(1, 1.0f);
109

    
110
        messageButton = new Button(FontAwesome.COMMENT);
111
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
112
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
113
        blockedByButton.setEnabled(false);
114
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
115
        messageButton.setEnabled(false);
116

    
117
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
118
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
119
        openButton.setVisible(false);
120

    
121
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
122
        addComponent(buttonGroup, 2, 0);
123
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
124

    
125
        citationSummaryLabel.setContentMode(ContentMode.HTML);
126
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
127

    
128
        createdLabel.setStyleName(LABEL_NOWRAP);
129
        createdLabel.setContentMode(ContentMode.HTML);
130
        createdLabel.setWidthUndefined();
131
        addComponent(createdLabel, 2, 1);
132
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
133

    
134
        publishedLabel.setStyleName(LABEL_NOWRAP);
135
        publishedLabel.setContentMode(ContentMode.HTML);
136
        publishedLabel.setWidthUndefined();
137
        publishedLabel.setVisible(false);
138
        addComponent(publishedLabel, 2, 2);
139
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
140

    
141
        releasedLabel.setStyleName(LABEL_NOWRAP);
142
        releasedLabel.setContentMode(ContentMode.HTML);
143
        releasedLabel.setWidthUndefined();
144
        releasedLabel.setVisible(false);
145
        addComponent(releasedLabel, 2, 3);
146
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
147

    
148
    }
149

    
150
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
151
        this.parentView = parentView;
152

    
153
        NavigationEvent openButtonEvent = new NavigationEvent(
154
                RegistrationWorkflowViewBean.NAME,
155
                RegistrationWorkflowViewBean.ACTION_EDIT,
156
                Integer.toString(regDto.getId())
157
                );
158

    
159
        updateUI(regDto.getCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
160
                openButtonEvent, null, regDto);
161
    }
162

    
163
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
164
        this.parentView = parentView;
165
        ReferenceEvent openButtonEvent;
166
        if(workingSet.getCitationId() != null){
167
            openButtonEvent = new ReferenceEvent(EntityEventType.EDIT, workingSet.getCitationId());
168
        } else {
169
            openButtonEvent = new ReferenceEvent(EntityEventType.ADD);
170
        }
171
        updateUI(workingSet.getCitation(), workingSet.getCreated(), null, workingSet.messagesCount(),
172
                openButtonEvent, FontAwesome.EDIT, null);
173
    }
174

    
175

    
176
    /**
177
     *
178
     */
179
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
180
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto) {
181

    
182
        StringBuffer labelMarkup = new StringBuffer();
183
        DateTime registrationDate = null;
184

    
185
        if(messagesCount > 0){
186
            getMessageButton().setEnabled(true);
187
            // getMessageButton().addStyleName(RegistrationStyles.STYLE_FRIENDLY_FOREGROUND);
188
            getMessageButton().addClickListener(e -> {
189
                ShowDetailsEvent detailsEvent;
190
                if(regDto != null){
191
                    detailsEvent = new ShowDetailsEvent<RegistrationDTO, Integer>(
192
                            e,
193
                            RegistrationDTO.class,
194
                            regDto.getId(),
195
                            "messages");
196
                } else {
197
                    detailsEvent = new ShowDetailsEvent<RegistrationWorkingSet, Integer>(
198
                            e,
199
                            RegistrationWorkingSet.class,
200
                            null,
201
                            "messages");
202
                }
203
                publishEvent(detailsEvent);
204
                }
205
            );
206
            getMessageButton().setCaption("<span class=\"" + RegistrationStyles.BUTTON_BADGE +"\"> " + messagesCount + "</span>");
207
            getMessageButton().setCaptionAsHtml(true);
208
        }
209

    
210
        labelMarkup.append(citationString);
211

    
212
        if(openButtonEvent != null){
213
            // Buttons
214
            getOpenButton().setVisible(true);
215
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
216
        }
217

    
218
        if(openButtonIcon != null){
219
            getOpenButton().setIcon(openButtonIcon);
220
        }
221

    
222
        if(regDto != null){
223
            labelMarkup.append("</br>").append(regDto.getSummary());
224

    
225
            typeStateLabel.setVisible(true);
226
            typeStateLabel.update(regDto.getRegistrationType(), regDto.getStatus());
227
            getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
228
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
229
            getIdentifierLink().setVisible(true);
230
            getIdentifierLink().setCaption(regDto.getIdentifier());
231

    
232
            registrationDate = regDto.getRegistrationDate();
233
        }
234

    
235

    
236
        getCitationSummaryLabel().setValue(labelMarkup.toString());
237
        updateDateLabels(created, datePublished, registrationDate);
238
    }
239

    
240

    
241
    /**
242
     *
243
     */
244
    private void updateDateLabels(DateTime created, TimePeriod published, DateTime released) {
245
        getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
246
        if(published != null){
247
            getPublishedLabel().setVisible(true);
248
            StringBuffer sb = new StringBuffer();
249
            if(published.getStart() != null){
250
                sb.append(published.getStart().toString(ISODateTimeFormat.yearMonthDay()));
251
            }
252
            if(published.getEnd() != null){
253
                if(sb.length() > 0){
254
                    sb.append('-');
255
                    sb.append(published.getEnd().toString(ISODateTimeFormat.yearMonthDay()));
256
                }
257
            }
258
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + sb.toString());
259
        }
260
        if(released != null){
261
            getReleasedLabel().setVisible(true);
262
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
263
        }
264
        // LABEL_CAPTION_RELEASED
265
    }
266

    
267
    private void publishEvent(Object event) {
268
        parentView.getEventBus().publishEvent(event);
269
    }
270

    
271
    /* ====== RegistrationItemDesign Getters ====== */
272
    /**
273
     * @return the typeStateLabel
274
     */
275
    public Label getTypeStateLabel() {
276
        return typeStateLabel;
277
    }
278

    
279
    /**
280
     * @return the identifierLink
281
     */
282
    public Link getIdentifierLink() {
283
        return identifierLink;
284
    }
285

    
286
    /**
287
     * @return the citationSummaryLabel
288
     */
289
    public Label getCitationSummaryLabel() {
290
        return citationSummaryLabel;
291
    }
292

    
293
    /**
294
     * @return the blockedByButton
295
     */
296
    public Button getBlockedByButton() {
297
        return blockedByButton;
298
    }
299

    
300
    /**
301
     * @return the messageButton
302
     */
303
    public Button getMessageButton() {
304
        return messageButton;
305
    }
306

    
307
    /**
308
     * @return the openButton
309
     */
310
    public Button getOpenButton() {
311
        return openButton;
312
    }
313

    
314
    /**
315
     * @return the createdLabel
316
     */
317
    public Label getCreatedLabel() {
318
        return createdLabel;
319
    }
320

    
321
    /**
322
     * @return the publishedLabel
323
     */
324
    public Label getPublishedLabel() {
325
        return publishedLabel;
326
    }
327

    
328

    
329
    /**
330
     * @return
331
     */
332
    public Label getReleasedLabel() {
333
        return releasedLabel;
334
    }
335

    
336
   /* --------------------------------------- */
337

    
338
}
(2-2/6)