Project

General

Profile

Download (4.43 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.fieldgroup.FieldGroup;
13
import com.vaadin.data.util.BeanItem;
14
import com.vaadin.server.UserError;
15
import com.vaadin.ui.Component;
16
import com.vaadin.ui.CssLayout;
17

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

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

    
33
    private static final long serialVersionUID = 660806402243118112L;
34

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

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

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

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

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

    
50
    public TeamOrPersonField(String caption){
51

    
52
        setCaption(caption);
53

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

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

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

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

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

    
92
        super.setInternalValue(newValue);
93

    
94
        newValue = HibernateProxyHelper.deproxy(newValue);
95

    
96
        if(newValue == null) {
97
            return;
98
        }
99

    
100
        if(Person.class.isAssignableFrom(newValue.getClass())){
101
            // value is a Person:
102
            compositeWrapper.addComponent(personField);
103

    
104
            personField.setValue((Person) newValue);
105
            personField.registerParentFieldGroup(fieldGroup);
106

    
107
        }
108
        else if(Team.class.isAssignableFrom(newValue.getClass())){
109
            // otherwise it a Team
110

    
111
            compositeWrapper.addComponents(titleField, nomenclaturalTitleField, personsListEditor);
112

    
113
            titleField.bindTo(fieldGroup, "titleCache", "protectedTitleCache");
114
            nomenclaturalTitleField.bindTo(fieldGroup, "nomenclaturalTitle", "protectedNomenclaturalTitleCache");
115
            fieldGroup.bind(personsListEditor, "teamMembers");
116

    
117
            fieldGroup.setItemDataSource(new BeanItem<Team>((Team)newValue));
118
            personsListEditor.registerParentFieldGroup(fieldGroup);
119

    
120

    
121
        } else {
122
            setComponentError(new UserError("TeamOrPersonField Error: Unsupported value type: " + newValue.getClass().getName()));
123
        }
124
    }
125

    
126
    /**
127
     * {@inheritDoc}
128
     */
129
    @Override
130
    protected void addDefaultStyles() {
131
        // no default styles here
132

    
133
    }
134

    
135
    /**
136
     * {@inheritDoc}
137
     */
138
    @Override
139
    public FieldGroup getFieldGroup() {
140
        return fieldGroup;
141
    }
142

    
143
    public Component[] getCachFields(){
144
        return new Component[]{titleField, nomenclaturalTitleField};
145
    }
146

    
147

    
148

    
149
}
(2-2/3)