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 com.vaadin.data.fieldgroup.BeanFieldGroup;
|
12
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
13
|
import com.vaadin.ui.Button;
|
14
|
import com.vaadin.ui.Component;
|
15
|
import com.vaadin.ui.CssLayout;
|
16
|
import com.vaadin.ui.GridLayout;
|
17
|
import com.vaadin.ui.themes.ValoTheme;
|
18
|
|
19
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
20
|
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
|
21
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
22
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
23
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
24
|
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
|
25
|
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
|
26
|
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityButtonUpdater;
|
27
|
import eu.etaxonomy.cdm.vaadin.model.name.NameRelationshipDTO;
|
28
|
|
29
|
|
30
|
/**
|
31
|
* @author a.kohlbecker
|
32
|
* @since May 3, 2018
|
33
|
*
|
34
|
*/
|
35
|
public class NameRelationField extends CompositeCustomField<NameRelationshipDTO> {
|
36
|
|
37
|
/**
|
38
|
*
|
39
|
*/
|
40
|
public static final String PRIMARY_STYLE = "v-name-relation-field";
|
41
|
|
42
|
private static final long serialVersionUID = -7080885013120044655L;
|
43
|
|
44
|
private CssLayout compositeWrapper = new CssLayout();
|
45
|
|
46
|
private CssLayout toolBar= new CssLayout();
|
47
|
|
48
|
private Button removeButton = ButtonFactory.REMOVE_ITEM.createButton();
|
49
|
|
50
|
private Button newButton = ButtonFactory.ADD_ITEM.createButton();
|
51
|
|
52
|
private BeanFieldGroup<NameRelationshipDTO> fieldGroup = new BeanFieldGroup<>(NameRelationshipDTO.class);
|
53
|
|
54
|
ToOneRelatedEntityCombobox<TaxonName> validatedNameComboBox;
|
55
|
|
56
|
ToOneRelatedEntityCombobox<Reference> citatonComboBox;
|
57
|
|
58
|
TextFieldNFix citationMicroReferenceField = new TextFieldNFix();
|
59
|
|
60
|
TextFieldNFix ruleConsideredField = new TextFieldNFix();
|
61
|
|
62
|
private Direction direction;
|
63
|
|
64
|
private NameRelationshipType type;
|
65
|
|
66
|
private GridLayout grid;
|
67
|
|
68
|
|
69
|
/**
|
70
|
* @param string
|
71
|
*/
|
72
|
public NameRelationField(String caption, Direction direction, NameRelationshipType type) {
|
73
|
this.direction = direction;
|
74
|
this.type = type;
|
75
|
|
76
|
setCaption(caption);
|
77
|
setPrimaryStyleName(PRIMARY_STYLE);
|
78
|
|
79
|
validatedNameComboBox = new ToOneRelatedEntityCombobox<TaxonName>("Validated name", TaxonName.class);
|
80
|
citatonComboBox = new ToOneRelatedEntityCombobox<Reference>("Reference", Reference.class);
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* {@inheritDoc}
|
85
|
*/
|
86
|
@Override
|
87
|
protected void addDefaultStyles() {
|
88
|
// TODO Auto-generated method stub
|
89
|
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* {@inheritDoc}
|
94
|
*/
|
95
|
@Override
|
96
|
public FieldGroup getFieldGroup() {
|
97
|
return fieldGroup;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* {@inheritDoc}
|
102
|
*/
|
103
|
@Override
|
104
|
protected Component initContent() {
|
105
|
|
106
|
newButton.addClickListener(e -> {
|
107
|
setValue(new NameRelationshipDTO(direction, type));
|
108
|
updateToolBarButtonStates();
|
109
|
});
|
110
|
removeButton.addClickListener(e -> {
|
111
|
setValue(null);
|
112
|
updateToolBarButtonStates();
|
113
|
});
|
114
|
validatedNameComboBox.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<TaxonName>(validatedNameComboBox));
|
115
|
citatonComboBox.getSelect().addValueChangeListener(new ToOneRelatedEntityButtonUpdater<Reference>(citatonComboBox));
|
116
|
|
117
|
grid = new GridLayout(2, 3);
|
118
|
|
119
|
grid.addComponent(validatedNameComboBox, 0, 0, 1, 0);
|
120
|
|
121
|
validatedNameComboBox.setCaption("Validated name");
|
122
|
citatonComboBox.setCaption("Reference");
|
123
|
citationMicroReferenceField.setCaption("Reference detail");
|
124
|
ruleConsideredField.setCaption("Rule considered");
|
125
|
|
126
|
grid.addComponent(citatonComboBox, 0, 1, 0, 1);
|
127
|
grid.addComponent(citationMicroReferenceField, 1, 1, 1, 1);
|
128
|
grid.addComponent(ruleConsideredField, 0, 2, 1, 2);
|
129
|
|
130
|
validatedNameComboBox.setWidth(100, Unit.PERCENTAGE);
|
131
|
ruleConsideredField.setWidth(100, Unit.PERCENTAGE);
|
132
|
citatonComboBox.setWidth(100, Unit.PERCENTAGE);
|
133
|
|
134
|
|
135
|
grid.setColumnExpandRatio(0, 7);
|
136
|
grid.setWidth(100, Unit.PERCENTAGE);
|
137
|
|
138
|
toolBar.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP + " toolbar");
|
139
|
toolBar.addComponents(newButton, removeButton);
|
140
|
|
141
|
compositeWrapper.setStyleName("margin-wrapper");
|
142
|
compositeWrapper.addComponents(toolBar);
|
143
|
|
144
|
addSizedComponents(compositeWrapper);
|
145
|
addStyledComponents(validatedNameComboBox, citationMicroReferenceField, citatonComboBox, ruleConsideredField, newButton, removeButton);
|
146
|
|
147
|
return compositeWrapper;
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* {@inheritDoc}
|
152
|
*/
|
153
|
@Override
|
154
|
public Class<NameRelationshipDTO> getType() {
|
155
|
return NameRelationshipDTO.class;
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* {@inheritDoc}
|
160
|
*/
|
161
|
@Override
|
162
|
protected void setInternalValue(NameRelationshipDTO newValue) {
|
163
|
|
164
|
NameRelationshipDTO oldValue = getValue();
|
165
|
|
166
|
super.setInternalValue(newValue);
|
167
|
|
168
|
|
169
|
newValue = HibernateProxyHelper.deproxy(newValue);
|
170
|
if(newValue != null) {
|
171
|
compositeWrapper.addComponent(grid);
|
172
|
getFieldGroup().bind(validatedNameComboBox, "otherName");
|
173
|
getFieldGroup().bind(citationMicroReferenceField, "citationMicroReference");
|
174
|
getFieldGroup().bind(citatonComboBox, "citation");
|
175
|
getFieldGroup().bind(ruleConsideredField, "ruleConsidered");
|
176
|
|
177
|
fieldGroup.setItemDataSource(newValue);
|
178
|
} else {
|
179
|
if(oldValue != null){
|
180
|
compositeWrapper.removeComponent(grid);
|
181
|
getFieldGroup().unbind(validatedNameComboBox);
|
182
|
getFieldGroup().unbind(citationMicroReferenceField);
|
183
|
getFieldGroup().unbind(citatonComboBox);
|
184
|
getFieldGroup().unbind(ruleConsideredField);
|
185
|
|
186
|
fieldGroup.setItemDataSource(newValue);
|
187
|
}
|
188
|
}
|
189
|
|
190
|
updateToolBarButtonStates();
|
191
|
|
192
|
}
|
193
|
|
194
|
|
195
|
private void updateToolBarButtonStates(){
|
196
|
boolean hasValue = getValue() != null;
|
197
|
removeButton.setEnabled(hasValue);
|
198
|
newButton.setEnabled(!hasValue);
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* @return the validatedNameComboBox
|
203
|
*/
|
204
|
public ToOneRelatedEntityCombobox<TaxonName> getValidatedNameComboBox() {
|
205
|
return validatedNameComboBox;
|
206
|
}
|
207
|
|
208
|
/**
|
209
|
* @return the citatonComboBox
|
210
|
*/
|
211
|
public ToOneRelatedEntityCombobox<Reference> getCitatonComboBox() {
|
212
|
return citatonComboBox;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* {@inheritDoc}
|
217
|
*/
|
218
|
@Override
|
219
|
public void setReadOnly(boolean readOnly) {
|
220
|
super.setReadOnly(readOnly);
|
221
|
setDeepReadOnly(readOnly, grid, null);
|
222
|
setDeepReadOnly(readOnly, toolBar, null);
|
223
|
}
|
224
|
|
225
|
|
226
|
}
|