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.vaadin.component;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collection;
|
14
|
import java.util.List;
|
15
|
|
16
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
17
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
|
18
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
|
19
|
import com.vaadin.data.fieldgroup.FieldGroup.CommitHandler;
|
20
|
import com.vaadin.ui.Component;
|
21
|
import com.vaadin.ui.CustomField;
|
22
|
import com.vaadin.ui.Field;
|
23
|
|
24
|
/**
|
25
|
* TODO implement height methods for full component size support
|
26
|
*
|
27
|
* @author a.kohlbecker
|
28
|
* @since May 12, 2017
|
29
|
*
|
30
|
* IMPORTANT see also {@link CompositeStyledComponent} which has almost the same functionality.
|
31
|
*
|
32
|
*/
|
33
|
@SuppressWarnings("serial")
|
34
|
public abstract class CompositeCustomField<T> extends CustomField<T> implements NestedFieldGroup {
|
35
|
|
36
|
private List<Component> styledComponents = new ArrayList<>();
|
37
|
|
38
|
private List<Component> sizedComponents = new ArrayList<>();
|
39
|
|
40
|
protected List<Component> getStyledComponents() {
|
41
|
if(styledComponents == null){
|
42
|
styledComponents = new ArrayList<>();
|
43
|
}
|
44
|
return styledComponents;
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* Implementations preferably call this method in the constructor
|
49
|
*
|
50
|
* @param component
|
51
|
* @return
|
52
|
*/
|
53
|
protected boolean addStyledComponent(Component component){
|
54
|
applyCurrentStyleNames(component);
|
55
|
return styledComponents.add(component);
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Implementations preferably call this method in the constructor
|
60
|
*
|
61
|
* @param component
|
62
|
* @return
|
63
|
*/
|
64
|
protected boolean addStyledComponents(Component ... component){
|
65
|
List<Component> componentList = Arrays.asList(component);
|
66
|
componentList.forEach(c -> applyCurrentStyleNames(c));
|
67
|
return styledComponents.addAll(componentList);
|
68
|
}
|
69
|
|
70
|
protected List<Component> getSizedComponents() {
|
71
|
if(sizedComponents == null){
|
72
|
sizedComponents = new ArrayList<>();
|
73
|
}
|
74
|
return sizedComponents;
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Implementations preferably call this method in the constructor
|
79
|
*
|
80
|
* @param component
|
81
|
* @return
|
82
|
*/
|
83
|
protected boolean addSizedComponent(Component component){
|
84
|
applyCurrentSize(component);
|
85
|
return sizedComponents.add(component);
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Implementations preferably call this method in the constructor
|
90
|
*
|
91
|
* @param component
|
92
|
* @return
|
93
|
*/
|
94
|
protected boolean addSizedComponents(Component ... component){
|
95
|
List<Component> componentList = Arrays.asList(component);
|
96
|
componentList.forEach(c -> applyCurrentSize(c));
|
97
|
return sizedComponents.addAll(componentList);
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public void setWidth(String width) {
|
102
|
super.setWidth(width);
|
103
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width);}});
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public void setWidth(float width, Unit unit){
|
108
|
super.setWidth(width, unit);
|
109
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width, unit);}});
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public void setWidthUndefined() {
|
114
|
super.setWidthUndefined();
|
115
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidthUndefined();}});
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
public void setStyleName(String style) {
|
120
|
super.setStyleName(style);
|
121
|
getStyledComponents().forEach(c -> c.setStyleName(style));
|
122
|
addDefaultStyles();
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
public void addStyleName(String style) {
|
127
|
super.addStyleName(style);
|
128
|
getStyledComponents().forEach(c -> c.addStyleName(style));
|
129
|
}
|
130
|
|
131
|
protected void applyCurrentStyleNames(Component newSubComponent){
|
132
|
newSubComponent.setStyleName(getStyleName());
|
133
|
}
|
134
|
|
135
|
protected void applyCurrentSize(Component newSubComponent){
|
136
|
newSubComponent.setWidth(this.getWidth(), this.getWidthUnits());
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Implementations can may apply default styles to components added to <code>StyledComponents</code>
|
141
|
* to prevent these styles from being overwritten when setStyleName() id called on the composite field.
|
142
|
*/
|
143
|
protected abstract void addDefaultStyles();
|
144
|
|
145
|
/**
|
146
|
* Implementations return the local fieldGroup
|
147
|
*
|
148
|
* @return
|
149
|
*/
|
150
|
@Override
|
151
|
public abstract FieldGroup getFieldGroup();
|
152
|
|
153
|
/**
|
154
|
* @return true if all fields having the value <code>null</code>
|
155
|
*/
|
156
|
@SuppressWarnings("rawtypes")
|
157
|
public boolean hasNullContent() {
|
158
|
Collection<Field> nullValueCheckIgnore = nullValueCheckIgnoreFields();
|
159
|
return getFieldGroup().getFields().stream()
|
160
|
.filter(
|
161
|
f -> !nullValueCheckIgnore.contains(f)
|
162
|
)
|
163
|
.peek( f -> System.out.println("###> " + f.getCaption() + ": " + f.getValue()))
|
164
|
.allMatch(
|
165
|
f -> {
|
166
|
if(f instanceof CompositeCustomField){
|
167
|
return ((CompositeCustomField)f).hasNullContent();
|
168
|
} else {
|
169
|
if(f.getValue() == null) {
|
170
|
return true;
|
171
|
} else {
|
172
|
return false;
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
);
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* @return
|
181
|
*/
|
182
|
protected List<Field> nullValueCheckIgnoreFields() {
|
183
|
// TODO Auto-generated method stub
|
184
|
return new ArrayList<Field>(0);
|
185
|
}
|
186
|
|
187
|
@Override
|
188
|
public void registerParentFieldGroup(FieldGroup parent) {
|
189
|
parent.addCommitHandler(new CommitHandler() {
|
190
|
|
191
|
@Override
|
192
|
public void preCommit(CommitEvent commitEvent) throws CommitException {
|
193
|
// commit the nested bean(s) first
|
194
|
if(getFieldGroup() != null){
|
195
|
getFieldGroup().commit();
|
196
|
}
|
197
|
}
|
198
|
|
199
|
@Override
|
200
|
public void postCommit(CommitEvent commitEvent) throws CommitException {
|
201
|
// noting to do
|
202
|
}}
|
203
|
);
|
204
|
}
|
205
|
|
206
|
}
|