OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 142 | Rev 156 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 142 Rev 149
Line 13... Line 13...
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.util.HashMap;
-
 
19
import java.util.Map;
-
 
20
 
18
import net.minidev.json.JSONObject;
21
import net.minidev.json.JSONObject;
19
 
22
 
20
public class LightUISlider extends LightUserControl {
23
public class LightUISlider extends LightUserControl {
21
    Integer maxValue = 1;
24
    private Integer maxValue = 1;
22
    Integer minValue = 0;
25
    private Integer minValue = 0;
23
    Integer increment = 1;
26
    private Integer increment = 1;
-
 
27
    private Map<Integer, String> mapLabels = new HashMap<>();
24
 
28
 
25
    public LightUISlider(final String id) {
29
    public LightUISlider(final String id) {
26
        super(id);
30
        super(id);
27
        this.setType(TYPE_SLIDER);
31
        this.setType(TYPE_SLIDER);
28
        this.setValueType(VALUE_TYPE_INTEGER);
32
        this.setValueType(VALUE_TYPE_INTEGER);
Line 38... Line 42...
38
        this.maxValue = slider.maxValue;
42
        this.maxValue = slider.maxValue;
39
        this.minValue = slider.minValue;
43
        this.minValue = slider.minValue;
40
        this.increment = slider.increment;
44
        this.increment = slider.increment;
41
    }
45
    }
42
 
46
 
-
 
47
    public void setLabel(int value, String label) {
-
 
48
        mapLabels.put(Integer.valueOf(value), label);
-
 
49
    }
-
 
50
 
-
 
51
    public String getLabelForValue(int value) {
-
 
52
        return mapLabels.get(Integer.valueOf(value));
-
 
53
    }
-
 
54
 
43
    public Integer getMaxValue() {
55
    public Integer getMaxValue() {
44
        return this.maxValue;
56
        return this.maxValue;
45
    }
57
    }
46
 
58
 
47
    public void setMaxValue(final int maxValue) {
59
    public void setMaxValue(final int maxValue) {
-
 
60
        if (maxValue < this.minValue) {
-
 
61
            throw new IllegalArgumentException("max cannot be < " + this.minValue);
-
 
62
        }
48
        this.maxValue = maxValue;
63
        this.maxValue = maxValue;
-
 
64
        if (this.getMinWidth() == null) {
-
 
65
            this.setMinWidth((this.maxValue - this.minValue) * 50);
-
 
66
        }
49
    }
67
    }
50
 
68
 
51
    public Integer getMinValue() {
69
    public Integer getMinValue() {
52
        return this.minValue;
70
        return this.minValue;
53
    }
71
    }
54
 
72
 
55
    public void setMinValue(final int minValue) {
73
    public void setMinValue(final int minValue) {
-
 
74
        if (minValue > this.maxValue) {
-
 
75
            throw new IllegalArgumentException("min cannot be > " + this.maxValue);
-
 
76
        }
56
        this.minValue = minValue;
77
        this.minValue = minValue;
-
 
78
        if (this.getMinWidth() == null) {
-
 
79
            this.setMinWidth((this.maxValue - this.minValue) * 50);
-
 
80
        }
57
    }
81
    }
58
 
82
 
59
    public Integer getIncrement() {
83
    public Integer getIncrement() {
60
        return this.increment;
84
        return this.increment;
61
    }
85
    }
62
 
86
 
63
    public void setIncrement(final int increment) {
87
    public void setIncrement(final int increment) {
64
        this.increment = increment;
88
        this.increment = increment;
65
    }
89
    }
66
 
90
 
-
 
91
    public void setSelectedValue(int value) {
-
 
92
        setValue(String.valueOf(value));
-
 
93
    }
-
 
94
 
-
 
95
    public int getSelectedValue() {
-
 
96
        return Integer.parseInt(getValue());
-
 
97
    }
-
 
98
 
67
    @Override
99
    @Override
68
    public JSONObject toJSON() {
100
    public JSONObject toJSON() {
69
        final JSONObject json = super.toJSON();
101
        final JSONObject json = super.toJSON();
70
        json.put("max-value", this.maxValue);
102
        json.put("max-value", this.maxValue);
71
        json.put("min-value", this.minValue);
103
        json.put("min-value", this.minValue);
72
        json.put("increment", this.increment);
104
        json.put("increment", this.increment);
-
 
105
        if (!this.mapLabels.isEmpty()) {
-
 
106
            final JSONObject labels = new JSONObject();
-
 
107
            for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
-
 
108
                final Integer key = entry.getKey();
-
 
109
                final String value = entry.getValue();
-
 
110
                labels.put(String.valueOf(key), value);
-
 
111
            }
-
 
112
            json.put("labels", labels);
73
 
113
        }
74
        return json;
114
        return json;
75
    }
115
    }
76
 
116
 
77
    @Override
117
    @Override
78
    public void fromJSON(JSONObject json) {
118
    public void fromJSON(JSONObject json) {
79
        super.fromJSON(json);
119
        super.fromJSON(json);
80
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
120
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
81
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
121
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
82
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
122
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
-
 
123
        this.mapLabels.clear();
-
 
124
        if (json.containsKey("labels")) {
-
 
125
            final JSONObject labels = (JSONObject) json.get("labels");
-
 
126
            for (String key : labels.keySet()) {
-
 
127
                final String v = labels.getAsString(key);
-
 
128
                mapLabels.put(Integer.parseInt(key), v);
-
 
129
            }
-
 
130
        }
83
    }
131
    }
84
 
132
 
85
    @Override
133
    @Override
86
    public void _setValueFromContext(Object value) {
134
    public void _setValueFromContext(Object value) {
87
        if (value instanceof Number) {
135
        if (value instanceof Number) {
Line 97... Line 145...
97
            return null;
145
            return null;
98
        } else {
146
        } else {
99
            return Integer.parseInt(this.getValue());
147
            return Integer.parseInt(this.getValue());
100
        }
148
        }
101
    }
149
    }
-
 
150
 
-
 
151
    @Override
-
 
152
    public JSONToLightUIConvertor getConvertor() {
-
 
153
        return new JSONToLightUIConvertor() {
-
 
154
            @Override
-
 
155
            public LightUIElement convert(JSONObject json) {
-
 
156
                return new LightUISlider(json);
-
 
157
            }
-
 
158
        };
-
 
159
    }
102
}
160
}