Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/webobjects/woextensions/WOAnyField.java


1   /*
2    * WOAnyField.java
3    * [JavaWOExtensions Project]
4    */
5   
6   package com.webobjects.woextensions;
7   
8   import com.webobjects.appserver.*;
9   import com.webobjects.foundation.*;
10  import com.webobjects.eoaccess.*;
11  
12  import java.text.ParseException;
13  
14  public class WOAnyField extends WOComponent {
15      protected static String DEFAULT_DATE_FORMAT = "YYYY/MM/DD";
16      protected static String DEFAULT_NUMBER_FORMAT = "0";
17  
18      private String _relationshipKey;
19      private String _selectedKey;
20      private String _selectedOperator;
21      private Object _value;
22      private String _textFieldValue;
23      private WODisplayGroup _displayGroup;
24      // ivars for PopUp
25      public String selectedKeyItem;
26      public String selectedOperatorItem;
27  
28      public WOAnyField(WOContext aContext)  {
29          super(aContext);
30      }
31  
32      public boolean isStateless() {
33          return true;
34      }
35  
36      public String relationshipKey() {
37          if (_relationshipKey == null) {
38              _relationshipKey = (String) _WOJExtensionsUtil.valueForBindingOrNull("relationshipKey", this);
39          }
40          return _relationshipKey;
41      }
42  
43      public String selectedKey() {
44          if (_selectedKey == null) {
45              _selectedKey = (String) _WOJExtensionsUtil.valueForBindingOrNull("selectedKey", this);
46          }
47          return _selectedKey;
48      }
49  
50      public void setSelectedKey(String key) {
51          _selectedKey = key;
52      }
53  
54      public String valueClassNameForKey(String key) {
55          String entityName = (String) _WOJExtensionsUtil.valueForBindingOrNull("sourceEntityName", this);
56          EOModelGroup modelGroup = EOModelGroup.defaultGroup();
57          EOEntity entity = modelGroup.entityNamed(entityName);
58          EOAttribute selectedAttribute = null;
59          if (relationshipKey() != null) {
60              EORelationship relationship = entity.relationshipNamed(relationshipKey());
61              EOEntity destinationEntity = relationship.destinationEntity();
62              selectedAttribute = destinationEntity.attributeNamed(key);
63          } else {
64              selectedAttribute = entity.attributeNamed(key);
65          }
66          return selectedAttribute.className();
67      }
68  
69      public String formatterForKey(String key) {
70          String formatter = null;
71          if (hasBinding("formatter")) {
72              setValueForBinding(key, "key");
73              formatter = (String) _WOJExtensionsUtil.valueForBindingOrNull("formatter", this);
74          }
75          if (formatter == null) {
76              String className = valueClassNameForKey(key);
77              if (className.equals("com.webobjects.foundation.NSTimestamp")) {
78                  formatter = DEFAULT_DATE_FORMAT;
79              } else if (className.equals("java.lang.Number") || className.equals("java.math.BigDecimal")) {
80                  formatter = DEFAULT_NUMBER_FORMAT;
81              }
82          }
83          return formatter;
84      }
85  
86      public WODisplayGroup displayGroup() {
87          if (_displayGroup == null) {
88              _displayGroup = (WODisplayGroup) _WOJExtensionsUtil.valueForBindingOrNull("displayGroup", this);
89          }
90          return _displayGroup;
91      }
92  
93      public String selectedOperator() {
94          return _selectedOperator;
95      }
96  
97      public void setSelectedOperator(String anOperator) {
98          _selectedOperator = (anOperator.equals("=")) ? "": anOperator;
99      }
100 
101     public Object value() {
102         if (_value == null) {
103             _value = _WOJExtensionsUtil.valueForBindingOrNull("value", this);
104         }
105         return _value;
106     }
107 
108     public void setValue(Object newValue) {
109         _value = newValue;
110         WODisplayGroup displayGroup = displayGroup();
111         if (displayGroup != null) {
112             displayGroup.queryMatch().removeAllObjects();
113             if (relationshipKey() != null) {
114                 displayGroup.queryMatch().takeValueForKey(newValue, relationshipKey() + "." + selectedKey());
115                 if (newValue != null) {
116                     displayGroup.queryOperator().takeValueForKey(selectedOperator(), relationshipKey() + "." + selectedKey());
117                 }
118             } else {
119                 displayGroup.queryMatch().takeValueForKey(newValue, selectedKey());
120                 if (newValue != null) {
121                     displayGroup.queryOperator().takeValueForKey(selectedOperator(), selectedKey());
122                 }
123             }
124         }
125     }
126 
127     public String textFieldValue() {
128         if (_textFieldValue != null) {
129             return _textFieldValue;
130         }
131         Object value = value();
132         setValue(value);
133         if (value == null) {
134             return null;
135         } else if (value instanceof String) {
136             return (String) value;
137         } else {
138             java.text.Format formatter = null;
139             String className = valueClassNameForKey(selectedKey());
140             if (className.equals("com.webobjects.foundation.NSTimestamp")) {
141                 String dateFormatterString = formatterForKey(selectedKey());
142                 formatter = new NSTimestampFormatter(dateFormatterString);
143             } else if (className.equals("java.lang.Number") || className.equals("java.math.BigDecimal")) {
144                 String numberFormatterString = formatterForKey(selectedKey());
145                 formatter = new NSNumberFormatter(numberFormatterString);
146             }
147             return (formatter != null) ? formatter.format(value) : value.toString();
148         }
149     }
150 
151     public void setTextFieldValue(String value) {
152         String className = valueClassNameForKey(selectedKey());
153         if (className.equals("com.webobjects.foundation.NSTimestamp")) {
154             String dateFormatterString = formatterForKey(selectedKey());
155             NSTimestampFormatter dateFormatter = new NSTimestampFormatter(dateFormatterString);
156             Object objectValue = null;
157             try {
158                 objectValue = dateFormatter.parseObject((value != null) ? value.toString() : "");
159             } catch (ParseException e) {
160                 if (NSLog.debugLoggingAllowedForLevelAndGroups(NSLog.DebugLevelDetailed, NSLog.DebugGroupWebObjects)) {
161                     NSLog.debug.appendln(e);
162                 }
163             }
164             setValue(objectValue);
165         } else if (className.equals("java.lang.Number") || className.equals("java.math.BigDecimal")) {
166             String numberFormatterString = formatterForKey(selectedKey());
167             NSNumberFormatter numberFormatter = new NSNumberFormatter(numberFormatterString);
168             Object objectValue = null;
169             try {
170                 objectValue = numberFormatter.parseObject((value != null) ? value.toString() : "");
171             } catch (ParseException e) {
172                 if (NSLog.debugLoggingAllowedForLevelAndGroups(NSLog.DebugLevelDetailed, NSLog.DebugGroupWebObjects)) {
173                     NSLog.debug.appendln(e);
174                 }
175             }
176             setValue(objectValue);
177         } else {
178             // Assume String
179             setValue(value);
180         }
181     }
182 
183     public void invalidateCaches() {
184         // In order for this to behave like an element, all instance
185         // variables need to be flushed before this components is used again
186         // so that it will pull via association.
187         _relationshipKey = null;
188         _selectedKey = null;
189         _selectedOperator = null;
190         _value = null;
191         _textFieldValue = null;
192         _displayGroup = null;
193     }
194 
195     public void finalize() throws Throwable {
196         super.finalize();
197         invalidateCaches();
198     }
199 
200     public void reset() {
201         invalidateCaches();
202     }
203 }