1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean; 26 27 import java.util.Collections ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 33 34 import java.util.HashMap ; 35 import java.util.ArrayList ; 36 37 import java.beans.PropertyChangeEvent ; 38 import java.beans.PropertyChangeListener ; 39 import java.beans.PropertyChangeSupport ; 40 41 42 47 public final class ErrorMessageDB { 48 49 56 private Map errorSets = new HashMap (19); 58 59 61 private ErrorMessageDB() { 62 } 63 64 65 69 public Set getErrorPartitions() { 70 return Collections.unmodifiableSet(errorSets.keySet()); 72 } 73 74 75 80 public List getErrors(ValidationError.Partition partition) { 81 List errorList = (List ) errorSets.get(partition); 84 if(errorList != null) { 85 errorList = Collections.unmodifiableList(errorList); 86 } 87 return errorList; 88 } 89 90 91 98 public void addErrors(Collection errors) { 99 for(Iterator iter = errors.iterator(); iter.hasNext(); ) { 100 addErrorImpl((ValidationError) iter.next()); 101 } 102 } 103 104 105 112 public void addError(ValidationError error) { 113 addErrorImpl(error); 114 } 115 116 117 125 public void removeError(ValidationError error) { 126 List errorList = (List ) errorSets.get(error.getPartition()); 127 if(errorList != null) { 128 int index = errorList.indexOf(error); 129 if(index != -1) { 130 errorList.remove(index); 131 firePartitionStateChanged(error.getPartition(), true, (errorList.size() != 0)); 132 } 133 134 if(errorList.size() == 0) { 135 errorSets.remove(error.getPartition()); 136 137 if(errorSets.size() == 0) { 141 fireValidationStateChanged(true); 142 } 143 } 144 } 145 } 146 147 148 154 public void updateError(ValidationError error) { 155 if(Utils.notEmpty(error.getMessage())) { 156 addErrorImpl(error); 157 } else { 158 removeError(error); 159 } 160 } 161 162 163 private void addErrorImpl(ValidationError error) { 164 boolean oldIsValid = (errorSets.size() == 0); 166 167 List errorList = getOrCreateErrorList(error.getPartition()); 168 169 boolean oldHasMessages = (errorList.size() != 0); 171 172 if(errorList.contains(error)) { 173 errorList.remove(error); 174 } 175 176 errorList.add(error); 177 178 if(oldIsValid) { 180 fireValidationStateChanged(false); 181 } 182 183 firePartitionStateChanged(error.getPartition(), oldHasMessages, true); 185 } 186 187 188 private List getOrCreateErrorList(ValidationError.Partition partition) { 189 List errorList = (List ) errorSets.get(partition); 190 191 if(errorList == null) { 192 errorList = new ArrayList (); 193 errorSets.put(partition, errorList); 194 } 195 196 return errorList; 197 } 198 199 200 202 public void clearErrors() { 203 int numInvalidPartitions = errorSets.size(); 204 205 if(numInvalidPartitions > 0) { 207 Collection partitionList = new ArrayList (errorSets.keySet()); 208 errorSets.clear(); 209 210 fireValidationStateChanged(true); 212 for(Iterator iter = partitionList.iterator(); iter.hasNext(); ) { 213 firePartitionStateChanged((ValidationError.Partition) iter.next(), true, false); 214 } 215 } 216 } 217 218 219 222 public boolean hasErrors() { 223 return (errorSets.size() > 0); 224 } 225 226 227 230 public boolean hasErrors(ValidationError.Partition partition) { 231 boolean result = false; 232 233 List errorList = (List ) errorSets.get(partition); 234 if(errorList != null && errorList.size() > 0) { 235 result = true; 236 } 237 238 return result; 239 } 240 241 242 245 public static final String VALIDATION_STATE_CHANGED = "validationStateChanged"; 246 public static final String PARTITION_STATE_CHANGED = "partitionStateChanged"; 247 248 private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport (this); 249 250 public void addPropertyChangeListener(PropertyChangeListener pCL) { 251 propertyChangeSupport.addPropertyChangeListener(pCL); 252 } 253 254 public void removePropertyChangeListener(PropertyChangeListener pCL) { 255 propertyChangeSupport.removePropertyChangeListener(pCL); 256 } 257 258 private void fireValidationStateChanged(boolean newState) { 259 propertyChangeSupport.firePropertyChange(VALIDATION_STATE_CHANGED, !newState, newState); 260 } 261 262 private void firePartitionStateChanged(ValidationError.Partition partition, boolean oldHasMessages, boolean newHasMessages) { 263 PartitionState oldPartitionState = new PartitionState(partition, oldHasMessages); 264 PartitionState newPartitionState = new PartitionState(partition, newHasMessages); 265 propertyChangeSupport.firePropertyChange(PARTITION_STATE_CHANGED, oldPartitionState, newPartitionState); 266 } 267 268 public static class PartitionState { 269 private final ValidationError.Partition thePartition; 270 private final boolean hasMessages; 271 272 private PartitionState(final ValidationError.Partition partition, final boolean hasMsgs) { 273 thePartition = partition; 274 hasMessages = hasMsgs; 275 } 276 277 public ValidationError.Partition getPartition() { 278 return thePartition; 279 } 280 281 public boolean hasMessages() { 282 return hasMessages; 283 } 284 }; 285 286 287 292 public static ErrorMessageDB getMessageDB(Base bean) { 293 ErrorMessageDB messageDB = null; 294 295 if(bean != null) { 299 messageDB = bean.getMessageDB(); 300 } 301 302 return messageDB; 303 } 304 305 307 static ErrorMessageDB createMessageDB() { 308 return new ErrorMessageDB(); 309 } 310 } 311 | Popular Tags |