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.List;
|
14
|
|
15
|
import com.vaadin.ui.Component;
|
16
|
import com.vaadin.ui.CustomField;
|
17
|
|
18
|
/**
|
19
|
* TODO implement height methods for full component size support
|
20
|
*
|
21
|
* @author a.kohlbecker
|
22
|
* @since May 12, 2017
|
23
|
*
|
24
|
*/
|
25
|
@SuppressWarnings("serial")
|
26
|
public abstract class CompositeCustomField<T> extends CustomField<T> {
|
27
|
|
28
|
private List<Component> styledComponents = new ArrayList<>();
|
29
|
|
30
|
private List<Component> sizedComponents = new ArrayList<>();
|
31
|
|
32
|
protected List<Component> getStyledComponents() {
|
33
|
if(styledComponents == null){
|
34
|
styledComponents = new ArrayList<>();
|
35
|
}
|
36
|
return styledComponents;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Implementations preferably call this method in the constructor
|
41
|
*
|
42
|
* @param component
|
43
|
* @return
|
44
|
*/
|
45
|
protected boolean addStyledComponent(Component component){
|
46
|
applyCurrentStyleNames(component);
|
47
|
return styledComponents.add(component);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Implementations preferably call this method in the constructor
|
52
|
*
|
53
|
* @param component
|
54
|
* @return
|
55
|
*/
|
56
|
protected boolean addStyledComponents(Component ... component){
|
57
|
List<Component> componentList = Arrays.asList(component);
|
58
|
componentList.forEach(c -> applyCurrentStyleNames(c));
|
59
|
return styledComponents.addAll(componentList);
|
60
|
}
|
61
|
|
62
|
protected List<Component> getSizedComponents() {
|
63
|
if(sizedComponents == null){
|
64
|
sizedComponents = new ArrayList<>();
|
65
|
}
|
66
|
return sizedComponents;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Implementations preferably call this method in the constructor
|
71
|
*
|
72
|
* @param component
|
73
|
* @return
|
74
|
*/
|
75
|
protected boolean addSizedComponent(Component component){
|
76
|
applyCurrentSize(component);
|
77
|
return sizedComponents.add(component);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Implementations preferably call this method in the constructor
|
82
|
*
|
83
|
* @param component
|
84
|
* @return
|
85
|
*/
|
86
|
protected boolean addSizedComponents(Component ... component){
|
87
|
List<Component> componentList = Arrays.asList(component);
|
88
|
componentList.forEach(c -> applyCurrentSize(c));
|
89
|
return sizedComponents.addAll(componentList);
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public void setWidth(String width) {
|
94
|
super.setWidth(width);
|
95
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width);}});
|
96
|
}
|
97
|
|
98
|
@Override
|
99
|
public void setWidth(float width, Unit unit){
|
100
|
super.setWidth(width, unit);
|
101
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width, unit);}});
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
public void setWidthUndefined() {
|
106
|
super.setWidthUndefined();
|
107
|
getSizedComponents().forEach(c -> {if(c != null) {c.setWidthUndefined();}});
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
public void setStyleName(String style) {
|
112
|
super.setStyleName(style);
|
113
|
getStyledComponents().forEach(c -> c.setStyleName(style));
|
114
|
addDefaultStyles();
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public void addStyleName(String style) {
|
119
|
super.addStyleName(style);
|
120
|
getStyledComponents().forEach(c -> c.addStyleName(style));
|
121
|
}
|
122
|
|
123
|
protected void applyCurrentStyleNames(Component newSubComponent){
|
124
|
newSubComponent.setStyleName(getStyleName());
|
125
|
}
|
126
|
|
127
|
protected void applyCurrentSize(Component newSubComponent){
|
128
|
newSubComponent.setWidth(this.getWidth(), this.getWidthUnits());
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Implementations can may apply default styles to components added to <code>StyledComponents</code>
|
133
|
* to prevent these styles from being overwritten when setStyleName() id called on the composite field.
|
134
|
*/
|
135
|
protected abstract void addDefaultStyles();
|
136
|
|
137
|
}
|