Home arrow Apache arrow Page 3 - PropertySelection and IPropertySelectionModel in Apache Tapestry

Working on the Model - Apache

We already encountered PropertySelection in one of the previous articles so we know that it is a Tapestry component used to display a drop-down list, allowing the user to choose one of multiple options. You might think that the way in which options are provided to this component (through its model binding) is somewhat cumbersome when all you need to do is select one of a few strings. However, PropertySelection was designed with a great deal of power and flexibility in mind, and I hope that today you will appreciate this.

TABLE OF CONTENTS:
  1. PropertySelection and IPropertySelectionModel in Apache Tapestry
  2. Configuring PropertySelection
  3. Working on the Model
  4. One More Detail
  5. So what have we done?
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 24
June 12, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: