/*
* 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 java.util.Locale;
import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.forms.view.ListModel;
/**
* HTML list option tag.
* The listoption tag requires an enclosing select tag with a list model.
* The <code>value</code> property contains a key from the list model, which
* is taken for the HTML value attribute. It is also used to retreive the
* label from the list model. If the <code>value</code> property is not set,
* the tag's body content is evaluated. In this case, the HTML value attribute
* is set to the empty string.
*
* @author Oliver Stuhr
* @author Christoph Beck
*/
public class ListOptionTag extends OptionTag {
/**
* Default constructor
*/
public ListOptionTag() {
super(true);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#init()
*/
protected void init() throws Exception {
super.init();
if (selectTag.getListModel() == null) {
throw new ConfigException("A list option tag for input '" + selectTag.getName() + "' requires a model!");
}
}
protected void checkValue() throws Exception {
if (getValue() != null) {
super.checkValue();
} else {
if (selectTag.isSelectable("")) {
throw new ConfigException("List option default value '' is contained in model for input '" + selectTag.getName() + "'!");
}
}
}
protected String getValueAttribute() {
if (getValue() == null) { // 'default option' mode
return "";
}
return super.getValueAttribute();
}
/**
* If no value attribute has been set, select this option if
* the model's selection is empty.
*/
protected String getSelectedAttribute() throws Exception {
if (getValue() == null) { // 'default option' mode
return selectTag.getGroupModel().getSelectedCount() > 0 ? null : "selected";
}
return super.getSelectedAttribute();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.taglib.html.AbstractTag#getBodyContent()
*/
protected String getBodyContent() throws Exception {
if (getValue() == null) { // 'default option' mode
return null;
}
// get label from list model
ListModel listModel = selectTag.getListModel();
Object value = listModel.getValue(getValueAttribute());
Locale locale = I18nSupport.getInstance(pageContext).getLocale(pageContext);
return listModel.getLabel(value, locale);
}
}