Project

General

Profile

Download (7.12 KB) Statistics
| Branch: | Tag: | Revision:
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.Button;
21
import com.vaadin.ui.Component;
22
import com.vaadin.ui.CustomField;
23
import com.vaadin.ui.Field;
24
import com.vaadin.ui.Layout;
25

    
26
/**
27
 * TODO implement height methods for full component size support
28
 *
29
 * @author a.kohlbecker
30
 * @since May 12, 2017
31
 *
32
 * IMPORTANT see also {@link CompositeStyledComponent} which has almost the same functionality.
33
 *
34
 */
35
@SuppressWarnings("serial")
36
public abstract class CompositeCustomField<T> extends CustomField<T> implements NestedFieldGroup {
37

    
38
    private List<Component> styledComponents = new ArrayList<>();
39

    
40
    private List<Component> sizedComponents = new ArrayList<>();
41

    
42
    protected List<Component> getStyledComponents() {
43
        if(styledComponents == null){
44
            styledComponents = new ArrayList<>();
45
        }
46
        return styledComponents;
47
    }
48

    
49
    /**
50
     * Implementations preferably call this method in the constructor
51
     *
52
     * @param component
53
     * @return
54
     */
55
    protected boolean addStyledComponent(Component component){
56
        applyCurrentStyleNames(component);
57
        return styledComponents.add(component);
58
    }
59

    
60
    /**
61
     * Implementations preferably call this method in the constructor
62
     *
63
     * @param component
64
     * @return
65
     */
66
    protected boolean addStyledComponents(Component ... component){
67
        List<Component> componentList = Arrays.asList(component);
68
        componentList.forEach(c -> applyCurrentStyleNames(c));
69
        return styledComponents.addAll(componentList);
70
    }
71

    
72
    protected List<Component> getSizedComponents() {
73
        if(sizedComponents == null){
74
            sizedComponents = new ArrayList<>();
75
        }
76
        return sizedComponents;
77
    }
78

    
79
    /**
80
     * Implementations preferably call this method in the constructor
81
     *
82
     * @param component
83
     * @return
84
     */
85
    protected boolean addSizedComponent(Component component){
86
        applyCurrentSize(component);
87
        return sizedComponents.add(component);
88
    }
89

    
90
    /**
91
     * Implementations preferably call this method in the constructor
92
     *
93
     * @param component
94
     * @return
95
     */
96
    protected boolean addSizedComponents(Component ... component){
97
        List<Component> componentList = Arrays.asList(component);
98
        componentList.forEach(c -> applyCurrentSize(c));
99
        return sizedComponents.addAll(componentList);
100
    }
101

    
102
    @Override
103
    public void setWidth(String width) {
104
        super.setWidth(width);
105
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width);}});
106
    }
107

    
108
    @Override
109
    public void setWidth(float width, Unit unit){
110
        super.setWidth(width, unit);
111
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width, unit);}});
112
    }
113

    
114
    @Override
115
    public void setWidthUndefined() {
116
        super.setWidthUndefined();
117
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidthUndefined();}});
118
    }
119

    
120
    @Override
121
    public void setStyleName(String style) {
122
        super.setStyleName(style);
123
        getStyledComponents().forEach(c -> c.setStyleName(style));
124
        addDefaultStyles();
125
    }
126

    
127
    @Override
128
    public void addStyleName(String style) {
129
        super.addStyleName(style);
130
        getStyledComponents().forEach(c -> c.addStyleName(style));
131
    }
132

    
133
    protected void applyCurrentStyleNames(Component newSubComponent){
134
        newSubComponent.setStyleName(getStyleName());
135
    }
136

    
137
    protected void applyCurrentSize(Component newSubComponent){
138
        newSubComponent.setWidth(this.getWidth(), this.getWidthUnits());
139
    }
140

    
141
    /**
142
     * Implementations can may apply default styles to components added to <code>StyledComponents</code>
143
     * to prevent these styles from being overwritten when setStyleName() id called on the composite field.
144
     */
145
    protected abstract void addDefaultStyles();
146

    
147
    /**
148
     * Implementations return the local fieldGroup
149
     *
150
     * @return
151
     */
152
    @Override
153
    public abstract FieldGroup getFieldGroup();
154

    
155
    /**
156
     * @return true if all fields having the value <code>null</code>
157
     */
158
    @SuppressWarnings("rawtypes")
159
    public boolean hasNullContent() {
160
        Collection<Field> nullValueCheckIgnore = nullValueCheckIgnoreFields();
161
        return getFieldGroup().getFields().stream()
162
                .filter(
163
                        f -> !nullValueCheckIgnore.contains(f)
164
                )
165
                //.peek( f -> System.out.println("###> " + f.getCaption() + ": " + f.getValue()))
166
                .allMatch(
167
                        f -> {
168
                            if(f instanceof CompositeCustomField){
169
                                return ((CompositeCustomField)f).hasNullContent();
170
                            } else {
171
                                if(f.getValue() == null) {
172
                                    return true;
173
                                } else {
174
                                    return false;
175
                                }
176
                            }
177
                        }
178
                );
179
    }
180

    
181
    /**
182
     * @return
183
     */
184
    protected List<Field> nullValueCheckIgnoreFields() {
185
        // TODO Auto-generated method stub
186
        return new ArrayList<Field>(0);
187
    }
188

    
189
    @Override
190
    public void registerParentFieldGroup(FieldGroup parent) {
191
        parent.addCommitHandler(new CommitHandler() {
192

    
193
            @Override
194
            public void preCommit(CommitEvent commitEvent) throws CommitException {
195
                // commit the nested bean(s) first
196
                if(getFieldGroup() != null){
197
                    getFieldGroup().commit();
198
                }
199
            }
200

    
201
            @Override
202
            public void postCommit(CommitEvent commitEvent) throws CommitException {
203
                // noting to do
204
            }}
205
       );
206
    }
207

    
208
    /**
209
     * {@inheritDoc}
210
     */
211
    @Override
212
    public void setReadOnly(boolean readOnly) {
213
        super.setReadOnly(readOnly);
214
        // setDeepReadOnly(readOnly, getContent());
215
    }
216

    
217
    /**
218
     * @param readOnly
219
     */
220
    protected void setDeepReadOnly(boolean readOnly, Component component) {
221

    
222
        component.setReadOnly(readOnly);
223
        if(Button.class.isAssignableFrom(component.getClass())){
224
            component.setEnabled(false);
225
        }
226
        if(Layout.class.isAssignableFrom(component.getClass())){
227
            for(Component nestedComponent : ((Layout)component)){
228
                setDeepReadOnly(readOnly, nestedComponent);
229
            }
230
        }
231
    }
232

    
233
    @Override
234
    public String toString(){
235
        return this.getClass().getSimpleName() + ": " +
236
                ( getValue() != null ? getValue() : "null");
237
    }
238

    
239
}
(1-1/11)