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 org.apache.tapestry.IMarkupWriter;
017    import org.apache.tapestry.IRequestCycle;
018    import org.apache.tapestry.IScript;
019    import org.apache.tapestry.TapestryUtils;
020    import org.apache.tapestry.form.TranslatedField;
021    import org.apache.tapestry.form.TranslatedFieldSupport;
022    import org.apache.tapestry.form.ValidatableFieldSupport;
023    import org.apache.tapestry.form.translator.DateTranslator;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidatorException;
026    
027    import java.util.HashMap;
028    import java.util.Map;
029    
030    /**
031     * Implementation of the dojo DropdownTimePicker widget as a tapestry
032     * component. Wraps a form input field with a date picker icon next to it
033     * that when clicked on reveals a pane to choose time values from. 
034     */
035    public abstract class DropdownTimePicker extends AbstractFormWidget implements TranslatedField
036    {
037        
038        /** parameter. */
039        public abstract Object getValue();
040        
041        public abstract void setValue(Object value);
042        
043        public abstract boolean isDisabled();
044        
045        /** Alt html text for the date icon, what is displayed when mouse hovers over icon. */
046        public abstract String getIconAlt();
047        
048        /**
049         * {@inheritDoc}
050         */
051        protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
052        {
053            // dojo dates are in POSIX style formats so we format the value manually
054            DateTranslator translator = (DateTranslator) getTranslator();
055            
056            renderDelegatePrefix(writer, cycle);
057            
058            // the html output doesn't matter very much as dojo
059            // will create an inline input field for us anyways, but we do need
060            // a node to reference
061            writer.begin(getTemplateTagName());
062            renderIdAttribute(writer, cycle);
063            
064            renderDelegateAttributes(writer, cycle);
065            
066            getValidatableFieldSupport().renderContributions(this, writer, cycle);
067            
068            renderInformalParameters(writer, cycle);
069            
070            writer.print(" ");
071            
072            writer.end();
073            renderDelegateSuffix(writer, cycle);
074            
075            // now create widget parms
076            JSONObject json = new JSONObject();
077            json.put("inputId", getClientId());
078            json.put("inputName", getName());
079            json.put("iconAlt", getIconAlt());
080            json.put("displayFormat", translator.getPattern(getPage().getLocale()));
081            json.put("saveFormat", translator.getPattern(getPage().getLocale()));
082            
083            if (getValue() != null) {
084                json.put("value", translator.formatRfc3339(getValue()));
085            }
086            
087            json.put("disabled", isDisabled());
088            
089            Map parms = new HashMap();
090            parms.put("clientId", getClientId());
091            parms.put("props", json.toString());
092            parms.put("widget", this);
093            
094            getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);
095        }
096        
097        /**
098         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
099         */
100        protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
101        {
102            String value = cycle.getParameter(getName());
103            
104            try
105            {
106                Object translated = getTranslatedFieldSupport().parse(this, value);
107                
108                getValidatableFieldSupport().validate(this, writer, cycle, translated);
109                
110                setValue(translated);
111            }
112            catch (ValidatorException e)
113            {
114                getForm().getDelegate().record(e);
115            }
116        }
117        
118        /**
119         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
120         */
121        public boolean isRequired()
122        {
123            return getValidatableFieldSupport().isRequired(this);
124        }
125        
126        /** Injected. */
127        public abstract IScript getScript();
128        
129        /** Injected. */
130        public abstract TranslatedFieldSupport getTranslatedFieldSupport();
131        
132        /** Injected. */
133        public abstract ValidatableFieldSupport getValidatableFieldSupport();
134        
135    }