Project

General

Profile

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

    
81
                label.setWidthUndefined();
82
                addComponent(label);
83
                labels.add(label);
84
            }
85
        }
86
        addTypeDesignationButton = new Button(FontAwesome.PLUS);
87
        addComponent(addTypeDesignationButton);
88

    
89

    
90
        iterator().forEachRemaining(c -> addStyledComponent(c));
91
        addDefaultStyles();
92

    
93
    }
94

    
95
    public IdButton<TaxonName> getNameButton() {
96
        return nameIdButton;
97
    }
98

    
99
    /**
100
     * {@inheritDoc}
101
     */
102
    @Override
103
    protected void addDefaultStyles() {
104
        addStyleName(STYLE_NAMES);
105
        nameIdButton.getButton().addStyleName(DEFAULT_BUTTON_STYLES);
106
        typeDesignationButtons.forEach(idb -> idb.getButton().addStyleName(DEFAULT_BUTTON_STYLES));
107
        addTypeDesignationButton.addStyleName(DEFAULT_BUTTON_STYLES);
108
    }
109

    
110
    public class IdButton<T> {
111
        private Integer id;
112
        private Class<T> type;
113
        private Button button;
114

    
115
        public IdButton(Class<T> type, Integer id, Button button){
116
            this.type = type;
117
            this.id = id;
118
            this.button = button;
119
        }
120

    
121
        /**
122
         * @return the id
123
         */
124
        public Integer getId() {
125
            return id;
126
        }
127

    
128
        /**
129
         * @return the button
130
         */
131
        public Button getButton() {
132
            return button;
133
        }
134

    
135
        /**
136
         * @return the type
137
         */
138
        public Class<T> getType() {
139
            return type;
140
        }
141

    
142
    }
143

    
144
    public class IdSetButton<T> {
145
        private Set<Integer> ids;
146
        private Class<T> type;
147
        private Button button;
148

    
149
        public IdSetButton(Class<T> type, Set<Integer> ids, Button button){
150
            this.type = type;
151
            this.ids = ids;
152
            this.button = button;
153
        }
154

    
155
        /**
156
         * @return the id
157
         */
158
        public Set<Integer> getIds() {
159
            return ids;
160
        }
161

    
162
        /**
163
         * @return the button
164
         */
165
        public Button getButton() {
166
            return button;
167
        }
168

    
169
        /**
170
         * @return the type
171
         */
172
        public Class<T> getType() {
173
            return type;
174
        }
175

    
176
    }
177

    
178
}
(3-3/7)