Apache
  Home arrow Apache arrow Page 2 - Tapestry and AJAX: Autocompleter and I...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Tapestry and AJAX: Autocompleter and InlineEditBox
By: Alexander Kolesnikov
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2007-06-18

    Table of Contents:
  • Tapestry and AJAX: Autocompleter and InlineEditBox
  • The IAutocompleteModel interface
  • Enabling JavaScript
  • Meet InlineEditBox

  • 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


    Tapestry and AJAX: Autocompleter and InlineEditBox - The IAutocompleteModel interface


    (Page 2 of 4 )

    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


       · I think this is one of the greatest features of Tapestry: you can make use of AJAX...
       · Very cool!Autocompleter is awesome but the InlineEditBox didn't seem to work...
       · Well, I do have InlineEdit running successfully, as screenshots show. Did you try...
       · Yes. I download the src, create a new net beans web app from existing source, add...
       · Okay, I've got a demonstration for you. I have deployed CelebrityCollector to my...
       · I tested it out in IE 6 and Firefox 2 on your site (sundraw) and it behaves (for me)...
       · Ufff... So this is what you meant by not working InlineEdit. Well, the problem you...
     

       

    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-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway