OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
132 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.ui.light;
15
 
16
import org.openconcerto.utils.convertor.ValueConvertor;
17
 
18
import java.util.LinkedHashMap;
19
import java.util.Map;
20
import java.util.Map.Entry;
21
 
22
public abstract class ComboValueConvertor<K> implements ValueConvertor<K, String> {
23
 
24
    private boolean hasNotSpecifedLine = false;
25
 
26
    protected Map<K, String> values = new LinkedHashMap<K, String>();
27
 
28
    public ComboValueConvertor() {
29
        this.init();
30
    }
31
 
32
    public ComboValueConvertor(final boolean hasNotSpecifedLine) {
33
        this.hasNotSpecifedLine = hasNotSpecifedLine;
34
        this.init();
35
    }
36
 
37
    protected abstract void init();
38
 
142 ilm 39
    public final boolean hasNotSpecifiedLine() {
132 ilm 40
        return this.hasNotSpecifedLine;
41
    }
42
 
142 ilm 43
    public final void fillCombo(final LightUIComboBox combo, final K selectedValue) {
132 ilm 44
        if (this.hasNotSpecifedLine) {
45
            combo.addValue(LightUIComboBox.getDefaultValue());
46
        }
142 ilm 47
        this.fillComboWithValue(combo, selectedValue);
48
        if (selectedValue == null) {
49
            combo.setSelectedValue(null);
50
        }
51
 
52
        if(!combo.hasSelectedValue()){
53
            if(combo.hasNotSpecifedLine()){
54
                combo.setSelectedValue(LightUIComboBox.getDefaultValue());
55
            } else if(!combo.getValues().isEmpty()) {
56
                combo.setSelectedValue(combo.getValues().get(0));
57
            }
58
        }
59
 
60
        combo.setAlreadyFilled(true);
132 ilm 61
    }
62
 
142 ilm 63
    protected abstract void fillComboWithValue(final LightUIComboBox combo, final K selectedValue);
132 ilm 64
 
65
    @Override
66
    public String convert(final K key) {
67
        if (key == null) {
68
            return null;
69
        } else {
70
            return this.values.get(key);
71
        }
72
    }
73
 
74
    @Override
75
    public K unconvert(final String value) {
76
        if (value == null) {
77
            return null;
78
        } else {
79
            for (final Entry<K, String> entry : this.values.entrySet()) {
80
                if (entry.getValue().equals(value)) {
81
                    return entry.getKey();
82
                }
83
            }
84
            return null;
85
        }
86
    }
87
}