Project

General

Profile

Download (4.09 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.data.util.converter.Converter;
14
import com.vaadin.ui.HorizontalLayout;
15
import com.vaadin.ui.TextField;
16

    
17
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
18

    
19
/**
20
 * @author a.kohlbecker
21
 * @since Jun 22, 2017
22
 *
23
 */
24
public class MinMaxTextField extends HorizontalLayout {
25

    
26
    /**
27
     *
28
     */
29
    private static final String PRIMARY_STYLE = "v-min-max-text-field";
30

    
31
    private static final long serialVersionUID = -6690659776664579698L;
32

    
33
    TextFieldNFix minField, textField;
34
    MaxTextField maxField;
35

    
36
    Converter<String, ?> numberConverter;
37

    
38
    String unitOfMeasure;
39

    
40
    public MinMaxTextField(String caption, String unitOfMeasure, Converter<String, ?> numberConverter){
41

    
42
        this.unitOfMeasure = unitOfMeasure;
43
        this.numberConverter = numberConverter;
44

    
45
        setCaption(caption);
46
        setPrimaryStyleName(PRIMARY_STYLE);
47

    
48
        initFields(unitOfMeasure);
49

    
50
    }
51

    
52
    public MinMaxTextField(String caption, String unitOfMeasure){
53
        this(caption, unitOfMeasure, null);
54
    }
55

    
56
    /**
57
     * @param unitOfMeasure
58
     */
59
    protected void initFields(String unitOfMeasure) {
60
        textField = new TextFieldNFix("free text");
61
        maxField = new MaxTextField(String.format("max (%s)", unitOfMeasure));
62
        minField = new TextFieldNFix(String.format("min (%s)", unitOfMeasure)) {
63

    
64
            private static final long serialVersionUID = -536012841624056585L;
65

    
66
            /**
67
             * {@inheritDoc}
68
             */
69
            @Override
70
            protected void setInternalValue(String newValue) {
71
                super.setInternalValue(newValue);
72
                updateMaxFieldEnablement();
73
            }
74

    
75
        };
76

    
77
        minField.setWidth("100px");
78
        maxField.setWidth("100px");
79
        textField.setWidth("100%");
80

    
81
        addComponents(minField, maxField, textField);
82

    
83
        setExpandRatio(textField, 1);
84

    
85
        minField.addValueChangeListener(e -> updateMaxFieldEnablement());
86
        maxField.setEnabled(false);
87
    }
88

    
89
    public void updateMaxFieldEnablement(){
90
        if(maxField != null && minField != null){
91
            boolean enabled = !StringUtils.isEmpty(minField.getValue());
92
            maxField.setSuperEnabled(enabled);
93
        }
94
    }
95

    
96
    /**
97
     * @return the minField
98
     */
99
    public TextField getMinField() {
100
        return minField;
101
    }
102

    
103
    /**
104
     * @return the maxField
105
     */
106
    public TextField getMaxField() {
107
        return maxField;
108
    }
109

    
110
    /**
111
     * @return the textField
112
     */
113
    public TextField getTextField() {
114
        return textField;
115
    }
116

    
117
    /**
118
     * @return the unitOfMeasure
119
     */
120
    public String getUnitOfMeasure() {
121
        return unitOfMeasure;
122
    }
123

    
124
    public void addSubComponentsStyleName(String style) {
125
        minField.addStyleName(style);
126
        maxField.addStyleName(style);
127
        textField.addStyleName(style);
128
    }
129

    
130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public void setEnabled(boolean enabled) {
135
        // TODO Auto-generated method stub
136
        super.setEnabled(enabled);
137
    }
138

    
139
    class MaxTextField extends TextFieldNFix {
140

    
141
        private static final long serialVersionUID = -536012841624056585L;
142

    
143

    
144

    
145
        /**
146
         * @param caption
147
         */
148
        public MaxTextField(String caption) {
149
            super(caption);
150
        }
151

    
152
        /**
153
         * {@inheritDoc}
154
         */
155
        @Override
156
        protected void setInternalValue(String newValue) {
157
            super.setInternalValue(newValue);
158
            updateMaxFieldEnablement();
159
        }
160

    
161
        /**
162
         * {@inheritDoc}
163
         */
164
        @Override
165
        public void setEnabled(boolean enabled) {
166
            updateMaxFieldEnablement();
167
        }
168

    
169
        public void setSuperEnabled(boolean enabled) {
170
            super.setEnabled(enabled);
171
        }
172

    
173
    }
174

    
175
}
(2-2/5)