Project

General

Profile

Download (4.97 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.List;
14

    
15
import com.vaadin.data.fieldgroup.FieldGroup;
16
import com.vaadin.data.fieldgroup.FieldGroup.CommitEvent;
17
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
18
import com.vaadin.data.fieldgroup.FieldGroup.CommitHandler;
19
import com.vaadin.ui.Component;
20
import com.vaadin.ui.CustomField;
21

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

    
34
    private List<Component> styledComponents = new ArrayList<>();
35

    
36
    private List<Component> sizedComponents = new ArrayList<>();
37

    
38
    protected List<Component> getStyledComponents() {
39
        if(styledComponents == null){
40
            styledComponents = new ArrayList<>();
41
        }
42
        return styledComponents;
43
    }
44

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

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

    
68
    protected List<Component> getSizedComponents() {
69
        if(sizedComponents == null){
70
            sizedComponents = new ArrayList<>();
71
        }
72
        return sizedComponents;
73
    }
74

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

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

    
98
    @Override
99
    public void setWidth(String width) {
100
        super.setWidth(width);
101
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width);}});
102
    }
103

    
104
    @Override
105
    public void setWidth(float width, Unit unit){
106
        super.setWidth(width, unit);
107
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidth(width, unit);}});
108
    }
109

    
110
    @Override
111
    public void setWidthUndefined() {
112
        super.setWidthUndefined();
113
        getSizedComponents().forEach(c -> {if(c != null) {c.setWidthUndefined();}});
114
    }
115

    
116
    @Override
117
    public void setStyleName(String style) {
118
        super.setStyleName(style);
119
        getStyledComponents().forEach(c -> c.setStyleName(style));
120
        addDefaultStyles();
121
    }
122

    
123
    @Override
124
    public void addStyleName(String style) {
125
        super.addStyleName(style);
126
        getStyledComponents().forEach(c -> c.addStyleName(style));
127
    }
128

    
129
    protected void applyCurrentStyleNames(Component newSubComponent){
130
        newSubComponent.setStyleName(getStyleName());
131
    }
132

    
133
    protected void applyCurrentSize(Component newSubComponent){
134
        newSubComponent.setWidth(this.getWidth(), this.getWidthUnits());
135
    }
136

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

    
143
    /**
144
     * Implementations return the local fieldGroup
145
     *
146
     * @return
147
     */
148
    @Override
149
    public abstract FieldGroup getFieldGroup();
150

    
151
    @Override
152
    public void registerParentFieldGroup(FieldGroup parent) {
153
        parent.addCommitHandler(new CommitHandler() {
154

    
155
            @Override
156
            public void preCommit(CommitEvent commitEvent) throws CommitException {
157
                // commit the nested bean(s) first
158
                if(getFieldGroup() != null){
159
                    getFieldGroup().commit();
160
                }
161
            }
162

    
163
            @Override
164
            public void postCommit(CommitEvent commitEvent) throws CommitException {
165
                // noting to do
166
            }}
167
       );
168
    }
169

    
170
}
(1-1/10)