1 20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation; 21 22 import java.util.List ; 23 import java.util.ArrayList ; 24 25 import org.enhydra.barracuda.core.forms.DefaultFormValidator; 26 import org.enhydra.barracuda.core.comp.ListModel; 27 import org.enhydra.barracuda.core.comp.DefaultListModel; 28 import org.apache.log4j.Logger; 29 import org.enhydra.barracuda.core.comp.ListSelectionModel; 30 import org.enhydra.barracuda.core.forms.ValidationException; 31 import org.enhydra.barracuda.core.forms.FormElement; 32 import org.enhydra.barracuda.core.forms.FormValidator; 33 import org.enhydra.barracuda.core.comp.ItemMap; 34 35 public class SelectFormValidator extends DefaultFormValidator { 36 protected static final Logger logger = Logger.getLogger(SelectFormValidator.class.getName()); 37 38 int selMode = -1; 39 String ignore; 40 41 public void setSelectionMode(int mode) { 42 selMode = mode == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ? 43 mode : mode == ListSelectionModel.SINGLE_INTERVAL_SELECTION ? 44 mode : mode == ListSelectionModel.SINGLE_SELECTION ? 45 mode : selMode; 46 } 47 48 public SelectFormValidator(String ignore) { 49 this.ignore = ignore; 51 } 52 53 public SelectFormValidator(String ignore, String message) { 54 super(message); 55 this.ignore = ignore; 56 } 57 58 public SelectFormValidator() { 59 } 61 62 public SelectFormValidator(int selMode) { 63 setSelectionMode(selMode); 65 } 66 67 public SelectFormValidator(int selMode, String message) { 68 super(message); 69 setSelectionMode(selMode); 70 } 71 72 public SelectFormValidator(int selMode, String ignore, String message) { 73 super(message); 74 this.ignore = ignore; 75 setSelectionMode(selMode); 76 } 77 78 79 public int getSelectionMode() { 80 return selMode; 81 } 82 83 public void setIgnore(String val) { 84 ignore = val; 85 } 86 87 public String getIgnore() { 88 return ignore; 89 } 90 91 92 public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException { 93 if(logger.isDebugEnabled()) { 94 logger.debug("Begin validation."); 95 } 96 if(!(element instanceof SelectFormElement)) { 97 logger.error("Not a SelectFormElement" + element); 98 throw this.generateException(element, deferExceptions, "Element must be SelectFormElement"); 99 } 100 101 boolean allContained = true; 102 SelectFormElement selEl = (SelectFormElement)element; 103 ListModel lm = selEl.getListModel(); 104 List selected = new ArrayList (); 105 Object origVal = selEl.getOrigVal(); 106 107 if(origVal != null) { 109 if(logger.isDebugEnabled()) { 110 logger.debug("OrigVal class is:" + origVal.getClass()); 111 } 112 if(origVal instanceof List ) { 113 selected = (List )origVal; 114 } else { 115 selected.add(origVal); 116 } 117 } 118 119 if(ignore != null) { 120 selected.remove(ignore); 121 } 122 123 124 List values = new ArrayList (lm.getSize()); 126 for(int i = 0; i < lm.getSize(); i++) { 127 String key; 128 String value; 129 Object item = lm.getItemAt(i); 130 if(item instanceof ItemMap) { 131 key = ((ItemMap)item).getKey().toString(); 132 value = ((ItemMap)item).getValue().toString(); 133 } else { 134 value = key = item.toString(); 135 } 136 if(!(ignore != null && ignore.equals(value))) 137 values.add(key); 138 } 139 for(int i = 0;i < selected.size(); i++) { 141 if(!values.contains(selected.get(i))) { 142 allContained = false; 143 break; 144 } 145 } 146 if(!allContained) { 147 if(logger.isDebugEnabled()) { 148 logger.debug("Not all selected options are among the permitted values."); 149 } 150 throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "Not all selected options are among the permitted values."); 151 } 152 if(ignore != null && selected.size() == 0) { 155 if(logger.isDebugEnabled()) { 156 logger.debug("The selected value was the ignored one."); 157 } 158 throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "The selected value was the ignored one."); 159 } 160 if(selMode != -1 && SelectFormElement.calcSelectionMode(values, selected) != selMode) { 161 if(logger.isDebugEnabled()) { 162 logger.debug("The selection mode differs."); 163 } 164 throw this.generateException(element, deferExceptions, getErrorMessage() != null ? getErrorMessage() : "The selection mode differs."); 165 } 166 } 167 } 168 | Popular Tags |