2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.vaadin
.component
;
11 import org
.vaadin
.teemu
.switchui
.Switch
;
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
;
18 import eu
.etaxonomy
.cdm
.vaadin
.component
.TextFieldNFix
;
22 * @author a.kohlbecker
26 public class SwitchableTextField
extends CompositeCustomField
<String
> {
28 private static final long serialVersionUID
= -4760153886584883137L;
30 private static final String PRIMARY_STYLE
= "v-switchable-textfield";
32 CssLayout root
= new CssLayout();
33 private TextField textField
= new TextFieldNFix();
34 private SwitchButton unlockSwitch
= new SwitchButton();
39 public SwitchableTextField(String caption
) {
41 textField
.setCaption(caption
);
42 unlockSwitch
.addValueChangeListener(e
-> {
43 textField
.setEnabled(unlockSwitch
.getValue());
46 unlockSwitch
.setValueSetLister(e
-> {
47 textField
.setEnabled(unlockSwitch
.getValue());
50 addSizedComponent(root
);
51 addSizedComponent(textField
);
53 addStyledComponent(textField
);
54 addStyledComponent(unlockSwitch
);
59 * {@inheritDoc}textField
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
);
77 public Class
<?
extends String
> getType() {
82 * @return the textField
84 public TextField
getTextField() {
89 * @return the unlockSwitch
91 public Switch
getUnlockSwitch() {
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());
105 protected void addDefaultStyles() {
106 unlockSwitch
.addStyleName(Switch
.DOM_STYLE
);
113 public FieldGroup
getFieldGroup() {
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
);