Project

General

Profile

Download (3.03 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

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

    
26
    private static final long serialVersionUID = -4760153886584883137L;
27

    
28
    private static final String PRIMARY_STYLE = "v-switchable-textfield";
29

    
30
    CssLayout root = new CssLayout();
31
    private TextField textField = new TextField();
32
    private SwitchButton unlockSwitch = new SwitchButton();
33

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

    
48
        addSizedComponent(root);
49
        addSizedComponent(textField);
50

    
51
        addStyledComponent(textField);
52
        addStyledComponent(unlockSwitch);
53
        addDefaultStyles();
54
    }
55

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

    
68
        return root;
69
    }
70

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

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

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

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

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

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

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

    
125

    
126

    
127

    
128
}
(5-5/10)