All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cedarsoft.utils.springrcp.form.FormManager Maven / Gradle / Ivy

package com.cedarsoft.utils.springrcp.form;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.binding.form.ValidatingFormModel;
import org.springframework.richclient.application.Application;
import org.springframework.richclient.form.Form;
import org.springframework.richclient.form.FormModelHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Manages the forms
 */
public class FormManager {
  @NotNull
  public static FormManager getInstance() {
    return ( FormManager ) Application.instance().getApplicationContext().getBean( "formManager" );
  }

  private Map, Class
> forms = new HashMap, Class>(); public void setForms( Map, Class> forms ) throws ClassNotFoundException { for ( Map.Entry, Class> entry : forms.entrySet() ) { Class value = ( Class ) entry.getValue(); this.forms.put( entry.getKey(), value ); } } @NotNull public Form createForm( @NotNull Object bean ) { ValidatingFormModel formModel = FormModelHelper.createFormModel( bean ); Class formClass = getFormClass( bean.getClass() ); try { return ( Form ) BeanUtils.instantiateClass( formClass.getDeclaredConstructor( ValidatingFormModel.class ), new Object[]{formModel} ); } catch ( NoSuchMethodException e ) { throw new RuntimeException( e ); } } @NotNull private Class getFormClass( @NotNull Class beanClass ) { Class formClass = forms.get( beanClass ); if ( formClass == null ) { throw new IllegalArgumentException( "No form registered for " + beanClass ); } return formClass; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy