Project

General

Profile

Download (6.92 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.Button;
16
import com.vaadin.ui.Component;
17
import com.vaadin.ui.CssLayout;
18
import com.vaadin.ui.GridLayout;
19
import com.vaadin.ui.themes.ValoTheme;
20

    
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
23
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
24
import eu.etaxonomy.cdm.model.name.TaxonName;
25
import eu.etaxonomy.cdm.model.reference.Reference;
26
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
27
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
28
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
29
import eu.etaxonomy.cdm.vaadin.model.name.NameRelationshipDTO;
30

    
31

    
32
/**
33
 * @author a.kohlbecker
34
 * @since May 3, 2018
35
 *
36
 */
37
public class NameRelationField extends CompositeCustomField<NameRelationshipDTO> {
38

    
39
    /**
40
     *
41
     */
42
    public static final String PRIMARY_STYLE = "v-name-relation-field";
43

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

    
46
    private CssLayout compositeWrapper = new CssLayout();
47

    
48
    private CssLayout toolBar= new CssLayout();
49

    
50
    private Button removeButton = ButtonFactory.REMOVE_ITEM.createButton();
51

    
52
    private Button newButton = ButtonFactory.ADD_ITEM.createButton();
53

    
54
    private BeanFieldGroup<NameRelationshipDTO> fieldGroup = new BeanFieldGroup<>(NameRelationshipDTO.class);
55

    
56
    ToOneRelatedEntityCombobox<TaxonName> relatedNameComboBox;
57

    
58
    ToOneRelatedEntityCombobox<Reference> citatonComboBox;
59

    
60
    TextFieldNFix citationMicroReferenceField = new TextFieldNFix();
61

    
62
    TextFieldNFix ruleConsideredField = new TextFieldNFix();
63

    
64
    private Direction direction;
65

    
66
    private NameRelationshipType type;
67

    
68
    private GridLayout grid;
69

    
70
    private String nameFieldCaption;
71

    
72

    
73
    /**
74
     * @param string
75
     */
76
    public NameRelationField(String caption, String nameFieldCaption, Direction direction, NameRelationshipType type) {
77
        this.direction = direction;
78
        this.type = type;
79

    
80
        setCaption(caption);
81
        setPrimaryStyleName(PRIMARY_STYLE);
82

    
83
        if(nameFieldCaption == null){
84
            this.nameFieldCaption = "Related name";
85
        }
86

    
87
        relatedNameComboBox = new ToOneRelatedEntityCombobox<TaxonName>(this.nameFieldCaption, TaxonName.class);
88
        citatonComboBox = new ToOneRelatedEntityCombobox<Reference>("Reference", Reference.class);
89

    
90
        setValidationVisible(false);
91
    }
92

    
93
    /**
94
     * {@inheritDoc}
95
     */
96
    @Override
97
    protected void addDefaultStyles() {
98
        // TODO Auto-generated method stub
99

    
100
    }
101

    
102
    /**
103
     * {@inheritDoc}
104
     */
105
    @Override
106
    public Optional<FieldGroup> getFieldGroup() {
107
        return Optional.of(fieldGroup);
108
    }
109

    
110
    /**
111
     * {@inheritDoc}
112
     */
113
    @Override
114
    protected Component initContent() {
115

    
116
        newButton.addClickListener(e -> {
117
            setValue(new NameRelationshipDTO(direction, type));
118
            updateToolBarButtonStates();
119
            });
120
        removeButton.addClickListener(e -> {
121
                setValue(null);
122
                updateToolBarButtonStates();
123
            });
124
        relatedNameComboBox.setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<TaxonName>(relatedNameComboBox));
125
        citatonComboBox.setNestedButtonStateUpdater(new ToOneRelatedEntityButtonUpdater<Reference>(citatonComboBox, false));
126

    
127
        grid = new GridLayout(2, 3);
128

    
129
        grid.addComponent(relatedNameComboBox, 0, 0, 1, 0);
130

    
131
        relatedNameComboBox.setCaption(nameFieldCaption);
132
        citatonComboBox.setCaption("Reference");
133
        citationMicroReferenceField.setCaption("Reference detail");
134
        ruleConsideredField.setCaption("Rule considered");
135

    
136
        grid.addComponent(citatonComboBox, 0, 1, 0, 1);
137
        grid.addComponent(citationMicroReferenceField, 1, 1, 1, 1);
138
        grid.addComponent(ruleConsideredField, 0, 2, 1, 2);
139

    
140
        relatedNameComboBox.setWidth(100, Unit.PERCENTAGE);
141
        ruleConsideredField.setWidth(100, Unit.PERCENTAGE);
142
        citatonComboBox.setWidth(100, Unit.PERCENTAGE);
143

    
144

    
145

    
146
        grid.setColumnExpandRatio(0, 7);
147
        grid.setWidth(100, Unit.PERCENTAGE);
148

    
149
        toolBar.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP + " toolbar");
150
        toolBar.addComponents(newButton, removeButton);
151

    
152
        compositeWrapper.setStyleName("margin-wrapper");
153
        compositeWrapper.addComponents(toolBar);
154

    
155
        addSizedComponents(compositeWrapper);
156
        addStyledComponents(relatedNameComboBox, citationMicroReferenceField, citatonComboBox, ruleConsideredField, newButton, removeButton);
157

    
158
        return compositeWrapper;
159
    }
160

    
161
    /**
162
     * {@inheritDoc}
163
     */
164
    @Override
165
    public Class<NameRelationshipDTO> getType() {
166
        return NameRelationshipDTO.class;
167
    }
168

    
169
    /**
170
     * {@inheritDoc}
171
     */
172
    @Override
173
    protected void setInternalValue(NameRelationshipDTO newValue) {
174

    
175
        NameRelationshipDTO oldValue = getValue();
176

    
177
        super.setInternalValue(newValue);
178

    
179

    
180
        newValue = HibernateProxyHelper.deproxy(newValue);
181
        if(newValue != null) {
182
            compositeWrapper.addComponent(grid);
183
            fieldGroup.bind(relatedNameComboBox, "otherName");
184
            fieldGroup.bind(citationMicroReferenceField, "citationMicroReference");
185
            fieldGroup.bind(citatonComboBox, "citation");
186
            fieldGroup.bind(ruleConsideredField, "ruleConsidered");
187

    
188
            fieldGroup.setItemDataSource(newValue);
189
        } else {
190
            if(oldValue != null){
191
                compositeWrapper.removeComponent(grid);
192
                fieldGroup.unbind(relatedNameComboBox);
193
                fieldGroup.unbind(citationMicroReferenceField);
194
                fieldGroup.unbind(citatonComboBox);
195
                fieldGroup.unbind(ruleConsideredField);
196

    
197
                fieldGroup.setItemDataSource(newValue);
198
            }
199
        }
200

    
201
        updateToolBarButtonStates();
202

    
203
    }
204

    
205

    
206
   private void updateToolBarButtonStates(){
207
       boolean hasValue = getValue() != null;
208
       removeButton.setEnabled(hasValue);
209
       newButton.setEnabled(!hasValue);
210
   }
211

    
212
    /**
213
     * @return the relatedNameComboBox
214
     */
215
    public ToOneRelatedEntityCombobox<TaxonName> getRelatedNameComboBox() {
216
        return relatedNameComboBox;
217
    }
218

    
219
    /**
220
     * @return the citatonComboBox
221
     */
222
    public ToOneRelatedEntityCombobox<Reference> getCitatonComboBox() {
223
        return citatonComboBox;
224
    }
225

    
226
    /**
227
     * {@inheritDoc}
228
     */
229
    @Override
230
    public void setReadOnly(boolean readOnly) {
231
        super.setReadOnly(readOnly);
232
        setDeepReadOnly(readOnly, grid, null);
233
        setDeepReadOnly(readOnly, toolBar, null);
234
    }
235

    
236

    
237
}
(5-5/19)