Project

General

Profile

Download (5.89 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.HashSet;
13
import java.util.List;
14
import java.util.Set;
15

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

    
21
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
22
import eu.etaxonomy.cdm.remote.dto.tdwg.voc.TaxonName;
23
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
24
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter.TypeDesignationWorkingSet;
25
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO;
26
import eu.etaxonomy.vaadin.component.CompositeStyledComponent;
27

    
28
/**
29
 * @author a.kohlbecker
30
 * @since May 19, 2017
31
 *
32
 */
33
public class RegistrationItemEditButtonGroup extends CompositeStyledComponent {
34

    
35

    
36
    /**
37
     *
38
     */
39
    private static final String DEFAULT_BUTTON_STYLES = ""; // ValoTheme.BUTTON_LINK;
40

    
41
    private static final long serialVersionUID = -5059163772392864050L;
42

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

    
45
    private IdButton<TaxonName> nameIdButton = null;
46

    
47
    private List<IdSetButton<TypeDesignationWorkingSet>> typeDesignationButtons = new ArrayList<>();
48

    
49
    private List<Label> labels = new ArrayList<>();
50

    
51
    private Button addTypeDesignationButton;
52

    
53

    
54
    public RegistrationItemEditButtonGroup(RegistrationDTO regDto){
55

    
56
        setWidth(100, Unit.PERCENTAGE);
57

    
58
        if(regDto.getName() != null){
59
            Button nameButton = new Button("Name:");
60
            nameButton.setDescription("Edit the Name");
61
            nameIdButton = new IdButton<TaxonName>(TaxonName.class, regDto.getName().getId(), nameButton);
62
            Label nameLabel = new Label(regDto.getName().getLabel());
63
            nameLabel.setWidthUndefined();
64
            addComponents(nameIdButton.getButton(), nameLabel);
65
        } else {
66
            // no name in the registration! we only show the typified name as label
67
            addComponent(new Label(regDto.getTypifiedName().getLabel()));
68
        }
69
        if(regDto.getOrderdTypeDesignationWorkingSets() != null){
70
            for(TypedEntityReference baseEntityRef : regDto.getOrderdTypeDesignationWorkingSets().keySet()) {
71
                TypeDesignationWorkingSet typeDesignationWorkingSet = regDto.getOrderdTypeDesignationWorkingSets().get(baseEntityRef);
72
                String buttonLabel = SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType()) ? "Type": "NameType";
73
                Button tdButton = new Button(buttonLabel + ":");
74
                tdButton.setDescription("Edit the type designation working set");
75
                addComponent(tdButton);
76
                Set<Integer> idSet = new HashSet<>();
77
                typeDesignationWorkingSet.getTypeDesignations().forEach(td -> idSet.add(td.getId()));
78
                typeDesignationButtons.add(new IdSetButton<TypeDesignationWorkingSet>(TypeDesignationWorkingSet.class, idSet, tdButton));
79
                String labelText = typeDesignationWorkingSet.getRepresentation();
80
                labelText = labelText.replaceAll("^[^:]+:", ""); // remove "Type:", "NameType:" from the beginning
81
                Label label = new Label(labelText);
82

    
83
                label.setWidthUndefined();
84
                addComponent(label);
85
                labels.add(label);
86
            }
87
        }
88
        addTypeDesignationButton = new Button(FontAwesome.PLUS);
89
        addTypeDesignationButton.setDescription("Add a new type designation");
90
        addComponent(addTypeDesignationButton);
91

    
92

    
93
        iterator().forEachRemaining(c -> addStyledComponent(c));
94
        addDefaultStyles();
95

    
96
    }
97

    
98
    public IdButton<TaxonName> getNameButton() {
99
        return nameIdButton;
100
    }
101

    
102
    public List<IdSetButton<TypeDesignationWorkingSet>> getTypeDesignationButtons() {
103
        return typeDesignationButtons;
104
    }
105

    
106
    public Button getAddTypeDesignationButton() {
107
        return addTypeDesignationButton;
108
    }
109

    
110
    /**
111
     * {@inheritDoc}
112
     */
113
    @Override
114
    protected void addDefaultStyles() {
115
        addStyleName(STYLE_NAMES);
116
        nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
117
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
118
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
119
    }
120

    
121
    public class IdButton<T> {
122
        private Integer id;
123
        private Class<T> type;
124
        private Button button;
125

    
126
        public IdButton(Class<T> type, Integer id, Button button){
127
            this.type = type;
128
            this.id = id;
129
            this.button = button;
130
        }
131

    
132
        /**
133
         * @return the id
134
         */
135
        public Integer getId() {
136
            return id;
137
        }
138

    
139
        /**
140
         * @return the button
141
         */
142
        public Button getButton() {
143
            return button;
144
        }
145

    
146
        /**
147
         * @return the type
148
         */
149
        public Class<T> getType() {
150
            return type;
151
        }
152

    
153
    }
154

    
155
    public class IdSetButton<T> {
156
        private Set<Integer> ids;
157
        private Class<T> type;
158
        private Button button;
159

    
160
        public IdSetButton(Class<T> type, Set<Integer> ids, Button button){
161
            this.type = type;
162
            this.ids = ids;
163
            this.button = button;
164
        }
165

    
166
        /**
167
         * @return the id
168
         */
169
        public Set<Integer> getIds() {
170
            return ids;
171
        }
172

    
173
        /**
174
         * @return the button
175
         */
176
        public Button getButton() {
177
            return button;
178
        }
179

    
180
        /**
181
         * @return the type
182
         */
183
        public Class<T> getType() {
184
            return type;
185
        }
186

    
187
    }
188

    
189
}
(3-3/7)