/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.taglib.html;
import javax.servlet.jsp.JspException;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.forms.view.GroupModel;
/**
* @author Oliver Stuhr
*/
public class RadioItemTag extends BasicInputTag {
protected GroupTag groupTag;
protected String type;
private String value;
private String checked;
/**
* Default constructor
*/
public RadioItemTag() {
this("radio");
}
/**
* Constructor
*/
protected RadioItemTag(String type) {
super("input", false);
this.type = type;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#init()
*/
protected void init() throws Exception {
super.init();
groupTag = (GroupTag)findAncestor(GroupTag.class);
if (groupTag == null) {
throw new JspException("A " + getTypeAttribute() + "item tag must be nested in a group tag!");
}
checkValue();
checkMultiple();
}
protected void checkValue() throws Exception {
String key = getValueAttribute();
if (!groupTag.isSelectable(key)) {
throw new ConfigException("A " + getTypeAttribute() + " item for group '" + groupTag.getName() + "' contains invalid value '" + key + "'!");
}
}
protected void checkMultiple() throws Exception {
if (groupTag.getGroupModel().isMultiple()) {
throw new ConfigException("A group of radio buttons cannot be multiple (input must not be an array or an index has to be set)!");
}
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#appendAttributes(java.lang.StringBuffer)
*/
protected void appendAttributes(StringBuffer buffer) throws Exception {
super.appendAttributes(buffer);
append(buffer, "name", getNameAttribute());
append(buffer, "type", getTypeAttribute());
append(buffer, "value", getValueAttribute());
append(buffer, "checked", getCheckedAttribute());
}
/**
* Get name attribute
*/
protected String getNameAttribute() throws Exception {
return groupTag.getNameAttribute();
}
/**
* Get type attribute
*/
protected String getTypeAttribute() {
return type;
}
/**
* Get value attribute
*/
protected String getValueAttribute() throws Exception {
return getValue() == null ? "" : getValue();
}
/**
* Get checked attribute
*/
protected String getCheckedAttribute() throws Exception {
GroupModel model = groupTag.getGroupModel();
boolean check = false;
if (model.isSelected(getValueAttribute())) {
check = true;
} else if (!model.isSelectionAvailable() && checked != null) {
check = true;
}
return check ? "checked" : null;
}
/**
* Set value property
*/
public void setValue(String string) {
value = string;
}
/**
* Set checked property
*
* @param string
*/
public final void setChecked(String string) {
checked = string;
}
/**
* Get value property
*/
public String getValue() {
return value;
}
/**
* Get checked property
*/
public final String getChecked() {
return checked;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#reset()
*/
protected void reset() {
super.reset();
value = null;
checked = null;
groupTag = null;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.BasicTag#getClassAttribute()
*/
protected String getClassAttribute() throws Exception {
String oldClass = super.getClassAttribute();
String newClass = groupTag.getClassAttribute();
if (newClass == null)
return oldClass;
if (oldClass == null)
return newClass;
StringBuffer buffer = new StringBuffer(newClass);
if (!newClass.endsWith(" "))
buffer.append(" ");
buffer.append(oldClass);
return buffer.toString();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.BasicTag#getStyleAttribute()
*/
protected String getStyleAttribute() throws Exception {
String oldStyle = super.getStyleAttribute();
String newStyle = groupTag.getStyleAttribute();
if (newStyle == null)
return oldStyle;
if (oldStyle == null)
return newStyle;
StringBuffer buffer = new StringBuffer(newStyle);
if (!newStyle.trim().endsWith(";"))
buffer.append("; ");
buffer.append(oldStyle);
return buffer.toString();
}
}