Advertisement
Guest User

Untitled

a guest
Jul 16th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.component.validator;
  2.  
  3. import java.io.Serializable;
  4. import java.util.List;
  5.  
  6. import javax.faces.component.FacesComponent;
  7. import javax.faces.component.UIInput;
  8. import javax.faces.context.FacesContext;
  9.  
  10. import org.omnifaces.component.validator.ValidateMultipleFields;
  11.  
  12. @FacesComponent(MinMaxSelectionValidator.COMPONENT_TYPE)
  13. public class MinMaxSelectionValidator extends ValidateMultipleFields {
  14.  
  15.     public static final String COMPONENT_TYPE = "com.example.component.validator.MinMaxSelectionValidator";
  16.    
  17.     public static final String DEFAULT_MESSAGE = "You have to select between {0} and {1} values";
  18.    
  19.     private enum PropertyKeys {
  20.         min, max
  21.     }
  22.    
  23.     private static final int DEFAULT_MIN = 0;
  24.     private static final int DEFAULT_MAX = -1;
  25.    
  26.     public MinMaxSelectionValidator() {
  27.         super(DEFAULT_MESSAGE);
  28.     }
  29.    
  30.     @Override
  31.     protected boolean validateValues(FacesContext fc, List<UIInput> components,
  32.             List<Object> values) {
  33.        
  34.         int countSelected = 0;
  35.         if (values != null) {
  36.             for (Object value : values) {
  37.                 if (value instanceof Boolean) {
  38.                     if (((Boolean)value) == true) {
  39.                         countSelected ++;
  40.                     }
  41.                 } else if (value instanceof String) {
  42.                     if (((String)value).equals("true")) {
  43.                         countSelected ++;
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.        
  49.         if (countSelected < getMin() || countSelected > getMax()) {
  50.             return false;
  51.         }
  52.        
  53.         return true;
  54.     }
  55.    
  56.     public int getMin() {
  57.         return (Integer)getStateHelper().eval(PropertyKeys.min, DEFAULT_MIN);
  58.     }
  59.    
  60.     public void setMin(int min) {
  61.         getStateHelper().put(PropertyKeys.min, min);
  62.     }
  63.    
  64.     public int getMax() {
  65.         return (Integer)getStateHelper().eval(PropertyKeys.max, DEFAULT_MAX);
  66.     }
  67.    
  68.     public void setMax(int max) {
  69.         getStateHelper().put(PropertyKeys.max, max);
  70.     }
  71.    
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement