Dépôt officiel du code source de l'ERP OpenConcerto
Rev 149 | Rev 156 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 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 java.util.ArrayList;
import java.util.List;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
public class LightUIRadioButtons extends LightUserControl {
private final List<String> choices = new ArrayList<>();
// Init from json constructor
public LightUIRadioButtons(final JSONObject json) {
super(json);
}
// Clone constructor
public LightUIRadioButtons(final LightUIRadioButtons labelElement) {
super(labelElement);
}
public LightUIRadioButtons(final String id, final String label, final List<String> choices) {
this(id, label, choices, -1);
}
/**
*
* @param selectedIndex index of the selected value (-1 if nothing is selected)
*/
public LightUIRadioButtons(final String id, final String label, final List<String> choices, int selectedIndex) {
super(id);
this.setType(LightUIElement.TYPE_RADIO_BUTTONS);
this.setLabel(label);
if (selectedIndex >= choices.size()) {
throw new IllegalArgumentException("invalid index " + selectedIndex + ", it must be between 0 and " + choices.size());
}
if (selectedIndex < 0) {
this.setValue(null);
} else {
this.setValue(String.valueOf(selectedIndex));
}
this.setChoices(choices);
}
public void setChoices(List<String> choices) {
this.choices.clear();
this.choices.addAll(choices);
}
public List<String> getChoices() {
return choices;
}
@Override
public JSONObject toJSON() {
final JSONObject json = super.toJSON();
final JSONArray array = new JSONArray();
for (String choice : choices) {
array.add(choice);
}
json.put("choices", array);
return json;
}
@Override
public void fromJSON(final JSONObject json) {
super.fromJSON(json);
final JSONArray array = (JSONArray) json.get("choices");
this.choices.clear();
for (Object o : array) {
this.choices.add(o.toString());
}
}
@Override
public JSONToLightUIConvertor getConvertor() {
return new JSONToLightUIConvertor() {
@Override
public LightUIElement convert(final JSONObject json) {
return new LightUIRadioButtons(json);
}
};
}
@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());
}
}
}