001 package org.hackystat.projectbrowser.page.projectportfolio.configurationpanel; 002 003 import java.util.Map; 004 import org.apache.wicket.markup.html.form.Form; 005 import org.apache.wicket.markup.html.form.FormComponent; 006 import org.apache.wicket.markup.html.form.TextField; 007 import org.apache.wicket.markup.html.form.validation.AbstractFormValidator; 008 009 /** 010 * Validator that valid values in configuration form. 011 * Make sure the higher value is no smaller than the lower value. 012 * @author Shaoxuan Zhang 013 * 014 */ 015 public class ConfigurationValueValidator extends AbstractFormValidator { 016 /** Support serialization. */ 017 private static final long serialVersionUID = -1148173388419682530L; 018 /** The TextField with higher value to validate. */ 019 private final TextField higherValueTextField; 020 /** The TextField with lower value validate. */ 021 private final TextField lowerValueTextField; 022 /** The name of the measure the two values belong to. */ 023 private final String measureName; 024 025 /** 026 * @param measureName The name of the measure the two values belong to. 027 * @param higherValueTextField The TextField with higher value. 028 * @param lowerValueTextField The TextField with lower value. 029 */ 030 public ConfigurationValueValidator(String measureName, TextField higherValueTextField, 031 TextField lowerValueTextField) { 032 if (higherValueTextField == null) { 033 throw new IllegalArgumentException("Higher value TextField cannot be null"); 034 } 035 if (lowerValueTextField == null) { 036 throw new IllegalArgumentException("Lower value TextField cannot be null"); 037 } 038 this.measureName = measureName; 039 this.higherValueTextField = higherValueTextField; 040 this.lowerValueTextField = lowerValueTextField; 041 } 042 043 /** 044 * @return array of FormComponents that this validator depends on 045 */ 046 public FormComponent[] getDependentFormComponents() { 047 return new FormComponent[]{higherValueTextField, lowerValueTextField}; 048 } 049 050 /** 051 * This method is ran if all components returned by getDependentFormComponents() are valid. 052 * @param form - form this validator is added to 053 */ 054 public void validate(Form form) { 055 if (higherValueTextField.getInput() == null && lowerValueTextField.getInput() == null) { 056 return; 057 } 058 if (higherValueTextField.getInput() == null || lowerValueTextField.getInput() == null) { 059 error(higherValueTextField, "HigherValueNotExisted"); 060 return; 061 } 062 double higherValue = Double.valueOf(higherValueTextField.getInput().replaceAll(",", "")); 063 double lowerValue = Double.valueOf(lowerValueTextField.getInput().replaceAll(",", "")); 064 if (higherValue <= lowerValue) { 065 error(higherValueTextField, "HigherValueSmallerThanLowerValue"); 066 } 067 } 068 069 /** 070 * Gets the default variables for interpolation. 071 * @return a map with the variables for interpolation 072 */ 073 @Override 074 @SuppressWarnings("unchecked") 075 protected Map variablesMap() { 076 Map map = super.variablesMap(); 077 // add keys and values for interpolation in error message 078 map.put("measure", measureName); 079 map.put("higherField", higherValueTextField.getId()); 080 map.put("lowerField", lowerValueTextField.getId()); 081 return map; 082 } 083 084 }