Apache
  Home arrow Apache arrow Page 5 - Apache Tapestry and Custom Components:...
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 
IBM Developerworks
 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

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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