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
142 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 org.openconcerto.utils.io.JSONConverter;
17
 
156 ilm 18
import java.io.Externalizable;
19
import java.io.IOException;
20
import java.io.ObjectInput;
21
import java.io.ObjectOutput;
149 ilm 22
import java.util.HashMap;
23
import java.util.Map;
24
 
142 ilm 25
import net.minidev.json.JSONObject;
26
 
156 ilm 27
public class LightUISlider extends LightUserControl implements Externalizable {
28
 
29
    private int minValue = 0;
30
    private int maxValue = 1;
31
    private int increment = 1;
149 ilm 32
    private Map<Integer, String> mapLabels = new HashMap<>();
142 ilm 33
 
156 ilm 34
    public LightUISlider() {
35
        // Serialization
36
    }
37
 
142 ilm 38
    public LightUISlider(final String id) {
39
        super(id);
40
        this.setType(TYPE_SLIDER);
41
        this.setValueType(VALUE_TYPE_INTEGER);
42
        this.setValue("0");
43
    }
44
 
45
    public LightUISlider(final JSONObject json) {
46
        super(json);
47
    }
48
 
49
    public LightUISlider(final LightUISlider slider) {
50
        super(slider);
51
        this.maxValue = slider.maxValue;
52
        this.minValue = slider.minValue;
53
        this.increment = slider.increment;
54
    }
55
 
149 ilm 56
    public void setLabel(int value, String label) {
57
        mapLabels.put(Integer.valueOf(value), label);
58
    }
59
 
60
    public String getLabelForValue(int value) {
61
        return mapLabels.get(Integer.valueOf(value));
62
    }
63
 
142 ilm 64
    public Integer getMaxValue() {
65
        return this.maxValue;
66
    }
67
 
68
    public void setMaxValue(final int maxValue) {
149 ilm 69
        if (maxValue < this.minValue) {
70
            throw new IllegalArgumentException("max cannot be < " + this.minValue);
71
        }
142 ilm 72
        this.maxValue = maxValue;
149 ilm 73
        if (this.getMinWidth() == null) {
74
            this.setMinWidth((this.maxValue - this.minValue) * 50);
75
        }
142 ilm 76
    }
77
 
78
    public Integer getMinValue() {
79
        return this.minValue;
80
    }
81
 
82
    public void setMinValue(final int minValue) {
149 ilm 83
        if (minValue > this.maxValue) {
84
            throw new IllegalArgumentException("min cannot be > " + this.maxValue);
85
        }
142 ilm 86
        this.minValue = minValue;
149 ilm 87
        if (this.getMinWidth() == null) {
88
            this.setMinWidth((this.maxValue - this.minValue) * 50);
89
        }
142 ilm 90
    }
91
 
92
    public Integer getIncrement() {
93
        return this.increment;
94
    }
95
 
96
    public void setIncrement(final int increment) {
97
        this.increment = increment;
98
    }
99
 
149 ilm 100
    public void setSelectedValue(int value) {
101
        setValue(String.valueOf(value));
102
    }
103
 
104
    public int getSelectedValue() {
105
        return Integer.parseInt(getValue());
106
    }
107
 
142 ilm 108
    @Override
109
    public JSONObject toJSON() {
110
        final JSONObject json = super.toJSON();
111
        json.put("max-value", this.maxValue);
112
        json.put("min-value", this.minValue);
113
        json.put("increment", this.increment);
149 ilm 114
        if (!this.mapLabels.isEmpty()) {
115
            final JSONObject labels = new JSONObject();
116
            for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
117
                final Integer key = entry.getKey();
118
                final String value = entry.getValue();
119
                labels.put(String.valueOf(key), value);
120
            }
121
            json.put("labels", labels);
122
        }
142 ilm 123
        return json;
124
    }
125
 
126
    @Override
127
    public void fromJSON(JSONObject json) {
128
        super.fromJSON(json);
129
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
130
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
131
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
149 ilm 132
        this.mapLabels.clear();
133
        if (json.containsKey("labels")) {
134
            final JSONObject labels = (JSONObject) json.get("labels");
135
            for (String key : labels.keySet()) {
136
                final String v = labels.getAsString(key);
137
                mapLabels.put(Integer.parseInt(key), v);
138
            }
139
        }
142 ilm 140
    }
141
 
142
    @Override
143
    public void _setValueFromContext(Object value) {
144
        if (value instanceof Number) {
145
            this.setValue(value.toString());
146
        } else {
147
            throw new IllegalArgumentException("Incorrect value " + value + "type for ui element: " + this.getId());
148
        }
149
    }
150
 
151
    @Override
152
    public Object getValueForContext() {
153
        if (this.getValue() == null) {
154
            return null;
155
        } else {
156
            return Integer.parseInt(this.getValue());
157
        }
158
    }
149 ilm 159
 
160
    @Override
156 ilm 161
    public void writeExternal(ObjectOutput out) throws IOException {
162
        super.writeExternal(out);
163
        out.writeInt(this.minValue);
164
        out.writeInt(this.maxValue);
165
        out.writeInt(this.increment);
166
        // map
167
        out.writeInt(this.mapLabels.size());
168
        for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
169
            out.writeInt(entry.getKey());
170
            out.writeUTF(entry.getValue());
171
        }
149 ilm 172
    }
156 ilm 173
 
174
    @Override
175
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
176
        super.readExternal(in);
177
        this.minValue = in.readInt();
178
        this.maxValue = in.readInt();
179
        this.increment = in.readInt();
180
        this.mapLabels.clear();
181
        int size = in.readInt();
182
        for (int i = 0; i < size; i++) {
183
            this.mapLabels.put(in.readInt(), in.readUTF());
184
        }
185
    }
142 ilm 186
}