Package javango.forms.fields

Source Code of javango.forms.fields.SelectField

package javango.forms.fields;

import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;

import javango.forms.fields.annotations.Choice;
import javango.forms.fields.annotations.ChoiceFieldProperties;
import javango.forms.widgets.WidgetFactory;

/**
*
* @author johns
*/

public class SelectField extends AbstractChoiceField<String, String> {

  @Inject
  public SelectField(WidgetFactory widgetFactory) {
    super(widgetFactory);
    // TODO Auto-generated constructor stub
  }

  @Override
  public String clean(String value, Map<String, String> errors) {
    if (StringUtils.isEmpty(value)) {
      if (isRequired()) {
        errors.put(getName(), REQUIRED_ERROR);
        return null;
      } // no need to test for allowNull,  select field with an empty value can only return null;
      return null;
    }
   
    Map<String,String> localChoices = getChoices();
    if (localChoices == null)   {
      errors.put(this.name, NO_CHOICES_ERROR);
    } else if (!localChoices.containsKey(value)) {
      errors.put(this.name, UNKNOWN_CHOICE_ERROR);
    }
   
    return value;
  }
 
  @Override
  public void handleAnnotation(Annotation annotation) {
    if (annotation instanceof ChoiceFieldProperties) {
      ChoiceFieldProperties props = (ChoiceFieldProperties)annotation;
      if (getChoices() == null) {
        setChoices(new LinkedHashMap<String,String>());
      }
     
      for(Choice c : props.choices()) {
        getChoices().put(c.key(), c.value());
      }     
    }
    super.handleAnnotation(annotation);
  }
}
TOP

Related Classes of javango.forms.fields.SelectField

TOP
Copyright © 2018 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.