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
|
}
|
54
|
|
55
|
/**
|
56
|
* {@inheritDoc}textField
|
57
|
*/
|
58
|
@Override
|
59
|
protected Component initContent() {
|
60
|
root = new CssLayout();
|
61
|
root.addComponent(textField);
|
62
|
root.setWidth(getWidth(), getWidthUnits());
|
63
|
textField.setWidth(getWidth(), getWidthUnits());
|
64
|
root.addComponent(unlockSwitch);
|
65
|
setPrimaryStyleName(PRIMARY_STYLE);
|
66
|
|
67
|
return root;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* {@inheritDoc}
|
72
|
*/
|
73
|
@Override
|
74
|
public Class<? extends String> getType() {
|
75
|
return String.class;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* @return the textField
|
80
|
*/
|
81
|
public TextField getTextField() {
|
82
|
return textField;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* @return the unlockSwitch
|
87
|
*/
|
88
|
public Switch getUnlockSwitch() {
|
89
|
return unlockSwitch;
|
90
|
}
|
91
|
|
92
|
public void bindTo(FieldGroup fieldGroup, Object textPropertyId, Object switchPropertyId){
|
93
|
fieldGroup.bind(textField, textPropertyId);
|
94
|
fieldGroup.bind(unlockSwitch, switchPropertyId);
|
95
|
textField.setEnabled(unlockSwitch.getValue());
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* {@inheritDoc}
|
100
|
*/
|
101
|
@Override
|
102
|
protected void addDefaultStyles() {
|
103
|
// no default styles
|
104
|
|
105
|
}
|
106
|
|
107
|
}
|