Project

General

Profile

Download (3.96 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.cdm.vaadin.component.common;
10

    
11
import com.vaadin.data.fieldgroup.BeanFieldGroup;
12
import com.vaadin.data.util.BeanItem;
13
import com.vaadin.server.UserError;
14
import com.vaadin.ui.Component;
15
import com.vaadin.ui.CssLayout;
16

    
17
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18
import eu.etaxonomy.cdm.model.agent.Person;
19
import eu.etaxonomy.cdm.model.agent.Team;
20
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
21
import eu.etaxonomy.vaadin.component.CompositeCustomField;
22
import eu.etaxonomy.vaadin.component.FieldListEditor;
23
import eu.etaxonomy.vaadin.component.SwitchableTextField;
24

    
25
/**
26
 * @author a.kohlbecker
27
 * @since May 11, 2017
28
 *
29
 */
30
public class TeamOrPersonField extends CompositeCustomField<TeamOrPersonBase<?>> {
31

    
32
    private static final long serialVersionUID = 660806402243118112L;
33

    
34
    private static final String PRIMARY_STYLE = "v-team-or-person-field";
35

    
36
    private CssLayout root = new CssLayout();
37
    private CssLayout compositeWrapper = new CssLayout();
38

    
39
    // Fields for case when value is a Person
40
    private PersonField personField = new PersonField();
41

    
42
    // Fields for case when value is a Team
43
    private SwitchableTextField titleField = new SwitchableTextField("Team (bibliographic)");
44
    private SwitchableTextField nomenclaturalTitleField = new SwitchableTextField("Team (nomenclatural)");
45
    private FieldListEditor<Person, PersonField> personsListEditor = new FieldListEditor<Person, PersonField>(Person.class, PersonField.class, "Teammembers");
46

    
47
    private BeanFieldGroup<Team> fieldGroup  = new BeanFieldGroup<>(Team.class);
48

    
49
    public TeamOrPersonField(String caption){
50

    
51
        setCaption(caption);
52

    
53
        addStyledComponent(personField);
54
        addStyledComponent(titleField);
55
        addStyledComponent(nomenclaturalTitleField);
56
        addStyledComponent(personsListEditor);
57

    
58
        addSizedComponent(root);
59
        addSizedComponent(compositeWrapper);
60
        addSizedComponent(personField);
61
        addSizedComponent(titleField);
62
        addSizedComponent(nomenclaturalTitleField);
63
        addSizedComponent(personsListEditor);
64
    }
65

    
66
    /**
67
     * {@inheritDoc}
68
     */
69
    @Override
70
    protected Component initContent() {
71
        root.setPrimaryStyleName(PRIMARY_STYLE);
72
        compositeWrapper.setStyleName("margin-wrapper");
73
        root.addComponent(compositeWrapper);
74
        return root;
75
    }
76

    
77
    /**
78
     * {@inheritDoc}
79
     */
80
    @Override
81
    public Class getType() {
82
        return TeamOrPersonBase.class;
83
    }
84

    
85
    /**
86
     * {@inheritDoc}
87
     */
88
    @Override
89
    protected void setInternalValue(TeamOrPersonBase<?> newValue) {
90

    
91
        super.setInternalValue(newValue);
92

    
93
        newValue = HibernateProxyHelper.deproxy(newValue);
94

    
95
        if(Person.class.isAssignableFrom(newValue.getClass())){
96
            // value is a Person:
97
            compositeWrapper.addComponent(personField);
98

    
99
            personField.setValue((Person) newValue);
100
        }
101
        else if(Team.class.isAssignableFrom(newValue.getClass())){
102
            // otherwise it a Team
103

    
104
            compositeWrapper.addComponents(titleField, nomenclaturalTitleField, personsListEditor);
105

    
106
            titleField.bindTo(fieldGroup, "titleCache", "protectedTitleCache");
107
            nomenclaturalTitleField.bindTo(fieldGroup, "nomenclaturalTitle", "protectedNomenclaturalTitleCache");
108
            fieldGroup.bind(personsListEditor, "teamMembers");
109

    
110
            fieldGroup.setItemDataSource(new BeanItem<Team>((Team)newValue));
111
        } else {
112
            setComponentError(new UserError("TeamOrPersonField Error: Unsupported value type: " + newValue.getClass().getName()));
113
        }
114
    }
115

    
116
    /**
117
     * {@inheritDoc}
118
     */
119
    @Override
120
    protected void addDefaultStyles() {
121
        // no default styles here
122

    
123
    }
124

    
125

    
126

    
127
}
(2-2/3)