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.MultiSelectControl;
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 = "checkboxMultiSelect"
034: * body-content = "JSP"
035: * description = "Form multiselect checkbox field, represents UiLib 'MultiSelectControl'."
036: */
037: public class FormCheckboxMultiSelectHtmlTag extends
038: BaseFormElementHtmlTag {
039: protected String type = "horizontal";
040: protected boolean labelBefore = false;
041:
042: public FormCheckboxMultiSelectHtmlTag() {
043: setHasElementContextSpan(false);
044: }
045:
046: protected int doStartTag(Writer out) throws Exception {
047: super .doStartTag(out);
048: addContextEntry(AttributedTagInterface.HTML_ELEMENT_KEY, null);
049: return EVAL_BODY_INCLUDE;
050: }
051:
052: public int doEndTag(Writer out) throws Exception {
053: assertControlType("MultiSelectControl");
054:
055: if (!"horizontal".equals(type) && !"vertical".equals(type))
056: throw new AraneaJspException(
057: "Attribute 'type' can be only either 'horizontal' or 'vertical'!");
058:
059: // Prepare
060: MultiSelectControl.ViewModel viewModel = ((MultiSelectControl.ViewModel) controlViewModel);
061:
062: FormCheckboxMultiSelectItemLabelHtmlTag label = new FormCheckboxMultiSelectItemLabelHtmlTag();
063: FormCheckboxMultiSelectItemHtmlTag item = new FormCheckboxMultiSelectItemHtmlTag();
064:
065: for (Iterator i = viewModel.getSelectItems().iterator(); i
066: .hasNext();) {
067: DisplayItem displayItem = (DisplayItem) i.next();
068:
069: // set the corresponding HTML id for label and checkbox so that clicking on label
070: // sets the checkbox value too
071: String checkboxId = viewModel.getScope().toString()
072: + displayItem.getValue();
073: item.setHtmlId(checkboxId);
074: label.setCheckboxId(checkboxId);
075:
076: if (labelBefore)
077: writeLabel(label, derivedId, displayItem.getValue());
078:
079: registerSubtag(item);
080:
081: item.setId(derivedId);
082: item.setValue(displayItem.getValue());
083: item.setEvents(events ? "true" : "false");
084: item.setValidateOnEvent(validateOnEvent ? "true" : "false");
085:
086: if (updateRegions != null)
087: item.setUpdateRegions(updateRegions);
088: if (globalUpdateRegions != null)
089: item.setGlobalUpdateRegions(globalUpdateRegions);
090: item.setStyleClass(getStyleClass());
091: if (getStyle() != null)
092: item.setStyle(getStyle());
093:
094: if (tabindex != null)
095: item.setTabindex(tabindex);
096:
097: executeStartSubtag(item);
098: executeEndTagAndUnregister(item);
099:
100: if (!labelBefore)
101: writeLabel(label, derivedId, displayItem.getValue());
102:
103: if ("horizontal".equals(type))
104: out.write(" ");
105: else if ("vertical".equals(type))
106: JspUtil.writeStartEndTag(out, "br");
107: }
108:
109: super .doEndTag(out);
110: return EVAL_PAGE;
111: }
112:
113: /**
114: * @jsp.attribute
115: * type = "java.lang.String"
116: * required = "false"
117: * description = "The way the checkboxes will be rendered - can be either "vertical" or "horizontal". By default "horizontal"."
118: */
119: public void setType(String type) throws JspException {
120: this .type = (String) evaluate("type", type, String.class);
121: }
122:
123: /**
124: * @jsp.attribute
125: * type = "java.lang.String"
126: * required = "false"
127: * description = "Boolean that controls whether label is before or after each checkbox. False by default."
128: */
129: public void setLabelBefore(String labelBefore) throws JspException {
130: this .labelBefore = ((Boolean) evaluateNotNull("labelBefore",
131: labelBefore, Boolean.class)).booleanValue();
132: }
133:
134: protected void writeLabel(
135: FormCheckboxMultiSelectItemLabelHtmlTag label, String id,
136: String value) throws JspException {
137: registerSubtag(label);
138: label.setId(id);
139: label.setValue(value);
140: executeStartSubtag(label);
141: executeEndTagAndUnregister(label);
142: }
143: }
|