Apache
  Home arrow Apache arrow Page 3 - PropertySelection and IPropertySelecti...
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

PropertySelection and IPropertySelectionModel in Apache Tapestry
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 18
    2007-06-12

    Table of Contents:
  • PropertySelection and IPropertySelectionModel in Apache Tapestry
  • Configuring PropertySelection
  • Working on the Model
  • One More Detail
  • So what have we done?

  • 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 Networking among IT Professionals. Learn more

    PropertySelection and IPropertySelectionModel in Apache Tapestry - Working on the Model
    (Page 3 of 5 )

    Let's begin by creating a new class, CelebrityModel, and let it implement the IPropertySelection interface:

    public class CelebrityModel implements IPropertySelectionModel {

     

      /** Creates a new instance of CelebrityModel */

      public CelebrityModel() {

      }

    }

    At first, NetBeans will not recognize the interface, but press Alt-Shift-F, and it will add an appropriate import statement automatically. Now NetBeans will complain that the class doesn't implement the methods of the interface, and there will be a little bulb to the left of the underlined line of code. Click that bulb, and the IDE will suggest adding the required methods. Let it do so, and you will see that five methods were added to the new class. We need to slightly modify the code to get rid of red underlining. Make it look like so:

    public class CelebrityModel implements IPropertySelectionModel {

     

     

      public CelebrityModel() {

      }

     

      public int getOptionCount() {

        return 0;

      }

     

      public Object getOption(int i) {

        return null;

      }

     

      public String getLabel(int i) {

        return null;

      }

     

      public String getValue(int i) {

        return null;

      }

     

      public Object translateValue(String value) {

        return null;

      }

     

    }

    Now let's modify this code to make it work with our celebrities, and learn what different methods do along the way.

    First of all, we need to pass our list of celebrities to the model. This is convenient to do in the constructor. Add a private List property and modify the constructor to look like this:

    private List celebrities;

     

    public CelebrityModel(List celebrities) {

      this.celebrities = celebrities;

    }

    Now let me tell you about the getOptionCount(() method. It serves to tell the PropertySelection how many options it is going to display. All we need to do is return the size of the list of celebrities:

    public int getOptionCount() {

      return celebrities.size();

    }

    The next method, getOption(int i), takes the index of an option and returns the object corresponding to this index. Say we've told PropertySelection in the previous method that there are four options to display. It will number them, starting from zero: 0, 1, 2, 3. Now it asks the model: which Celebrity object corresponds to, say, option 2? We can answer this question easily:

    public Object getOption(int i) {

      return celebrities.get(i);

    }

    The next method, getLabel(int i), is equally simple. It answers a PropertySelection's question like "What should be the label for option number 0?" Say we want to display the full names of celebrities as labels, in which case the answer will be:

    public String getLabel(int i) {

      Celebrity c = (Celebrity) celebrities.get(i);

      return c.getFirstName() + " " + c.getLastName();

    }

    The next method, getValue(), answers a question like "What should contain the value attribute for option number 3?" The simplest possible approach is to use the index itself as a value -- just convert it into String:

    public String getValue(int i) {

      return Integer.toString(i);

    }

    The final method, translateValue(), will work when the user has made some selection and submitted the form. His or her choice will arrive at the server as the contents of the value attribute of the selected option. As we have specified in the previous method, this is going to be a string representation of the option's index, like "0," or "1," or "2." The translateValue() method should define how to find the appropriate Celebrity object using the submitted value, which is quite simple in our case:

    public Object translateValue(String value) {

      int index = Integer.parseInt(value);

      return celebrities.get(index);

    }

    Finally, we need to change the getCelebrityModel() method of CelebritiesList page so that it returns an instance of the just created CelebrityModel class:

    public IPropertySelectionModel getCelebrityModel() {

      return new CelebrityModel(getCelebrities());

    }

    You can run the application and see that it works, and the drop-down list is filled with celebrities from the DataSource. When we press the Save button, the selected celebrity (i.e. a Celebrity object filled with his or here data) is stored as an Application State Object, but at the moment we can't see that and can't be sure of that. To complete what we've planned to do, we need to display the Celebrity of the Week at the Home page, and perhaps display that page after the selection was made.

    Add the following piece of HTML to the contents of the Home.html file, wherever you see as appropriate:

    <p><strong>Celebrity of the Week</strong>:

          <span jwcid="celebrityOfTheWeek">John Smith</span></p>

    The component we are using here is an Insert. Let's configure it in the Home.page specification:

    <component id="celebrityOfTheWeek" type="Insert">

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

    </component>

    And then add to the Home class a method returning the full name:

    public String getFullName() {

      Celebrity c = getTheCelebrity();

      return c.getFirstName() + " " + c.getLastName();

    }

    As you see, we also need a method in the Home class which will give us a reference to the celebrityOfTheWeek ASO:

    @InjectState("celebrityOfTheWeek")

    public abstract Celebrity getCelebrityOfTheWeek();

    Finally, after the Celebrity of the Week was selected at the CelebritiesList page, we want to show the Home page, and this turns our attention back to the CelebritiesList class. This class should have some listener method invoked when the user presses the Save button. We don't have a listener yet, so let's add it. Here is the addition to the CelebritiesList.html template:

    <form action="" jwcid="@Form"

            listener="listener:onSaveCelebrityOfTheWeek">

      <select jwcid="selectCelebrity">

        <option value="1">Angelina Jolie</option>

        <option value="2">Bill Gates</option>

      </select>

      <input type="submit" value="Save"/>

    </form>

    And here is the listener method itself:

    public String onSaveCelebrityOfTheWeek() {

      return "Home";

    }

    More Apache Articles
    More By Alexander Kolesnikov


       · PropertySelection is a very often used component, and it is important to learn how...
       · Hello:)is it possible to internationalize the values of the PropertySelection?
       · Yes, there are a few ways how you can do that, depending on the task at hand. I was...
       · Thanks :)I'm looking forward to the issue 14. :)
       · May be I missed something, but it seems there is a small issue with the sample you...
       · Well, I understand why you are asking this. Let's look at the whole code of the...
       · Somehow, the closing bracket of the annotation has turned into a winking smiley, but...
       · Hi, great tutorial at all, I'm also looking forward to article 14. Worked this...
     

       

    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 4 hosted by Hostway