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 java.util.List;
142 ilm 17
import java.util.Map.Entry;
132 ilm 18
 
19
public abstract class StringValueConvertor extends ComboValueConvertor<String> {
20
 
21
    protected List<String> listId;
22
 
23
    public StringValueConvertor() {
24
        super();
25
    }
26
 
27
    public StringValueConvertor(final boolean hasNotSpecifed) {
28
        super(hasNotSpecifed);
29
    }
30
 
31
    /**
32
     * @param index: index of listId.
33
     *
142 ilm 34
     * @return id store at index in listId or null if index is under 1.
35
     *
36
     * @throws IllegalArgumentException when index is out out bounds or if list of ids is not initialized
132 ilm 37
     */
142 ilm 38
    public String getIdFromIndex(final Integer index) throws IllegalArgumentException {
132 ilm 39
        int realIndex = index;
40
        // values start at 1.
41
        realIndex = index - 1;
42
        if (this.listId == null) {
43
            throw new IllegalArgumentException("listId is not initialized");
44
        }
45
        if (realIndex >= this.listId.size()) {
46
            throw new IllegalArgumentException("index is out of bounds");
47
        }
142 ilm 48
 
49
        if(realIndex == -1){
50
            return null;
51
        } else {
52
            return this.listId.get(realIndex);
53
        }
132 ilm 54
    }
55
 
56
    /**
57
     * @param id: id store in listId.
58
     *
142 ilm 59
     * @return index of id in listId or null if not found.
132 ilm 60
     */
61
    public Integer getIndexFromId(final String id) {
62
        if (this.listId == null) {
63
            throw new IllegalArgumentException("listId is not initialized");
64
        }
65
 
66
        final int listIdSize = this.listId.size();
67
        for (int i = 0; i < listIdSize; i++) {
68
            if (this.listId.get(i).equals(id)) {
142 ilm 69
                // values start at 1.
132 ilm 70
                return i + 1;
71
            }
72
        }
73
        return null;
74
    }
142 ilm 75
 
132 ilm 76
    @Override
142 ilm 77
    public void fillComboWithValue(final LightUIComboBox combo, final String selectedValue) {
132 ilm 78
        int i = 1;
142 ilm 79
        for (final Entry<String, String> entry : this.values.entrySet()) {
132 ilm 80
            final LightUIComboBoxElement comboElement = new LightUIComboBoxElement(i);
142 ilm 81
            comboElement.setValue1(entry.getValue());
132 ilm 82
            combo.addValue(comboElement);
142 ilm 83
 
84
            if (entry.getKey().equals(selectedValue)) {
85
                combo.setSelectedValue(comboElement);
86
            }
132 ilm 87
            i++;
88
        }
89
    }
90
}