001: /*
002: * Copyright 2002-2005 the original author or authors.
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: */
016:
017: package info.jtrac.wicket.yui;
018:
019: import info.jtrac.wicket.ErrorHighlighter;
020: import java.text.DateFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.Date;
023: import java.util.Locale;
024: import org.apache.wicket.AttributeModifier;
025: import org.apache.wicket.behavior.HeaderContributor;
026: import org.apache.wicket.markup.html.IHeaderContributor;
027: import org.apache.wicket.markup.html.IHeaderResponse;
028: import org.apache.wicket.markup.html.WebMarkupContainer;
029: import org.apache.wicket.markup.html.form.FormComponentPanel;
030: import org.apache.wicket.markup.html.form.TextField;
031: import org.apache.wicket.model.AbstractReadOnlyModel;
032: import org.apache.wicket.model.IModel;
033: import org.apache.wicket.util.convert.ConversionException;
034: import org.apache.wicket.util.convert.IConverter;
035: import org.apache.wicket.util.convert.converters.AbstractConverter;
036:
037: /**
038: * yui date picker panel
039: */
040: public class YuiCalendar extends FormComponentPanel implements
041: IHeaderContributor {
042:
043: private TextField dateField;
044: private WebMarkupContainer container;
045:
046: public YuiCalendar(String id, IModel model, boolean required) {
047:
048: super (id, null);
049:
050: add(HeaderContributor
051: .forJavaScript("resources/yui/yahoo/yahoo-min.js"));
052: add(HeaderContributor
053: .forJavaScript("resources/yui/event/event-min.js"));
054: add(HeaderContributor
055: .forJavaScript("resources/yui/dom/dom-min.js"));
056: add(HeaderContributor
057: .forJavaScript("resources/yui/calendar/calendar-min.js"));
058: add(HeaderContributor
059: .forJavaScript("resources/yui/calendar/calendar-utils.js"));
060: add(HeaderContributor
061: .forCss("resources/yui/calendar/assets/calendar.css"));
062:
063: dateField = new TextField("field", model, Date.class) {
064: @Override
065: public IConverter getConverter(Class clazz) {
066: return new AbstractConverter() {
067: private DateFormat df = new SimpleDateFormat(
068: "yyyy-MM-dd");
069:
070: public Object convertToObject(String s,
071: Locale locale) {
072: if (s == null || s.trim().length() == 0) {
073: return null;
074: }
075: try {
076: return df.parse(s);
077: } catch (Exception e) {
078: throw new ConversionException(e);
079: }
080: }
081:
082: protected Class getTargetType() {
083: return Date.class;
084: }
085:
086: @Override
087: public String convertToString(Object o,
088: Locale locale) {
089: Date d = (Date) o;
090: return df.format(d);
091: }
092: };
093: }
094:
095: @Override
096: public IModel getLabel() {
097: return YuiCalendar.this .getLabel();
098: }
099: };
100: dateField.setOutputMarkupId(true);
101: dateField.setRequired(required);
102: dateField.add(new ErrorHighlighter());
103: add(dateField);
104:
105: final WebMarkupContainer button = new WebMarkupContainer(
106: "button");
107: button.setOutputMarkupId(true);
108: button.add(new AttributeModifier("onclick", true,
109: new AbstractReadOnlyModel() {
110: public Object getObject() {
111: return "showCalendar(" + getCalendarId()
112: + ", '" + getInputId() + "');";
113: }
114: }));
115: add(button);
116:
117: container = new WebMarkupContainer("container");
118: container.setOutputMarkupId(true);
119: add(container);
120: }
121:
122: @Override
123: public void updateModel() {
124: dateField.updateModel();
125: }
126:
127: private String getCalendarId() {
128: return getMarkupId();
129: }
130:
131: private String getInputId() {
132: return dateField.getMarkupId();
133: }
134:
135: private String getContainerId() {
136: return container.getMarkupId();
137: }
138:
139: public void renderHead(IHeaderResponse response) {
140: String calendarId = getCalendarId();
141: response.renderOnDomReadyJavascript("init" + calendarId + "()");
142: response.renderJavascript("function init" + calendarId
143: + "() { " + calendarId
144: + " = new YAHOO.widget.Calendar('" + calendarId
145: + "', '" + getContainerId() + "'); " + calendarId
146: + ".selectEvent.subscribe(handleSelect, [ "
147: + calendarId + ", '" + getInputId() + "' ], true); }",
148: null);
149: }
150: }
|