KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import java.util.*;
3
4 /**
5  * A PropertyEditorListener which sets a property as required.
6  */

7 public class RequiredFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener {
8
9   PropertyEditorManager manager;
10   String JavaDoc propertyBase;
11
12   Map<String JavaDoc,String JavaDoc> requiredIfMap = new HashMap<String JavaDoc,String JavaDoc>();
13   boolean always = false;
14
15   /**
16    * Configures this filter from the given key.
17    */

18   public void configureListener(String JavaDoc key, String JavaDoc property, String JavaDoc pPropertyBase, String JavaDoc editorTemplate, PropertyEditorManager pManager) {
19     //System.err.println("configuring requiredfilter for property " + property);
20
manager = pManager;
21     propertyBase = pPropertyBase;
22     List<String JavaDoc> requiredKeys = manager.getPropertyAsList(key + ".map", "");
23     if (requiredKeys.size() < 1) {
24       always = true;
25       //System.err.println("always.");
26
} else {
27       for (String JavaDoc requiredKey: requiredKeys) {
28         String JavaDoc[] pair = requiredKey.split("=");
29         if (pair != null && pair.length == 2) {
30           //System.err.println("adding requirement for " + pair[0] + " = '" + pair[1] + "'");
31
requiredIfMap.put(pair[0], pair[1]);
32         }
33       }
34     }
35   }
36
37   /**
38    * Checks to make sure that this value is set.
39    */

40   public void propertyCommitting(PropertyEditorUI source, String JavaDoc property, String JavaDoc 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 JavaDoc> keys = requiredIfMap.keySet().iterator();
46       while ( keys.hasNext()) {
47         String JavaDoc affectedProperty = keys.next();
48         String JavaDoc 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 JavaDoc propertyValue = uiValue.getProperty(fullProperty);
56           if (requiredIfMap.get(affectedProperty).equals(propertyValue)) {
57             //System.err.println("throwing exception.");
58
throw new PropertyValueVetoException(source.getDisplayValue(), newValue, "property is required", this);
59           }
60         }
61       }
62     }
63
64   }
65 }
66
Popular Tags