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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 156
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui.light;
14
 package org.openconcerto.ui.light;
15
 
15
 
16
import org.openconcerto.utils.io.JSONConverter;
16
import org.openconcerto.utils.io.JSONConverter;
17
 
17
 
-
 
18
import java.io.Externalizable;
-
 
19
import java.io.IOException;
-
 
20
import java.io.ObjectInput;
-
 
21
import java.io.ObjectOutput;
18
import java.util.HashMap;
22
import java.util.HashMap;
19
import java.util.Map;
23
import java.util.Map;
20
 
24
 
21
import net.minidev.json.JSONObject;
25
import net.minidev.json.JSONObject;
22
 
26
 
23
public class LightUISlider extends LightUserControl {
27
public class LightUISlider extends LightUserControl implements Externalizable {
-
 
28
 
24
    private Integer maxValue = 1;
29
    private int minValue = 0;
25
    private Integer minValue = 0;
30
    private int maxValue = 1;
26
    private Integer increment = 1;
31
    private int increment = 1;
27
    private Map<Integer, String> mapLabels = new HashMap<>();
32
    private Map<Integer, String> mapLabels = new HashMap<>();
28
 
33
 
-
 
34
    public LightUISlider() {
-
 
35
        // Serialization
-
 
36
    }
-
 
37
 
29
    public LightUISlider(final String id) {
38
    public LightUISlider(final String id) {
30
        super(id);
39
        super(id);
31
        this.setType(TYPE_SLIDER);
40
        this.setType(TYPE_SLIDER);
32
        this.setValueType(VALUE_TYPE_INTEGER);
41
        this.setValueType(VALUE_TYPE_INTEGER);
33
        this.setValue("0");
42
        this.setValue("0");
34
    }
43
    }
35
 
44
 
36
    public LightUISlider(final JSONObject json) {
45
    public LightUISlider(final JSONObject json) {
37
        super(json);
46
        super(json);
38
    }
47
    }
39
 
48
 
40
    public LightUISlider(final LightUISlider slider) {
49
    public LightUISlider(final LightUISlider slider) {
41
        super(slider);
50
        super(slider);
42
        this.maxValue = slider.maxValue;
51
        this.maxValue = slider.maxValue;
43
        this.minValue = slider.minValue;
52
        this.minValue = slider.minValue;
44
        this.increment = slider.increment;
53
        this.increment = slider.increment;
45
    }
54
    }
46
 
55
 
47
    public void setLabel(int value, String label) {
56
    public void setLabel(int value, String label) {
48
        mapLabels.put(Integer.valueOf(value), label);
57
        mapLabels.put(Integer.valueOf(value), label);
49
    }
58
    }
50
 
59
 
51
    public String getLabelForValue(int value) {
60
    public String getLabelForValue(int value) {
52
        return mapLabels.get(Integer.valueOf(value));
61
        return mapLabels.get(Integer.valueOf(value));
53
    }
62
    }
54
 
63
 
55
    public Integer getMaxValue() {
64
    public Integer getMaxValue() {
56
        return this.maxValue;
65
        return this.maxValue;
57
    }
66
    }
58
 
67
 
59
    public void setMaxValue(final int maxValue) {
68
    public void setMaxValue(final int maxValue) {
60
        if (maxValue < this.minValue) {
69
        if (maxValue < this.minValue) {
61
            throw new IllegalArgumentException("max cannot be < " + this.minValue);
70
            throw new IllegalArgumentException("max cannot be < " + this.minValue);
62
        }
71
        }
63
        this.maxValue = maxValue;
72
        this.maxValue = maxValue;
64
        if (this.getMinWidth() == null) {
73
        if (this.getMinWidth() == null) {
65
            this.setMinWidth((this.maxValue - this.minValue) * 50);
74
            this.setMinWidth((this.maxValue - this.minValue) * 50);
66
        }
75
        }
67
    }
76
    }
68
 
77
 
69
    public Integer getMinValue() {
78
    public Integer getMinValue() {
70
        return this.minValue;
79
        return this.minValue;
71
    }
80
    }
72
 
81
 
73
    public void setMinValue(final int minValue) {
82
    public void setMinValue(final int minValue) {
74
        if (minValue > this.maxValue) {
83
        if (minValue > this.maxValue) {
75
            throw new IllegalArgumentException("min cannot be > " + this.maxValue);
84
            throw new IllegalArgumentException("min cannot be > " + this.maxValue);
76
        }
85
        }
77
        this.minValue = minValue;
86
        this.minValue = minValue;
78
        if (this.getMinWidth() == null) {
87
        if (this.getMinWidth() == null) {
79
            this.setMinWidth((this.maxValue - this.minValue) * 50);
88
            this.setMinWidth((this.maxValue - this.minValue) * 50);
80
        }
89
        }
81
    }
90
    }
82
 
91
 
83
    public Integer getIncrement() {
92
    public Integer getIncrement() {
84
        return this.increment;
93
        return this.increment;
85
    }
94
    }
86
 
95
 
87
    public void setIncrement(final int increment) {
96
    public void setIncrement(final int increment) {
88
        this.increment = increment;
97
        this.increment = increment;
89
    }
98
    }
90
 
99
 
91
    public void setSelectedValue(int value) {
100
    public void setSelectedValue(int value) {
92
        setValue(String.valueOf(value));
101
        setValue(String.valueOf(value));
93
    }
102
    }
94
 
103
 
95
    public int getSelectedValue() {
104
    public int getSelectedValue() {
96
        return Integer.parseInt(getValue());
105
        return Integer.parseInt(getValue());
97
    }
106
    }
98
 
107
 
99
    @Override
108
    @Override
100
    public JSONObject toJSON() {
109
    public JSONObject toJSON() {
101
        final JSONObject json = super.toJSON();
110
        final JSONObject json = super.toJSON();
102
        json.put("max-value", this.maxValue);
111
        json.put("max-value", this.maxValue);
103
        json.put("min-value", this.minValue);
112
        json.put("min-value", this.minValue);
104
        json.put("increment", this.increment);
113
        json.put("increment", this.increment);
105
        if (!this.mapLabels.isEmpty()) {
114
        if (!this.mapLabels.isEmpty()) {
106
            final JSONObject labels = new JSONObject();
115
            final JSONObject labels = new JSONObject();
107
            for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
116
            for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
108
                final Integer key = entry.getKey();
117
                final Integer key = entry.getKey();
109
                final String value = entry.getValue();
118
                final String value = entry.getValue();
110
                labels.put(String.valueOf(key), value);
119
                labels.put(String.valueOf(key), value);
111
            }
120
            }
112
            json.put("labels", labels);
121
            json.put("labels", labels);
113
        }
122
        }
114
        return json;
123
        return json;
115
    }
124
    }
116
 
125
 
117
    @Override
126
    @Override
118
    public void fromJSON(JSONObject json) {
127
    public void fromJSON(JSONObject json) {
119
        super.fromJSON(json);
128
        super.fromJSON(json);
120
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
129
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
121
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
130
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
122
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
131
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
123
        this.mapLabels.clear();
132
        this.mapLabels.clear();
124
        if (json.containsKey("labels")) {
133
        if (json.containsKey("labels")) {
125
            final JSONObject labels = (JSONObject) json.get("labels");
134
            final JSONObject labels = (JSONObject) json.get("labels");
126
            for (String key : labels.keySet()) {
135
            for (String key : labels.keySet()) {
127
                final String v = labels.getAsString(key);
136
                final String v = labels.getAsString(key);
128
                mapLabels.put(Integer.parseInt(key), v);
137
                mapLabels.put(Integer.parseInt(key), v);
129
            }
138
            }
130
        }
139
        }
131
    }
140
    }
132
 
141
 
133
    @Override
142
    @Override
134
    public void _setValueFromContext(Object value) {
143
    public void _setValueFromContext(Object value) {
135
        if (value instanceof Number) {
144
        if (value instanceof Number) {
136
            this.setValue(value.toString());
145
            this.setValue(value.toString());
137
        } else {
146
        } else {
138
            throw new IllegalArgumentException("Incorrect value " + value + "type for ui element: " + this.getId());
147
            throw new IllegalArgumentException("Incorrect value " + value + "type for ui element: " + this.getId());
139
        }
148
        }
140
    }
149
    }
141
 
150
 
142
    @Override
151
    @Override
143
    public Object getValueForContext() {
152
    public Object getValueForContext() {
144
        if (this.getValue() == null) {
153
        if (this.getValue() == null) {
145
            return null;
154
            return null;
146
        } else {
155
        } else {
147
            return Integer.parseInt(this.getValue());
156
            return Integer.parseInt(this.getValue());
148
        }
157
        }
149
    }
158
    }
150
 
159
 
151
    @Override
160
    @Override
-
 
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()) {
152
    public JSONToLightUIConvertor getConvertor() {
169
            out.writeInt(entry.getKey());
153
        return new JSONToLightUIConvertor() {
170
            out.writeUTF(entry.getValue());
-
 
171
        }
-
 
172
    }
-
 
173
 
154
            @Override
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();
155
            public LightUIElement convert(JSONObject json) {
179
        this.increment = in.readInt();
-
 
180
        this.mapLabels.clear();
-
 
181
        int size = in.readInt();
-
 
182
        for (int i = 0; i < size; i++) {
156
                return new LightUISlider(json);
183
            this.mapLabels.put(in.readInt(), in.readUTF());
157
            }
184
        }
158
        };
-
 
159
    }
185
    }
160
}
186
}