Apache
  Home arrow Apache arrow Page 5 - Apache Tapestry and Custom Components: DateInput
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 11
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Apache Tapestry and Custom Components: DateInput - Implementing PageBeginRenderListener
    ( Page 5 of 5 )

    To manipulate dates, we use an instance of java.util.Calendar class and simply get or set appropriate values using the appropriate keys (like Calendar.YEAR). In the very beginning, we need to set this Calendar to some initial value. In the simplest case, we just set it to the value received from the parameter (through getDate() method). Quite often, however, the initial value can be null. What should we do in this case? One reasonable option is to display the current date, hence the code:

    c.setTime(getDate() == null ? new Date() : getDate());

    But when should we run this line of code? Perhaps just before the DateInput component will have to render itself, as it will need to know which values to display. We can imagine that our component tells the page: “Hey, Page, could you please let me know when you’re going to render me? I am going to do some initialization just before that.” Translated into Tapestry API, this will make our component a PageBeginRenderListener (you see, the component class implements this interface).

    The PageBeginRenderListener interface has only one method, pageBeginRender(), and this method will be invoked by the page just before it begins rendering itself and any components contained by itself.

    Now that we’ve sorted out initialization, the PropertySelection components displaying the day, month and year can obtain their values from the Calendar and report whatever was selected by the user back to the Calendar.

    When the page is submitted to the server, the setters will work exactly in the order in which the components are placed: setMonth(), then setDay() and finally setYear(). Since setYear() runs last, we made it responsible for setting the value of the date parameter to whatever was put into the Calendar by this time:

    public void setYear(int year) {

       c.set(Calendar.YEAR, year);

       setDate(c.getTime());

    }

    This completes the explanation of how the DateInput component works. The explanation was somewhat verbose, but only because I wanted to explain every detail. All we need to do now is put this component on some page and test it.

    One possible solution is to replace the existing DatePicker component. It is used on the AddCelebrity page and configured like this:

    <component id="dateOfBirth" type="DatePicker">

       <binding name="value" value="dateOfBirth"/>

    </component>

    To make the change, we just edit the type of component and the name for its binding:

    <component id="dateOfBirth" type="DateInput">

       <binding name="date" value="dateOfBirth"/>

    </component>

    Now you can run the application, add some new celebrity, and the date-related functionality should work exactly as it worked before, with the only difference that we don’t need any JavaScript harness now and there is no way to misinterpret user input.

    To summarize, I think you will agree that creating a fully functional custom component wasn’t more difficult than creating a simple Tapestry page. We did have to write a number of methods, but most of them contained just one line of code (and the remaining few contained two lines of code).

    We could further enhance the DateInput component – for example, add a parameter for disabling it, or enable tabulation through its drop-down lists. This however has to be left until a later article, or you might want to try and do this yourself.

    What comes next

    In the next article I am planning to show you how using the NBTapestry module for NetBeans can make our life somewhat easier. I also plan to demonstrate different ways of submitting forms in Tapestry, something I have been putting off since the very beginning of the series.



     
     
    >>> More Apache Articles          >>> More By Alexander Kolesnikov
     

       

    APACHE ARTICLES

    - Creating a VAMP (Vista, Apache, MySQL, PHP) ...
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT