KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import java.util.*;
3
4 /**
5  * A PropertyEditorListener which translates one value to another. Useful
6  * for version migrations
7  */

8 public class PropertyTranslatorFilter extends PropertyEditorAdapter implements ConfigurablePropertyEditorListener {
9
10   Map<String JavaDoc,String JavaDoc> translator = new HashMap<String JavaDoc,String JavaDoc>();
11   String JavaDoc sourceProperty = null;
12   PropertyEditorManager manager = null;
13
14   /**
15    * Configures this filter from the given key.
16    */

17   public void configureListener(String JavaDoc key, String JavaDoc property, String JavaDoc propertyBase, String JavaDoc editorTemplate, PropertyEditorManager pManager) {
18     manager = pManager;
19     //System.err.println("configuring translator filter for " + property);
20
List<String JavaDoc> translatorKeys = manager.getPropertyAsList(key + ".map", "");
21     for (String JavaDoc translatorKey: translatorKeys) {
22       String JavaDoc[] pair = translatorKey.split("=");
23       if (pair != null && pair.length == 2) {
24         //System.err.println("adding map " + pair[0] + ", " + pair[1]);
25
translator.put(pair[0], pair[1]);
26       }
27     }
28
29     sourceProperty = manager.getProperty(key + ".sourceProperty", "");
30     if (sourceProperty.startsWith(".")) {
31       sourceProperty = propertyBase + sourceProperty;
32     }
33   }
34
35   /**
36    * On initialization, if the property is set to one of the values to
37    * be translated, we set the new value to the translated value.
38    */

39   public void propertyInitialized(PropertyEditorUI source, String JavaDoc property, String JavaDoc newValue) {
40     //System.err.println("property " + property + " initialized; newValue = " + newValue);
41
Properties currentProperties = source.getValue();
42     String JavaDoc currentValue = currentProperties.getProperty(property);
43     //System.err.println("currentValue = '" + currentValue + "'");
44
if (newValue.equals(currentValue) || currentValue == null || currentValue.equals("")) {
45       if (sourceProperty != null && ! sourceProperty.equals("")) {
46         //System.err.println("have sourceProperty(" + sourceProperty + ")");
47
if (newValue == "") {
48           //System.err.println("newValue is blank.");
49
String JavaDoc testValue = manager.getProperty(sourceProperty, "");
50           //System.err.println("testValue is " + testValue);
51
String JavaDoc translatedValue = translator.get(testValue);
52           if (translatedValue != null) {
53             //System.err.println("setting value to " + translatedValue);
54
source.setOriginalValue(translatedValue);
55           }
56         }
57       } else {
58         String JavaDoc translatedValue = translator.get(newValue);
59         //System.err.println("checking translation; value for value " + newValue + " = " + translatedValue);
60
if (translatedValue != null) {
61           source.setOriginalValue(translatedValue);
62           //System.err.println("setting value to " + translatedValue);
63
}
64       }
65     } else {
66       //System.err.println("value changed; not checking.");
67
}
68   }
69
70 }
71
Popular Tags