Dépôt officiel du code source de l'ERP OpenConcerto
Rev 151 | 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.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
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<>();
public LightUIRadioButtons() {
// Serialization
}
// 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 void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeInt(this.choices.size());
for (String c : this.choices) {
out.writeUTF(c);
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
int size = in.readInt();
this.choices.clear();
for (int i = 0; i < size; i++) {
this.choices.add(in.readUTF());
}
}
@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());
}
}
}