Project

General

Profile

Revision cd29f770

IDcd29f770bb8e87244b7a0646e3fc976c5bd333f9
Parent 9a1b0c01
Child bc5816ee

Added by Andreas Kohlbecker over 3 years ago

ref #6612 Custom Textfield with switch for all protectable cache fields

View differences:

pom.xml
450 450
      <artifactId>contextmenu</artifactId>
451 451
      <version>4.5</version>
452 452
    </dependency>
453
    <dependency>
454
       <groupId>org.vaadin.teemu</groupId>
455
       <artifactId>switch</artifactId>
456
       <version>3.0.0</version>
457
    </dependency>
458
    <dependency>
459
       <groupId>com.vaadin.addon</groupId>
460
       <artifactId>vaadin-onoffswitch</artifactId>
461
       <version>0.0.1</version>
462
    </dependency>
453 463
<!--     <dependency>
454 464
        only in the edit repo
455 465
      <groupId>org.vaadin.addons</groupId>
src/main/java/eu/etaxonomy/cdm/vaadin/AppWidgetSet.gwt.xml
40 40
    
41 41

  
42 42
    
43

  
44
    <inherits name="org.vaadin.teemu.switchui.SwitchComponentWidgetset" />
45

  
46
    <inherits name="com.vaadin.addon.onoffswitch.WidgetSet" />
43 47
</module>
src/main/java/eu/etaxonomy/cdm/vaadin/component/TimePeriodField.java
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;
10

  
11
import java.util.HashSet;
12
import java.util.Set;
13

  
14
import com.vaadin.data.fieldgroup.BeanFieldGroup;
15
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
16
import com.vaadin.data.util.BeanItem;
17
import com.vaadin.event.FieldEvents.TextChangeEvent;
18
import com.vaadin.server.FontAwesome;
19
import com.vaadin.ui.Alignment;
20
import com.vaadin.ui.Button;
21
import com.vaadin.ui.Component;
22
import com.vaadin.ui.CssLayout;
23
import com.vaadin.ui.CustomField;
24
import com.vaadin.ui.GridLayout;
25
import com.vaadin.ui.Label;
26
import com.vaadin.ui.TextField;
27
import com.vaadin.ui.themes.ValoTheme;
28

  
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
31
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
32
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
33
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
34

  
35
/**
36
 * @author a.kohlbecker
37
 * @since Apr 6, 2017
38
 *
39
 */
40
public class TimePeriodField extends CustomField<TimePeriod> {
41

  
42
    private static final long serialVersionUID = -7377778547595966252L;
43

  
44
    private static final String PRIMARY_STYLE = "v-time-period-field";
45

  
46
    private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
47

  
48
    TextField parseField = null;
49

  
50
    TextField freeText = null;
51

  
52
    Label toLabel = null;
53

  
54
    GridLayout grid = new GridLayout(3, 4);
55

  
56
    CssLayout detailsView = new CssLayout();
57

  
58
    //TODO implement custom button textfield which does not require a gridLayout
59
    GridLayout buttonTextField = new GridLayout(2, 1);
60
    GridLayout simpleView = new GridLayout(2, 1);
61

  
62
    TextField cacheField = new TextField();
63

  
64
    Set<Component> styledComponents = new HashSet<>();
65

  
66
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
67

  
68
    /**
69
     *
70
     */
71
    public TimePeriodField() {
72
        super();
73

  
74
    }
75

  
76
    /**
77
     * @param string
78
     */
79
    public TimePeriodField(String string) {
80
        this();
81
        setCaption(string);
82
    }
83

  
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    protected Component initContent() {
89

  
90
        super.setPrimaryStyleName(PRIMARY_STYLE);
91

  
92
        CssLayout root = new CssLayout();
93

  
94
        initSimpleView();
95
        initDetailsView();
96

  
97
        root.addComponent(simpleView);
98
        root.addComponent(detailsView);
99

  
100
        applyDefaultStyles();
101

  
102
        showSimple();
103

  
104
        return root;
105
    }
106

  
107
    /**
108
     *
109
     */
110
    private void initSimpleView() {
111

  
112
        Button showDetailsButton = new Button(FontAwesome.CALENDAR);
113
        showDetailsButton.addClickListener(e -> showDetails());
114
        cacheField.setWidth(353, Unit.PIXELS); // FIXME 100% does not work
115

  
116
        simpleView.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
117
        simpleView.setWidth(100, Unit.PERCENTAGE);
118
        simpleView.addComponent(showDetailsButton, 0, 0);
119
        simpleView.addComponent(cacheField, 1, 0);
120
        simpleView.setColumnExpandRatio(1, 0.9f);
121
    }
122

  
123
    /**
124
     *
125
     */
126
    private void initDetailsView() {
127

  
128
        parseField = new TextField();
129
        // parseField.setWidth(100, Unit.PERCENTAGE);
130
        parseField.setInputPrompt("This field will parse the entered time period");
131
        parseField.addTextChangeListener(e -> parseInput(e));
132
        parseField.setWidth(100, Unit.PERCENTAGE);
133

  
134
        Button closeDetailsButton = new Button(FontAwesome.CLOSE);
135
        closeDetailsButton.addClickListener(e -> {
136
            try {
137
                fieldGroup.commit();
138
            } catch (CommitException e1) {
139
                // TODO Auto-generated catch block
140
                e1.printStackTrace();
141
            }
142
            updateCacheField();
143
            showSimple();
144
        });
145

  
146
        buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
147
        buttonTextField.setWidth(100, Unit.PERCENTAGE);
148
        buttonTextField.addComponent(closeDetailsButton, 0, 0);
149
        buttonTextField.addComponent(parseField, 1, 0);
150
        buttonTextField.setColumnExpandRatio(1, 1.0f);
151

  
152
        PartialDateField startDate = new PartialDateField("Start");
153
        startDate.setInputPrompt("dd.mm.yyyy");
154
        PartialDateField endDate = new PartialDateField("End");
155
        endDate.setInputPrompt("dd.mm.yyyy");
156
        freeText = new TextField("FreeText");
157
        freeText.setWidth(100, Unit.PERCENTAGE);
158

  
159
        fieldGroup.bind(startDate, "start");
160
        fieldGroup.bind(endDate, "end");
161
        fieldGroup.bind(freeText, "freeText");
162

  
163
        toLabel = new Label("\u2014"); // EM DASH : 0x2014
164

  
165
        int row = 0;
166
        grid.addComponent(buttonTextField, 0, row, 2, row);
167
        row++;
168
        grid.addComponent(startDate, 0, row);
169
        grid.addComponent(toLabel, 1, row);
170
        grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
171
        grid.addComponent(endDate, 2, row);
172
        row++;
173
        grid.addComponent(freeText, 0, row, 2, row);
174

  
175
        // apply the style of the container to all child components. E.g. make all tiny
176
        addStyleName((getStyleName()));
177

  
178
        detailsView.setStyleName("margin-wrapper");
179
        detailsView.addComponent(grid);
180

  
181
    }
182

  
183

  
184
    /**
185
     * @return
186
     */
187
    private void showSimple() {
188
        detailsView.setVisible(false);
189
        simpleView.setVisible(true);
190
    }
191

  
192
    /**
193
     * @return
194
     */
195
    private void showDetails() {
196
        detailsView.setVisible(true);
197
        simpleView.setVisible(false);
198
    }
199

  
200
    /**
201
     * @param e
202
     * @return
203
     */
204
    private void parseInput(TextChangeEvent e) {
205
        if(!e.getText().isEmpty()){
206
            TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
207
            fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
208
        }
209
    }
210

  
211
    /**
212
     *
213
     */
214
    private void applyDefaultStyles() {
215
        if(parseField != null) {
216
            parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
217
            toLabel.addStyleName("to-label");
218
            buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
219
        }
220

  
221
    }
222

  
223
    @Override
224
    protected void setInternalValue(TimePeriod newValue) {
225
        super.setInternalValue(newValue);
226
        fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
227

  
228
        updateCacheField();
229
    }
230

  
231
    /**
232
     * @param newValue
233
     */
234
    private void updateCacheField() {
235
        TimePeriod newValue = fieldGroup.getItemDataSource().getBean();
236
        cacheField.setReadOnly(false);
237
        cacheField.setValue(timePeriodFormatter.print(newValue));
238
        cacheField.setReadOnly(true);
239
    }
240

  
241
    @Override
242
    public void setStyleName(String style) {
243
        super.setStyleName(style);
244
        grid.iterator().forEachRemaining(c -> c.setStyleName(style));
245
        buttonTextField.iterator().forEachRemaining(c -> c.setStyleName(style));
246
        simpleView.iterator().forEachRemaining(c -> c.setStyleName(style));
247
        applyDefaultStyles();
248
    }
249

  
250
    @Override
251
    public void addStyleName(String style) {
252
        super.addStyleName(style);
253
        grid.iterator().forEachRemaining(c -> c.addStyleName(style));
254
        simpleView.iterator().forEachRemaining(c -> {
255
            c.addStyleName(style);
256
        });
257

  
258
        buttonTextField.iterator().forEachRemaining(c -> c.addStyleName(style));
259
    }
260

  
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public Class<? extends TimePeriod> getType() {
266
        return TimePeriod.class;
267
    }
268

  
269

  
270

  
271
}
src/main/java/eu/etaxonomy/cdm/vaadin/component/common/TimePeriodField.java
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 java.util.HashSet;
12
import java.util.Set;
13

  
14
import com.vaadin.data.fieldgroup.BeanFieldGroup;
15
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
16
import com.vaadin.data.util.BeanItem;
17
import com.vaadin.event.FieldEvents.TextChangeEvent;
18
import com.vaadin.server.FontAwesome;
19
import com.vaadin.ui.Alignment;
20
import com.vaadin.ui.Button;
21
import com.vaadin.ui.Component;
22
import com.vaadin.ui.CssLayout;
23
import com.vaadin.ui.CustomField;
24
import com.vaadin.ui.GridLayout;
25
import com.vaadin.ui.Label;
26
import com.vaadin.ui.TextField;
27
import com.vaadin.ui.themes.ValoTheme;
28

  
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
31
import eu.etaxonomy.cdm.vaadin.component.PartialDateField;
32
import eu.etaxonomy.cdm.vaadin.component.registration.RegistrationStyles;
33
import eu.etaxonomy.cdm.vaadin.util.formatter.DateTimeFormat;
34
import eu.etaxonomy.cdm.vaadin.util.formatter.TimePeriodFormatter;
35

  
36
/**
37
 * @author a.kohlbecker
38
 * @since Apr 6, 2017
39
 *
40
 */
41
public class TimePeriodField extends CustomField<TimePeriod> {
42

  
43
    private static final long serialVersionUID = -7377778547595966252L;
44

  
45
    private static final String PRIMARY_STYLE = "v-time-period-field";
46

  
47
    private BeanFieldGroup<TimePeriod> fieldGroup = new BeanFieldGroup<>(TimePeriod.class);
48

  
49
    TextField parseField = null;
50

  
51
    TextField freeText = null;
52

  
53
    Label toLabel = null;
54

  
55
    GridLayout grid = new GridLayout(3, 4);
56

  
57
    CssLayout detailsView = new CssLayout();
58

  
59
    //TODO implement custom button textfield which does not require a gridLayout
60
    GridLayout buttonTextField = new GridLayout(2, 1);
61
    GridLayout simpleView = new GridLayout(2, 1);
62

  
63
    TextField cacheField = new TextField();
64

  
65
    Set<Component> styledComponents = new HashSet<>();
66

  
67
    private TimePeriodFormatter timePeriodFormatter = new TimePeriodFormatter(DateTimeFormat.ISO8601_DATE);
68

  
69
    /**
70
     *
71
     */
72
    public TimePeriodField() {
73
        super();
74

  
75
    }
76

  
77
    /**
78
     * @param string
79
     */
80
    public TimePeriodField(String string) {
81
        this();
82
        setCaption(string);
83
    }
84

  
85
    /**
86
     * {@inheritDoc}
87
     */
88
    @Override
89
    protected Component initContent() {
90

  
91
        super.setPrimaryStyleName(PRIMARY_STYLE);
92

  
93
        CssLayout root = new CssLayout();
94

  
95
        initSimpleView();
96
        initDetailsView();
97

  
98
        root.addComponent(simpleView);
99
        root.addComponent(detailsView);
100

  
101
        applyDefaultStyles();
102

  
103
        showSimple();
104

  
105
        return root;
106
    }
107

  
108
    /**
109
     *
110
     */
111
    private void initSimpleView() {
112

  
113
        Button showDetailsButton = new Button(FontAwesome.CALENDAR);
114
        showDetailsButton.addClickListener(e -> showDetails());
115
        cacheField.setWidth(353, Unit.PIXELS); // FIXME 100% does not work
116

  
117
        simpleView.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
118
        simpleView.setWidth(100, Unit.PERCENTAGE);
119
        simpleView.addComponent(showDetailsButton, 0, 0);
120
        simpleView.addComponent(cacheField, 1, 0);
121
        simpleView.setColumnExpandRatio(1, 0.9f);
122
    }
123

  
124
    /**
125
     *
126
     */
127
    private void initDetailsView() {
128

  
129
        parseField = new TextField();
130
        // parseField.setWidth(100, Unit.PERCENTAGE);
131
        parseField.setInputPrompt("This field will parse the entered time period");
132
        parseField.addTextChangeListener(e -> parseInput(e));
133
        parseField.setWidth(100, Unit.PERCENTAGE);
134

  
135
        Button closeDetailsButton = new Button(FontAwesome.CLOSE);
136
        closeDetailsButton.addClickListener(e -> {
137
            try {
138
                fieldGroup.commit();
139
            } catch (CommitException e1) {
140
                // TODO Auto-generated catch block
141
                e1.printStackTrace();
142
            }
143
            updateCacheField();
144
            showSimple();
145
        });
146

  
147
        buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
148
        buttonTextField.setWidth(100, Unit.PERCENTAGE);
149
        buttonTextField.addComponent(closeDetailsButton, 0, 0);
150
        buttonTextField.addComponent(parseField, 1, 0);
151
        buttonTextField.setColumnExpandRatio(1, 1.0f);
152

  
153
        PartialDateField startDate = new PartialDateField("Start");
154
        startDate.setInputPrompt("dd.mm.yyyy");
155
        PartialDateField endDate = new PartialDateField("End");
156
        endDate.setInputPrompt("dd.mm.yyyy");
157
        freeText = new TextField("FreeText");
158
        freeText.setWidth(100, Unit.PERCENTAGE);
159

  
160
        fieldGroup.bind(startDate, "start");
161
        fieldGroup.bind(endDate, "end");
162
        fieldGroup.bind(freeText, "freeText");
163

  
164
        toLabel = new Label("\u2014"); // EM DASH : 0x2014
165

  
166
        int row = 0;
167
        grid.addComponent(buttonTextField, 0, row, 2, row);
168
        row++;
169
        grid.addComponent(startDate, 0, row);
170
        grid.addComponent(toLabel, 1, row);
171
        grid.setComponentAlignment(toLabel, Alignment.BOTTOM_CENTER);
172
        grid.addComponent(endDate, 2, row);
173
        row++;
174
        grid.addComponent(freeText, 0, row, 2, row);
175

  
176
        // apply the style of the container to all child components. E.g. make all tiny
177
        addStyleName((getStyleName()));
178

  
179
        detailsView.setStyleName("margin-wrapper");
180
        detailsView.addComponent(grid);
181

  
182
    }
183

  
184

  
185
    /**
186
     * @return
187
     */
188
    private void showSimple() {
189
        detailsView.setVisible(false);
190
        simpleView.setVisible(true);
191
    }
192

  
193
    /**
194
     * @return
195
     */
196
    private void showDetails() {
197
        detailsView.setVisible(true);
198
        simpleView.setVisible(false);
199
    }
200

  
201
    /**
202
     * @param e
203
     * @return
204
     */
205
    private void parseInput(TextChangeEvent e) {
206
        if(!e.getText().isEmpty()){
207
            TimePeriod parsedPeriod = TimePeriodParser.parseString(e.getText());
208
            fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(parsedPeriod));
209
        }
210
    }
211

  
212
    /**
213
     *
214
     */
215
    private void applyDefaultStyles() {
216
        if(parseField != null) {
217
            parseField.addStyleName(RegistrationStyles.HELPER_FIELD);
218
            toLabel.addStyleName("to-label");
219
            buttonTextField.addStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
220
        }
221

  
222
    }
223

  
224
    @Override
225
    protected void setInternalValue(TimePeriod newValue) {
226
        super.setInternalValue(newValue);
227
        fieldGroup.setItemDataSource(new BeanItem<TimePeriod>(newValue));
228

  
229
        updateCacheField();
230
    }
231

  
232
    /**
233
     * @param newValue
234
     */
235
    private void updateCacheField() {
236
        TimePeriod newValue = fieldGroup.getItemDataSource().getBean();
237
        cacheField.setReadOnly(false);
238
        cacheField.setValue(timePeriodFormatter.print(newValue));
239
        cacheField.setReadOnly(true);
240
    }
241

  
242
    @Override
243
    public void setStyleName(String style) {
244
        super.setStyleName(style);
245
        grid.iterator().forEachRemaining(c -> c.setStyleName(style));
246
        buttonTextField.iterator().forEachRemaining(c -> c.setStyleName(style));
247
        simpleView.iterator().forEachRemaining(c -> c.setStyleName(style));
248
        applyDefaultStyles();
249
    }
250

  
251
    @Override
252
    public void addStyleName(String style) {
253
        super.addStyleName(style);
254
        grid.iterator().forEachRemaining(c -> c.addStyleName(style));
255
        simpleView.iterator().forEachRemaining(c -> {
256
            c.addStyleName(style);
257
        });
258

  
259
        buttonTextField.iterator().forEachRemaining(c -> c.addStyleName(style));
260
    }
261

  
262
    /**
263
     * {@inheritDoc}
264
     */
265
    @Override
266
    public Class<? extends TimePeriod> getType() {
267
        return TimePeriod.class;
268
    }
269

  
270

  
271

  
272
}
src/main/java/eu/etaxonomy/cdm/vaadin/ui/RegistrationUI.java
77 77
    }
78 78
    //---------------------------------------------
79 79

  
80
    //private final String INITIAL_VIEW = "workflow/edit/100002";
80
    // public static final String INITIAL_VIEW = "workflow/edit/10";
81 81
    public static final String INITIAL_VIEW =  DashBoardView.NAME;
82
    static boolean debugMode = false;
82 83

  
83 84
    /*
84 85
     * this HACKY solution forces the bean to be instantiated, TODO do it properly
......
116 117

  
117 118
        eventBus.publishEvent(new UIInitializedEvent());
118 119

  
119
        //navigate to initial view
120
        eventBus.publishEvent(new NavigationEvent(INITIAL_VIEW));
121

  
122 120
        String brand = "phycobank";
123 121
        //TODO create annotation:
124 122
        // @Styles(files={""}, branding="brand")
......
128 126
        // the 'vaadin://' protocol refers to the VAADIN folder
129 127
        Resource registryCssFile = new ExternalResource("vaadin://branding/" + brand + "/css/branding.css");
130 128
        Page.getCurrent().getStyles().add(registryCssFile);
129

  
130
        //navigate to initial view
131
        String state = pageFragmentAsState();
132

  
133
        if(debugMode && state != null){
134
            eventBus.publishEvent(new NavigationEvent(state));
135
        } else {
136
            eventBus.publishEvent(new NavigationEvent(INITIAL_VIEW));
137
        }
138
    }
139

  
140
    /**
141
     * @return
142
     */
143
    private String pageFragmentAsState() {
144
        Page page = Page.getCurrent();
145
        String fragment = page.getUriFragment();
146
        String state = null;
147
        if(fragment.startsWith("!")){
148
            state = fragment.substring(1, fragment.length());
149
        }
150
        return state;
131 151
    }
132 152
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/reference/ReferencePopupEditor.java
22 22

  
23 23
import eu.etaxonomy.cdm.model.reference.Reference;
24 24
import eu.etaxonomy.cdm.model.reference.ReferenceType;
25
import eu.etaxonomy.cdm.vaadin.component.TimePeriodField;
25
import eu.etaxonomy.cdm.vaadin.component.common.TimePeriodField;
26 26
import eu.etaxonomy.cdm.vaadin.security.AccessRestrictedView;
27 27
import eu.etaxonomy.vaadin.mvp.AbstractCdmPopupEditor;
28 28

  
......
86 86
        typeSelect.setRows(1);
87 87
        addField(typeSelect, "type", 3, row);
88 88
        row++;
89
        addTextField("Reference cache", "titleCache", 0, row, GRID_COLS-1, row).setWidth(100, Unit.PERCENTAGE);
89
        addSwitchableTextField("Reference cache", "titleCache", "protectedTitleCache", 0, row, GRID_COLS-1, row).setWidth(100, Unit.PERCENTAGE);
90 90
        row++;
91
        addTextField("Abbrev. cache", "abbrevTitleCache", 0, row, GRID_COLS-1, row).setWidth(100, Unit.PERCENTAGE);
91
        addSwitchableTextField("Abbrev. cache", "abbrevTitleCache", "protectedAbbrevTitleCache", 0, row, GRID_COLS-1, row).setWidth(100, Unit.PERCENTAGE);
92 92
        row++;
93 93
        titleField = addTextField("Title", "title", 0, row, GRID_COLS-1, row);
94 94
        titleField.setRequired(true);
src/main/java/eu/etaxonomy/vaadin/component/SwitchableTextField.java
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.CustomField;
17
import com.vaadin.ui.TextField;
18

  
19

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

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

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

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

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

  
49
    /**
50
     * {@inheritDoc}textField
51
     */
52
    @Override
53
    protected Component initContent() {
54
        root = new CssLayout();
55
        root.addComponent(textField);
56
        root.setWidth(getWidth(), getWidthUnits());
57
        textField.setWidth(getWidth(), getWidthUnits());
58
        root.addComponent(unlockSwitch);
59
        setPrimaryStyleName(PRIMARY_STYLE);
60
        return root;
61
    }
62

  
63
    /**
64
     * {@inheritDoc}
65
     */
66
    @Override
67
    public Class<? extends String> getType() {
68
        return String.class;
69
    }
70

  
71
    /**
72
     * @return the serialversionuid
73
     */
74
    public static long getSerialversionuid() {
75
        return serialVersionUID;
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
    public void setWidthUndefined() {
103
        super.setWidthUndefined();
104
        root.setWidthUndefined();
105
        textField.setWidthUndefined();
106
    }
107

  
108
    /**
109
     * {@inheritDoc}
110
     */
111
    @Override
112
    public void setWidth(String width) {
113
        super.setWidth(width);
114
        root.setWidth(width);
115
        textField.setWidth(width);
116
    }
117

  
118
    @Override
119
    public void setWidth(float width, Unit unit){
120
        super.setWidth(width, unit);
121
        if(root != null){
122
            root.setWidth(width, unit);
123
            textField.setWidth(width, unit);
124
        }
125
    }
126

  
127
    @Override
128
    public void setStyleName(String style) {
129
        super.setStyleName(style);
130
        textField.setStyleName(style);
131
        unlockSwitch.setStyleName(style);
132
    }
133

  
134
    @Override
135
    public void addStyleName(String style) {
136
        super.addStyleName(style);
137
        textField.addStyleName(style);
138
        unlockSwitch.addStyleName(style);
139
    }
140

  
141
    private class SwitchButton extends Switch {
142

  
143
        private static final long serialVersionUID = 2557108593729214773L;
144

  
145
        private ValueChangeListener valueSetListener = null;
146

  
147
        /**
148
         * {@inheritDoc}
149
         */
150
        @Override
151
        protected void setInternalValue(Boolean newValue) {
152
            super.setInternalValue(newValue);
153
            if(valueSetListener != null){
154
                valueSetListener.valueChange(new ValueChangeEvent(this));
155
            }
156
        }
157

  
158
        public void setValueSetLister(ValueChangeListener valueSetListener){
159
            this.valueSetListener = valueSetListener;
160
        }
161

  
162

  
163

  
164
    }
165
}
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractPopupEditor.java
41 41
import com.vaadin.ui.VerticalLayout;
42 42
import com.vaadin.ui.themes.ValoTheme;
43 43

  
44
import eu.etaxonomy.vaadin.component.SwitchableTextField;
44 45
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
45 46
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
46 47

  
......
217 218
        return addField(new TextField(caption), propertyId, column, row);
218 219
    }
219 220

  
221
    protected SwitchableTextField addSwitchableTextField(String caption, String textPropertyId, String switchPropertyId, int column1, int row1,
222
            int column2, int row2)
223
            throws OverlapsException, OutOfBoundsException {
224

  
225
        SwitchableTextField field = new SwitchableTextField(caption);
226
        field.bindTo(fieldGroup, textPropertyId, switchPropertyId);
227
        addComponent(field, column1, row1, column2, row2);
228
        return field;
229
    }
230

  
231
    protected SwitchableTextField addSwitchableTextField(String caption, String textPropertyId, String switchPropertyId, int column, int row)
232
            throws OverlapsException, OutOfBoundsException {
233

  
234
        SwitchableTextField field = new SwitchableTextField(caption);
235
        field.bindTo(fieldGroup, textPropertyId, switchPropertyId);
236
        addComponent(field, column, row);
237
        return field;
238
    }
239

  
220 240
    protected PopupDateField addDateField(String caption, String propertyId) {
221 241
        return addField(new PopupDateField(caption), propertyId);
222 242
    }
src/main/java/eu/etaxonomy/vaadin/ui/DevDayWidgetset.gwt.xml
14 14
    
15 15

  
16 16
    
17

  
18
    <inherits name="org.vaadin.teemu.switchui.SwitchComponentWidgetset" />
19

  
20
    <inherits name="com.vaadin.addon.onoffswitch.WidgetSet" />
17 21
</module>
src/main/webapp/VAADIN/themes/edit-valo/custom-fields.scss
24 24
          color: $helper-color;
25 25
          background-color: valo-font-color($helper-color);
26 26
          border-radius: $v-border-radius;
27
      }
27
   }
28
  
29
  // v-switchable-textfield
30
  .v-switchable-textfield {
31
    height: $v-line-height * 2;
32
    .v-caption {
33
        display: block;
34
    }   
35
    .v-textfield {
36
        padding-right: $v-unit-size;
37
    }
38
    .v-switch {
39
        z-index: 10;
40
        width: $v-unit-size;
41
        position: absolute;
42
        right: 1px;
43
        bottom: 1px;
44
    }
45
  }
28 46
} 

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)