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.date;
016:
017: import java.io.Writer;
018: import javax.servlet.jsp.JspException;
019: import org.apache.commons.lang.StringUtils;
020: import org.araneaframework.http.util.FileImportUtil;
021: import org.araneaframework.jsp.AraneaAttributes;
022: import org.araneaframework.jsp.tag.uilib.form.BaseFormElementHtmlTag;
023: import org.araneaframework.jsp.util.JspUtil;
024: import org.araneaframework.uilib.form.control.DateControl;
025: import org.araneaframework.uilib.form.control.TimeControl;
026:
027: /**
028: * Standard date time input form element base tag.
029: *
030: * @author Oleg Mürk
031: * @author <a href='mailto:margus@webmedia.ee'>Margus Väli</a> 2.05.2005 - precondition and onblur event notifying listeners
032: */
033: public class BaseFormDateTimeInputHtmlTag extends
034: BaseFormElementHtmlTag {
035: public final static String CALENDAR_BUTTON_ID_SUFFIX = "_cbutton";
036:
037: public final static Long DEFAULT_DATE_INPUT_SIZE = new Long(11);
038: public final static Long DEFAULT_TIME_INPUT_SIZE = new Long(5);
039:
040: protected String onChangePrecondition;
041: protected String calendarAlignment;
042: protected String calendarIconClass = "middle";
043:
044: /**
045: * @jsp.attribute
046: * type = "java.lang.String"
047: * required = "false"
048: * description = "Precondition for deciding whether go to server side or not."
049: */
050: public void setOnChangePrecondition(String onChangePrecondition)
051: throws JspException {
052: this .onChangePrecondition = (String) evaluate(
053: "onChangePrecondition", onChangePrecondition,
054: String.class);
055: }
056:
057: /**
058: * @jsp.attribute
059: * type = "java.lang.String"
060: * required = "false"
061: * description = "Alignment for popup calendar. In form 'zx' where z is in {TBCtb} and x in {LRClr}. Default is 'Br' (Bottom, right)."
062: */
063: public void setCalendarAlignment(String calendarAlignment)
064: throws JspException {
065: this .calendarAlignment = (String) evaluate("calendarAlignment",
066: calendarAlignment, String.class);
067: }
068:
069: /**
070: * Writes out date input
071: */
072: protected void writeDateInput(Writer out, String id, String name,
073: String value, String label, boolean isMandatory,
074: boolean isValid, Long size, boolean disabled,
075: String styleClass, String accessKey,
076: DateControl.ViewModel viewModel) throws Exception {
077: if (viewModel.getInputFilter() != null) {
078: attributes
079: .put(
080: AraneaAttributes.FilteredInputControl.CHARACTER_FILTER,
081: viewModel.getInputFilter()
082: .getCharacterFilter());
083: }
084:
085: // Write input tag
086: JspUtil.writeOpenStartTag(out, "input");
087: if (!StringUtils.isBlank(id))
088: JspUtil.writeAttribute(out, "id", id);
089: JspUtil.writeAttribute(out, "name", name);
090: JspUtil.writeAttribute(out, "class", getStyleClass());
091: JspUtil.writeAttribute(out, "style", getStyle());
092: JspUtil.writeAttribute(out, "type", "text");
093: JspUtil.writeAttribute(out, "value", value);
094: JspUtil.writeAttribute(out, "size", size);
095: JspUtil.writeAttribute(out, "tabindex", tabindex);
096: if (!StringUtils.isBlank(accessKey))
097: JspUtil.writeAttribute(out, "accesskey", accessKey);
098:
099: if (disabled) {
100: JspUtil.writeAttribute(out, "disabled", "true");
101: } else if (events && viewModel.isOnChangeEventRegistered()) {
102: writeSubmitScriptForUiEvent(out, "onchange",
103: this .derivedId, "onChanged", onChangePrecondition,
104: updateRegionNames);
105: }
106:
107: // validation won't occur with Event.observe registered in aranea-behaviour when date selected from calendar
108: if (!viewModel.isOnChangeEventRegistered()
109: && backgroundValidation) {
110: JspUtil.writeAttribute(out, "onchange",
111: "formElementValidationActionCall(this)");
112: }
113:
114: writeBackgroundValidationAttribute(out);
115:
116: JspUtil.writeAttributes(out, attributes);
117: JspUtil.writeCloseStartEndTag_SS(out);
118:
119: if (!disabled) {
120:
121: JspUtil.writeOpenStartTag(out, "a");
122: JspUtil.writeAttribute(out, "href", "javascript:;");
123: JspUtil.writeCloseStartTag_SS(out);
124:
125: String calendarImgId = id + CALENDAR_BUTTON_ID_SUFFIX;
126: JspUtil.writeOpenStartTag(out, "img");
127: out.write(" src=\"");
128: out.write(FileImportUtil.getImportString(
129: "gfx/ico_calendar.gif", pageContext.getRequest()));
130: out.write("\" ");
131: JspUtil.writeAttribute(out, "id", calendarImgId);
132: JspUtil.writeAttribute(out, "class", calendarIconClass);
133: JspUtil.writeCloseStartEndTag_SS(out);
134:
135: JspUtil.writeEndTag_SS(out, "a");
136:
137: writeCalendarScript(out, id, "%d.%m.%Y");
138: }
139: }
140:
141: /**
142: * @since 1.0.3
143: */
144: protected String getTimeSelectScript(String selectId,
145: Integer value, int valueCount) {
146: StringBuffer sb = new StringBuffer();
147: sb.append("Aranea.UI.addOptions('" + selectId + "',"
148: + String.valueOf(valueCount) + ",");
149: sb.append(value != null ? value.toString() : "null").append(
150: ");");
151:
152: return sb.toString();
153: }
154:
155: /**
156: * @since 1.0.3
157: */
158: protected String getTimeInputOnChangePrecondition(String timeInputId) {
159: if (onChangePrecondition != null)
160: return onChangePrecondition;
161: String timeInputRef = "document.getElementById('" + timeInputId
162: + "')";
163: String precondition = "return Aranea.UI.isChanged('"
164: + timeInputId + "')" + " && ((" + timeInputRef
165: + ".value.length==5) || (" + timeInputRef
166: + ".value.length==0))";
167: return precondition;
168: }
169:
170: /**
171: * @since 1.0.3
172: */
173: protected String getHourSelectOnChangePrecondition(
174: String timeInputId) {
175: return getSelectOnChangePrecondition(timeInputId);
176: }
177:
178: /**
179: * @since 1.0.3
180: */
181: protected String getMinuteSelectOnChangePrecondition(
182: String timeInputId) {
183: return getSelectOnChangePrecondition(timeInputId);
184: }
185:
186: /**
187: * @since 1.0.3
188: */
189: protected String getSelectOnChangePrecondition(String timeInputId) {
190: String precondition = onChangePrecondition == null ? "return (document.getElementById('"
191: + timeInputId + "').value.length==5)"
192: : onChangePrecondition;
193: return precondition;
194: }
195:
196: /**
197: * Writes out time input
198: * The id and accessKey parameters may be null.
199: * Note that the ID attribute is [for the moment, at least: 28.12.2004] only needed
200: * for the access keys to function. That is, the typical pattern as seen in HTML is:
201: * <pre>
202: * <label for="some.control.id" accesskey="some.key"> ...
203: * <input id="some.control.id" ... >
204: * </pre>
205: * As you see, the <code>input</code> tag outputs its ID so that the <code>label</code> tag
206: * could reference it.
207: */
208: // XXX: not used ANYWHERE in Aranea classes.
209: protected void writeTimeInput(Writer out, String id, String name,
210: String value, String label, Long size, boolean disabled,
211: String styleClass, String accessKey,
212: TimeControl.ViewModel viewModel) throws Exception {
213: if (viewModel.getInputFilter() != null) {
214: attributes
215: .put(
216: AraneaAttributes.FilteredInputControl.CHARACTER_FILTER,
217: viewModel.getInputFilter()
218: .getCharacterFilter());
219: }
220:
221: // Write input tag
222: JspUtil.writeOpenStartTag(out, "input");
223: if (!StringUtils.isBlank(id))
224: JspUtil.writeAttribute(out, "id", id);
225: JspUtil.writeAttribute(out, "name", name);
226: JspUtil.writeAttribute(out, "class", styleClass);
227: JspUtil.writeAttribute(out, "type", "text");
228: JspUtil.writeAttribute(out, "value", value);
229: JspUtil.writeAttribute(out, "size", size);
230: JspUtil.writeAttribute(out, "tabindex", tabindex);
231: if (!StringUtils.isBlank(accessKey))
232: JspUtil.writeAttribute(out, "accesskey", accessKey);
233: if (disabled) {
234: JspUtil.writeAttribute(out, "disabled", "true");
235: } else if (events && viewModel.isOnChangeEventRegistered()) {
236: writeSubmitScriptForUiEvent(out, "onchange",
237: this .derivedId, "onChanged", onChangePrecondition,
238: updateRegionNames);
239: }
240:
241: writeBackgroundValidationAttribute(out);
242:
243: JspUtil.writeAttributes(out, attributes);
244: JspUtil.writeCloseStartEndTag_SS(out);
245: }
246:
247: protected void writeCalendarScript(Writer out, String id,
248: String format) throws Exception {
249: JspUtil.writeOpenStartTag(out, "script");
250: JspUtil.writeAttribute(out, "type", "text/javascript");
251: JspUtil.writeCloseStartTag(out);
252:
253: StringBuffer script = new StringBuffer();
254: script.append("Aranea.UI.calendarSetup('");
255: script.append(id);
256: script.append("', '");
257: script.append(format);
258: script.append("', ");
259: if (calendarAlignment == null)
260: script.append("null");
261: else {
262: script.append("'");
263: script.append(calendarAlignment);
264: script.append("'");
265: }
266: script.append(");");
267:
268: out.write(script.toString());
269: JspUtil.writeEndTag_SS(out, "script");
270: }
271: }
|