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     * @author jkuhnert
036     */
037    public abstract class DropdownTimePicker extends AbstractFormWidget implements TranslatedField
038    {
039        
040        /** parameter. */
041        public abstract Object getValue();
042        
043        public abstract void setValue(Object value);
044        
045        public abstract boolean isDisabled();
046        
047        /** Alt html text for the date icon, what is displayed when mouse hovers over icon. */
048        public abstract String getIconAlt();
049        
050        /**
051         * {@inheritDoc}
052         */
053        protected void renderFormWidget(IMarkupWriter writer, IRequestCycle cycle)
054        {
055            // dojo dates are in POSIX style formats so we format the value manually
056            DateTranslator translator = (DateTranslator) getTranslator();
057            
058            renderDelegatePrefix(writer, cycle);
059            
060            // the html output doesn't matter very much as dojo
061            // will create an inline input field for us anyways, but we do need
062            // a node to reference
063            writer.begin("div");
064            renderIdAttribute(writer, cycle);
065            
066            renderDelegateAttributes(writer, cycle);
067            
068            getValidatableFieldSupport().renderContributions(this, writer, cycle);
069            
070            renderInformalParameters(writer, cycle);
071            
072            writer.print(" ");
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(getPage().getLocale()));
083            json.put("saveFormat", translator.getPattern(getPage().getLocale()));
084            
085            if (getValue() != null) {
086                json.put("value", translator.formatRfc3339(getValue()));
087            }
088            
089            json.put("disabled", isDisabled());
090            
091            Map parms = new HashMap();
092            parms.put("clientId", getClientId());
093            parms.put("props", json.toString());
094            parms.put("widget", this);
095            
096            getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);
097        }
098        
099        /**
100         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
101         */
102        protected void rewindFormWidget(IMarkupWriter writer, IRequestCycle cycle)
103        {
104            String value = cycle.getParameter(getName());
105            
106            try
107            {
108                Object translated = getTranslatedFieldSupport().parse(this, value);
109                
110                getValidatableFieldSupport().validate(this, writer, cycle, translated);
111                
112                setValue(translated);
113            }
114            catch (ValidatorException e)
115            {
116                getForm().getDelegate().record(e);
117            }
118        }
119        
120        /**
121         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
122         */
123        public boolean isRequired()
124        {
125            return getValidatableFieldSupport().isRequired(this);
126        }
127        
128        /** Injected. */
129        public abstract IScript getScript();
130        
131        /** Injected. */
132        public abstract TranslatedFieldSupport getTranslatedFieldSupport();
133        
134        /** Injected. */
135        public abstract ValidatableFieldSupport getValidatableFieldSupport();
136        
137    }