Project

General

Profile

Download (6.08 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 org.vaadin.teemu.switchui.Switch;
12

    
13
import com.vaadin.data.fieldgroup.BeanFieldGroup;
14
import com.vaadin.data.fieldgroup.FieldGroup;
15
import com.vaadin.ui.Component;
16
import com.vaadin.ui.CssLayout;
17
import com.vaadin.ui.TextField;
18
import com.vaadin.ui.themes.ValoTheme;
19

    
20
import eu.etaxonomy.cdm.model.agent.Person;
21
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
22
import eu.etaxonomy.vaadin.component.CompositeCustomField;
23
import eu.etaxonomy.vaadin.component.SwitchButton;
24

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

    
32
    private static final long serialVersionUID = 8346575511284469356L;
33

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

    
36
    private BeanFieldGroup<Person> fieldGroup = new BeanFieldGroup<>(Person.class);
37

    
38
    enum Mode {
39
        CACHE_MODE, DETAILS_MODE;
40

    
41
        public String toCssClass() {
42
            return name().toLowerCase().replace("_", "-");
43
        }
44
    }
45

    
46
    private Mode currentMode = null;
47

    
48
    private float baseWidth = 100 / 8;
49

    
50
    private CssLayout root = new CssLayout();
51
    private TextField cacheField = new TextField();
52
    private CssLayout detailsContainer = new CssLayout();
53
    private TextField firstNameField = new TextField();
54
    private TextField lastNameField = new TextField();
55
    private TextField prefixField = new TextField();
56
    private TextField suffixField = new TextField();
57
    private SwitchButton unlockSwitch = new SwitchButton();
58

    
59
    /**
60
     * @param caption
61
     */
62
    public PersonField(String caption) {
63

    
64
        this();
65
        setCaption(caption);
66
    }
67

    
68
    /**
69
     * @param caption
70
     */
71
    public PersonField() {
72

    
73
        root.setPrimaryStyleName(PRIMARY_STYLE);
74

    
75
        addStyledComponent(cacheField);
76
        addStyledComponent(firstNameField);
77
        addStyledComponent(lastNameField);
78
        addStyledComponent(prefixField);
79
        addStyledComponent(suffixField);
80
        addStyledComponent(unlockSwitch);
81

    
82
        addSizedComponent(root);
83

    
84
    }
85

    
86
    /**
87
     *
88
     */
89
    private void checkUserPermissions(Person newValue) {
90
        boolean userCanEdit = UserHelper.fromSession().userHasPermission(newValue, "DELETE", "UPDATE");
91
        cacheField.setEnabled(userCanEdit);
92
        firstNameField.setEnabled(userCanEdit);
93
        lastNameField.setEnabled(userCanEdit);
94
        prefixField.setEnabled(userCanEdit);
95
        suffixField.setEnabled(userCanEdit);
96
    }
97

    
98
    private void setMode(Mode mode){
99
        if(mode.equals(currentMode)){
100
            return;
101
        }
102
        if(currentMode != null){
103
            String removeMode = currentMode.toCssClass();
104
            root.removeStyleName(removeMode);
105
        }
106
        String newMode = mode.toCssClass();
107
        root.addStyleName(newMode);
108
        currentMode = mode;
109
    }
110

    
111

    
112
    /**
113
     * {@inheritDoc}
114
     */
115
    @Override
116
    protected Component initContent() {
117

    
118
        root.addComponent(cacheField);
119
        root.addComponent(unlockSwitch);
120

    
121
        cacheField.setWidth(100, Unit.PERCENTAGE);
122

    
123
        prefixField.setWidth(baseWidth, Unit.PERCENTAGE);
124
        prefixField.setInputPrompt("Prefix");
125

    
126
        firstNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
127
        firstNameField.setInputPrompt("First Name");
128

    
129
        lastNameField.setWidth(baseWidth * 3, Unit.PERCENTAGE);
130
        lastNameField.setInputPrompt("Last Name");
131

    
132
        suffixField.setWidth(baseWidth, Unit.PERCENTAGE);
133
        suffixField.setInputPrompt("Suffix");
134

    
135
        detailsContainer.setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
136
        detailsContainer.addComponent(prefixField);
137
        detailsContainer.addComponent(firstNameField);
138
        detailsContainer.addComponent(lastNameField);
139
        detailsContainer.addComponent(suffixField);
140
        root.addComponent(detailsContainer);
141

    
142
        unlockSwitch.addValueChangeListener(e -> {
143
            if(refreshMode()){
144
                switch (currentMode) {
145
                    case CACHE_MODE:
146
                        cacheField.focus();
147
                        break;
148
                    case DETAILS_MODE:
149
                        firstNameField.focus();
150
                        break;
151
                    default:
152
                        break;
153

    
154
                }
155
            }
156
        });
157
        unlockSwitch.setValueSetLister(e -> {
158
            refreshMode();
159
        });
160

    
161
        addDefaultStyles();
162
        setMode(Mode.DETAILS_MODE);
163

    
164
        fieldGroup.bind(cacheField, "titleCache");
165
        fieldGroup.bind(prefixField, "prefix");
166
        fieldGroup.bind(firstNameField, "firstname");
167
        fieldGroup.bind(lastNameField, "lastname");
168
        fieldGroup.bind(suffixField, "suffix");
169
        fieldGroup.bind(unlockSwitch, "protectedTitleCache");
170
        fieldGroup.setBuffered(false);
171

    
172
        return root;
173
    }
174

    
175
    /**
176
     *
177
     * @return true if the mode has changed
178
     */
179
    protected boolean refreshMode() {
180
        Mode lastMode = currentMode;
181
        setMode(unlockSwitch.getValue() ? Mode.CACHE_MODE: Mode.DETAILS_MODE);
182
        return !lastMode.equals(currentMode);
183
    }
184

    
185
    /**
186
     * {@inheritDoc}
187
     */
188
    @Override
189
    public Class<? extends Person> getType() {
190
        return Person.class;
191
    }
192

    
193
    @Override
194
    public void setValue(Person person){
195
        super.setValue(person);
196
    }
197

    
198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    protected void setInternalValue(Person newValue) {
203
        super.setInternalValue(newValue);
204
        fieldGroup.setItemDataSource(newValue);
205
        checkUserPermissions(newValue);
206
    }
207

    
208
    @Override
209
    protected void addDefaultStyles(){
210
        cacheField.addStyleName("cache-field");
211
        detailsContainer.addStyleName("details-fields");
212
        unlockSwitch.addStyleName(Switch.ICON_STYLE);
213
    }
214

    
215
    /**
216
     * {@inheritDoc}
217
     */
218
    @Override
219
    public FieldGroup getFieldGroup() {
220
        return fieldGroup;
221
    }
222
}
(3-3/5)