package com.cedarsoft.spring.rcp.binding;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.springframework.binding.form.FormModel;
import org.springframework.binding.form.HierarchicalFormModel;
import org.springframework.binding.form.support.DefaultFormModel;
import org.springframework.richclient.form.Form;
import org.springframework.richclient.form.binding.Binding;
import org.springframework.richclient.form.binding.support.AbstractBinder;
import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.Map;
/**
* Binds a property using a (sub) form.
*
* @param <T> the type
*/
public abstract class AbstractFormBinder<T> extends AbstractBinder {
/**
* Creates a new form binder for the given type
*
* @param type the type
*/
protected AbstractFormBinder( @NotNull Class<T> type ) {
super( type );
}
@Override
protected JComponent createControl( Map context ) {
return new JPanel( new BorderLayout() );
}
@Override
protected Binding doBind( final JComponent control, final FormModel formModel, final String formPropertyPath, Map context ) {
//Create the child model
HierarchicalFormModel childModel = createFormModel( formModel, formPropertyPath );
( ( HierarchicalFormModel ) formModel ).addChild( childModel );
//Create the form
Form form = createForm( childModel );
control.add( form.getControl(), BorderLayout.CENTER );
return new Binding() {
@Override
public FormModel getFormModel() {
return formModel;
}
@Override
public String getProperty() {
return formPropertyPath;
}
@Override
public JComponent getControl() {
return control;
}
};
}
/**
* Creates the form model
*
* @param formModel the form model
* @param formPropertyPath the property path
* @return the form model
*/
@NotNull
protected HierarchicalFormModel createFormModel( @NotNull FormModel formModel, @NotNull @NonNls String formPropertyPath ) {
return new DefaultFormModel( formModel.getValueModel( formPropertyPath ) );
}
/**
* Creates the form
*
* @param childModel the child model
* @return the created form
*/
@NotNull
protected abstract Form createForm( @NotNull HierarchicalFormModel childModel );
}