Package de.mhus.lib.form

Source Code of de.mhus.lib.form.ConfigForm

package de.mhus.lib.form;

import javax.xml.parsers.ParserConfigurationException;

import de.mhus.lib.MException;
import de.mhus.lib.MSingleton;
import de.mhus.lib.config.IConfig;
import de.mhus.lib.form.builders.FormElementRegistry;
import de.mhus.lib.form.builders.FormLayoutSimpleBuilder;
import de.mhus.lib.form.objects.FObject;
import de.mhus.lib.form.objects.ObjectList;
import de.mhus.lib.lang.MObject;

/**
* This class creates a form for a config object. Therefore it iterates every key in the config. If the config has a corresponding
* sub-config object with the same name and prefix 'element_' it will display the attribute.
*
* @author mikehummel
*
*/
public class ConfigForm extends MObject implements IFormDynamic, IConfigurable {

  private IConfig config;

  public ConfigForm(IConfig config) {
    this.config = config;
  }

  @Override
  public void setFormValue(String name, Object value) throws FormException {
    try {
      config.setProperty(name, String.valueOf(value));
    } catch (MException e) {
    }
  }

  @Override
  public Object getFormValue(String name) throws FormException {
    return config.getString(name, "");
  }

  @Override
  public void validateFormAttribute(String name, Class<?> class1)
      throws FormException {
   
  }

  @Override
  public MFormModel getConfigurationForm() throws FormException {
    FormElementRegistry registry = new FormElementRegistry();
    MFormModel model = new MFormModel(MSingleton.instance().getActivator(),this);
    ObjectList list = model.getList();
    for (String key : config.getPropertyKeys()) {
      log().t("key",key);
      try {
        IConfig elementConfig = config.getConfig("element_" + key);
        if (elementConfig != null) {
          log().t("accept fobject",key);
          FObject element = registry.createElement(model, elementConfig.getString("type", "input"), elementConfig);
          element.setName(key);
          element.setTitle(key);
          list.add(element);
        }
      } catch (Exception e) {
        log().i(key,e);
      }
    }
    try {
      model.setLayout(new FormLayoutSimpleBuilder(model));
    } catch (ParserConfigurationException e) {
      log().i(e);
    }
   
    return model;
  }

}
TOP

Related Classes of de.mhus.lib.form.ConfigForm

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.