Project

General

Profile

Download (11.5 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 = 4;
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 createdLabel = new Label();
78
    private Label publishedLabel = new Label();
79
    private Label releasedLabel = new Label();
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 navigationEvent = 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
                navigationEvent, null, regDto);
165
    }
166

    
167
    public void setWorkingSet(RegistrationWorkingSet workingSet, AbstractView<?> parentView){
168
        this.parentView = parentView;
169

    
170
        ReferenceEditorAction referenceEditorAction;
171
        if(workingSet.getCitationId() != null){
172
            referenceEditorAction = new ReferenceEditorAction(Type.EDIT, workingSet.getCitationId());
173
        } else {
174
            referenceEditorAction = new ReferenceEditorAction(Type.ADD);
175
        }
176
        TimePeriod datePublished = workingSet.getRegistrationDTOs().get(0).getDatePublished();
177
        updateUI(workingSet.getCitation(), workingSet.getCreated(), datePublished, workingSet.messagesCount(),
178
                referenceEditorAction, FontAwesome.EDIT, null);
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
            Collection<?> removeCandidates = getOpenButton().getListeners(ClickListener.class);
221
            removeCandidates.forEach(l -> getOpenButton().removeClickListener((ClickListener)l));
222
            getOpenButton().addClickListener(e -> publishEvent(openButtonEvent));
223
        }
224

    
225
        if(openButtonIcon != null){
226
            getOpenButton().setIcon(openButtonIcon);
227
        }
228

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

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

    
239
            registrationDate = regDto.getRegistrationDate();
240
        }
241

    
242

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

    
247

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

    
256

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

    
266

    
267

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

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

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

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

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

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

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

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

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

    
329

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

    
337
   /* --------------------------------------- */
338

    
339
}
(2-2/6)