Using static method in DefaultFieldFactory (bad design?)

Why do you use

package com.vaadin.ui;

public class DefaultFieldFactory implements FormFieldFactory, TableFieldFactory {
public static DefaultFieldFactory get() {
return instance;
}
public static Field createFieldByPropertyType(Class<?> type) {
public static String createCaptionByPropertyId(Object propertyId) {

inside Singleton Factory- it is really hard to extend these classes… I think that non static method should be better and could be easy fixed as minor update
PP

Hi,

Those methods are static to allow their usage in your own field factory implementations like this:


		form.setFormFieldFactory(new FormFieldFactory() {
			@Override
			public Field createField(Item item, Object propertyId,
					Component uiContext) {
				if ("myProperty".equals(propertyId)) {
					// Custom field type but the caption convetion from default
					// field factory
					EnumSelect enumSelect = new EnumSelect();
					enumSelect.setCaption(DefaultFieldFactory
							.createCaptionByPropertyId(propertyId));
					return enumSelect;
				} else if ("otherProperty".equals(propertyId)) {
					// Field by default field factory, but custom caption
					Field field = DefaultFieldFactory
							.createFieldByPropertyType(item.getItemProperty(
									propertyId).getType());
					field.setCaption("Custom caption");
				}

				// Else use what the default fieldfactory produces
				return DefaultFieldFactory.get().createField(item, propertyId,
						uiContext);
			}
		});

In your project you’ll most likely want to use a top level class. If you really want to extend the DefaultFieldFactory itself you can just override the instance methods. And maybe create make that singleton as well if your field factory is stateless.

BTW. If you generally like the idea of field factory and automatically generated form views, you might be interested in my in progress
SmartFields add-on
. It contains a set of simple general purpose fields to complement those simple field types Vaadin builds by default. And there is also a configurable field factory. I have so far used that only once to start when I had a “backend” ready. I was able to build the initial UI really quickly. Then I converted some views to
FormBinder
style to modify them with VisualDesigner. And to make that process smoother I had to create yet another
add-on

cheers,
matti