Project

General

Profile

Download (7.28 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 java.util.ArrayList;
12
import java.util.EnumSet;
13
import java.util.List;
14

    
15
import com.vaadin.server.ExternalResource;
16
import com.vaadin.server.FontAwesome;
17
import com.vaadin.ui.Button;
18
import com.vaadin.ui.Label;
19
import com.vaadin.ui.Link;
20
import com.vaadin.ui.themes.ValoTheme;
21

    
22
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
23
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
24
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.TaxonName;
25
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
26
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
27
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
28
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
29
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @since May 19, 2017
34
 *
35
 */
36
public class RegistrationItemEditButtonGroup extends CompositeStyledComponent {
37

    
38

    
39
    /**
40
     *
41
     */
42
    private static final String DEFAULT_BUTTON_STYLES = "";
43

    
44
    private static final long serialVersionUID = -5059163772392864050L;
45

    
46
    public static final String STYLE_NAMES = "edit-button-group  " + ValoTheme.LAYOUT_COMPONENT_GROUP;
47

    
48
    private IdButton<TaxonName> nameIdButton = null;
49

    
50
    private List<TypeDesignationWorkingSetButton> typeDesignationButtons = new ArrayList<>();
51

    
52
    private List<Label> labels = new ArrayList<>();
53

    
54
    private Button addTypeDesignationButton;
55

    
56
    private Label nameLabel = null;
57

    
58
    private Link identifierLink;
59

    
60
    public RegistrationItemEditButtonGroup(RegistrationDTO regDto){
61

    
62
        boolean isRegistrationLocked = EnumSet.of(
63
                RegistrationStatus.PUBLISHED, RegistrationStatus.REJECTED)
64
                .contains(regDto.getStatus());
65

    
66
        setWidth(100, Unit.PERCENTAGE);
67

    
68
        if(regDto.getName() != null){
69
            Button nameButton = new Button("Name:");
70
            nameButton.setDescription("Edit the Name");
71
            nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getName().getId(), nameButton);
72
            Label nameLabel = new Label(regDto.getName().getLabel());
73
            nameLabel.setWidthUndefined();
74
            nameButton.setEnabled(!isRegistrationLocked);
75
            addComponents(nameIdButton.getButton(), nameLabel);
76
        } else {
77
            // no name in the registration! we only show the typified name as label
78
            if(regDto.getTypifiedName() != null){
79
                nameLabel = new Label(regDto.getTypifiedName().getLabel());
80
                addComponent(nameLabel);
81
            }
82
        }
83
        if(regDto.getOrderdTypeDesignationWorkingSets() != null){
84
            for(TypedEntityReference baseEntityRef : regDto.getOrderdTypeDesignationWorkingSets().keySet()) {
85
                TypeDesignationWorkingSet typeDesignationWorkingSet = regDto.getOrderdTypeDesignationWorkingSets().get(baseEntityRef);
86
                String buttonLabel = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ? "Type": "NameType";
87
                Button tdButton = new Button(buttonLabel + ":");
88
                tdButton.setDescription("Edit the type designation working set");
89
                tdButton.setEnabled(!isRegistrationLocked);
90
                addComponent(tdButton);
91
//                Set<Integer> idSet = new HashSet<>();
92
//                typeDesignationWorkingSet.getTypeDesignations().forEach(td -> idSet.add(td.getId()));
93

    
94
                typeDesignationButtons.add(new TypeDesignationWorkingSetButton(
95
                        typeDesignationWorkingSet.getWorkingsetType(),
96
                        typeDesignationWorkingSet.getWorkingSetId(),
97
                        tdButton)
98
                        );
99
                String labelText = typeDesignationWorkingSet.getRepresentation();
100
                labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
101
                Label label = new Label(labelText);
102

    
103
                label.setWidthUndefined();
104
                addComponent(label);
105
                labels.add(label);
106
            }
107
        }
108
        addTypeDesignationButton = new Button(FontAwesome.PLUS);
109
        addTypeDesignationButton.setDescription("Add a new type designation workingset");
110
        addTypeDesignationButton.setVisible(!isRegistrationLocked);
111
        addComponent(addTypeDesignationButton);
112

    
113
        //TODO make responsive and use specificIdentifier in case the space gets too narrow
114
        identifierLink = new Link(regDto.getIdentifier(), new ExternalResource(regDto.getIdentifier()));
115
        identifierLink.setEnabled(regDto.getStatus() == RegistrationStatus.PUBLISHED);
116

    
117
        addComponents(identifierLink);
118

    
119
        iterator().forEachRemaining(c -> addStyledComponent(c));
120
        addDefaultStyles();
121

    
122
    }
123

    
124
    public IdButton<TaxonName> getNameButton() {
125
        return nameIdButton;
126
    }
127

    
128
    public List<TypeDesignationWorkingSetButton> getTypeDesignationButtons() {
129
        return typeDesignationButtons;
130
    }
131

    
132
    public Button getAddTypeDesignationButton() {
133
        return addTypeDesignationButton;
134
    }
135

    
136
    /**
137
     * {@inheritDoc}
138
     */
139
    @Override
140
    protected void addDefaultStyles() {
141
        addStyleName(STYLE_NAMES);
142
        if(nameIdButton != null){
143
            nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
144
        }
145
        if(nameLabel != null){
146
            nameLabel.addStyleName("v-disabled");
147
        }
148
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
149
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
150
    }
151

    
152
    public class TypeDesignationWorkingSetButton {
153
        private Integer id;
154
        private TypeDesignationWorkingSetType type;
155
        private Button button;
156

    
157
        public TypeDesignationWorkingSetButton(TypeDesignationWorkingSetType type, Integer id, Button button){
158
            this.type = type;
159
            this.id = id;
160
            this.button = button;
161
        }
162

    
163
        /**
164
         * @return the id
165
         */
166
        public Integer getId() {
167
            return id;
168
        }
169

    
170
        /**
171
         * @return the button
172
         */
173
        public Button getButton() {
174
            return button;
175
        }
176

    
177
        /**
178
         * @return the type
179
         */
180
        public TypeDesignationWorkingSetType getType() {
181
            return type;
182
        }
183

    
184
    }
185

    
186
    public class IdButton<T> {
187
        private Integer id;
188
        private Class<T> entityType;
189
        private Button button;
190

    
191
        public IdButton(Class<T> type, Integer id, Button button){
192
            this.entityType = type;
193
            this.id = id;
194
            this.button = button;
195
        }
196

    
197
        /**
198
         * @return the id
199
         */
200
        public Integer getId() {
201
            return id;
202
        }
203

    
204
        /**
205
         * @return the button
206
         */
207
        public Button getButton() {
208
            return button;
209
        }
210

    
211
        /**
212
         * @return the type
213
         */
214
        public Class<T> getType() {
215
            return entityType;
216
        }
217

    
218
    }
219

    
220
}
(3-3/8)