/* This file is part of OpenMyEWB. OpenMyEWB is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenMyEWB is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenMyEWB. If not, see <http://www.gnu.org/licenses/>. OpenMyEWB is Copyright 2005-2009 Nicolas Kruchten (nicolas@kruchten.com), Francis Kung, Engineers Without Borders Canada, Michael Trauttmansdorff, Jon Fishbein, David Kadish */ package ca.myewb.frame.forms.element; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.log4j.Logger; public class Radio extends Element { private List<List> options; private int numAcross = 1; public Radio(String name, String label, String value, boolean required) { super(name, label, value, required); this.options = new LinkedList<List>(); type = "radio"; } public void addOption(String name, String option) { List<String> l = new LinkedList<String>(); l.add(name); l.add(option); options.add(l); } public boolean validate() { // Make sure chosen option was either one of the allowed ones or nothing if (value.equals("") || (value == null)) { return super.validate(); } Iterator i = options.iterator(); boolean optionFound = false; while (i.hasNext() && !optionFound) { LinkedList option = (LinkedList)i.next(); if (option.get(0).equals(value)) { optionFound = true; } } if (!optionFound) { error = "Invalid option selected"; Logger.getLogger(this.getClass()) .warn("invalid radio button detected", new Throwable()); } return (super.validate() && optionFound); } public List getOptions() { return options; } public int getNumOptions() { return options.size(); } public int getNumAcross() { return numAcross; } public void setNumAcross(int numAcross) { this.numAcross = numAcross; } }