views:

278

answers:

2

I started using netbeans to design forms to edit the instances of various classes I have made in a small app I am writing. Basically, the app starts, an initial set of objects is selected from the DB and presented in a list, then an item in the list can be selected for editing. When the editor comes up it has form fields for many of the data fields in the class.

The problem I run into is that I have to create a controller that maps each of the data elements to the correct form element, and create an inordinate number of small conversion mapping lines of code to convert numbers into strings and set the correct element in a dropdown, then another inordinate amount of code to go back and update the underlying object with all the values from the form when the save button is clicked.

My question is; is there a more directly way to make the editing of the form directly modify the contents of my class instance? I would like to be able to have a default mapping "controller" that I can configure, then override the getter/setter for a particular field if needed. Ideally, there would be standard field validation for things like phone numbers, integers, floats, zip codes, etc...

I'm not averse to writing this myself, I would just like to see if it is already out there and use the right tool for the right job.

+2  A: 

There are many approaches,

JBoss Seam, for instance, uses an Ant (And whether you do not know, NetBeans uses Ant behind the scenes) tool called hbmtemplate. It is a Template based Engine in which can be controlled by a user provided template or class. Along with Freemarker Template (.flt extension), it generates all of Application. If you want to see how see how Seam generates its Applications, Take a look at <SEAM_HOME>/seam-gen/view. There, you can see o lot of Freemarker Template.

Here is how Seam generates its Application

<hibernate templatepath="${templates.dir}">
    <jpaconfiguration persistenceunit="${project.name}"/>
    <classpath>
        <dirset dir="${project.home}/exploded-archives">
            <include name="*.war/WEB-INF/classes" if="project.war"/>
            <include name="*.war/WEB-INF/dev" if="project.war"/>
            <include name="*.jar" if="project.ear"/>
        </dirset>
    </classpath>
    <property key="hibernatetool.util.toolclass" value="org.jboss.seam.tool.Util"/>
        <hbmtemplate filepattern="{class-name}List.xhtml" template="view/list.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}.xhtml" template="view/view.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}.page.xml" template="view/view.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}Edit.xhtml" template="view/edit.xhtml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="{class-name}Edit.page.xml"                template="view/edit.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="${action.dir}/{class-name}List.java" template="src/EntityList.java.ftl" destdir="${project.home}/src" foreach="entity">
            <property key="actionPackage" value="${action.package}"/>
        </hbmtemplate>
        <hbmtemplate filepattern="{class-name}List.page.xml" template="view/list.page.xml.ftl" destdir="${project.home}/view" foreach="entity"/>
        <hbmtemplate filepattern="${action.dir}/{class-name}Home.java" template="src/EntityHome.java.ftl" destdir="${project.home}/src" foreach="entity">
            <property key="actionPackage" value="${action.package}"/>
        </hbmtemplate>
        <hbmtemplate filepattern="menu.xhtml" template="view/layout/menu.xhtml.ftl"  destdir="${project.home}/view/layout" foreach="entity"/>
    </hibernate>

Here goes some code, not all, from FreeMarket Template view.xhtml.ftl

<#foreach property in pojo.allPropertiesIterator>
    <#if !c2h.isCollection(property) && !isToOne(property) && property != pojo.versionProperty!>
        <#include "viewproperty.xhtml.ftl">
    </#if>
</#foreach>

I hope it can be useful to you

Arthur Ronald F D Garcia
+1  A: 

See my answer to your other question here. (In short: using beans binding would help a bit I hope)

Karussell