001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.jsp.tag.uilib.form.element.select;
016:
017: import java.io.Writer;
018: import java.util.Iterator;
019: import javax.servlet.jsp.JspException;
020: import org.araneaframework.jsp.exception.AraneaJspException;
021: import org.araneaframework.jsp.tag.basic.AttributedTagInterface;
022: import org.araneaframework.jsp.tag.uilib.form.BaseFormElementHtmlTag;
023: import org.araneaframework.jsp.util.JspUtil;
024: import org.araneaframework.uilib.form.control.SelectControl;
025: import org.araneaframework.uilib.support.DisplayItem;
026:
027: /**
028: * Standard select form element tag.
029: *
030: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
031: *
032: * @jsp.tag
033: * name = "radioSelect"
034: * body-content = "JSP"
035: * description = "Form radioselect buttons field, represents UiLib "SelectControl"."
036: */
037: public class FormRadioSelectHtmlTag extends BaseFormElementHtmlTag {
038: protected String type = "horizontal";
039: protected boolean labelBefore = false;
040:
041: public FormRadioSelectHtmlTag() {
042: setHasElementContextSpan(false);
043: }
044:
045: public int doEndTag(Writer out) throws Exception {
046: assertControlType("SelectControl");
047:
048: if (!"horizontal".equals(type) && !"vertical".equals(type))
049: throw new AraneaJspException(
050: "Attribute 'type' can be only either 'horizontal' or 'vertical'!");
051:
052: // Prepare
053: addContextEntry(AttributedTagInterface.HTML_ELEMENT_KEY, null);
054: SelectControl.ViewModel viewModel = ((SelectControl.ViewModel) controlViewModel);
055:
056: FormRadioSelectItemLabelHtmlTag label = new FormRadioSelectItemLabelHtmlTag();
057: FormRadioSelectItemHtmlTag item = new FormRadioSelectItemHtmlTag();
058:
059: for (Iterator i = viewModel.getSelectItems().iterator(); i
060: .hasNext();) {
061: DisplayItem displayItem = (DisplayItem) i.next();
062:
063: // set the corresponding HTML id for label and radiobutton so that clicking on label
064: // affects radiobutton selection
065: String radioId = viewModel.getScope().toString()
066: + displayItem.getValue();
067: item.setHtmlId(radioId);
068: label.setRadioId(radioId);
069:
070: if (labelBefore)
071: writeLabel(label, derivedId, displayItem.getValue());
072:
073: registerSubtag(item);
074:
075: item.setId(derivedId);
076: item.setValue(displayItem.getValue());
077:
078: item.setEvents(events ? "true" : "false");
079: item.setValidateOnEvent(validateOnEvent ? "true" : "false");
080: if (updateRegions != null)
081: item.setUpdateRegions(updateRegions);
082: if (globalUpdateRegions != null)
083: item.setGlobalUpdateRegions(globalUpdateRegions);
084: item.setStyleClass(getStyleClass());
085: if (getStyle() != null)
086: item.setStyle(getStyle());
087:
088: if (tabindex != null)
089: item.setTabindex(tabindex);
090:
091: executeStartSubtag(item);
092: executeEndTagAndUnregister(item);
093:
094: if (!labelBefore)
095: writeLabel(label, derivedId, displayItem.getValue());
096:
097: if ("horizontal".equals(type))
098: out.write(" ");
099: else if ("vertical".equals(type))
100: JspUtil.writeStartEndTag(out, "br");
101: }
102:
103: super .doEndTag(out);
104: return EVAL_PAGE;
105: }
106:
107: /* ***********************************************************************************
108: * Tag attributes
109: * ***********************************************************************************/
110:
111: /**
112: * @jsp.attribute
113: * type = "java.lang.String"
114: * required = "false"
115: * description = "The way the radio buttons will be rendered - can be either vertical or horizontal. By default horizontal."
116: */
117: public void setType(String type) throws JspException {
118: this .type = (String) evaluate("type", type, String.class);
119: }
120:
121: /**
122: * @jsp.attribute
123: * type = "java.lang.String"
124: * required = "false"
125: * description = "Boolean that controls whether label is before or after each radio button. False by default."
126: */
127: public void setLabelBefore(String labelBefore) throws JspException {
128: this .labelBefore = ((Boolean) evaluateNotNull("labelBefore",
129: labelBefore, Boolean.class)).booleanValue();
130: }
131:
132: protected void writeLabel(FormRadioSelectItemLabelHtmlTag label,
133: String id, String value) throws JspException {
134: registerSubtag(label);
135: label.setId(id);
136: label.setValue(value);
137: executeStartSubtag(label);
138: executeEndTagAndUnregister(label);
139: }
140: }
|