OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.ui.light;

import org.openconcerto.utils.io.JSONConverter;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.HashMap;
import java.util.Map;

import net.minidev.json.JSONObject;

public class LightUISlider extends LightUserControl implements Externalizable {

    private int minValue = 0;
    private int maxValue = 1;
    private int increment = 1;
    private Map<Integer, String> mapLabels = new HashMap<>();

    public LightUISlider() {
        // Serialization
    }

    public LightUISlider(final String id) {
        super(id);
        this.setType(TYPE_SLIDER);
        this.setValueType(VALUE_TYPE_INTEGER);
        this.setValue("0");
    }

    public LightUISlider(final JSONObject json) {
        super(json);
    }

    public LightUISlider(final LightUISlider slider) {
        super(slider);
        this.maxValue = slider.maxValue;
        this.minValue = slider.minValue;
        this.increment = slider.increment;
    }

    public void setLabel(int value, String label) {
        mapLabels.put(Integer.valueOf(value), label);
    }

    public String getLabelForValue(int value) {
        return mapLabels.get(Integer.valueOf(value));
    }

    public Integer getMaxValue() {
        return this.maxValue;
    }

    public void setMaxValue(final int maxValue) {
        if (maxValue < this.minValue) {
            throw new IllegalArgumentException("max cannot be < " + this.minValue);
        }
        this.maxValue = maxValue;
        if (this.getMinWidth() == null) {
            this.setMinWidth((this.maxValue - this.minValue) * 50);
        }
    }

    public Integer getMinValue() {
        return this.minValue;
    }

    public void setMinValue(final int minValue) {
        if (minValue > this.maxValue) {
            throw new IllegalArgumentException("min cannot be > " + this.maxValue);
        }
        this.minValue = minValue;
        if (this.getMinWidth() == null) {
            this.setMinWidth((this.maxValue - this.minValue) * 50);
        }
    }

    public Integer getIncrement() {
        return this.increment;
    }

    public void setIncrement(final int increment) {
        this.increment = increment;
    }

    public void setSelectedValue(int value) {
        setValue(String.valueOf(value));
    }

    public int getSelectedValue() {
        return Integer.parseInt(getValue());
    }

    @Override
    public JSONObject toJSON() {
        final JSONObject json = super.toJSON();
        json.put("max-value", this.maxValue);
        json.put("min-value", this.minValue);
        json.put("increment", this.increment);
        if (!this.mapLabels.isEmpty()) {
            final JSONObject labels = new JSONObject();
            for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
                final Integer key = entry.getKey();
                final String value = entry.getValue();
                labels.put(String.valueOf(key), value);
            }
            json.put("labels", labels);
        }
        return json;
    }

    @Override
    public void fromJSON(JSONObject json) {
        super.fromJSON(json);
        this.maxValue = JSONConverter.getParameterFromJSON(json, "max-value", Integer.class, 1);
        this.minValue = JSONConverter.getParameterFromJSON(json, "min-value", Integer.class, 0);
        this.increment = JSONConverter.getParameterFromJSON(json, "increment", Integer.class, 1);
        this.mapLabels.clear();
        if (json.containsKey("labels")) {
            final JSONObject labels = (JSONObject) json.get("labels");
            for (String key : labels.keySet()) {
                final String v = labels.getAsString(key);
                mapLabels.put(Integer.parseInt(key), v);
            }
        }
    }

    @Override
    public void _setValueFromContext(Object value) {
        if (value instanceof Number) {
            this.setValue(value.toString());
        } else {
            throw new IllegalArgumentException("Incorrect value " + value + "type for ui element: " + this.getId());
        }
    }

    @Override
    public Object getValueForContext() {
        if (this.getValue() == null) {
            return null;
        } else {
            return Integer.parseInt(this.getValue());
        }
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        super.writeExternal(out);
        out.writeInt(this.minValue);
        out.writeInt(this.maxValue);
        out.writeInt(this.increment);
        // map
        out.writeInt(this.mapLabels.size());
        for (Map.Entry<Integer, String> entry : mapLabels.entrySet()) {
            out.writeInt(entry.getKey());
            out.writeUTF(entry.getValue());
        }
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal(in);
        this.minValue = in.readInt();
        this.maxValue = in.readInt();
        this.increment = in.readInt();
        this.mapLabels.clear();
        int size = in.readInt();
        for (int i = 0; i < size; i++) {
            this.mapLabels.put(in.readInt(), in.readUTF());
        }
    }
}