FormUtil addendum

Franck Routier <[email protected]> Thu, 15 Sep 2005 17:52:51 +0200
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------030007040005010507000405
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

what about adding a new method to FormUtil, similar to 
FormUtil.repopulate(FormMap fm, BComponent bc) but with a slightly 
different signature :  FormUtil.repopulate(Map pm, BComponent bc)

pm would be a plain map containing key / value instead of FormElement. 
This would allow to have a generic (re)population, either from 
previously typed in values (from a FormMap), but also initially from a 
plain map (say a value object coming from the business tier).

The code could be as in the file attached.

Any comments on that ? (maybe there is a much better approch ?)

Regards,

Franck







--------------030007040005010507000405
Content-Type: text/plain;
 name="FormUtil-addendum.txt"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="FormUtil-addendum.txt"

    /**
     * Given a BComponent and a reference to a FormMap, this method will automatically
     * repopulate the form value into the component. The way it works is fairly simply: 
     * you create the appropriate component and name it - the component name will be used
     * to locate the appropriate form element and repopulate the component based on the 
     * form element value.
     *
     * @param fm the backing form map
     * @param bcomp the component to be repopulated
     * @since csc_110304_1
     */
    public static void repopulate(Map aMap, BComponent bcomp) {

        String key = bcomp.getName();
        if (key==null) return;
	if (aMap == null) return;
        
        Object val = aMap.get(key);

        if (bcomp instanceof BToggleButton) {
            ((BToggleButton) bcomp).setSelected(Boolean.TRUE.equals(val));
            
        } else if ((val!=null) && (bcomp instanceof BInput) && (((BInput) bcomp).getValue()==null)) {
            ((BInput) bcomp).setValue(val);
        } else if ((val!=null) && (bcomp instanceof BSelect) && (((BSelect) bcomp).getSelectedIndex()==-1)) {
            ListModel lm = ((BSelect) bcomp).getModel();
            if (lm!=null) {
                for (int i=0, max=lm.getSize(); i<max; i++) {
                    Object item = lm.getItemAt(i);
                    boolean match = (item instanceof ItemMap && CollectionsUtil.checkEquals(val, ((ItemMap) item).getKey()));
                    if (!match) match = (item instanceof ItemMap && CollectionsUtil.checkEquals(val, ((ItemMap) item).getValue()));
                    if (!match) match = CollectionsUtil.checkEquals(val, item);
                    if (match) {
                        ((BSelect) bcomp).setSelectedIndex(i);
                        break;
                    }
                }
            }
        }
    }

--------------030007040005010507000405
Content-Type: text/plain; name="message-footer.txt"
Content-Disposition: inline; filename="message-footer.txt"
Content-Transfer-Encoding: 8bit



--

Barracuda mailing list

[email protected]

http://www.objectweb.org/wws/lists/projects/barracuda


--------------030007040005010507000405--