/*
* 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 de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.taglib.html.BasicTag;
import de.odysseus.calyxo.forms.view.GroupModel;
/**
* HTML option tag
*
* @author Oliver Stuhr
*/
public class OptionTag extends BasicTag {
protected SelectTag selectTag;
private String disabled;
private String selected;
private String value;
/**
* Default constructor
*/
public OptionTag() {
super("option", true);
}
/**
* Constructor
*/
protected OptionTag(boolean body) {
super("option", body);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#init()
*/
protected void init() throws Exception {
super.init();
selectTag = (SelectTag)findAncestor(SelectTag.class);
if (selectTag == null) {
throw new ConfigException("An option tag must be nested in a select tag!");
}
checkValue();
}
protected void checkValue() throws Exception {
String key = getValueAttribute();
if (!selectTag.isSelectable(key)) {
throw new ConfigException("An option for select '" + selectTag.getName() + "' contains invalid value '" + key + "'!");
}
}
/*
* (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, "disabled", getDisabledAttribute());
append(buffer, "selected", getSelectedAttribute());
append(buffer, "value", getValueAttribute());
}
/**
* Get disabled attribute
*/
protected String getDisabledAttribute() {
return disabled == null ? null : "disabled";
}
/**
* Get selected attribute
*/
protected String getSelectedAttribute() throws Exception {
boolean select = false;
GroupModel model = selectTag.getGroupModel();
if (model.isSelected(getValueAttribute())) {
select = true;
} else if (!model.isSelectionAvailable() && selected != null) {
select = true;
}
return select ? "selected" : null;
}
/**
* Get value attribute
*/
protected String getValueAttribute() {
return value;
}
/**
* Reset tag properties
*/
protected void reset() {
super.reset();
disabled = null;
selected = null;
value = null;
selectTag = null;
}
/**
* Get disabled property
*/
public String getDisabled() {
return disabled;
}
/**
* Set disabled property
*
* @param string
*/
public void setDisabled(String string) {
disabled = string;
}
/**
* Get selected property
*/
public String getSelected() {
return selected;
}
/**
* Set selected property
*
* @param string
*/
public void setSelected(String string) {
selected = string;
}
/**
* Get value property
*/
public String getValue() {
return value;
}
/**
* Set value property
*
* @param string
*/
public void setValue(String string) {
value = string;
}
}