Project

General

Profile

Download (7.81 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.vaadin.component;
10

    
11
import java.util.Optional;
12

    
13
import com.vaadin.data.fieldgroup.BeanFieldGroup;
14
import com.vaadin.data.fieldgroup.FieldGroup;
15
import com.vaadin.ui.AbstractSelect;
16
import com.vaadin.ui.Button;
17
import com.vaadin.ui.Component;
18
import com.vaadin.ui.CssLayout;
19
import com.vaadin.ui.GridLayout;
20
import com.vaadin.ui.NativeSelect;
21
import com.vaadin.ui.themes.ValoTheme;
22

    
23
import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter.LabelType;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
26
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
30
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
31
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
32
import eu.etaxonomy.cdm.vaadin.model.name.NameRelationshipDTO;
33
import eu.etaxonomy.cdm.vaadin.util.ReferenceEllypsisCaptionGenerator;
34

    
35

    
36
/**
37
 * @author a.kohlbecker
38
 * @since May 3, 2018
39
 *
40
 */
41
public class NameRelationField extends CompositeCustomField<NameRelationshipDTO> {
42

    
43
    /**
44
     *
45
     */
46
    public static final String PRIMARY_STYLE = "v-name-relation-field";
47

    
48
    private static final long serialVersionUID = -7080885013120044655L;
49

    
50
    private CssLayout compositeWrapper = new CssLayout();
51

    
52
    private CssLayout toolBar= new CssLayout();
53

    
54
    private Button removeButton = ButtonFactory.REMOVE_ALL_ITEMS.createButton();
55

    
56
    private Button newButton = ButtonFactory.CREATE_NEW.createButton();
57

    
58
    private BeanFieldGroup<NameRelationshipDTO> fieldGroup = new BeanFieldGroup<>(NameRelationshipDTO.class);
59

    
60
    ToOneRelatedEntityCombobox<TaxonName> relatedNameComboBox;
61

    
62
    ToOneRelatedEntityCombobox<Reference> citatonComboBox;
63

    
64
    TextFieldNFix citationMicroReferenceField = new TextFieldNFix();
65

    
66
    TextFieldNFix ruleConsideredField = new TextFieldNFix();
67

    
68
    NativeSelect codeEditionSelect = new NativeSelect();
69

    
70
    private Direction direction;
71

    
72
    private NameRelationshipType type;
73

    
74
    private GridLayout grid;
75

    
76
    private String nameFieldCaption;
77

    
78

    
79
    /**
80
     * @param string
81
     */
82
    public NameRelationField(String caption, String nameFieldCaption, Direction direction, NameRelationshipType type) {
83
        this.direction = direction;
84
        this.type = type;
85

    
86
        setCaption(caption);
87
        setPrimaryStyleName(PRIMARY_STYLE);
88

    
89
        if(nameFieldCaption == null){
90
            nameFieldCaption = "Related name";
91
        }
92
        this.nameFieldCaption = nameFieldCaption;
93

    
94
        relatedNameComboBox = new ToOneRelatedEntityCombobox<TaxonName>(this.nameFieldCaption, TaxonName.class);
95
        citatonComboBox = new ToOneRelatedEntityCombobox<Reference>("Reference", Reference.class);
96

    
97
        setValidationVisible(false);
98
    }
99

    
100
    /**
101
     * {@inheritDoc}
102
     */
103
    @Override
104
    protected void addDefaultStyles() {
105
        // TODO Auto-generated method stub
106

    
107
    }
108

    
109
    /**
110
     * {@inheritDoc}
111
     */
112
    @Override
113
    public Optional<FieldGroup> getFieldGroup() {
114
        return Optional.of(fieldGroup);
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    protected Component initContent() {
122

    
123
        newButton.addClickListener(e -> {
124
            setValue(new NameRelationshipDTO(direction, type));
125
            updateToolBarButtonStates();
126
            });
127
        removeButton.addClickListener(e -> {
128
                setValue(null);
129
                updateToolBarButtonStates();
130
            });
131
        relatedNameComboBox.setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<TaxonName>(relatedNameComboBox));
132
        citatonComboBox.setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(citatonComboBox, false));
133
        citatonComboBox.getSelect().setCaptionGenerator(
134
                new ReferenceEllypsisCaptionGenerator(LabelType.BIBLIOGRAPHIC, citatonComboBox.getSelect())
135
                );
136

    
137
        grid = new GridLayout(2, 3);
138

    
139
        grid.addComponent(relatedNameComboBox, 0, 0, 1, 0);
140

    
141
        relatedNameComboBox.setCaption(nameFieldCaption);
142
        citatonComboBox.setCaption("Reference");
143
        citationMicroReferenceField.setCaption("Reference detail");
144
        ruleConsideredField.setCaption("Rule considered");
145
        codeEditionSelect.setCaption("Nom. code");
146

    
147
        grid.addComponent(citatonComboBox, 0, 1, 0, 1);
148
        grid.addComponent(citationMicroReferenceField, 1, 1, 1, 1);
149
        grid.addComponent(ruleConsideredField, 0, 2, 0, 2);
150
        grid.addComponent(codeEditionSelect, 1, 2, 1, 2);
151

    
152
        relatedNameComboBox.setWidth(100, Unit.PERCENTAGE);
153
        ruleConsideredField.setWidth(100, Unit.PERCENTAGE);
154
        citatonComboBox.setWidth(100, Unit.PERCENTAGE);
155
        codeEditionSelect.setWidth(100, Unit.PERCENTAGE);
156

    
157

    
158

    
159
        grid.setColumnExpandRatio(0, 7);
160
        grid.setWidth(100, Unit.PERCENTAGE);
161

    
162
        toolBar.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP + " toolbar");
163
        toolBar.addComponents(newButton, removeButton);
164

    
165
        compositeWrapper.setStyleName("margin-wrapper");
166
        compositeWrapper.addComponents(toolBar);
167

    
168
        addSizedComponents(compositeWrapper);
169
        addStyledComponents(relatedNameComboBox, citationMicroReferenceField, citatonComboBox, ruleConsideredField, codeEditionSelect, newButton, removeButton);
170

    
171
        return compositeWrapper;
172
    }
173

    
174
    /**
175
     * {@inheritDoc}
176
     */
177
    @Override
178
    public Class<NameRelationshipDTO> getType() {
179
        return NameRelationshipDTO.class;
180
    }
181

    
182
    /**
183
     * {@inheritDoc}
184
     */
185
    @Override
186
    protected void setInternalValue(NameRelationshipDTO newValue) {
187

    
188
        NameRelationshipDTO oldValue = getValue();
189

    
190
        super.setInternalValue(newValue);
191

    
192
        newValue = HibernateProxyHelper.deproxy(newValue);
193
        if(newValue != null) {
194
            compositeWrapper.addComponent(grid);
195
            fieldGroup.bind(relatedNameComboBox, "otherName");
196
            fieldGroup.bind(citationMicroReferenceField, "citationMicroReference");
197
            fieldGroup.bind(citatonComboBox, "citation");
198
            fieldGroup.bind(ruleConsideredField, "ruleConsidered");
199
            fieldGroup.bind(codeEditionSelect, "codeEdition");
200

    
201
            fieldGroup.setItemDataSource(newValue);
202
        } else {
203
            if(oldValue != null){
204
                compositeWrapper.removeComponent(grid);
205
                fieldGroup.unbind(relatedNameComboBox);
206
                fieldGroup.unbind(citationMicroReferenceField);
207
                fieldGroup.unbind(citatonComboBox);
208
                fieldGroup.unbind(ruleConsideredField);
209
                fieldGroup.unbind(codeEditionSelect);
210

    
211
                fieldGroup.setItemDataSource(newValue);
212
            }
213
        }
214

    
215
        updateToolBarButtonStates();
216

    
217
    }
218

    
219

    
220
   private void updateToolBarButtonStates(){
221
       boolean hasValue = getValue() != null;
222
       removeButton.setVisible(hasValue);
223
       newButton.setVisible(!hasValue);
224
   }
225

    
226
    /**
227
     * @return the relatedNameComboBox
228
     */
229
    public ToOneRelatedEntityCombobox<TaxonName> getRelatedNameComboBox() {
230
        return relatedNameComboBox;
231
    }
232

    
233
    /**
234
     * @return the citatonComboBox
235
     */
236
    public ToOneRelatedEntityCombobox<Reference> getCitatonComboBox() {
237
        return citatonComboBox;
238
    }
239

    
240
    public AbstractSelect getCodeEditionSelect(){
241
        return codeEditionSelect;
242
    }
243

    
244
    /**
245
     * {@inheritDoc}
246
     */
247
    @Override
248
    public void setReadOnly(boolean readOnly) {
249
        super.setReadOnly(readOnly);
250
        setDeepReadOnly(readOnly, grid, null);
251
        setDeepReadOnly(readOnly, toolBar, null);
252
    }
253

    
254

    
255
}
(5-5/19)