Refine search
PropertyEditor editor = PropertyEditorManager.findEditor(clazz); if (editor != null) { editor.setAsText((String) value); return editor.getValue();
/** * Convert the given text value using the given property editor. * @param oldValue the previous value, if available (may be {@code null}) * @param newTextValue the proposed text value * @param editor the PropertyEditor to use * @return the converted value */ private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) { try { editor.setValue(oldValue); } catch (Exception ex) { if (logger.isDebugEnabled()) { logger.debug("PropertyEditor [" + editor.getClass().getName() + "] does not support setValue call", ex); } // Swallow and proceed. } editor.setAsText(newTextValue); return editor.getValue(); }
@Test public void testClassEditor() { PropertyEditor classEditor = new ClassEditor(); classEditor.setAsText(TestBean.class.getName()); assertEquals(TestBean.class, classEditor.getValue()); assertEquals(TestBean.class.getName(), classEditor.getAsText()); classEditor.setAsText(null); assertEquals("", classEditor.getAsText()); classEditor.setAsText(""); assertEquals("", classEditor.getAsText()); classEditor.setAsText("\t "); assertEquals("", classEditor.getAsText()); }
/** * Build the display value of the supplied {@code Object}, HTML escaped * as required. If the supplied value is not a {@link String} and the supplied * {@link PropertyEditor} is not null then the {@link PropertyEditor} is used * to obtain the display value. * @see #getDisplayString(Object, boolean) */ public static String getDisplayString( @Nullable Object value, @Nullable PropertyEditor propertyEditor, boolean htmlEscape) { if (propertyEditor != null && !(value instanceof String)) { try { propertyEditor.setValue(value); String text = propertyEditor.getAsText(); if (text != null) { return getDisplayString(text, htmlEscape); } } catch (Throwable ex) { // The PropertyEditor might not support this value... pass through. } } return getDisplayString(value, htmlEscape); }
public void setWithConversion(String name, String value) { PropertyDescriptor property = dataSourceProperties.get(name); PropertyEditor editor = findEditor(property.getPropertyType()); editor.setAsText(value); try { property.getWriteMethod().invoke(commonDataSource, editor.getValue()); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new IllegalStateException(e); } }
public static String convertToString(Object value, Class<?> type) { PropertyEditor editor = PropertyEditorManager.findEditor(type); if (editor != null) { editor.setValue(value); return editor.getAsText(); } return null; }
@Test public void testBindingWithCustomFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); binder.addCustomFormatter(new NumberStyleFormatter(), Float.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(1.2), tb.getMyFloat()); assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat")); PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class); assertNotNull(editor); editor.setValue(new Float(1.4)); assertEquals("1,4", editor.getAsText()); editor = binder.getBindingResult().findEditor("myFloat", null); assertNotNull(editor); editor.setAsText("1,6"); assertTrue(((Number) editor.getValue()).floatValue() == 1.6f); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Test(expected = IllegalArgumentException.class) public void testWithNonExistentResource() throws Exception { PropertyEditor propertyEditor = new FileEditor(); propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"); }
private void update(final Method write, final Object bean, final PropertyEditor pe, final PropertyDescriptor pd) { try { LOG.debug("update " + pd.getDisplayName() + " to " + pe.getValue()); write.invoke(bean, new Object[] { pe.getValue() }); } catch (IllegalAccessException e) { LOG.error("Unable to set property " + pd.getDisplayName(), e); } catch (InvocationTargetException e) { LOG.error("Unable to set property " + pd.getDisplayName(), e); } }
public Component getCustomiser(DataThing dataThing) { PropertyEditor editor = PropertyEditorManager.findEditor( Scanner.class); if(editor != null) { editor.setValue(this); return editor.getCustomEditor(); } else { return null; } }
@Override public void setValue(Object o) { if ((null != o) && (null != editor) && editor.supportsCustomEditor()) { editor.setValue(o); String val = editor.getAsText(); if( null == val ) val = ""; setText(val); } else { setText((o != null) ? o.toString() : ""); //NOI18N } }
@Override public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column) { editor.setValue(value); return editor.getCustomEditor(); } }
@Test public void withCustomObjectAndEditorSelected() throws Exception { final PropertyEditor floatEditor = new SimpleFloatEditor(); floatEditor.setValue(new Float("12.34")); String selectName = "testBean.someNumber"; BindStatus bindStatus = new BindStatus(getRequestContext(), selectName, false) { @Override public PropertyEditor getEditor() { return floatEditor; } }; getPageContext().setAttribute(SelectTag.LIST_VALUE_PAGE_ATTRIBUTE, bindStatus); this.tag.setValue(new Float(12.34)); this.tag.setLabel("12.34f"); int result = this.tag.doStartTag(); assertEquals(BodyTag.EVAL_BODY_BUFFERED, result); result = this.tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, result); String output = getOutput(); assertOptionTagOpened(output); assertOptionTagClosed(output); assertContainsAttribute(output, "selected", "selected"); assertBlockTagContains(output, "12.34f"); }
@Test public void getAsTextReturnsEmptyStringIfValueNotSet() throws Exception { PropertyEditor uriEditor = new URIEditor(); assertEquals("", uriEditor.getAsText()); }
@Test public void testWithNonExistentPath() throws Exception { PropertyEditor pathEditor = new PathEditor(); pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); Object value = pathEditor.getValue(); assertTrue(value instanceof Path); Path path = (Path) value; assertTrue(!path.toFile().exists()); }
public static String convertToString(Object value, Class<?> type) { PropertyEditor editor = PropertyEditorManager.findEditor(type); if (editor != null) { editor.setValue(value); return editor.getAsText(); } return null; }
@Test public void testBindingWithFormatter() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb); FormattingConversionService conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter()); binder.setConversionService(conversionService); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("myFloat", "1,2"); LocaleContextHolder.setLocale(Locale.GERMAN); try { binder.bind(pvs); assertEquals(new Float(1.2), tb.getMyFloat()); assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat")); PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class); assertNotNull(editor); editor.setValue(new Float(1.4)); assertEquals("1,4", editor.getAsText()); editor = binder.getBindingResult().findEditor("myFloat", null); assertNotNull(editor); editor.setAsText("1,6"); assertEquals(new Float(1.6), editor.getValue()); } finally { LocaleContextHolder.resetLocaleContext(); } }