Announcement Announcement Module
Collapse
No announcement yet.
Creating a Custom PropertyEditor Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a Custom PropertyEditor

    As per a tip from Ollie

    Also a few suggestions:

    * For the special date pickers use a custom PropertyEditor registered for the Date class. That way they get automatically inserted where needed.
    * Let the binding framework create all of your controls rather than doing it yourself. In your initComponents() method you are creating whole lot of components expect for the JFormattedTextFields there's not need. In fact you can even avoid explicitly creating the JFormattedTextFields by registering custom PropertyEditors for the fields that need the special JFormattedTextField formatting.

    HTH

    Ollie
    in this thread http://forum.springframework.org/showthread.php?t=15213 I'm trying to create a custom PropertyEditor. The one for the date picker got way too complex too quickly so I scaled my effords back some. Now the one I'm writeing is a simple JComboBox that uses a class called PaceValidValue as its list object which is nothing more that a name value pair of which when its toString() method is called the value is returned.

    The problem I'm having is that the PropertyEditor I created does not seam to bind to the form model and I can not see why it does not. Here is the code:

    Code:
    /**
     *
     * @author stavares
     */
    public class PaceValidValuePropertyEditor extends PropertyEditorSupport 
            implements ChangeListener, ActionListener
    {
        private JComboBox jComboBox;
        private List theList;    
        
        /** Creates a new instance of PaceValidValuePropertyEditor */
        public PaceValidValuePropertyEditor(List theList)
        {        
            jComboBox = new JComboBox(theList.toArray());        
            jComboBox.addActionListener(this);
        }
        
        public boolean supportsCustomEditor()
        {
            return true;
        }
        
        public Component getCustomEditor()
        {        
            return jComboBox;
        }
        
        public void setValue(Object value)
        {
            PaceValidValue paceValidValue = (PaceValidValue)value;
            jComboBox.setSelectedItem(paceValidValue);        
        }
        
        public Object getValue()
        {
            return jComboBox.getSelectedItem();
        }
            
        public void stateChanged(ChangeEvent e)
        {
            firePropertyChange();
        }
        
        public void actionPerformed(ActionEvent e)
        {
            JComboBox cb = (JComboBox)e.getSource();        
            this.setValue(cb.getSelectedItem());        
        }
    }
    and here is the form that is using the above PropertyEditor

    Code:
    public class CreateOffenderFormPageOffender extends AbstractForm
    {
        public static final String OFFENDER_PAGE = "offenderPage";
        private IdentificationSupport identificationSupport;
        
        /** Creates a new instance of CreateOffenderFormPageOffender */
        public CreateOffenderFormPageOffender(FormModel formModel)
        {
            super(formModel, OFFENDER_PAGE);
        }
        
        protected JComponent createFormControl()
        {
            JComponent[] jcomponent;
            JLabel jlabel;
            JComboBox jcomboBox;
            
            // builder.addSeparator("separator.addressDetails");        
            TableFormBuilder formBuilder = new TableFormBuilder(getFormModel());
            
            registerCustomEditors();
            
            formBuilder.add("iincarcerationNumber");
            formBuilder.add("isrg");
            formBuilder.add("ijobCode","colspan=1");
            formBuilder.row();
            formBuilder.add("iinmateId");
            formBuilder.add("ipinNo");
            formBuilder.add("ibciNumber");
            formBuilder.add("icommConfEligDate");
            formBuilder.row();
            formBuilder.add("ifirstName");
            formBuilder.add("iminit");
            formBuilder.add("ilastName");
            formBuilder.add("isuffix");
            formBuilder.row();
            formBuilder.add("idob","colspan=1");
                    
            jcomponent = formBuilder.add("isexPvv","colspan=1");        
            jcomboBox = (JComboBox)jcomponent[1];
            jcomboBox.setEditable(false);
            
            jcomponent = formBuilder.add("iclassCodePvv","colspan=3");        
            jcomboBox = (JComboBox)jcomponent[1];
            jcomboBox.setEditable(false);        
                    
            return formBuilder.getForm();
        }
        
        private void registerCustomEditors()
        {
            identificationSupport = (IdentificationSupport)this.getFormObject();
            
            getFormModel().registerCustomEditor("isexPvv", 
                        new PaceValidValuePropertyEditor(identificationSupport
                        .getIsexList()));
            
            getFormModel().registerCustomEditor("iclassCodePvv", 
                        new PaceValidValuePropertyEditor(identificationSupport
                        .getIclassCodeList()));
        }
    }
    The reason I belive its not getting binded is I have a validation rule on each of the two fields using the PropertyEditor of requried yet no matter if I selected an Item out of the list of the comboBox the fields' invalid decorator icon remains on the screen.

    So the Question is what am I doing wrong????
    Last edited by robyn; May 16th, 2006, 04:15 AM.

  • #2
    So the Question is what am I doing wrong????
    PropertyEditors registered directly with a form model are only used as simple type converters. If they support custom editors they are ignored....

    Though this is all academic as the new code base does not use custom property editors at all. I suggest you do an update and then have a look at the CustomDatePickerBinder in the pet clinic sample.

    Ollie

    Comment

    Working...
    X