Project

General

Profile

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

    
13
import com.vaadin.data.fieldgroup.FieldGroup;
14
import com.vaadin.ui.Component;
15
import com.vaadin.ui.CssLayout;
16
import com.vaadin.ui.TextField;
17

    
18
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
19

    
20

    
21
/**
22
 * @author a.kohlbecker
23
 * @since May 11, 2017
24
 *
25
 */
26
public class SwitchableTextField extends CompositeCustomField<String> {
27

    
28
    private static final long serialVersionUID = -4760153886584883137L;
29

    
30
    private static final String PRIMARY_STYLE = "v-switchable-textfield";
31

    
32
    CssLayout root = new CssLayout();
33
    private TextField textField = new TextFieldNFix();
34
    private SwitchButton unlockSwitch = new SwitchButton();
35

    
36
    /**
37
     * @param caption
38
     */
39
    public SwitchableTextField(String caption) {
40
        super();
41
        textField.setCaption(caption);
42
        unlockSwitch.addValueChangeListener(e -> {
43
            textField.setEnabled(unlockSwitch.getValue());
44
            textField.focus();
45
        });
46
        unlockSwitch.setValueSetLister(e -> {
47
            textField.setEnabled(unlockSwitch.getValue());
48
        });
49

    
50
        addSizedComponent(root);
51
        addSizedComponent(textField);
52

    
53
        addStyledComponent(textField);
54
        addStyledComponent(unlockSwitch);
55
        addDefaultStyles();
56
    }
57

    
58
    /**
59
     * {@inheritDoc}textField
60
     */
61
    @Override
62
    protected Component initContent() {
63
        root = new CssLayout();
64
        root.addComponent(textField);
65
        root.setWidth(getWidth(), getWidthUnits());
66
        textField.setWidth(getWidth(), getWidthUnits());
67
        root.addComponent(unlockSwitch);
68
        setPrimaryStyleName(PRIMARY_STYLE);
69

    
70
        return root;
71
    }
72

    
73
    /**
74
     * {@inheritDoc}
75
     */
76
    @Override
77
    public Class<? extends String> getType() {
78
        return String.class;
79
    }
80

    
81
    /**
82
     * @return the textField
83
     */
84
    public TextField getTextField() {
85
        return textField;
86
    }
87

    
88
    /**
89
     * @return the unlockSwitch
90
     */
91
    public Switch getUnlockSwitch() {
92
        return unlockSwitch;
93
    }
94

    
95
    public void bindTo(FieldGroup fieldGroup, Object textPropertyId, Object switchPropertyId){
96
        fieldGroup.bind(textField, textPropertyId);
97
        fieldGroup.bind(unlockSwitch, switchPropertyId);
98
        textField.setEnabled(unlockSwitch.getValue());
99
    }
100

    
101
    /**
102
     * {@inheritDoc}
103
     */
104
    @Override
105
    protected void addDefaultStyles() {
106
        unlockSwitch.addStyleName(Switch.DOM_STYLE);
107
    }
108

    
109
    /**
110
     * {@inheritDoc}
111
     */
112
    @Override
113
    public FieldGroup getFieldGroup() {
114
        return null;
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    public void setVisible(boolean visible) {
122
        super.setVisible(visible);
123
        // the unlockSwitch needs to be handled explicitly, for the textField this is not needed.
124
        unlockSwitch.setVisible(visible);
125
    }
126

    
127
}
(6-6/11)