Home arrow Apache arrow Page 2 - Tapestry and AJAX: Autocompleter and InlineEditBox

The IAutocompleteModel interface - Apache

Although the term "AJAX" was coined not so long ago, AJAX-like functionality has already been available in Tapestry for quite some time. In Tapestry 4.0 we can use Tacos, a rich and interesting library of custom Tapestry components that includes a number of AJAX-driven components. Tapestry 4.1 has Dojo JavaScript library built into it, so it comes with some core AJAX-enabled components.

TABLE OF CONTENTS:
  1. Tapestry and AJAX: Autocompleter and InlineEditBox
  2. The IAutocompleteModel interface
  3. Enabling JavaScript
  4. Meet InlineEditBox
By: Alexander Kolesnikov
Rating: starstarstarstarstar / 11
June 18, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Create a new Java class, named, say, AutoModel, let it implement the IAutocompleteModel interface and then allow NetBeans to automatically create all the required methods (follow the steps outlined in the previous article for the IPropertySelectionModel). As a result, you should have something similar to this:

package com.devshed.tapestry.celebrities;

 

import java.util.ArrayList;

import java.util.List;

import org.apache.tapestry.dojo.form.IAutocompleteModel;

 

public class AutoModel implements IAutocompleteModel {

 

  public AutoModel() {

  }

 

  public String getLabelFor(Object obj) {

    return null;

  }

 

  public List getValues(String input) {

    return null;

  }

 

  public Object getPrimaryKey(Object obj) {

    return null;

  }

 

  public Object getValue(Object id) {

    return null;

  }

 

}

First of all, let's pass to the model a list of available Celebrity objects in its constructor, in the same way we did this for the IPropertySelectionModel. The only difference is that I decided to use the Java 5 coding style in this example:

private List<Celebrity> celebrities;

 

public AutoModel(List<Celebrity> celebrities) {

  this.celebrities = celebrities;

}

Now let's look at the methods. The first one, getLabelFor(), is self-descriptive. It accepts an object from the displayed list, in this case a Celebrity object, and should return a string to be displayed for this object by the Autocompleter. Let's just return the celebrity's surname:

public String getLabelFor(Object obj) {

  Celebrity c = (Celebrity) obj;

  return c.getLastName();

}

The next method, getValues(), is probably the most interesting, and the most Autocompleter-specific. When the user enters the first few characters of the surname, Autocompleter needs to make a request to the server behind the scenes, passing the entered characters and asking for the server to return all celebrities that match the input. In other words, the Autocompleter will ask the server to filter all available celebrities according to some criteria. Which criteria? This is exactly what we define in the getValues() method. The server will invoke this method on the Autocompleter's model and return whatever the method returns. Let's have a look at the completed method first and then discuss what it does:

public List getValues(String input) {

  List<Celebrity> list = new ArrayList<Celebrity>();

  for (Celebrity c : celebrities) {

    if (c.getLastName().toLowerCase().startsWith(

               input.toLowerCase())) {

      list.add(c);

    }

  }

  return list;

}

First of all, we are preparing a new empty list to be returned by the server:

List<Celebrity> list = new ArrayList<Celebrity>();

Than we take all the available celebrities and iterate through them using that convenient new Java 5 version of the for operator:

for (Celebrity c : celebrities) {

  ...

}

On each iteration we are checking whether the current celebrity's surname starts with the characters entered by the user. We are converting both the surname and the entered characters to lower case, so it doesn't matter which case was used for input. If the surname matches, we are adding that celebrity to the returned list:

if (c.getLastName().toLowerCase().startsWith(input.toLowerCase())) {

list.add(c);

}

The remaining two methods are simple. The getPrimaryKey() should return some unique identifier for the given Celebrity object. As Celebrity has an id property, we can simply return it:

public Object getPrimaryKey(Object obj) {

  Celebrity c = (Celebrity) obj;

  return c.getId();

}

Note that the id is of the primitive int type while the method returns an Object; that's fine from the Java 5 point of view as it will use autoboxing - i.e. automatically convert int into Integer.

Finally, in the getValue() method we are asked to find an appropriate Celebrity object by the given unique identifier. We are simply iterating through the available celebrities, and as soon as one's id matches the method's parameter, we are returning that object:

public Object getValue(Object id) {

  Integer i = (Integer) id;

 

  for (Celebrity c : celebrities) {

    if (c.getId() == i.intValue()) return c;

  }

 

  return null;

}

The last step in getting Autocompleter ready is to provide to it a model in the CelebritiesList class:

public IAutocompleteModel getAutoModel() {

  return new AutoModel(getCelebrities());

}



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