Project

General

Profile

Download (3.81 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
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
17

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

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

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

    
32
    TextFieldNFix minField, textField;
33
    MaxTextField maxField;
34

    
35
    String unitOfMeasure;
36

    
37
    public MinMaxTextField(String caption, String unitOfMeasure){
38

    
39
        this.unitOfMeasure = unitOfMeasure;
40

    
41
        setCaption(caption);
42
        setPrimaryStyleName(PRIMARY_STYLE);
43

    
44
        initFields(unitOfMeasure);
45

    
46
    }
47

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

    
56
            private static final long serialVersionUID = -536012841624056585L;
57

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

    
67
        };
68

    
69
        minField.setWidth("100px");
70
        maxField.setWidth("100px");
71
        textField.setWidth("100%");
72

    
73
        addComponents(minField, maxField, textField);
74

    
75
        setExpandRatio(textField, 1);
76

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

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

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

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

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

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

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

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

    
131
    class MaxTextField extends TextFieldNFix {
132

    
133
        private static final long serialVersionUID = -536012841624056585L;
134

    
135

    
136

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

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

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

    
161
        public void setSuperEnabled(boolean enabled) {
162
            super.setEnabled(enabled);
163
        }
164

    
165
    }
166

    
167
}
(2-2/5)