001: package com.salmonllc.html;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlSSNSingleComponent.java $
005: //$Author: Dan $
006: //$Revision: 11 $
007: //$Modtime: 10/20/03 3:30p $
008: /////////////////////////
009: import java.util.Hashtable;
010:
011: import com.salmonllc.html.events.ValueChangedEvent;
012: import com.salmonllc.util.Util;
013:
014: /**
015: * This class creates a component for the editing of a social security number as a single edit field.
016: */
017:
018: public class HtmlSSNSingleComponent extends HtmlFormComponent {
019: private HtmlTextEdit _editSSN;
020: private char _fSeparator = '-';
021: private static String _separatorStr = "-";
022:
023: /**
024: * This is the constructor of this class, and initializes the SSN Component.
025: * @param name java.lang.String Name of component.
026: * @param p HtmlPage Page containing component.
027: */
028: public HtmlSSNSingleComponent(String name, HtmlPage p) {
029: super (name, p);
030: _editSSN = new HtmlTextEdit(name, p);
031: _editSSN.setGenerateNewline(false);
032: _editSSN.setSize(11);
033: _editSSN.setMaxLength(11);
034: }
035:
036: /**
037: * This method returns the SSN of a user with proper format (i.e. with dashes).
038: * @param s java.lang.String A string of the SSN value to be formatted.
039: * @return java.lang.String
040: */
041:
042: public static String formatValue(String s) {
043: String ssn;
044:
045: if (s == null) {
046: ssn = "";
047: } else if (s.length() == 9) {
048: ssn = s.substring(0, 3) + _separatorStr + s.substring(3, 5)
049: + _separatorStr + s.substring(5);
050: } else {
051: ssn = s;
052: }
053: return ssn;
054: }
055:
056: /**
057: * This method generates the part of the HTML for the page used to render the SSN component.
058: * @param p java.io.PrintWriter
059: * @param row int
060: */
061:
062: public void generateHTML(java.io.PrintWriter p, int row)
063: throws Exception {
064: // It is essential to call getValue() here because there may be a ValueChanged
065: // event in the queue for this object, which needs to be removed, which getValue()
066: // does. The secondary calls to getValue() from the container will not
067: // do this because there are no events associated with them.
068: String val = getValue(row, true);
069: if (val == null) {
070: _editSSN.setValue(null, row);
071: } else {
072: String sVal;
073: if (_dsBuff != null) {
074: if (row >= 0)
075: sVal = _dsBuff.getString(row, _dsColNo);
076: else
077: sVal = _dsBuff.getString(_dsColNo);
078: } else {
079: sVal = val;
080: }
081: _editSSN.setValue(formatValue(sVal), row);
082: }
083: _editSSN.generateHTML(p, row);
084: if (_visible && _enabled)
085: p.println("");
086: }
087:
088: /**
089: * This method returns the sub-component to be used for setting focus.
090: * @return com.salmonllc.html.HtmlComponent
091: */
092: public HtmlComponent getFocus() {
093: return _editSSN;
094: }
095:
096: /**
097: * This method returns the SSN of a user with proper format (i.e. with dashes).
098: * @return java.lang.String
099: */
100:
101: public String getFormattedValue() {
102: return formatValue(super .getValue());
103: }
104:
105: /**
106: * This method returns the seperator character for the social security number.
107: */
108: public String getSeparator() {
109: return new Character(_fSeparator).toString();
110: }
111:
112: /**
113: * This method gets the value of the SSN# and returns it in a string format.
114: * @return java.lang.String
115: */
116:
117: public String getValue() {
118: return Util.stripChars(super .getValue(), "0123456789", null);
119: }
120:
121: /**
122: * This method checks to see if the user-entered value for the SSN# is valid.
123: * @return boolean if the SSN# is valid.
124: */
125:
126: public boolean isValid() {
127: String s = getValue();
128: if (s == null) {
129: return false;
130: } else if (s.length() == 9) {
131: return Util.isNumeric(s);
132: } else if (s.length() == 11) {
133: if ((s.charAt(3) != _fSeparator)
134: || (s.charAt(6) != _fSeparator)
135: || !Util.isNumeric(s.substring(0, 3))
136: || !Util.isNumeric(s.substring(4, 6))
137: || !Util.isNumeric(s.substring(7, 11))) {
138: return false;
139: } else {
140: setValue(s.substring(0, 3) + s.substring(4, 6)
141: + s.substring(7, 11));
142: return true;
143: }
144: } else {
145: return false;
146: }
147: }
148:
149: /**
150: * This method compares the contents of the SSN component with that in the corresponding row and column in the datastore to determine if a change has occurred.
151: * @param parms Hashtable
152: * @param rowNo int
153: * @return boolean
154: */
155:
156: public boolean processParms(Hashtable parms, int rowNo)
157: throws Exception {
158:
159: if (!getVisible() || !getEnabled())
160: return false;
161:
162: String oldValue;
163: if (_dsBuff == null) {
164: oldValue = _editSSN.getValue();
165: } else {
166: String s;
167: if (rowNo > -1)
168: s = _dsBuff.getString(rowNo, _dsColNo);
169: else
170: s = _dsBuff.getString(_dsColNo);
171: if (s == null)
172: oldValue = null;
173: else
174: oldValue = s;
175: }
176:
177: String newValue;
178: String name1 = _editSSN.getFullName();
179: if (rowNo > -1)
180: name1 += "_" + rowNo;
181: String val[] = (String[]) parms.get(name1);
182: if (val != null) {
183: newValue = val[0].trim();
184: if (newValue.equals(""))
185: newValue = null;
186: else if (newValue.length() == 11)
187: newValue = newValue.substring(0, 3)
188: + newValue.substring(4, 6)
189: + newValue.substring(7, 11);
190: } else {
191: newValue = null;
192: }
193:
194: // Compare old and new values and possibly create a ValueChangedEvent.
195: if (!valuesEqual(oldValue, newValue)) {
196: ValueChangedEvent e = new ValueChangedEvent(getPage(),
197: this , getName(), getFullName(), oldValue, newValue,
198: rowNo, _dsColNo, _dsBuff);
199: addEvent(e);
200: }
201: return false;
202: }
203:
204: /**
205: * This method will clear all pending events from the event queue for this component.
206: */
207: public void reset() {
208: super .reset();
209: _editSSN.reset();
210: }
211:
212: /**
213: * This method is used to associate a specific set of properties from the properties file with the SSN component to change its presentation from the default one.
214: * @param theme java.lang.String
215: */
216:
217: public void setClassName(String sClass) {
218: super .setClassName(sClass);
219: if (sClass != null)
220: _editSSN.setClassName(sClass);
221: }
222:
223: /**
224: * Sets the font end tag for disabled mode.
225: * @param tag java.lang.String
226: */
227: public void setDisabledFontEndTag(String tag) {
228: _editSSN.setDisabledFontEndTag(tag);
229:
230: }
231:
232: /**
233: * Sets the font tag for disabled mode.
234: * @param tag java.lang.String
235: */
236: public void setDisabledFontStartTag(String tag) {
237:
238: _editSSN.setDisabledFontStartTag(tag);
239:
240: }
241:
242: /**
243: * This method enables or disables the ability of this component to respond to user input.
244: * @param editable boolean
245: */
246: public void setEnabled(boolean enabled) {
247: super .setEnabled(enabled);
248: _editSSN.setEnabled(enabled);
249: }
250:
251: /**
252: * This method sets the parent of the SSN component, i.e. the component containing the SSN component, so that the component can be named properly.
253: * @param parent HtmlComponent
254: */
255:
256: public void setParent(HtmlComponent parent) {
257: super .setParent(parent);
258: // This is necessary for the name to be generated correctly, else the leading
259: // sequence of parent names will be lost.
260: _editSSN.setParent(parent);
261:
262: }
263:
264: /**
265: * This method sets the seperator character for the ssn
266: */
267: public void setSeparator(String newSeparator) {
268: _fSeparator = newSeparator.charAt(0);
269: _separatorStr = newSeparator;
270: }
271:
272: /**
273: * This method is used to associate a specific set of properties from the properties file with the SSN component to change its presentation from the default one.
274: * @param theme java.lang.String
275: */
276:
277: public void setTheme(String theme) {
278: super .setTheme(theme);
279: if (theme != null)
280: _editSSN.setTheme(theme);
281: }
282:
283: /**
284: * @returns the access key html attribute
285: */
286: public String getAccessKey() {
287: return _editSSN.getAccessKey();
288: }
289:
290: /**
291: * @returns the read only html attribute
292: */
293: public boolean getReadOnly() {
294: return _editSSN.getReadOnly();
295: }
296:
297: /**
298: * @returns the tab index html attribute
299: */
300: public int getTabIndex() {
301: return _editSSN.getTabIndex();
302: }
303:
304: /**
305: * @param sets the access key html attribute
306: */
307: public void setAccessKey(String val) {
308: _editSSN.setAccessKey(val);
309: }
310:
311: /**
312: * @param sets the read only html attribute
313: */
314: public void setReadOnly(boolean val) {
315: _editSSN.setReadOnly(val);
316: }
317:
318: /**
319: * @param sets the tab index html attribute. You can also pass TAB_INDEX_DEFAULT to use the default tab index for the component or TAB_INDEX_NONE to keep this component from being tabbed to
320: */
321: public void setTabIndex(int val) {
322: _editSSN.setTabIndex(val);
323: }
324: }
|