1 package net.suberic.util.gui.propedit; 2 import java.util.*; 3 4 7 public class RequiredFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener { 8 9 PropertyEditorManager manager; 10 String propertyBase; 11 12 Map<String ,String > requiredIfMap = new HashMap<String ,String >(); 13 boolean always = false; 14 15 18 public void configureListener(String key, String property, String pPropertyBase, String editorTemplate, PropertyEditorManager pManager) { 19 manager = pManager; 21 propertyBase = pPropertyBase; 22 List<String > requiredKeys = manager.getPropertyAsList(key + ".map", ""); 23 if (requiredKeys.size() < 1) { 24 always = true; 25 } else { 27 for (String requiredKey: requiredKeys) { 28 String [] pair = requiredKey.split("="); 29 if (pair != null && pair.length == 2) { 30 requiredIfMap.put(pair[0], pair[1]); 32 } 33 } 34 } 35 } 36 37 40 public void propertyCommitting(PropertyEditorUI source, String property, String newValue) throws PropertyValueVetoException{ 41 if (newValue == null || newValue.trim().length() == 0) { 42 if (always) { 43 throw new PropertyValueVetoException(source.getDisplayValue(), newValue, "property is required", this); 44 } 45 Iterator<String > keys = requiredIfMap.keySet().iterator(); 46 while ( keys.hasNext()) { 47 String affectedProperty = keys.next(); 48 String fullProperty = affectedProperty; 49 if (affectedProperty != null && affectedProperty.startsWith(".")) { 50 fullProperty = propertyBase + affectedProperty; 51 } 52 PropertyEditorUI ui = manager.getPropertyEditor(fullProperty); 53 if (ui != null) { 54 Properties uiValue = ui.getValue(); 55 String propertyValue = uiValue.getProperty(fullProperty); 56 if (requiredIfMap.get(affectedProperty).equals(propertyValue)) { 57 throw new PropertyValueVetoException(source.getDisplayValue(), newValue, "property is required", this); 59 } 60 } 61 } 62 } 63 64 } 65 } 66 | Popular Tags |