OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.ArrayList;
17
import java.util.List;
18
 
19
import net.minidev.json.JSONArray;
20
import net.minidev.json.JSONObject;
21
 
22
public class LightUIRadioButtons extends LightUserControl {
23
    private final List<String> choices = new ArrayList<>();
24
 
25
    // Init from json constructor
26
    public LightUIRadioButtons(final JSONObject json) {
27
        super(json);
28
    }
29
 
30
    // Clone constructor
31
    public LightUIRadioButtons(final LightUIRadioButtons labelElement) {
32
        super(labelElement);
33
    }
34
 
35
    public LightUIRadioButtons(final String id, final String label, final List<String> choices) {
36
        this(id, label, choices, -1);
37
    }
38
 
39
    /**
40
     *
41
     * @param selectedIndex index of the selected value (-1 if nothing is selected)
42
     */
43
    public LightUIRadioButtons(final String id, final String label, final List<String> choices, int selectedIndex) {
44
        super(id);
45
        this.setType(LightUIElement.TYPE_RADIO_BUTTONS);
46
        this.setLabel(label);
47
        if (selectedIndex >= choices.size()) {
48
            throw new IllegalArgumentException("invalid index " + selectedIndex + ", it must be between 0 and " + choices.size());
49
        }
50
        if (selectedIndex < 0) {
51
            this.setValue(null);
52
        } else {
53
            this.setValue(String.valueOf(selectedIndex));
54
        }
55
        this.setChoices(choices);
56
    }
57
 
58
    public void setChoices(List<String> choices) {
59
        this.choices.clear();
60
        this.choices.addAll(choices);
61
    }
62
 
151 ilm 63
    public List<String> getChoices() {
64
        return choices;
65
    }
66
 
149 ilm 67
    @Override
68
    public JSONObject toJSON() {
69
        final JSONObject json = super.toJSON();
70
        final JSONArray array = new JSONArray();
71
        for (String choice : choices) {
72
            array.add(choice);
73
        }
74
        json.put("choices", array);
75
        return json;
76
    }
77
 
78
    @Override
79
    public void fromJSON(final JSONObject json) {
80
        super.fromJSON(json);
81
        final JSONArray array = (JSONArray) json.get("choices");
82
        this.choices.clear();
83
        for (Object o : array) {
84
            this.choices.add(o.toString());
85
        }
86
    }
87
 
88
    @Override
89
    public JSONToLightUIConvertor getConvertor() {
90
        return new JSONToLightUIConvertor() {
91
            @Override
92
            public LightUIElement convert(final JSONObject json) {
93
                return new LightUIRadioButtons(json);
94
            }
95
        };
96
    }
97
 
98
    @Override
99
    public void _setValueFromContext(Object value) {
100
        if (value instanceof Number) {
101
            this.setValue(value.toString());
102
        } else {
103
            throw new IllegalArgumentException("Incorrect value " + value + "type for ui element: " + this.getId());
104
        }
105
    }
106
 
107
    @Override
108
    public Object getValueForContext() {
109
        if (this.getValue() == null) {
110
            return null;
111
        } else {
112
            return Integer.parseInt(this.getValue());
113
        }
114
    }
115
 
116
}