Example usage for java.beans PropertyEditorSupport PropertyEditorSupport

List of usage examples for java.beans PropertyEditorSupport PropertyEditorSupport

Introduction

In this page you can find the example usage for java.beans PropertyEditorSupport PropertyEditorSupport.

Prototype

public PropertyEditorSupport() 

Source Link

Document

Constructs a PropertyEditorSupport object.

Usage

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithTypeConversion() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
        @Override//ww w .j  a v a  2  s  . c  o m
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "rob");
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get("key1")).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get("key2")).getName());

    pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "");
    try {
        bw.setPropertyValues(pvs);
        fail("Should have thrown TypeMismatchException");
    } catch (PropertyBatchUpdateException ex) {
        PropertyAccessException pae = ex.getPropertyAccessException("map[key2]");
        assertTrue(pae instanceof TypeMismatchException);
    }
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithUnmodifiableMap() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
        @Override/*w w w.  ja  v  a  2 s  . c  om*/
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    Map<Integer, String> inputMap = new HashMap<Integer, String>();
    inputMap.put(new Integer(1), "rod");
    inputMap.put(new Integer(2), "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", Collections.unmodifiableMap(inputMap));
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName());
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testMapAccessWithCustomUnmodifiableMap() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(bean);
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
        @Override/*from   w  w  w  .j av a  2 s  .c  o m*/
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    Map<Object, Object> inputMap = new HashMap<Object, Object>();
    inputMap.put(new Integer(1), "rod");
    inputMap.put(new Integer(2), "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", new ReadOnlyMap(inputMap));
    bw.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) bean.getMap().get(new Integer(1))).getName());
    assertEquals("rob", ((TestBean) bean.getMap().get(new Integer(2))).getName());
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testLargeMatchingPrimitiveArrayWithSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {
        @Override/*from w  w w .ja va 2  s  . c o  m*/
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(1, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

From source file:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testLargeMatchingPrimitiveArrayWithIndexSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {
        @Override/*from   w w w  .j  av a  2s  .  co  m*/
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

From source file:org.broadleafcommerce.core.web.controller.account.AbstractCustomerAddressController.java

/**
 * Initializes some custom binding operations for the managing an address.
 * More specifically, this method will attempt to bind state and country
 * abbreviations to actual State and Country objects when the String
 * representation of the abbreviation is submitted.
 *
 * @param request/* w  w  w. j a  v a  2  s .  c  o  m*/
 * @param binder
 * @throws Exception
 */
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {

    /**
     * @deprecated - address.setState() is deprecated in favor of ISO standardization
     * This is here for legacy compatibility
     */
    binder.registerCustomEditor(State.class, "address.state", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                State state = stateService.findStateByAbbreviation(text);
                setValue(state);
            } else {
                setValue(null);
            }
        }
    });

    /**
     * @deprecated - address.setCountry() is deprecated in favor of ISO standardization
     * This is here for legacy compatibility
     */
    binder.registerCustomEditor(Country.class, "address.country", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                Country country = countryService.findCountryByAbbreviation(text);
                setValue(country);
            } else {
                setValue(null);
            }
        }
    });

    binder.registerCustomEditor(ISOCountry.class, "address.isoCountryAlpha2", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                ISOCountry isoCountry = isoService.findISOCountryByAlpha2Code(text);
                setValue(isoCountry);
            } else {
                setValue(null);
            }
        }
    });

    binder.registerCustomEditor(Phone.class, "address.phonePrimary", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });

    binder.registerCustomEditor(Phone.class, "address.phoneSecondary", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });

    binder.registerCustomEditor(Phone.class, "address.phoneFax", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });
}

From source file:org.broadleafcommerce.core.web.controller.checkout.AbstractCheckoutController.java

/**
 * Initializes some custom binding operations for the checkout flow.
 * More specifically, this method will attempt to bind state and country
 * abbreviations to actual State and Country objects when the String
 * representation of the abbreviation is submitted.
 *
 * @param request//from   w w w.j  a v  a  2s.  c  o  m
 * @param binder
 * @throws Exception
 */
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    /**
     * @deprecated - address.setState() is deprecated in favor of ISO standardization
     * This is here for legacy compatibility
     */
    binder.registerCustomEditor(State.class, "address.state", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                State state = stateService.findStateByAbbreviation(text);
                setValue(state);
            } else {
                setValue(null);
            }
        }
    });

    /**
     * @deprecated - address.setCountry() is deprecated in favor of ISO standardization
     * This is here for legacy compatibility
     */
    binder.registerCustomEditor(Country.class, "address.country", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                Country country = countryService.findCountryByAbbreviation(text);
                setValue(country);
            } else {
                setValue(null);
            }
        }
    });

    binder.registerCustomEditor(ISOCountry.class, "address.isoCountryAlpha2", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                ISOCountry isoCountry = isoService.findISOCountryByAlpha2Code(text);
                setValue(isoCountry);
            } else {
                setValue(null);
            }
        }
    });

    binder.registerCustomEditor(Phone.class, "address.phonePrimary", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });

    binder.registerCustomEditor(Phone.class, "address.phoneSecondary", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });

    binder.registerCustomEditor(Phone.class, "address.phoneFax", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            Phone phone = new PhoneImpl();
            phone.setPhoneNumber(text);
            setValue(phone);
        }

    });
}

From source file:org.openmrs.module.lagtimereport.web.controller.LagTimeReportController.java

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(LagTimeReportSetup.class, new LagTimeReportSetupEditor());
    binder.registerCustomEditor(Form.class, new PropertyEditorSupport() {

        @Override/*from w  w  w  . java 2 s  .  c  o  m*/
        public void setAsText(String text) {
            Form form = formService.getForm(Integer.parseInt(text));
            super.setValue(form);
        }
    });
}

From source file:org.sparkcommerce.core.web.controller.account.SparkManageCustomerAddressesController.java

/**
 * Initializes some custom binding operations for the managing an address. 
 * More specifically, this method will attempt to bind state and country
 * abbreviations to actual State and Country objects when the String 
 * representation of the abbreviation is submitted.
 * // w w w.j  a v a2 s  . c om
 * @param request
 * @param binder
 * @throws Exception
 */
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {

    binder.registerCustomEditor(State.class, "address.state", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            State state = stateService.findStateByAbbreviation(text);
            setValue(state);
        }
    });

    binder.registerCustomEditor(Country.class, "address.country", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            Country country = countryService.findCountryByAbbreviation(text);
            setValue(country);
        }
    });

    binder.registerCustomEditor(Phone.class, "address.phonePrimary", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (!StringUtils.isBlank(text)) {
                Phone phone = new PhoneImpl();
                phone.setPhoneNumber(text);
                setValue(phone);
            } else {
                setValue(null);
            }
        }
    });
}

From source file:org.sparkcommerce.core.web.controller.checkout.AbstractCheckoutController.java

/**
 * Initializes some custom binding operations for the checkout flow.
 * More specifically, this method will attempt to bind state and country
 * abbreviations to actual State and Country objects when the String
 * representation of the abbreviation is submitted.
 *
 * @param request//from   ww  w .j  a  v  a2 s  . c om
 * @param binder
 * @throws Exception
 */
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    binder.registerCustomEditor(State.class, "address.state", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            if (StringUtils.isNotEmpty(text)) {
                State state = stateService.findStateByAbbreviation(text);
                setValue(state);
            } else {
                setValue(null);
            }
        }
    });

    binder.registerCustomEditor(Country.class, "address.country", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            Country country = countryService.findCountryByAbbreviation(text);
            setValue(country);
        }
    });
    binder.registerCustomEditor(Phone.class, "address.phonePrimary", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) {
            if (!StringUtils.isBlank(text)) {
                Phone phone = new PhoneImpl();
                phone.setPhoneNumber(text);
                setValue(phone);
            } else {
                setValue(null);
            }
        }

    });
}