Project

General

Profile

Download (3.74 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.cdm.vaadin.component.common;
10

    
11
import org.apache.commons.lang.StringUtils;
12

    
13
import com.vaadin.ui.HorizontalLayout;
14
import com.vaadin.ui.TextField;
15

    
16
/**
17
 * @author a.kohlbecker
18
 * @since Jun 22, 2017
19
 *
20
 */
21
public class MinMaxTextField extends HorizontalLayout {
22

    
23
    /**
24
     *
25
     */
26
    private static final String PRIMARY_STYLE = "v-min-max-text-field";
27

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

    
30
    TextField minField, textField;
31
    MaxTextField maxField;
32

    
33
    String unitOfMeasure;
34

    
35
    public MinMaxTextField(String caption, String unitOfMeasure){
36

    
37
        this.unitOfMeasure = unitOfMeasure;
38

    
39
        setCaption(caption);
40
        setPrimaryStyleName(PRIMARY_STYLE);
41

    
42
        initFields(unitOfMeasure);
43

    
44
    }
45

    
46
    /**
47
     * @param unitOfMeasure
48
     */
49
    protected void initFields(String unitOfMeasure) {
50
        textField = new TextField("free text");
51
        maxField = new MaxTextField(String.format("min (%s)", unitOfMeasure));
52
        minField = new TextField(String.format("min (%s)", unitOfMeasure)) {
53

    
54
            private static final long serialVersionUID = -536012841624056585L;
55

    
56
            /**
57
             * {@inheritDoc}
58
             */
59
            @Override
60
            protected void setInternalValue(String newValue) {
61
                super.setInternalValue(newValue);
62
                updateMaxFieldEnablement();
63
            }
64

    
65
        };
66

    
67
        minField.setWidth("100px");
68
        maxField.setWidth("100px");
69
        textField.setWidth("100%");
70

    
71
        addComponents(minField, maxField, textField);
72

    
73
        setExpandRatio(textField, 1);
74

    
75
        minField.addValueChangeListener(e -> updateMaxFieldEnablement());
76
        maxField.setEnabled(false);
77
    }
78

    
79
    public void updateMaxFieldEnablement(){
80
        if(maxField != null && minField != null){
81
            boolean enabled = !StringUtils.isEmpty(minField.getValue());
82
            maxField.setSuperEnabled(enabled);
83
        }
84
    }
85

    
86
    /**
87
     * @return the minField
88
     */
89
    public TextField getMinField() {
90
        return minField;
91
    }
92

    
93
    /**
94
     * @return the maxField
95
     */
96
    public TextField getMaxField() {
97
        return maxField;
98
    }
99

    
100
    /**
101
     * @return the textField
102
     */
103
    public TextField getTextField() {
104
        return textField;
105
    }
106

    
107
    /**
108
     * @return the unitOfMeasure
109
     */
110
    public String getUnitOfMeasure() {
111
        return unitOfMeasure;
112
    }
113

    
114
    public void addSubComponentsStyleName(String style) {
115
        minField.addStyleName(style);
116
        maxField.addStyleName(style);
117
        textField.addStyleName(style);
118
    }
119

    
120
    /**
121
     * {@inheritDoc}
122
     */
123
    @Override
124
    public void setEnabled(boolean enabled) {
125
        // TODO Auto-generated method stub
126
        super.setEnabled(enabled);
127
    }
128

    
129
    class MaxTextField extends TextField {
130

    
131
        private static final long serialVersionUID = -536012841624056585L;
132

    
133

    
134

    
135
        /**
136
         * @param caption
137
         */
138
        public MaxTextField(String caption) {
139
            super(caption);
140
        }
141

    
142
        /**
143
         * {@inheritDoc}
144
         */
145
        @Override
146
        protected void setInternalValue(String newValue) {
147
            super.setInternalValue(newValue);
148
            updateMaxFieldEnablement();
149
        }
150

    
151
        /**
152
         * {@inheritDoc}
153
         */
154
        @Override
155
        public void setEnabled(boolean enabled) {
156
            updateMaxFieldEnablement();
157
        }
158

    
159
        public void setSuperEnabled(boolean enabled) {
160
            super.setEnabled(enabled);
161
        }
162

    
163
    }
164

    
165
}
(1-1/4)