/*
 *  Copyright 2001-2013 Stephen Colebourne
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.joda.beans.ui.swing;

import java.util.Map;

import javax.swing.JPanel;

import org.joda.beans.Bean;
import org.joda.beans.ui.form.DefaultUIComponentFactory;
import org.joda.beans.ui.form.MetaUIComponent;
import org.joda.beans.ui.form.MetaUIFactory;
import org.joda.beans.ui.form.MetaUIForm;
import org.joda.beans.ui.form.UIComponentFactory;
import org.joda.beans.ui.form.UIForm;
import org.joda.beans.ui.swing.type.EnumSwingUIComponent;

/**
 * Entry point factory class capable of creating a Swing UI for a bean.
 */
public class JodaBeanSwingUI {

    /**
     * Creates a form for the specified bean.
     * <p>
     * The bean is examined for meta-data and a form created.
     * 
     * @param bean  the bean to examine, not null
     * @return the created form panel, not null
     */
    public JPanel createForm(final Bean bean) {
        MetaUIForm metaForm = createMetaForm(bean);
        selectSwingComponents(bean, metaForm);
        UIForm<JPanel> form = createSwingForm(bean, metaForm);
        form.updateUI(bean);
        return form.getForm();
    }

    protected MetaUIForm createMetaForm(Bean bean) {
        return new MetaUIFactory().createForm(bean.metaBean());
    }

    protected void selectSwingComponents(Bean bean, MetaUIForm form) {
        Map<Class<?>, UIComponentFactory> factories = SwingUISettings.INSTANCE.getFactories();
        for (MetaUIComponent comp : form.getComponents()) {
            UIComponentFactory factory = factories.get(comp.getMetaProperty().propertyType());
            if (factory == null) {
                if (Enum.class.isAssignableFrom(comp.getMetaProperty().propertyType())) {
                    factory = DefaultUIComponentFactory.of(EnumSwingUIComponent.class);
                }
            }
            comp.setComponentFactory(factory);
        }
    }

    protected SwingUIForm createSwingForm(Bean bean, MetaUIForm form) {
        return new SwingUIForm(form);
    }

}

@@ -723,16 +723,48 @@
 s.Bean;%0A
+import org.joda.beans.MetaBean;%0A
 import o
@@ -1506,24 +1506,24 @@
     
-Meta
 UIForm
- metaF
+%3CJPanel%3E f
 orm 
@@ -1534,160 +1534,568 @@
 eate
-MetaForm(bean);%0A        selectSwingComponents(bean, metaForm);%0A        UIForm%3CJPanel%3E form = createSwingForm(bean, metaForm);%0A        form.updateUI(bean
+Form(bean.metaBean());%0A        form.updateUI(bean);%0A        return form.getForm();%0A    %7D%0A%0A    /**%0A     * Creates a form for the specified meta-bean.%0A     * %3Cp%3E%0A     * The bean is examined for meta-data and a form created.%0A     * %0A     * @param metaBean  the meta-bean to examine, not null%0A     * @return the created form panel, not null%0A     */%0A    public UIForm%3CJPanel%3E createForm(final MetaBean metaBean) %7B%0A        MetaUIForm metaForm = createMetaForm(metaBean);%0A        selectSwingComponents(metaForm);%0A        UIForm%3CJPanel%3E form = createSwingForm(metaForm
 );%0A 
@@ -2108,34 +2108,24 @@
  return form
-.getForm()
 ;%0A    %7D%0A%0A   
@@ -2161,22 +2161,30 @@
 etaForm(
+Meta
 Bean 
-b
+metaB
 ean) %7B%0A 
@@ -2224,37 +2224,32 @@
 .createForm(
-bean.
 metaBean
 ());%0A    %7D%0A%0A
@@ -2232,26 +2232,24 @@
 orm(metaBean
-()
 );%0A    %7D%0A%0A  
@@ -2287,27 +2287,16 @@
 ponents(
-Bean bean, 
 MetaUIFo
@@ -2911,19 +2911,8 @@
 orm(
-Bean bean, 
 Meta

Add form creation for MetaBean