Apache
  Home arrow Apache arrow Page 3 - Apache Tapestry and Custom Components:...
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Apache Tapestry and Custom Components: DateInput
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2007-06-19

    Table of Contents:
  • Apache Tapestry and Custom Components: DateInput
  • Creating the DateInput Component
  • Creating the Models
  • Wiring everything
  • Implementing PageBeginRenderListener

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Apache Tapestry and Custom Components: DateInput - Creating the Models
    (Page 3 of 5 )

    As you already know pretty well, we have to give PropertySelection components their models, explaining to them what exactly to display and what to return to the page class when the user makes a selection. The model for days is very simple and can be implemented in a few different ways. Here is one of the options:

    public class DayModel implements IPropertySelectionModel {

       

        public String getLabel(int index) {

            return "" + (index + 1);

        }

       

        public Object getOption(int index) {

            return index + 1;

        }

       

        public int getOptionCount() {

            return 31;

        }

       

        public String getValue(int index) {

            return "" + index;

        }

       

        public Object translateValue(String value) {

            return Integer.parseInt(value) + 1;

        }

    }

    We are simply displaying numbers from 1 to 31 as labels and use the indexes of the options for value attributes. I don’t feel like any explanations are needed here, but if you do have a question, please ask it at the discussions page. In any case, reviewing the article devoted to IPropertySelectionModel might be useful.

    The model for selecting a year is also quite simple:

    public class YearModel implements IPropertySelectionModel {

       

        private final static int START_YEAR = 1900;

        private final static int END_YEAR =

              new GregorianCalendar().get(Calendar.YEAR);

       

        public String getLabel(int index) {

            return Integer.toString(index + START_YEAR);

        }

       

        public Object getOption(int index) {

            return index + START_YEAR;

        }

       

        public int getOptionCount() {

            return END_YEAR - START_YEAR + 1;

        }

       

        public String getValue(int index) {

            return "" + index;

        }

       

        public Object translateValue(String value) {

            return Integer.parseInt(value) + START_YEAR;

        }

       

    }

    Everything should be more or less clear to you here, or if not, welcome to the discussions page. The model for months is slightly more interesting because it returns months’ names using the existing locality settings. For example, they will be automatically displayed in Spanish if your preferred language is Spanish, etc. This means that DateInput can be very easily localized, and we shall discuss this in more detail in a coming article on internationalization. For now, just have a look at the code and, of course, add this class to your application:

    public class LocalisedMonthsModel implements IPropertySelectionModel {

       

        private String[] months;

       

        public LocalisedMonthsModel(Locale locale) {

            DateFormatSymbols symbols = new DateFormatSymbols(locale);

            months = symbols.getMonths();

        }

       

        public String getLabel(int index) {

            return months[index];

        }

       

        public Object getOption(int index) {

            return index;

        }

       

        public int getOptionCount() {

            return 12;

        }

       

        public String getValue(int index) {

            return "" + index;

        }

       

        public Object translateValue(String value) {

            return Integer.parseInt(value);

        }

       

    }

    More Apache Articles
    More By Alexander Kolesnikov


       · I know that the topic of creating custom components raises significant number of...
       · I think I tightly followed your code, but I still get an error when I try to...
       · Check if the 'date' binding of the DateInput is actually bound to something in the...
       · Hi :)I have built the component where one introduces the address. How I can pass...
       · I am not completely sure what you mean, but it sounds like you could have an Address...
       · I have the same problem. There is [i]public abstract Date getDateOfBirth();[/i]...
       · Sorry, I see the mistake. Not `name="value"` but `name="date"`.
       · Sorry for not answering in time - I am completing a book on Tapestry 5 and working...
       · Sorry for not continuing the tutorial for a while. A publisher contacted me and...
       · It's a pleasure to learn Tapestry with your great tutorial!I'm truely looking...
       · It seems that some people dont like to use the specifications page and like to make...
     

       

    APACHE ARTICLES

    - Putting Apache in Jail
    - Containing Intrusions in Apache
    - Server Limits for Apache Security
    - Setting Permissions in Apache
    - Installing Apache
    - Apache Installation and Configuration
    - Apache Tapestry and Custom Components: DateI...
    - Tapestry and AJAX: Autocompleter and InlineE...
    - PropertySelection and IPropertySelectionMode...
    - The DatePicker and Shell Components of Apach...
    - Apache Tapestry: ASO and More Components
    - Apache Tapestry and DirectLink, IoC and DI
    - Making a CelebrityCollector with Apache Tape...
    - Apache Tapestry and Listener Methods, Condit...
    - The Properties of Tapestry Pages




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway