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.UiUpdateEvent;
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.jsp.util.JspWidgetCallUtil;
025: import org.araneaframework.uilib.event.OnChangeEventListener;
026: import org.araneaframework.uilib.form.control.SelectControl;
027: import org.araneaframework.uilib.support.DisplayItem;
028:
029: /**
030: * Standard select form element tag.
031: *
032: * @author Oleg Mürk
033: *
034: * @jsp.tag
035: * name = "select"
036: * body-content = "JSP"
037: * description = "Form dropdown list input field, represents UiLib "SelectControl"."
038: */
039: public class FormSelectHtmlTag extends BaseFormElementHtmlTag {
040: protected Long size = null;
041: protected String onChangePrecondition;
042:
043: {
044: baseStyleClass = "aranea-select";
045: }
046:
047: protected int doStartTag(Writer out) throws Exception {
048: int r = super .doStartTag(out);
049: addContextEntry(AttributedTagInterface.HTML_ELEMENT_KEY, null);
050: return r;
051: }
052:
053: public int doEndTag(Writer out) throws Exception {
054: // Type check
055: assertControlType("SelectControl");
056:
057: // Prepare
058: String name = this .getFullFieldId();
059: SelectControl.ViewModel viewModel = ((SelectControl.ViewModel) controlViewModel);
060:
061: // Write input tag
062: JspUtil.writeOpenStartTag(out, "select");
063: JspUtil.writeAttribute(out, "id", name);
064: JspUtil.writeAttribute(out, "name", name);
065: JspUtil.writeAttribute(out, "class", getStyleClass());
066: JspUtil.writeAttribute(out, "style", getStyle());
067: JspUtil.writeAttribute(out, "tabindex", tabindex);
068: JspUtil.writeAttribute(out, "size", size);
069:
070: if (viewModel.isDisabled())
071: JspUtil.writeAttribute(out, "disabled", "true");
072: if (events && viewModel.isOnChangeEventRegistered()) {
073: UiUpdateEvent event = new UiUpdateEvent(
074: OnChangeEventListener.ON_CHANGE_EVENT, formFullId
075: + "." + derivedId, null, updateRegionNames);
076: event.setEventPrecondition(onChangePrecondition);
077: JspUtil.writeEventAttributes(out, event);
078: JspWidgetCallUtil
079: .writeSubmitScriptForEvent(out, "onchange");
080: }
081: JspUtil.writeAttributes(out, attributes);
082: writeBackgroundValidationAttribute(out);
083: JspUtil.writeCloseStartTag(out);
084:
085: // Write items
086: String selectedValue = viewModel.getSimpleValue();
087:
088: for (Iterator i = viewModel.getSelectItems().iterator(); i
089: .hasNext();) {
090: DisplayItem item = (DisplayItem) i.next();
091: if (!item.isDisabled()) {
092: String value = item.getValue();
093: String label = item.getDisplayString();
094:
095: JspUtil.writeOpenStartTag(out, "option");
096: JspUtil.writeAttribute(out, "value",
097: value != null ? value : "");
098: if ((value == null && selectedValue == null)
099: || (value != null && value
100: .equals(selectedValue)))
101: JspUtil.writeAttribute(out, "selected", "true");
102: JspUtil.writeCloseStartTag_SS(out);
103: JspUtil.writeEscaped(out, label);
104: JspUtil.writeEndTag(out, "option");
105: }
106: }
107:
108: // Close tag
109: JspUtil.writeEndTag_SS(out, "select");
110:
111: // Continue
112: super .doEndTag(out);
113: return EVAL_PAGE;
114: }
115:
116: /* ***********************************************************************************
117: * Tag attributes
118: * ***********************************************************************************/
119:
120: /**
121: * @jsp.attribute
122: * type = "java.lang.String"
123: * required = "false"
124: * description = "Number of select elements visible at once."
125: */
126: public void setSize(String size) throws JspException {
127: this .size = (Long) evaluate("size", size, Long.class);
128: }
129:
130: /**
131: * @jsp.attribute
132: * type = "java.lang.String"
133: * required = "false"
134: * description = "Precondition for deciding whether go to server side or not."
135: */
136: public void setOnChangePrecondition(String onChangePrecondition)
137: throws JspException {
138: this .onChangePrecondition = (String) evaluate(
139: "onChangePrecondition", onChangePrecondition,
140: String.class);
141: }
142: }
|