001: package com.salmonllc.html;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlZipCodeComponent.java $
005: //$Author: Dan $
006: //$Revision: 11 $
007: //$Modtime: 10/20/03 4:14p $
008: /////////////////////////
009:
010: import java.util.Hashtable;
011:
012: import com.salmonllc.html.events.ValueChangedEvent;
013:
014: /**
015: * This class creates an edit for a zip code
016: */
017: public class HtmlZipCodeComponent extends HtmlFormComponent {
018: protected HtmlTextEdit _editZip;
019: private boolean _extendedZip = false;
020: private String _zip, _zip5, _zip4;
021:
022: /**
023: * @param name Name of component.
024: * @param p Page containing component.
025: */
026: public HtmlZipCodeComponent(String name, HtmlPage p) {
027: this (name, p, false);
028:
029: }
030:
031: /** @param name java.lang.String
032: * @param p com.salmonllc.html.HtmlPage
033: * @param extendedZip boolean
034: */
035: public HtmlZipCodeComponent(String name, HtmlPage p,
036: boolean extendedZip) {
037: super (name, p);
038: _extendedZip = extendedZip;
039: _editZip = new HtmlTextEdit(name, p);
040: _editZip.setGenerateNewline(false);
041: _editZip.setSize(_extendedZip ? 10 : 5);
042: _editZip.setMaxLength(_extendedZip ? 10 : 5);
043: }
044:
045: public void generateHTML(java.io.PrintWriter p, int row)
046: throws Exception {
047: // It is essential to call getValue() here because there may be a ValueChanged
048: // event in the queue for this object, which needs to be removed, which getValue()
049: // does. The secondary calls to getValue() from the container will not
050: // do this because there are no events associated with them.
051: String val = getValue(row, true);
052: if (val == null) {
053: _editZip.setValue(null, row);
054: } else {
055: String sVal;
056: if (_dsBuff != null) {
057: if (row >= 0)
058: sVal = _dsBuff.getString(row, _dsColNo);
059: else
060: sVal = _dsBuff.getString(_dsColNo);
061: } else {
062: sVal = val;
063: }
064: _editZip.setValue(sVal, row);
065: }
066: _editZip.generateHTML(p, row);
067: if (_visible && _enabled)
068: p.println("");
069: }
070:
071: /**
072: * Returns the sub-component to be used for setting focus.
073: * @return com.salmonllc.html.HtmlComponent
074: */
075: public HtmlComponent getFocus() {
076: return _editZip;
077: }
078:
079: public boolean isNumeric(String s, int length) {
080: for (int i = 0; i < length; i++) {
081: if (!Character.isDigit(s.charAt(i)))
082: return false;
083: }
084: return true;
085: }
086:
087: /**
088: * Returns true if the zip code is valid
089: */
090: public boolean isValid() {
091: _editZip.setValue(_zip);
092: int length = _zip.length();
093: if (length == 5) {
094: _zip5 = _zip.substring(0, 5);
095: _zip4 = "";
096: return isNumeric(_zip5, 5);
097: } else if (length == 9) {
098: _zip5 = _zip.substring(0, 5);
099: _zip4 = _zip.substring(5);
100: return isNumeric(_zip5, 5) && isNumeric(_zip4, 4);
101: } else if (length == 10 && _zip.indexOf('-') == 5) {
102: _zip5 = _zip.substring(0, 5);
103: _zip4 = _zip.substring(6);
104: return isNumeric(_zip5, 5) && isNumeric(_zip4, 4);
105: } else {
106: return false;
107: }
108: }
109:
110: public boolean processParms(Hashtable parms, int rowNo)
111: throws Exception {
112:
113: if (!getVisible() || !getEnabled())
114: return false;
115:
116: String oldValue;
117: if (_dsBuff == null) {
118: oldValue = _editZip.getValue();
119: } else {
120: String s;
121: if (rowNo > -1)
122: s = _dsBuff.getString(rowNo, _dsColNo);
123: else
124: s = _dsBuff.getString(_dsColNo);
125: if (s == null)
126: oldValue = null;
127: else
128: oldValue = s;
129: }
130:
131: String newValue;
132: String name1 = _editZip.getFullName();
133: if (rowNo > -1)
134: name1 += "_" + rowNo;
135: String val[] = (String[]) parms.get(name1);
136: if (val != null) {
137: _zip = newValue = val[0].trim();
138: if (newValue.equals(""))
139: newValue = null;
140: } else {
141: newValue = null;
142: }
143:
144: // Compare old and new values and possibly create a ValueChangedEvent.
145: if (!valuesEqual(oldValue, newValue)) {
146: ValueChangedEvent e = new ValueChangedEvent(getPage(),
147: this , getName(), getFullName(), oldValue, newValue,
148: rowNo, _dsColNo, _dsBuff);
149: addEvent(e);
150: }
151: return false;
152: }
153:
154: public void reset() {
155: super .reset();
156: _editZip.reset();
157: }
158:
159: public void setClassName(String sClass) {
160: super .setClassName(sClass);
161: if (_editZip != null)
162: _editZip.setClassName(sClass);
163:
164: }
165:
166: /**
167: * Sets the font end tag for disabled mode.
168: * @param tag java.lang.String
169: */
170: public void setDisabledFontEndTag(String tag) {
171: _editZip.setDisabledFontEndTag(tag);
172: }
173:
174: /**
175: * Sets the font tag for disabled mode.
176: * @param tag java.lang.String
177: */
178: public void setDisabledFontStartTag(String tag) {
179: _editZip.setDisabledFontStartTag(tag);
180: }
181:
182: public void setEnabled(boolean enabled) {
183: super .setEnabled(enabled);
184: _editZip.setEnabled(enabled);
185: }
186:
187: public void setParent(HtmlComponent parent) {
188: super .setParent(parent);
189: // This is necessary for the name to be generated correctly, else the leading
190: // sequence of parent names will be lost.
191: _editZip.setParent(parent);
192:
193: }
194:
195: public void setTheme(String sTheme) {
196: if (sTheme == null)
197: return;
198:
199: if (_editZip != null)
200: _editZip.setTheme(sTheme);
201:
202: }
203:
204: /**
205: * @returns the access key html attribute
206: */
207: public String getAccessKey() {
208: return _editZip.getAccessKey();
209: }
210:
211: /**
212: * @returns the read only html attribute
213: */
214: public boolean getReadOnly() {
215: return _editZip.getReadOnly();
216: }
217:
218: /**
219: * @returns the tab index html attribute
220: */
221: public int getTabIndex() {
222: return _editZip.getTabIndex();
223: }
224:
225: /**
226: * @param sets the access key html attribute
227: */
228: public void setAccessKey(String val) {
229: _editZip.setAccessKey(val);
230:
231: }
232:
233: /**
234: * @param sets the read only html attribute
235: */
236: public void setReadOnly(boolean val) {
237: _editZip.setReadOnly(val);
238: }
239:
240: /**
241: * @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
242: */
243: public void setTabIndex(int val) {
244: _editZip.setTabIndex(val);
245: }
246: }
|