Project

General

Profile

Download (2.38 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.ui.Component;
16
import com.vaadin.ui.CssLayout;
17

    
18
/**
19
 * @author a.kohlbecker
20
 * @since May 22, 2017
21
 *
22
 * IMPORTANT see also {@link CompositeCustomField} which has almost the same functionality.
23
 *
24
 */
25
@SuppressWarnings("serial")
26
public abstract class CompositeStyledComponent extends CssLayout {
27

    
28
    //-------
29
    private List<Component> styledComponents = new ArrayList<>();
30

    
31
    protected List<Component> getStyledComponents() {
32
        if(styledComponents == null){
33
            styledComponents = new ArrayList<>();
34
        }
35
        return styledComponents;
36
    }
37

    
38
    @Override
39
    public void setStyleName(String style) {
40
        super.setStyleName(style);
41
        getStyledComponents().forEach(c -> c.setStyleName(style));
42
        addDefaultStyles();
43
    }
44

    
45
    @Override
46
    public void addStyleName(String style) {
47
        super.addStyleName(style);
48
        getStyledComponents().forEach(c -> c.addStyleName(style));
49
    }
50

    
51
    protected void applyCurrentStyleNames(Component newSubComponent){
52
        newSubComponent.setStyleName(getStyleName());
53
    }
54

    
55
    /**
56
     * Implementations preferably call this method in the constructor
57
     *
58
     * @param component
59
     * @return
60
     */
61
    protected boolean addStyledComponent(Component component){
62
        applyCurrentStyleNames(component);
63
        return styledComponents.add(component);
64
    }
65

    
66
    /**
67
     * Implementations preferably call this method in the constructor
68
     *
69
     * @param component
70
     * @return
71
     */
72
    protected boolean addStyledComponents(Component ... component){
73
        List<Component> componentList = Arrays.asList(component);
74
        componentList.forEach(c -> applyCurrentStyleNames(c));
75
        return styledComponents.addAll(componentList);
76
    }
77

    
78
    /**
79
     * Implementations can may apply default styles to components added to <code>StyledComponents</code>
80
     * to prevent these styles from being overwritten when setStyleName() id called on the composite field.
81
     */
82
    protected abstract void addDefaultStyles();
83
    //--------
84

    
85
}
(2-2/6)