OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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