001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.html;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlFormattedEntryComponent.java $
024: //$Author: Dan $
025: //$Revision: 8 $
026: //$Modtime: 10/20/03 3:43p $
027: /////////////////////////
028: import java.util.Hashtable;
029:
030: import com.salmonllc.html.events.ValueChangedEvent;
031: import com.salmonllc.properties.Props;
032:
033: /**
034: * This object creates text edit boxes to accept formated input like invoice number, Tax id etc...
035: * In order to create a series of text boxes to accept "###-####-####-##" format, enter an array of
036: * integers, {3,4,3,2}, in the contstructor, "lengthsOfFields".
037: * This will create 4 text edit boxes with each size 3,4,4 and 2 characters.
038: */
039: public class HtmlFormattedEntryComponent extends HtmlFormComponent {
040: protected HtmlTextEdit _hte[];
041:
042: protected HtmlContainer _cont;
043:
044: public HtmlComponent _hcFocusComp;
045:
046: public String _sStr, _sSeparator;
047: private HtmlText _htSeparator1;
048:
049: private String _sPart[];
050:
051: private int _iLength[];
052:
053: private String _font;
054: private String _fontStartTag;
055: private String _fontEndTag;
056:
057: /**
058: * TaxIdComponent constructor comment.
059: *
060: * @param name Name of component.
061: * @param lengthsOfFields An array of integers for the length of fields.
062: * @param p Page containing component.
063: * @param separator Character to display between the parts of the component
064: * @param bEnableFlag Flag to set the component to editable (true) or read only (false)
065: */
066: public HtmlFormattedEntryComponent(String name,
067: int[] lengthsOfFields, HtmlPage p, String separator,
068: boolean bEnableFlag) {
069: super (name, p);
070: _cont = new HtmlContainer(name, p);
071: _sSeparator = separator;
072:
073: _iLength = lengthsOfFields;
074: _sPart = new String[_iLength.length];
075: _hte = new HtmlTextEdit[_iLength.length];
076:
077: for (int i = 0; i < _iLength.length; i++) {
078: _hte[i] = new HtmlTextEdit("hte" + i, p);
079: _hte[i].setGenerateNewline(false);
080: _hte[i].setSize(lengthsOfFields[i]);
081: _hte[i].setMaxLength(lengthsOfFields[i]);
082: _cont.add(_hte[i]);
083:
084: if (_sSeparator != null && !_sSeparator.trim().equals("")
085: && i < _iLength.length - 1) //Do not add Seperator after the last text edit box
086: _cont.add(new HtmlText(_sSeparator, getPage()));
087: }
088:
089: setEnabled(bEnableFlag);
090: }
091:
092: public void generateHTML(java.io.PrintWriter p, int row)
093: throws Exception {
094: // It is essential to call getValue() here because there may be a ValueChanged
095: // event in the queue for this object, which needs to be removed, which getValue()
096: // does. The secondary calls to getValue() from the container will not
097: // do this because there are no events associated with them.
098: String val = getValue(row, true);
099:
100: if (val == null || val.trim().equals("")) {
101: for (int i = 0; i < _iLength.length; i++)
102: _hte[i].setValue(null, row);
103: } else {
104: String sVal;
105:
106: if (_dsBuff != null) {
107: if (row >= 0)
108: sVal = _dsBuff.getString(row, _dsColNo);
109: else
110: sVal = _dsBuff.getString(_dsColNo);
111: } else
112: sVal = val;
113:
114: int iTotalStrLen = 0;
115:
116: for (int i = 0; i < _iLength.length; i++)
117: iTotalStrLen += _iLength[i];
118:
119: if (sVal.length() >= iTotalStrLen) {
120: int iBegIdex = 0;
121: for (int i = 0; i < _iLength.length; i++) {
122: _hte[i].setValue(sVal.substring(iBegIdex, iBegIdex
123: + _iLength[i]), row);
124: iBegIdex += _iLength[i];
125: }
126: }
127: }
128:
129: _cont.generateHTML(p, row);
130:
131: if (_visible && _enabled)
132: p.println("");
133: }
134:
135: /**
136: * Returns the sub-component to be used for setting focus.
137: * @return com.salmonllc.html.HtmlComponent
138: */
139: public HtmlComponent getFocusComponent() {
140: return _hcFocusComp;
141: }
142:
143: /**
144: * Returns the sub-component to be used for setting focus.
145: * @return com.salmonllc.html.HtmlComponent
146: */
147: public HtmlComponent getFocusPart(int iPartNo) {
148: if (_hte != null && iPartNo < _hte.length
149: && _hte[iPartNo] != null)
150: return _hte[iPartNo];
151: else
152: return _hte[0];
153: }
154:
155: /**
156: * This method will return the font type used by this control.See the Constants at the top of the class for valid font types.
157: */
158: public String getFont() {
159: return _font;
160: }
161:
162: /**
163: * This method returns the formatted string provided that the component
164: * contains a valid value otherwise it returns an empty string
165: */
166: public String getFormattedValue() {
167: String sFormattedStr = "";
168:
169: if (isValid()) {
170: for (int i = 0; i < _iLength.length; i++)
171: sFormattedStr += _sPart[i] + _sSeparator;
172: }
173:
174: //Strip the last seperator
175: if (sFormattedStr.length() > 1)
176: sFormattedStr = sFormattedStr.substring(0, sFormattedStr
177: .length() - 1);
178:
179: return sFormattedStr;
180: }
181:
182: /**
183: * Gets the separator char.
184: * @return java.lang.String
185: */
186: public java.lang.String getSeparator() {
187: return _sSeparator;
188: }
189:
190: /**
191: * This method returns the string provided that the component
192: * contains a valid value otherwise it returns an empty string
193: */
194: public String getValue() {
195: _sStr = "";
196:
197: if (isValid())
198: for (int i = 0; i < _iLength.length; i++)
199: _sStr += _sPart[i];
200:
201: return _sStr;
202: }
203:
204: /**
205: * This method sets the property theme for the component.
206: * @param theme The theme to use.
207: */
208: public boolean getVisible() {
209:
210: return _cont.getVisible();
211: }
212:
213: /**
214: * This methot checks if all rewuired fields entered. If not, sets the focus component to the component which generated the error.
215: */
216: public boolean isValid() {
217: boolean bRetVal = true;
218: //verify if all fields are entered
219: for (int i = 0; i < _iLength.length; i++)
220: _hte[i].setValue(_sPart[i]);
221:
222: for (int i = 0; i < _iLength.length; i++) {
223: int iValidLength = _iLength[i];
224: int iCurrLength = _sPart[i].length();
225:
226: if (iCurrLength != iValidLength) {
227: _hcFocusComp = getFocusPart(i);
228: bRetVal = false;
229: }
230: }
231: return bRetVal;
232: }
233:
234: public boolean processParms(Hashtable htParms, int iRowNo)
235: throws Exception {
236: if (!getVisible() || !getEnabled())
237: return false;
238:
239: // Determine the old value from both edit fields.
240:
241: String sOldValue = null;
242:
243: if (_dsBuff == null) {
244: for (int i = 0; i < _iLength.length; i++)
245: sOldValue += _hte[i].getValue();
246: } else {
247: String s;
248: if (iRowNo > -1)
249: s = _dsBuff.getString(iRowNo, _dsColNo);
250: else
251: s = _dsBuff.getString(_dsColNo);
252:
253: sOldValue = (s != null) ? s : null;
254:
255: if (s == null)
256: sOldValue = null;
257: else
258: sOldValue = s;
259: }
260:
261: // Determine the new value from both edit fields.
262:
263: String sNewValue = null;
264: for (int i = 0; i < _iLength.length; i++) {
265: String sName = _hte[i].getFullName();
266:
267: if (iRowNo > -1)
268: sName += "_" + iRowNo;
269:
270: String[] asValues = (String[]) htParms.get(sName);
271:
272: if (asValues != null) {
273: String s = _sPart[i] = asValues[0].trim();
274:
275: if (!s.equals("")) {
276: if (sNewValue == null)
277: sNewValue = "";
278: sNewValue += s;
279: }
280: }
281: }
282:
283: // Compare old and new values and possibly create a ValueChangedEvent.
284: if (!valuesEqual(sOldValue, sNewValue)) {
285: ValueChangedEvent e = new ValueChangedEvent(getPage(),
286: this , getName(), getFullName(), sOldValue,
287: sNewValue, iRowNo, _dsColNo, _dsBuff);
288: addEvent(e);
289: }
290: return false;
291: }
292:
293: /**
294: * This method will clear all pending events from the event queue for this component.
295: */
296: public void reset() {
297: super .reset();
298: _cont.reset();
299:
300: setValue("");
301:
302: }
303:
304: /**
305: * Enables or disables the ability of this component to respond to user input.
306: * @param editable boolean
307: */
308: public void setEnabled(boolean enabled) {
309: super .setEnabled(enabled);
310: _cont.setEnabled(enabled);
311: }
312:
313: /**
314: * This method will load the font start and end tags from the page properties object.See the Constants at the top of the class for valid values to pass to this method.
315: */
316: public void setFont(String font) {
317: _font = font;
318:
319: Props props = getPage().getPageProperties();
320:
321: if (_font != null) {
322: _fontStartTag = props.getThemeProperty(null, _font
323: + Props.TAG_START);
324: _fontEndTag = props.getThemeProperty(null, _font
325: + Props.TAG_END);
326: } else {
327: _fontStartTag = props.getThemeProperty(null,
328: HtmlText.FONT_DEFAULT + Props.TAG_START);
329: _fontEndTag = props.getThemeProperty(null,
330: HtmlText.FONT_DEFAULT + Props.TAG_END);
331: }
332:
333: for (int i = 0; i < _iLength.length; i++) {
334: _hte[i].setFontStartTag(_fontStartTag);
335: _hte[i].setFontEndTag(_fontEndTag);
336: }
337:
338: if (_htSeparator1 != null) {
339: _htSeparator1.setFontStartTag(_fontStartTag);
340: _htSeparator1.setFontEndTag(_fontEndTag);
341: }
342:
343: }
344:
345: public void setParent(HtmlComponent parent) {
346: super .setParent(parent);
347: // This is necessary for the name to be generated correctly, else the leading
348: // sequence of parent names will be lost.
349: _cont.setParent(parent);
350: }
351:
352: /**
353: * Replaces the separator between the ssn numbers
354: * @param newSeparator java.lang.String
355: */
356: public void setSeparator(java.lang.String newSeparator) {
357: _sSeparator = newSeparator;
358:
359: // create new text comp
360: HtmlText txtNewSeparator1 = new HtmlText(newSeparator,
361: getPage());
362:
363: // replace old area code end comp with new one
364: _cont.replaceComponent(txtNewSeparator1, _htSeparator1);
365:
366: // save ref of new comp
367: _htSeparator1 = txtNewSeparator1;
368:
369: }
370:
371: /**
372: * This method sets the property theme for the component.
373: * @param theme The theme to use.
374: */
375: public void setTheme(String theme) {
376:
377: super .setTheme(theme);
378:
379: if (theme != null) {
380: for (int i = 0; i < _iLength.length; i++) {
381: _hte[i].setTheme(theme);
382: }
383:
384: if (_htSeparator1 != null)
385: _htSeparator1.setTheme(theme);
386: }
387: }
388:
389: /**
390: * This method sets the property theme for the component.
391: * @param theme The theme to use.
392: */
393: public void setVisible(boolean visible) {
394:
395: _cont.setVisible(visible);
396: }
397:
398: /**
399: * @returns the access key html attribute
400: */
401: public String getAccessKey() {
402: return _hte[0].getAccessKey();
403: }
404:
405: /**
406: * @returns the read only html attribute
407: */
408: public boolean getReadOnly() {
409: return _hte[0].getReadOnly();
410: }
411:
412: /**
413: * @returns the tab index html attribute
414: */
415: public int getTabIndex() {
416: return _hte[0].getTabIndex();
417: }
418:
419: /**
420: * @param sets the access key html attribute
421: */
422: public void setAccessKey(String val) {
423: _hte[0].setAccessKey(val);
424: }
425:
426: /**
427: * @param sets the read only html attribute
428: */
429: public void setReadOnly(boolean val) {
430: for (int i = 0; i < _hte.length; i++)
431: _hte[i].setReadOnly(val);
432: }
433:
434: /**
435: * @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
436: */
437: public void setTabIndex(int val) {
438: for (int i = 0; i < _hte.length; i++)
439: _hte[i].setTabIndex(val + i);
440: }
441: }
|