diff --git a/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/converter/StringToEnumConverter.java b/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/converter/StringToEnumConverter.java new file mode 100644 index 0000000..6c90c4a --- /dev/null +++ b/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/converter/StringToEnumConverter.java @@ -0,0 +1,70 @@ +/** + * This file Copyright (c) 2013-2014 Magnolia International + * Ltd. (http://www.magnolia-cms.com). All rights reserved. + * + * + * This file is dual-licensed under both the Magnolia + * Network Agreement and the GNU General Public License. + * You may elect to use one or the other of these licenses. + * + * This file is distributed in the hope that it will be + * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the + * implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. + * Redistribution, except as permitted by whichever of the GPL + * or MNA you select, is prohibited. + * + * 1. For the GPL license (GPL), you can redistribute and/or + * modify this file under the terms of the GNU General + * Public License, Version 3, as published by the Free Software + * Foundation. You should have received a copy of the GNU + * General Public License, Version 3 along with this program; + * if not, write to the Free Software Foundation, Inc., 51 + * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * 2. For the Magnolia Network Agreement (MNA), this file + * and the accompanying materials are made available under the + * terms of the MNA which accompanies this distribution, and + * is available at http://www.magnolia-cms.com/mna.html + * + * Any modifications to this file must keep this entire header + * intact. + * + */ +package info.magnolia.ui.form.field.converter; + +import java.util.Locale; + +import com.vaadin.data.util.converter.Converter; + +/** + * StringToEnumConverter. + */ +public class StringToEnumConverter implements Converter { + + private static final long serialVersionUID = 1L; + + @Override + public Enum convertToModel(String value, Class targetType, Locale locale) throws Converter.ConversionException { + return null; + } + + @Override + public String convertToPresentation(Enum value, Class targetType, Locale locale) throws Converter.ConversionException { + if (value == null) { + return null; + } + return value.name(); + } + + @Override + public Class getModelType() { + return Enum.class; + } + + @Override + public Class getPresentationType() { + return String.class; + } + +} \ No newline at end of file diff --git a/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/factory/AbstractFieldFactory.java b/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/factory/AbstractFieldFactory.java index 8ecb296..ac4f562 100644 --- a/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/factory/AbstractFieldFactory.java +++ b/magnolia-ui-form/src/main/java/info/magnolia/ui/form/field/factory/AbstractFieldFactory.java @@ -37,6 +37,7 @@ import info.magnolia.objectfactory.ComponentProvider; import info.magnolia.ui.api.view.View; import info.magnolia.ui.form.AbstractFormItem; +import info.magnolia.ui.form.field.converter.StringToEnumConverter; import info.magnolia.ui.form.field.definition.FieldDefinition; import info.magnolia.ui.form.field.definition.TextFieldDefinition; import info.magnolia.ui.form.field.transformer.TransformedProperty; @@ -108,6 +109,7 @@ if (Long.class.equals(property.getType()) && field instanceof AbstractTextField) { ((AbstractTextField) field).setConverter(new StringToLongConverter()); } + // Set the created property with the default value as field Property datasource. setPropertyDataSourceAndDefaultValue(property); @@ -195,8 +197,10 @@ label.addStyleName("textarea"); } } + if (Enum.class.isAssignableFrom(property.getType())) { + label.setConverter(new StringToEnumConverter()); + } label.setPropertyDataSource(property); - fieldView.addComponent(label); return new View() {