List of usage examples for java.beans PropertyEditorSupport PropertyEditorSupport
public PropertyEditorSupport()
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testStringArrayPropertyWithCustomStringEditor() throws Exception { PropsTester pt = new PropsTester(); BeanWrapper bw = new BeanWrapperImpl(pt); bw.registerCustomEditor(String.class, "stringArray", new PropertyEditorSupport() { @Override//from w w w. j av a 2 s . c o m public void setAsText(String text) { setValue(text.substring(1)); } }); bw.setPropertyValue("stringArray", new String[] { "4foo", "7fi", "6fi", "5fum" }); assertTrue("stringArray length = 4", pt.stringArray.length == 4); assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); List<String> list = new ArrayList<String>(); list.add("4foo"); list.add("7fi"); list.add("6fi"); list.add("5fum"); bw.setPropertyValue("stringArray", list); assertTrue("stringArray length = 4", pt.stringArray.length == 4); assertTrue("correct values", pt.stringArray[0].equals("foo") && pt.stringArray[1].equals("fi") && pt.stringArray[2].equals("fi") && pt.stringArray[3].equals("fum")); Set<String> set = new HashSet<String>(); set.add("4foo"); set.add("7fi"); set.add("6fum"); bw.setPropertyValue("stringArray", set); assertTrue("stringArray length = 3", pt.stringArray.length == 3); List<String> result = Arrays.asList(pt.stringArray); assertTrue("correct values", result.contains("foo") && result.contains("fi") && result.contains("fum")); bw.setPropertyValue("stringArray", "8one"); assertTrue("stringArray length = 1", pt.stringArray.length == 1); assertTrue("correct values", pt.stringArray[0].equals("one")); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testStringPropertyWithCustomEditor() throws Exception { TestBean tb = new TestBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(String.class, "name", new PropertyEditorSupport() { @Override// w w w .ja v a 2s . c om 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()); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testIntArrayPropertyWithCustomEditor() { PropsTester pt = new PropsTester(); BeanWrapper bw = new BeanWrapperImpl(pt); bw.registerCustomEditor(int.class, new PropertyEditorSupport() { @Override/*from www .ja v a 2 s . c om*/ public void setAsText(String text) { setValue(new Integer(Integer.parseInt(text) + 1)); } }); bw.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); bw.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" }); assertTrue("intArray length = 4", pt.intArray.length == 4); assertTrue("correct values", pt.intArray[0] == 4 && pt.intArray[1] == 5 && pt.intArray[2] == 2 && pt.intArray[3] == 3); bw.setPropertyValue("intArray", new Integer(1)); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", new String[] { "0" }); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); bw.setPropertyValue("intArray", "0"); assertTrue("intArray length = 4", pt.intArray.length == 1); assertTrue("correct values", pt.intArray[0] == 1); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithTypeConversion() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() { @Override//from ww w . ja v a2 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:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithUnmodifiableMap() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override/*from w w w. j a v a 2s. c o m*/ 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:org.springframework.beans.BeanWrapperTests.java
@Test public void testMapAccessWithCustomUnmodifiableMap() { IndexedTestBean bean = new IndexedTestBean(); BeanWrapper bw = new BeanWrapperImpl(bean); bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() { @Override//from w ww . java 2 s .com 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:org.springframework.beans.BeanWrapperTests.java
@Test public void testLargeMatchingPrimitiveArrayWithSpecificEditor() { PrimitiveArrayBean tb = new PrimitiveArrayBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(int.class, "array", new PropertyEditorSupport() { @Override/*from w w w .j a va 2 s.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(1, tb.getArray()[0]); assertEquals(1, tb.getArray()[1]); }
From source file:org.springframework.beans.BeanWrapperTests.java
@Test public void testLargeMatchingPrimitiveArrayWithIndexSpecificEditor() { PrimitiveArrayBean tb = new PrimitiveArrayBean(); BeanWrapper bw = new BeanWrapperImpl(tb); bw.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() { @Override/*from w w w .j av a2s. 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(0, tb.getArray()[0]); assertEquals(1, tb.getArray()[1]); }
From source file:org.webcurator.ui.target.controller.QaTiSummaryController.java
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(java.util.Date.class, DateUtils.get().getFullDateTimeEditor(true)); NumberFormat nf = NumberFormat.getInstance(request.getLocale()); binder.registerCustomEditor(java.lang.Long.class, new CustomNumberEditor(java.lang.Long.class, nf, true)); binder.registerCustomEditor(Date.class, new PropertyEditorSupport() { public void setAsText(String value) { try { setValue(new SimpleDateFormat("dd/MM/yyyy").parse(value)); } catch (ParseException e) { setValue(null);/*from w w w. j a v a2 s . c o m*/ } } public String getAsText() { Date value = (Date) getValue(); if (value != null) { return new SimpleDateFormat("dd/MM/yyyy").format(value); } else { return ""; } } }); }
From source file:ru.org.linux.topic.AddTopicController.java
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Group.class, new PropertyEditorSupport() { @Override//from www . j av a2s .c o m public void setAsText(String text) throws IllegalArgumentException { try { setValue(groupDao.getGroup(Integer.parseInt(text))); } catch (BadGroupException e) { throw new IllegalArgumentException(e); } } @Override public String getAsText() { if (getValue() == null) { return null; } else { return Integer.toString(((Group) getValue()).getId()); } } }); binder.registerCustomEditor(User.class, new UserPropertyEditor(userDao)); }