Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2004, 2005 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package org.apache.tapestry.form;
 
 import java.util.Date;
 import java.util.Map;
 
Provides a Form java.util.Date field component for selecting dates. [ Component Reference ] As of 4.0, DatePicker can indicate that it is required, use a custom translator (e.g. for java.sql.Date), and perform validation on the submitted date.

As of 4.0, this component can be configurably translated and validated.

Author(s):
Paul Geerts
Malcolm Edgar
Paul Ferraro
Since:
2.2
 
 
 public abstract class DatePicker extends AbstractFormComponent implements TranslatedField
 {
     private static final String SYM_NAME = "name";
 
     private static final String SYM_FORMNAME = "formName";
 
     private static final String SYM_MONTHNAMES = "monthNames";
 
     private static final String SYM_SHORT_MONTHNAMES = "shortMonthNames";
 
     private static final String SYM_WEEKDAYNAMES = "weekDayNames";
 
     private static final String SYM_SHORT_WEEKDAYNAMES = "shortWeekDayNames";
 
     private static final String SYM_FIRSTDAYINWEEK = "firstDayInWeek";
 
     private static final String SYM_MINDAYSINFIRSTWEEK = "minimalDaysInFirstWeek";
 
     private static final String SYM_FORMAT = "format";
 
     private static final String SYM_INCL_WEEK = "includeWeek";
 
     private static final String SYM_CLEAR_BUTTON_LABEL = "clearButtonLabel";
 
     private static final String SYM_VALUE = "value";
 
     private static final String SYM_BUTTONONCLICKHANDLER = "buttonOnclickHandler";
     
     public abstract Date getValue();
 
     public abstract void setValue(Date value);
 
     public abstract boolean isDisabled();
 
     public abstract boolean getIncludeWeek();
 
     public abstract IAsset getIcon();
     
     public abstract String getImageClass();
    
    

Since:
4.1.1
     
     public abstract String getTitle();

    
Injected.

Since:
4.0
 
     public abstract IScript getScript();

    
    protected void renderFormComponent(IMarkupWriter writerIRequestCycle cycle)
    {
        PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cyclethis);
        boolean disabled = isDisabled();
        DateTranslator translator = (DateTranslatorgetTranslator();
        Locale locale = getPage().getLocale();
        SimpleDateFormat format = translator.getDateFormat(locale);
        DateFormatSymbols dfs = format.getDateFormatSymbols();
        Calendar cal = Calendar.getInstance(locale);
        String name = getName();
        
        String title = getTitle();
        if (title == null)
            title = format.toLocalizedPattern();
        String value = getTranslatedFieldSupport().format(thisgetValue());
        Map symbols = new HashMap();
        symbols.put(name);
        symbols.put(format.toPattern());
        symbols.put(getIncludeWeek() ? . : .);
        symbols.put(makeStringList(dfs.getMonths(), 0, 12));
        symbols.put(makeStringList(dfs.getShortMonths(), 0, 12));
        symbols.put(makeStringList(dfs.getWeekdays(), 1, 8));
        symbols.put(makeStringList(dfs.getShortWeekdays(), 1, 8));
        symbols.put(new Integer(cal.getFirstDayOfWeek() - 1));
        symbols.put(new Integer(cal.getMinimalDaysInFirstWeek()));
        symbols.put(getMessages().getMessage("clear"));
        symbols.put(getForm().getName());
        symbols.put(getValue());
        
        getScript().execute(thiscyclepageRenderSupportsymbols);
        
        renderDelegatePrefix(writercycle);
        writer.beginEmpty("input");
        writer.attribute("type""text");
        writer.attribute("name"name);
        writer.attribute("value"value);
        writer.attribute("title"title);
        if (disabled)
            writer.attribute("disabled""disabled");
        renderIdAttribute(writercycle);
        renderDelegateAttributes(writercycle);
        getTranslatedFieldSupport().renderContributions(thiswritercycle);
        getValidatableFieldSupport().renderContributions(thiswritercycle);
        renderInformalParameters(writercycle);
        writer.printRaw(" ");
        if (!disabled)
        {
            writer.begin("a");
            writer.attribute("href", (Stringsymbols.get());
        }
        IAsset icon = getIcon();
        writer.beginEmpty("img");
        writer.attribute("src"icon.buildURL());
        writer.attribute("alt"getMessages().getMessage("alt"));
        writer.attribute("border", 0);
        writer.attribute("class"getImageClass());
        
        if (!disabled)
            writer.end();
        renderDelegateSuffix(writercycle);
    }

    
    protected void rewindFormComponent(IMarkupWriter writerIRequestCycle cycle)
    {
        String value = cycle.getParameter(getName());
        try
        {
            Date date = (DategetTranslatedFieldSupport().parse(thisvalue);
            getValidatableFieldSupport().validate(thiswritercycledate);
            setValue(date);
        }
        catch (ValidatorException e)
        {
            getForm().getDelegate().record(e);
        }
    }

    
Create a list of quoted strings. The list is suitable for initializing a JavaScript array.
    private String makeStringList(String[] aint offsetint length)
    {
        StringBuffer b = new StringBuffer();
        for (int i = offseti < lengthi++)
        {
            // JavaScript is sensitive to some UNICODE characters. So for
            // the sake of simplicity, we just escape everything
            b.append('"');
            char[] ch = a[i].toCharArray();
            for (int j = 0; j < ch.lengthj++)
            {
                if (ch[j] < 128)
                {
                    b.append(ch[j]);
                }
                else
                {
                    b.append(escape(ch[j]));
                }
            }
            b.append('"');
            if (i < length - 1)
            {
                b.append(", ");
            }
        }
        return b.toString();
    }

    
Create an escaped Unicode character.

Parameters:
c
Returns:
The unicode character in escaped string form
    private static String escape(char c)
    {
        char unescapedChar = c;
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < 4; i++)
        {
            b.append(Integer.toHexString(unescapedChar & 0x000F).toUpperCase());
            unescapedChar >>>= 4;
        }
        b.append("u\\");
        return b.reverse().toString();
    }

    
Injected.
    public abstract ValidatableFieldSupport getValidatableFieldSupport();

    
Injected.
    public abstract TranslatedFieldSupport getTranslatedFieldSupport();

    
    public boolean isRequired()
    {
        return getValidatableFieldSupport().isRequired(this);
    }
New to GrepCode? Check out our FAQ X