Project

General

Profile

Download (7.75 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.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_ITEM.createButton();
55

    
56
    private Button newButton = ButtonFactory.ADD_ITEM.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
            this.nameFieldCaption = "Related name";
91
        }
92

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

    
96
        setValidationVisible(false);
97
    }
98

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

    
106
    }
107

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

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

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

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

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

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

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

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

    
156

    
157

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

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

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

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

    
170
        return compositeWrapper;
171
    }
172

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

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

    
187
        NameRelationshipDTO oldValue = getValue();
188

    
189
        super.setInternalValue(newValue);
190

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

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

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

    
214
        updateToolBarButtonStates();
215

    
216
    }
217

    
218

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

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

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

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

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

    
253

    
254
}
(5-5/19)