001 // Copyright Jun 10, 2006 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014 package org.apache.tapestry.dojo.form;
015
016 import java.util.Date;
017 import java.util.HashMap;
018 import java.util.Map;
019
020 import org.apache.tapestry.IMarkupWriter;
021 import org.apache.tapestry.IRequestCycle;
022 import org.apache.tapestry.IScript;
023 import org.apache.tapestry.TapestryUtils;
024 import org.apache.tapestry.form.TranslatedField;
025 import org.apache.tapestry.form.TranslatedFieldSupport;
026 import org.apache.tapestry.form.ValidatableFieldSupport;
027 import org.apache.tapestry.form.translator.DateTranslator;
028 import org.apache.tapestry.json.JSONObject;
029 import org.apache.tapestry.valid.ValidatorException;
030
031 /**
032 * Implementation of the dojo DropdownDatePicker widget as a tapestry
033 * component. Wraps a form input field with a date picker icon next to it
034 * that when clicked on reveals a calendar to choose date values from.
035 *
036 * @author jkuhnert
037 */
038 public abstract class DropdownDatePicker extends AbstractFormWidget
039 implements TranslatedField
040 {
041
042 /** parameter. */
043 public abstract Date getValue();
044
045 public abstract void setValue(Date value);
046
047 public abstract boolean isDisabled();
048
049 /** Alt html text for the date icon, what is displayed when mouse hovers over icon. */
050 public abstract String getIconAlt();
051
052 /**
053 * {@inheritDoc}
054 */
055 protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
056 {
057 // dojo dates are in POSIX style formats so we format the value manually
058 DateTranslator translator = (DateTranslator) getTranslator();
059
060 renderDelegatePrefix(writer, cycle);
061
062 // the html output doesn't matter very much as dojo
063 // will create an inline input field for us anyways, but we do need
064 // a node to reference
065 writer.begin("div");
066 renderIdAttribute(writer, cycle);
067
068 renderDelegateAttributes(writer, cycle);
069
070 getValidatableFieldSupport().renderContributions(this, writer, cycle);
071
072 renderInformalParameters(writer, cycle);
073
074 writer.end();
075 renderDelegateSuffix(writer, cycle);
076
077 // now create widget parms
078 JSONObject json = new JSONObject();
079 json.put("inputId", getClientId());
080 json.put("inputName", getName());
081 json.put("iconAlt", getIconAlt());
082 json.put("displayFormat", translator.getPattern());
083
084 if (getValue() != null) {
085 json.put("date", getTranslatedFieldSupport().format(this, getValue()));
086 }
087
088 Map parms = new HashMap();
089 parms.put("clientId", getClientId());
090 parms.put("props", json.toString());
091
092 getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);
093 }
094
095 /**
096 * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
097 */
098 protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
099 {
100 String value = cycle.getParameter(getName());
101
102 try
103 {
104 Date date = (Date) getTranslatedFieldSupport().parse(this, value);
105
106 getValidatableFieldSupport().validate(this, writer, cycle, date);
107
108 setValue(date);
109 }
110 catch (ValidatorException e)
111 {
112 getForm().getDelegate().record(e);
113 }
114 }
115
116 /**
117 * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
118 */
119 public boolean isRequired()
120 {
121 return getValidatableFieldSupport().isRequired(this);
122 }
123
124 /** Injected. */
125 public abstract IScript getScript();
126
127 /** Injected. */
128 public abstract TranslatedFieldSupport getTranslatedFieldSupport();
129
130 /** Injected. */
131 public abstract ValidatableFieldSupport getValidatableFieldSupport();
132
133 }