Example usage for org.springframework.beans BeanWrapper registerCustomEditor

List of usage examples for org.springframework.beans BeanWrapper registerCustomEditor

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper registerCustomEditor.

Prototype

void registerCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath,
        PropertyEditor propertyEditor);

Source Link

Document

Register the given custom property editor for the given type and property, or for all properties of the given type.

Usage

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithString() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);/*  ww  w .j a v a  2s.  c  om*/
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", "problems");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStringArrayOfOne() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);//  ww  w. j av  a2  s  .co m
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", new String[] { "problems" });
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStringArrayOfTwo() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);//from  www.j  av a2s . c  o  m
    assertTrue("collections must exist", colMan.getCollections().size() > 0);

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", new String[] { "problems", "testdata" });
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    assertEquals(colMan.getCollections().size(), col3s.length);
    assertEquals("testdata", col3s[0].getName());
}

From source file:org.zilverline.web.TestCustomCollectionEditor.java

public void testCustomCollectionEditorWithStrings() {
    CollectionManager colMan = (CollectionManager) applicationContext.getBean("collectionMan");
    assertNotNull(colMan);/*from ww w. j a  v a2s . c om*/
    assertTrue("collections must exist", colMan.getCollections().size() > 0);
    log.debug("test");

    SearchForm sf = new SearchForm();
    BeanWrapper bw = new BeanWrapperImpl(sf);
    bw.registerCustomEditor(CollectionTriple[].class, "collections", new CustomCollectionEditor(colMan));
    try {
        bw.setPropertyValue("collections", "problems, testdata");
    } catch (BeansException ex) {
        fail("Should not throw BeansException: " + ex.getMessage());
    }
    CollectionTriple[] col3s = (CollectionTriple[]) bw.getPropertyValue("collections");
    log.debug(col3s);
    assertEquals(colMan.getCollections().size(), col3s.length);
    for (int i = 0; i < colMan.getCollections().size(); i++) {
        assertTrue(col3s[0].isSelected());
    }
}

From source file:org.springmodules.validation.valang.ValangValidator.java

public void validate(Object target, Errors errors) {

    BeanWrapper beanWrapper = (target instanceof BeanWrapper) ? (BeanWrapper) target
            : new BeanWrapperImpl(target);

    if (getCustomPropertyEditors() != null) {
        for (Iterator iter = getCustomPropertyEditors().iterator(); iter.hasNext();) {
            CustomPropertyEditor customPropertyEditor = (CustomPropertyEditor) iter.next();

            if (customPropertyEditor.getRequiredType() == null) {
                throw new IllegalArgumentException(
                        "[requiredType] is required on CustomPropertyEditor instances!");
            } else if (customPropertyEditor.getPropertyEditor() == null) {
                throw new IllegalArgumentException(
                        "[propertyEditor] is required on CustomPropertyEditor instances!");
            }/*w w  w . j  a  v a2 s.com*/

            if (StringUtils.hasLength(customPropertyEditor.getPropertyPath())) {
                beanWrapper.registerCustomEditor(customPropertyEditor.getRequiredType(),
                        customPropertyEditor.getPropertyPath(), customPropertyEditor.getPropertyEditor());
            } else {
                beanWrapper.registerCustomEditor(customPropertyEditor.getRequiredType(),
                        customPropertyEditor.getPropertyEditor());
            }
        }
    }

    for (Iterator iter = rules.iterator(); iter.hasNext();) {
        ValidationRule rule = (ValidationRule) iter.next();
        rule.validate(beanWrapper, errors);
    }
}

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

@Test
public void testStringArrayPropertyWithCustomStringDelimiter() throws Exception {
    PropsTester pt = new PropsTester();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(pt);
    bw.registerCustomEditor(String[].class, "stringArray", new StringArrayPropertyEditor("-"));
    bw.setPropertyValue("stringArray", "a1-b2");
    assertTrue("stringArray length = 2", pt.stringArray.length == 2);
    assertTrue("correct values", pt.stringArray[0].equals("a1") && pt.stringArray[1].equals("b2"));
}

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  ww  w  .j av  a 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   www.  j  a va 2  s.  com
        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:net.sf.juffrou.reflect.spring.BeanWrapperTests.java

@Test
public void testCollectionsWithStringValuesAndCustomEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
    bw.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

    bw.setPropertyValue("set", "set1 ");
    bw.setPropertyValue("sortedSet", "sortedSet1");
    bw.setPropertyValue("list", "list1 ");
    assertEquals(1, tb.getSet().size());
    assertTrue(tb.getSet().contains("set1"));
    assertEquals(1, tb.getSortedSet().size());
    assertTrue(tb.getSortedSet().contains("sortedSet1"));
    assertEquals(1, tb.getList().size());
    assertTrue(tb.getList().contains("list1"));

    bw.setPropertyValue("list", Arrays.asList(new String[] { "list1 " }));
    assertTrue(tb.getList().contains("list1"));
}

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

@Test
public void testStringPropertyWithCustomEditor() throws Exception {
    TestBean tb = new TestBean();
    BeanWrapper bw = new JuffrouSpringBeanWrapper(tb);
    bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() {
        @Override/* w  ww  . j a v a 2s .co m*/
        public void setValue(Object value) {
            if (value instanceof String[]) {
                setValue(StringUtils.arrayToDelimitedString(((String[]) value), "-"));
            } else {
                super.setValue(value != null ? value : "");
            }
        }
    });
    bw.setPropertyValue("name", new String[] {});
    assertEquals("", tb.getName());
    bw.setPropertyValue("name", new String[] { "a1", "b2" });
    assertEquals("a1-b2", tb.getName());
    bw.setPropertyValue("name", null);
    assertEquals("", tb.getName());
}