Home arrow Apache arrow Page 3 - Apache Tapestry and Custom Components: DateInput

Creating the Models - Apache

Tapestry has many benefits, but perhaps one of the most important is that it makes creation of custom components very easy. In this article, I'll create a custom component to illustrate this point.

TABLE OF CONTENTS:
  1. Apache Tapestry and Custom Components: DateInput
  2. Creating the DateInput Component
  3. Creating the Models
  4. Wiring everything
  5. Implementing PageBeginRenderListener
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 12
June 19, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

APACHE ARTICLES

- Apache Unveils Cassandra 1.2
- Apache on ARM Chips? Dell and Calxeda Help M...
- The Down Side of Open Source Software
- VMware Unveils Serengeti for Apache Hadoop
- SAP Takes Steps to Improve Hadoop Integration
- Looking to Hone Apache Hadoop Skills?
- How to Install Joomla on WAMPP
- Working with XAMPP and Wordpress
- GUI Available for Apache Camel
- Reduce Server Load for Apache and PHP Websit...
- Creating a VAMP (Vista, Apache, MySQL, PHP) ...
- Putting Apache in Jail
- Containing Intrusions in Apache
- Server Limits for Apache Security
- Setting Permissions in Apache

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: