1 package net.suberic.util.gui.propedit; 2 import java.util.*; 3 4 8 public class DisableFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener { 9 Map<String ,Set<String >> disableValues = new HashMap<String , Set<String >>(); 10 Map<String ,Set<String >> enableValues = new HashMap<String , Set<String >>(); 11 PropertyEditorManager manager; 12 String propertyBase; 13 String property; 14 15 18 public void configureListener(String key, String pProperty, String pPropertyBase, String editorTemplate, PropertyEditorManager pManager) { 19 manager = pManager; 22 propertyBase = pPropertyBase; 23 property = pProperty; 24 List<String > disableKeys = manager.getPropertyAsList(key + ".disableValues", ""); 25 for (String keyString: disableKeys) { 26 String [] pair = keyString.split("="); 27 if (pair != null && pair.length == 1) { 29 String [] newPair = new String [2]; 30 newPair[0] = pair[0]; 31 newPair[1] = ""; 32 pair = newPair; 33 } 34 35 if (pair != null && pair.length == 2) { 36 if (pair[0].startsWith(".")) { 37 pair[0] = propertyBase + pair[0]; 38 } 39 Set<String > valueSet = disableValues.get(pair[0]); 40 if (valueSet == null) { 41 manager.addPropertyEditorListener(pair[0], this); 42 valueSet = new HashSet<String >(); 43 disableValues.put(pair[0], valueSet); 44 } 45 valueSet.add(pair[1]); 46 } 47 } 48 49 List<String > enableKeys = manager.getPropertyAsList(key + ".enableValues", ""); 50 for (String keyString: enableKeys) { 51 String [] pair = keyString.split("="); 52 if (pair != null && pair.length == 1) { 54 String [] newPair = new String [2]; 55 newPair[0] = pair[0]; 56 newPair[1] = ""; 57 pair = newPair; 58 } 59 if (pair != null && pair.length == 2) { 60 if (pair[0].startsWith(".")) { 61 pair[0] = propertyBase + pair[0]; 62 } 63 Set<String > valueSet = enableValues.get(pair[0]); 64 if (valueSet == null) { 65 valueSet = new HashSet<String >(); 66 enableValues.put(pair[0], valueSet); 67 manager.addPropertyEditorListener(pair[0], this); 68 } 69 valueSet.add(pair[1]); 70 } 71 } 72 } 73 74 78 public void propertyInitialized(PropertyEditorUI source, String pProperty, String newValue) { 79 if (property.equals(pProperty)) { 81 checkEnabledStatus(source); 82 } 83 } 84 85 89 public void propertyChanged(PropertyEditorUI source, String pProperty, String newValue) { 90 if (enableValues.keySet().contains(pProperty) || disableValues.keySet().contains(pProperty)) { 92 checkEnabledStatus(null); 93 } 94 } 95 96 99 public void checkEnabledStatus(PropertyEditorUI source) { 100 boolean enable = true; 102 for (String key: enableValues.keySet()) { 103 boolean enableFound = false; 104 String fullProperty = key; 105 String propValue = manager.getCurrentProperty(fullProperty, ""); 106 Set<String > valueSet = enableValues.get(key); 108 for (String value: valueSet) { 109 if (propValue.equals(value)) { 111 enableFound=true; 113 } 114 } 115 if (! enableFound) 116 enable = false; 117 } 118 119 for (String key: disableValues.keySet()) { 120 String fullProperty = key; 121 String propValue = manager.getCurrentProperty(fullProperty, ""); 122 Set<String > valueSet = disableValues.get(key); 124 for (String value: valueSet) { 125 if (propValue.equals(value)) { 127 enable = false; 129 } 130 } 131 } 132 if (source != null) { 133 if (enable) { 135 source.removeDisableMask(this); 136 } else { 137 source.addDisableMask(this); 138 } 139 } else { 140 if (manager.getPropertyEditor(property) != null) 142 if (enable) { 143 manager.getPropertyEditor(property).removeDisableMask(this); 144 } else { 145 manager.getPropertyEditor(property).addDisableMask(this); 146 } 147 } 148 } 149 } 150 | Popular Tags |