KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > DisableFilter


1 package net.suberic.util.gui.propedit;
2 import java.util.*;
3
4 /**
5  * A PropertyEditorListener which enables or disables this editor if
6  * certain values are set.
7  */

8 public class DisableFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener {
9   Map<String JavaDoc,Set<String JavaDoc>> disableValues = new HashMap<String JavaDoc, Set<String JavaDoc>>();
10   Map<String JavaDoc,Set<String JavaDoc>> enableValues = new HashMap<String JavaDoc, Set<String JavaDoc>>();
11   PropertyEditorManager manager;
12   String JavaDoc propertyBase;
13   String JavaDoc property;
14
15   /**
16    * Configures this filter from the given key.
17    */

18   public void configureListener(String JavaDoc key, String JavaDoc pProperty, String JavaDoc pPropertyBase, String JavaDoc editorTemplate, PropertyEditorManager pManager) {
19     //System.err.println("");
20
//System.err.println("init for " + key);
21
manager = pManager;
22     propertyBase = pPropertyBase;
23     property = pProperty;
24     List<String JavaDoc> disableKeys = manager.getPropertyAsList(key + ".disableValues", "");
25     for (String JavaDoc keyString: disableKeys) {
26       String JavaDoc[] pair = keyString.split("=");
27       //System.err.println("split '" + keyString + "'; pair.length = " + pair.length);
28
if (pair != null && pair.length == 1) {
29         String JavaDoc[] newPair = new String JavaDoc[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 JavaDoc> valueSet = disableValues.get(pair[0]);
40         if (valueSet == null) {
41           manager.addPropertyEditorListener(pair[0], this);
42           valueSet = new HashSet<String JavaDoc>();
43           disableValues.put(pair[0], valueSet);
44         }
45         valueSet.add(pair[1]);
46       }
47     }
48
49     List<String JavaDoc> enableKeys = manager.getPropertyAsList(key + ".enableValues", "");
50     for (String JavaDoc keyString: enableKeys) {
51       String JavaDoc[] pair = keyString.split("=");
52       //System.err.println("split '" + keyString + "'; pair.length = " + pair.length);
53
if (pair != null && pair.length == 1) {
54         String JavaDoc[] newPair = new String JavaDoc[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 JavaDoc> valueSet = enableValues.get(pair[0]);
64         if (valueSet == null) {
65           valueSet = new HashSet<String JavaDoc>();
66           enableValues.put(pair[0], valueSet);
67           manager.addPropertyEditorListener(pair[0], this);
68         }
69         valueSet.add(pair[1]);
70       }
71     }
72   }
73
74   /**
75    * On initialization, if any of the source properties are set to
76    * values to be disabled, disable the editor.
77    */

78   public void propertyInitialized(PropertyEditorUI source, String JavaDoc pProperty, String JavaDoc newValue) {
79     //System.err.println("property " + pProperty + " initializing.");
80
if (property.equals(pProperty)) {
81       checkEnabledStatus(source);
82     }
83   }
84
85   /**
86    * On a property change, if any of the source properties are set to
87    * values to be disabled, disable the editor.
88    */

89   public void propertyChanged(PropertyEditorUI source, String JavaDoc pProperty, String JavaDoc newValue) {
90     //System.err.println("property " + pProperty + " changed.");
91
if (enableValues.keySet().contains(pProperty) || disableValues.keySet().contains(pProperty)) {
92       checkEnabledStatus(null);
93     }
94   }
95
96   /**
97    * Checks the enabled status of the affected editor.
98    */

99   public void checkEnabledStatus(PropertyEditorUI source) {
100     //System.err.println("checking enabled status for property " + property );
101
boolean enable = true;
102     for (String JavaDoc key: enableValues.keySet()) {
103       boolean enableFound = false;
104       String JavaDoc fullProperty = key;
105       String JavaDoc propValue = manager.getCurrentProperty(fullProperty, "");
106       //System.err.println("fullProperty to check is " + fullProperty + ", value = '" + propValue + "'");
107
Set<String JavaDoc> valueSet = enableValues.get(key);
108       for (String JavaDoc value: valueSet) {
109         //System.err.println("checking value " + value);
110
if (propValue.equals(value)) {
111           //System.err.println("found enable value for " + key);
112
enableFound=true;
113         }
114       }
115       if (! enableFound)
116         enable = false;
117     }
118
119     for (String JavaDoc key: disableValues.keySet()) {
120       String JavaDoc fullProperty = key;
121       String JavaDoc propValue = manager.getCurrentProperty(fullProperty, "");
122       //System.err.println("fullProperty to check is " + fullProperty + ", value = '" + propValue + "'");
123
Set<String JavaDoc> valueSet = disableValues.get(key);
124       for (String JavaDoc value: valueSet) {
125         //System.err.println("checking value " + value);
126
if (propValue.equals(value)) {
127           //System.err.println("match found; setting enabled to false.");
128
enable = false;
129         }
130       }
131     }
132     if (source != null) {
133       //System.err.println("setting source editor to " + enable);
134
if (enable) {
135         source.removeDisableMask(this);
136       } else {
137         source.addDisableMask(this);
138       }
139     } else {
140       //System.err.println("getPropertyEditor(" + property + ")=" + manager.getPropertyEditor(property));
141
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