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.EditorActionType;
30
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
31
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
32
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
33
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
34
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
35
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
36
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationTypeConverter;
37
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationWorkflowViewBean;
38
import eu.etaxonomy.vaadin.mvp.AbstractView;
39
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
40

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

    
48

    
49
    private static final String LABEL_CAPTION_CREATED = "Created";
50

    
51
    private static final String LABEL_CAPTION_PUBLISHED = "Published";
52

    
53
    private static final String LABEL_CAPTION_RELEASED = "Released";
54

    
55
    private static final int GRID_ROWS = 4;
56

    
57
    private static final int GRID_COLS = 3;
58

    
59
    private static final long serialVersionUID = -211003770452173644L;
60

    
61
    private RegistrationTypeConverter regTypeConverter = new RegistrationTypeConverter();
62

    
63
    private AbstractView<?> parentView;
64

    
65
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
66

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

    
78
    private String citationString;
79
    // --------------------------------------------------
80

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

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

    
99
    public void init() {
100

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

    
104
        typeStateLabel.setStyleName(LABEL_NOWRAP);
105
        typeStateLabel.setVisible(false);
106
        addComponent(typeStateLabel, 0, 0);
107
        setComponentAlignment(typeStateLabel, Alignment.TOP_LEFT);
108

    
109
        identifierLink.setVisible(false);
110
        addComponent(identifierLink, 1, 0);
111
        setComponentAlignment(identifierLink, Alignment.TOP_CENTER);
112
        setColumnExpandRatio(1, 1.0f);
113

    
114
        messageButton = new Button(FontAwesome.COMMENT);
115
        CssLayout buttonGroup = new CssLayout(blockedByButton, messageButton, openButton);
116
        blockedByButton.setStyleName(ValoTheme.BUTTON_TINY);
117
        blockedByButton.setEnabled(false);
118
        messageButton.setStyleName(ValoTheme.BUTTON_TINY);
119
        messageButton.setEnabled(false);
120

    
121
        openButton.setStyleName(ValoTheme.BUTTON_TINY);
122
        openButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
123
        openButton.setVisible(false);
124

    
125
        buttonGroup.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
126
        addComponent(buttonGroup, 2, 0);
127
        setComponentAlignment(buttonGroup, Alignment.TOP_RIGHT);
128

    
129
        citationSummaryLabel.setContentMode(ContentMode.HTML);
130
        addComponent(citationSummaryLabel, 0, 1, 1, 3);
131

    
132
        createdLabel.setStyleName(LABEL_NOWRAP);
133
        createdLabel.setContentMode(ContentMode.HTML);
134
        createdLabel.setWidthUndefined();
135
        addComponent(createdLabel, 2, 1);
136
        setComponentAlignment(createdLabel, Alignment.BOTTOM_RIGHT);
137

    
138
        publishedLabel.setStyleName(LABEL_NOWRAP);
139
        publishedLabel.setContentMode(ContentMode.HTML);
140
        publishedLabel.setWidthUndefined();
141
        publishedLabel.setVisible(false);
142
        addComponent(publishedLabel, 2, 2);
143
        setComponentAlignment(publishedLabel, Alignment.BOTTOM_RIGHT);
144

    
145
        releasedLabel.setStyleName(LABEL_NOWRAP);
146
        releasedLabel.setContentMode(ContentMode.HTML);
147
        releasedLabel.setWidthUndefined();
148
        releasedLabel.setVisible(false);
149
        addComponent(releasedLabel, 2, 3);
150
        setComponentAlignment(releasedLabel, Alignment.BOTTOM_RIGHT);
151

    
152
    }
153

    
154
    public void setItem(RegistrationDTO regDto, AbstractView<?> parentView){
155
        this.parentView = parentView;
156

    
157
        NavigationEvent openButtonEvent = new NavigationEvent(
158
                RegistrationWorkflowViewBean.NAME,
159
                RegistrationWorkflowViewBean.ACTION_EDIT,
160
                Integer.toString(regDto.getId())
161
                );
162

    
163
        updateUI(regDto.getBibliographicCitationString(), regDto.getCreated(), regDto.getDatePublished(), regDto.getMessages().size(),
164
                openButtonEvent, null, regDto);
165
    }
166

    
167
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
168
        this.parentView = parentView;
169
        ReferenceEditorAction openButtonEvent;
170
        if(workingSet.getCitationId() != null){
171
            openButtonEvent = new ReferenceEditorAction(EditorActionType.EDIT, workingSet.getCitationId());
172
        } else {
173
            openButtonEvent = new ReferenceEditorAction(EditorActionType.ADD);
174
        }
175
        TimePeriod datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
176
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
177
                openButtonEvent, FontAwesome.EDIT, null);
178
    }
179

    
180

    
181
    /**
182
     *
183
     */
184
    private void updateUI(String citationString,  DateTime created, TimePeriod datePublished,  int messagesCount,
185
            Object openButtonEvent, Resource openButtonIcon, RegistrationDTO regDto) {
186

    
187
        StringBuffer labelMarkup = new StringBuffer();
188
        DateTime registrationDate = null;
189

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

    
215
        labelMarkup.append(citationString);
216

    
217
        if(openButtonEvent != null){
218
            // Buttons
219
            getOpenButton().setVisible(true);
220
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
221
        }
222

    
223
        if(openButtonIcon != null){
224
            getOpenButton().setIcon(openButtonIcon);
225
        }
226

    
227
        if(regDto != null){
228
            labelMarkup.append("</br>").append(regDto.getSummary());
229

    
230
            typeStateLabel.setVisible(true);
231
            typeStateLabel.update(regDto.getRegistrationType(), regDto.getStatus());
232
            getIdentifierLink().setResource(new ExternalResource(regDto.getIdentifier()));
233
            //TODO make responsive and use specificIdentifier in case the space gets too narrow
234
            getIdentifierLink().setVisible(true);
235
            getIdentifierLink().setCaption(regDto.getIdentifier());
236

    
237
            registrationDate = regDto.getRegistrationDate();
238
        }
239

    
240

    
241
        getCitationSummaryLabel().setValue(labelMarkup.toString());
242
        updateDateLabels(created, datePublished, registrationDate);
243
    }
244

    
245

    
246
    /**
247
     *
248
     */
249
    private void updateDateLabels(DateTime created, TimePeriod datePublished, DateTime released) {
250
        getCreatedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_CREATED + "</span>&nbsp;" + created.toString(ISODateTimeFormat.yearMonthDay()));
251
        if(datePublished != null){
252
            getPublishedLabel().setVisible(true);
253

    
254

    
255
            getPublishedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_PUBLISHED + "</span>&nbsp;" + timePeriodFormatter.print(datePublished));
256
        }
257
        if(released != null){
258
            getReleasedLabel().setVisible(true);
259
            getReleasedLabel().setValue("<span class=\"caption\">" + LABEL_CAPTION_RELEASED + "</span>&nbsp;" + released.toString(ISODateTimeFormat.yearMonthDay()));
260
        }
261
        // LABEL_CAPTION_RELEASED
262
    }
263

    
264

    
265

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

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

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

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

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

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

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

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

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

    
327

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

    
335
   /* --------------------------------------- */
336

    
337
}
(2-2/6)