Differences between revisions 2 and 3
Revision 2 as of 2007-11-25 19:56:08
Size: 4554
Editor: SvenHomburg
Comment:
Revision 3 as of 2009-09-20 23:20:09
Size: 4554
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

in my applications many times i use pages with the grid component and i am too lazy ;) to write in every page a setter and getter methode for grid's row parameter. so i help my self with the following classes and code segments...

the annotation ...

   1 /**
   2  * generates setter/getter for a grid bean.
   3  *
   4  * @author <a href="mailto:shomburg@hsofttec.com">shomburg</a>
   5  * @version $Id: GridRowBean.java 41 2007-11-25 18:54:58Z shomburg $
   6  */
   7 @Target(FIELD)
   8 @Retention(RUNTIME)
   9 @Documented
  10 public @interface GridRowBean
  11 {
  12 }

the worker, it adds the setter and getter for the grid bean.

   1 /**
   2  * add setter/getter methodes.
   3  *
   4  * @author <a href="mailto:shomburg@hsofttec.com">shomburg</a>
   5  * @version $Id: GridRowBeanWorker.java 41 2007-11-25 18:54:58Z shomburg $
   6  */
   7 public class GridRowBeanWorker implements ComponentClassTransformWorker
   8 {
   9     /**
  10      * Invoked to perform a transformation on an as-yet unloaded component class, represented by the
  11      * {@link org.apache.tapestry.services.ClassTransformation} instance. In some cases, 
  12      * the worker may make changes to the
  13      * component model -- for example, a worker that deals with parameters may update the model to
  14      * reflect those parameters.
  15      */
  16     public void transform(ClassTransformation transformation, MutableComponentModel model)
  17     {
  18         List<String> names = transformation.findFieldsWithAnnotation(GridRowBean.class);
  19 
  20         if (names.isEmpty())
  21             return;
  22 
  23         for (String name : names)
  24         {
  25             addGridRowBeanGetter(transformation, name);
  26             addGridRowBeanSetter(transformation, name);
  27         }
  28     }
  29 
  30     private void addGridRowBeanGetter(ClassTransformation transformation, String fieldName)
  31     {
  32         String fieldType = transformation.getFieldType(fieldName);
  33         String methodName = "get" + StringUtils.capitalize(InternalUtils.stripMemberPrefix(fieldName));
  34 
  35         TransformMethodSignature sig = 
  36                        new TransformMethodSignature(Modifier.PUBLIC, fieldType, methodName, null, null);
  37 
  38         BodyBuilder builder = new BodyBuilder();
  39         builder.begin();
  40         builder.addln("return %s;", fieldName);
  41         builder.end();
  42 
  43         transformation.addMethod(sig, builder.toString());
  44     }
  45 
  46     private void addGridRowBeanSetter(ClassTransformation transformation, String fieldName)
  47     {
  48         String fieldType = transformation.getFieldType(fieldName);
  49         String methodName = "set" + StringUtils.capitalize(InternalUtils.stripMemberPrefix(fieldName));
  50 
  51         TransformMethodSignature sig = 
  52                    new TransformMethodSignature(Modifier.PUBLIC, "void", 
  53                                  methodName, new String[]{fieldType}, null);
  54 
  55         BodyBuilder builder = new BodyBuilder();
  56         builder.begin();
  57         builder.addln("%s = $1;", fieldName);
  58         builder.end();
  59 
  60         transformation.addMethod(sig, builder.toString());
  61     }
  62 }

... contribute the new worker to Tapestry's ComponentClassTransformWorker ...

   1     /**
   2      * Adds a number of standard component class transform workers:
   3      * <ul>
   4      * <li>GridRowBean -- generates setter/getter for a grid bean</li>
   5      * </ul>
   6      */
   7     public static void contributeComponentClassTransformWorker(
   8                                 OrderedConfiguration<ComponentClassTransformWorker> configuration)
   9     {
  10         configuration.add("GridRowBean", new GridRowBeanWorker(), "after:Inject*");
  11     }

... mark the bean _client with @GridRowBean annotation ...

   1     @GridRowBean
   2     private Client _clientRow;
   3 
   4     /**
   5      * get the grid data from datasource.
   6      *
   7      * @return grid data from datasource
   8      */
   9     public GridDataSource getGridSource()
  10     {
  11         ClientDAO entityDAO = (ClientDAO) getDefaultDAO(Client.class);
  12         return new HibernateDataSource(entityDAO, "FROM Client");
  13     }

... tell the grid component wich property should use for the row parameter ...

    <table t:type="Grid" model="gridModel" source="gridSource" row="clientRow">

        <t:parameter name="recIdCell">
            <span t:type="core5/link/EditAction" context="clientRow.recId"/>
        </t:parameter>

        <t:parameter name="addressCityCell">
            ${clientRow.address.country.isoSign}
            ${clientRow.address.zip}
            ${clientRow.address.city}
        </t:parameter>

    </table>

Tapestry5ManipulateClassesAtRuntime (last edited 2009-09-20 23:20:09 by localhost)