Apache
  Home arrow Apache arrow Page 2 - Making a CelebrityCollector with Apache Tapestry: the For Component
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

Making a CelebrityCollector with Apache Tapestry: the For Component
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 25
    2007-05-09


    Table of Contents:
  • Making a CelebrityCollector with Apache Tapestry: the For Component
  • Creating an object model
  • Creating pages
  • The For component

  • 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


    Making a CelebrityCollector with Apache Tapestry: the For Component - Creating an object model
    ( Page 2 of 4 )

    First of all, we need some class to represent a celebrity; it would be natural to name it Celebrity. For the first iteration of the project, this class will have only five properties:

    • A numeric ID (an int);
    • First name (a String);
    • Last name (a String);
    • Date of birth (a Date);
    • Occupation (a String).

    We need to have this numeric ID because, well, theoretically speaking it is not against the laws of nature to have two celebrities named John Smith born on the same day who became famous in the same occupation. But even if that happened, we would have this ID as a way to distinguish between them. In a real life application, this could be a primary key in a database.

    Create a new class named Celebrity (right-click the earlier created com.devshed.tapestry.celebrities package, New -> Java Class…) and add to it five private members:

    private int id;

    private String firstName;

    private String lastName;

    private Date dateOfBirth;

    private String occupation;

    Again, make use of this handy shortcut: after typing “Date” press Ctrl-Space and select java.util.Date class.

    Then allow NetBeans to create getter and setter methods: right-click somewhere in your code and choose Refactor -> Encapsulate Fields… Make sure that everything is selected as the picture shows:

    Press Next, and NetBeans will create an overview of the changes it is going to make and show it in the Refactoring view below the code editor. Just press Do Refactoring there, and you will see getters and setters added to the Celebrity class.

    Now, we need to have a source of data that will give us a number of Celebrity objects. In a real life application, this would be a database, but for now let’s leave everything as simple as possible and use a mock data source – a simple Java class that creates a List of Celebrity objects and provides methods to retrieve either the whole list or just one specified object. Later, we might replace the mock data source with a real one.

    Let’s add a DataSource class to the existing Java package. There is nothing Tapestry-specific or terribly clever in this class, so you can simply copy the code:

    package com.devshed.tapestry.celebrities;

     

    import java.util.ArrayList;

    import java.util.Calendar;

    import java.util.Iterator;

    import java.util.List;

     

    public class DataSource {

       

        List list = new ArrayList();

       

        /** Creates a new instance of DataSource */

        public DataSource() {

           

            Calendar calendar = Calendar.getInstance();

           

           

            Celebrity fonda = new Celebrity();

            fonda.setFirstName("Jane");

            fonda.setLastName("Fonda");

            fonda.setOccupation("Actress");

            calendar.set(1937, 11, 21);

            fonda.setDateOfBirth(calendar.getTime());

            fonda.setId(1);

           

            list.add(fonda);

           

            Celebrity gallo = new Celebrity();

            gallo.setFirstName("Ernest");

            gallo.setLastName("Gallo");

            gallo.setOccupation("Wine-maker");

            calendar.set(1909, 2, 18); // Months are 0-based (2 is March)

            gallo.setDateOfBirth(calendar.getTime());

            gallo.setId(2);

           

            list.add(gallo);

           

            Celebrity gates = new Celebrity();

            gates.setFirstName("Bill");

            gates.setLastName("Gates");

            gates.setOccupation("Programmer ;)");

            calendar.set(1955, 9, 28);

            gates.setDateOfBirth(calendar.getTime());

            gates.setId(3);

           

            list.add(gates);

           

            Celebrity jolie = new Celebrity();

            jolie.setFirstName("Angelina");

            jolie.setLastName("Jolie");

            jolie.setOccupation("Actress");

            calendar.set(1975, 5, 4);

            jolie.setDateOfBirth(calendar.getTime());

            jolie.setId(4);

           

            list.add(jolie);       

           

        }

       

        public List getCelebrities() {

            return list;

        }

       

        public Celebrity getCelebrityById(int id) {

           

            for (Iterator i = list.iterator(); i.hasNext();) {

                Celebrity celebrity = (Celebrity)i.next();

                if (celebrity.getId() == id) return celebrity;

            }

           

            return null;

        }      

    }

    This class simply creates a List of a few celebrities in its constructor using some hard-coded data. We can obtain the whole lot by using the getCelebrities() method, or retrieve a single celebrity by specifying his or her ID and using the getCelebrityById() method.

    Now that we have all supporting code ready, we can begin converting page mock ups into Tapestry templates.



     
     
    >>> 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