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.wml;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/wml/WmlSelect.java $
024: //$Author: Dan $
025: //$Revision: 6 $
026: //$Modtime: 6/11/03 4:52p $
027: /////////////////////////
028:
029: import java.io.PrintWriter;
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: import com.salmonllc.html.HtmlPage;
034: import com.salmonllc.html.events.ValueChangedEvent;
035: import com.salmonllc.jsp.JspController;
036: import com.salmonllc.sql.*;
037: import com.salmonllc.util.*;
038:
039: /**
040: * This class is used for Wml Select Component.
041: */
042: public class WmlSelect extends WmlFormComponent {
043: private String _title;
044: private String _value;
045: private String _iname;
046: private String _ivalue;
047: private boolean _multiple = false;
048: private int _tabindex = -1;
049: private Vector _options = new Vector();
050: private Vector _wmloptions = new Vector();
051: private String _hrefonpick;
052: private DataStoreEvaluator _dsEval;
053:
054: /**
055: * Returns the href for the onpick action
056: */
057: public String getHrefOnPick() {
058: return _hrefonpick;
059: }
060:
061: /**
062: * Sets the href for the onpick action
063: */
064: public void setHrefOnPick(String _hrefonpick) {
065: this ._hrefonpick = _hrefonpick;
066: }
067:
068: /**
069: * Use this method to add new choices to the list.
070: * @param wo An WmlOption component
071: */
072: public void addOption(WmlOption wo) {
073: _wmloptions.addElement(wo);
074: }
075:
076: /**
077: * Use this method to add new choices to the list.
078: * @param key The internal name of the item (must be unique)
079: * @param disp The value to be displayed on the list.
080: */
081: public void addOption(String key, String disp) {
082: addOption(key, disp, false);
083: }
084:
085: /**
086: * Use this method to add new choices to the list.
087: * @param key The internal name of the item (must be unique)
088: * @param disp The value to be displayed on the list.
089: */
090: public void addOption(String key, String disp, boolean selected) {
091: ThreeObjectContainer t = new ThreeObjectContainer(key, disp,
092: new Boolean(selected));
093: _options.addElement(t);
094: }
095:
096: /**
097: * This method removes all options from the component.
098: */
099: public void resetOptions() {
100: _options.removeAllElements();
101: }
102:
103: /**
104: * This method removes all options from the component.
105: */
106: public void resetWmlOptions() {
107: _wmloptions.removeAllElements();
108: }
109:
110: /**
111: * Creates a list box based on a table with an <BR>
112: * integer primary key column (typically an id) and a string column.<BR>
113: * A simplifying assumption is that each of the following <BR>
114: * is the same: <BR>
115: * - name of column in the main table which refers to the simple table <BR>
116: * - name of integer column in simple table <BR>
117: * @param table - name of table to look up keys and displays from
118: * @param keyColumn - column to get key values from
119: * @param dispColumn - column to get display values from
120: * @param criteria - optional selection criteria
121: * @param inputVersion - optional value that allows for a null to be placed at the top
122: * @param trimResults - optional value that trims the rtesults before adding the options
123: * @param toUpper - optional value that makes the option's kays and display values all upper case
124: */
125: public void initialize(String table, String keyColumn,
126: String dispColumn, String criteria, boolean inputVersion,
127: boolean trimResults, boolean toUpper) {
128: resetOptions();
129: if (inputVersion)
130: addOption(null, "");
131:
132: DBConnection connection = null;
133: try {
134: connection = DBConnection.getConnection(getPage()
135: .getApplicationName());
136: java.sql.Statement s = connection.createStatement();
137: String query = null;
138: if (criteria != null) {
139: query = "SELECT " + keyColumn + "," + dispColumn
140: + " FROM " + table + " WHERE " + criteria
141: + " ORDER BY ";
142:
143: if (toUpper)
144: query = query + "UPPER(" + dispColumn + ")";
145: else
146: query += dispColumn;
147: } else {
148: query = "SELECT " + keyColumn + "," + dispColumn
149: + " FROM " + table + " ORDER BY ";
150:
151: if (toUpper)
152: query = query + "UPPER(" + dispColumn + ")";
153: else
154: query += dispColumn;
155: }
156:
157: java.sql.ResultSet r = s.executeQuery(query);
158: System.out.println(query);
159: if (r.next()) {
160: do {
161: if (trimResults) {
162: addOption(r.getObject(1).toString().trim(), r
163: .getObject(2).toString().trim());
164: } else {
165: addOption(r.getObject(1).toString(), r
166: .getObject(2).toString());
167: }
168: } while (r.next());
169: }
170: r.close();
171: s.close();
172: } catch (java.sql.SQLException se) {
173: MessageLog.writeErrorMessage("initialize", se, null);
174: } catch (Exception e) {
175: MessageLog.writeErrorMessage("initialize", e, this );
176: } finally {
177: if (connection != null)
178: connection.freeConnection();
179: }
180:
181: }
182:
183: /**
184: * Constructs a new WmlSelect component.
185: * @param name The name of the component
186: * @param p The page the component will be placed in.
187: */
188: public WmlSelect(String name, HtmlPage p) {
189: super (name, p);
190: }
191:
192: /**
193: * This method returns the key of the first selected option in the list box..
194: */
195: private String getInternalValue() {
196: int optionsSize = _options.size();
197: ThreeObjectContainer t = null;
198: Boolean b = null;
199: for (int i = 0; i < optionsSize; i++) {
200: t = (ThreeObjectContainer) _options.elementAt(i);
201: b = (Boolean) t.getObject3();
202: if (b.booleanValue())
203: return (String) t.getObject1();
204: }
205: return null;
206: }
207:
208: /**
209: *Processes the submitted parameters. This method is called by the framework and should not be called directly
210: */
211: public boolean processParms(Hashtable parms, int rowNo)
212: throws Exception {
213: Object oldValue = _value;
214: String name = getName();
215: if (rowNo > -1) {
216: name += "_" + rowNo;
217: if (_dsBuff != null)
218: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
219: } else {
220: if (_dsBuff != null)
221: oldValue = _dsBuff.getAny(_dsColNo);
222: }
223: String val[] = (String[]) parms.get(name);
224: int optionsSize = _options.size();
225: ThreeObjectContainer t = null;
226: for (int i = 0; i < optionsSize; i++) {
227: t = (ThreeObjectContainer) _options.elementAt(i);
228: t.setObject3(new Boolean(false));
229: }
230: if (val != null) {
231: for (int i = 0; i < val.length; i++) {
232: for (int k = 0; k < optionsSize; k++) {
233: t = (ThreeObjectContainer) _options.elementAt(k);
234: if (t.getObject1().equals(val[i])) {
235: t.setObject3(new Boolean(true));
236: break;
237: }
238: }
239: }
240: }
241: _value = getInternalValue();
242: if (!valuesEqual(oldValue, _value)) {
243: String s = null;
244: if (oldValue != null)
245: s = oldValue.toString();
246: ValueChangedEvent e = new ValueChangedEvent(getPage(),
247: this , getName(), getFullName(), s, _value, rowNo,
248: _dsColNo, _dsBuff);
249: addEvent(e);
250: }
251: return false;
252: }
253:
254: /**
255: *Generates the Html for the component. This method is called by the framework and should not be called directly
256: */
257: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
258: if (!_visible)
259: return;
260:
261: if (!getEnabled()) {
262: String out = getValue(rowNo, true);
263: if (out != null)
264: out = fixSpecialHTMLCharacters(out);
265: else
266: out = "";
267:
268: // Code added in an attempt to remove unsightly ' ' in the HtmlTelephonecomponent and
269: // HtmlSSNComponent.
270: // The premise is that the ' ' was introduced to provide for the case when the field
271: // is disabled and when the string is empty or null
272: // The IF statement is introduced to do as much without impacting non-null and non-empty strings
273: if ((out == null) || ((out != null) && (out.equals(""))))
274: out += " ";
275:
276: if (_disabledFontStartTag != null)
277: p.print(_disabledFontStartTag + out
278: + _disabledFontEndTag);
279: else
280: p.print(out);
281: return;
282: }
283:
284: String name = getName();
285: if (rowNo > -1)
286: name += "_" + rowNo;
287: String tag = "<select id=\"" + name + "\" name=\"" + name
288: + "\"";
289:
290: if (_multiple)
291: tag += " multiple=\"TRUE\"";
292:
293: String value = getValue(rowNo, true);
294:
295: if (value != null)
296: tag += " value=\"" + fixSpecialHTMLCharacters(value) + "\"";
297:
298: if (_class != null)
299: tag += " class=\"" + _class + "\"";
300:
301: if (_iname != null)
302: tag += " iname=\"" + _iname + "\"";
303:
304: if (_ivalue != null)
305: tag += " ivalue=\"" + _ivalue + "\"";
306:
307: if (_tabindex > -1)
308: tag += " tabindex=\"" + _tabindex + "\"";
309:
310: if (_title != null)
311: tag += " title=\"" + _title + "\"";
312:
313: tag += ">";
314:
315: p.print(tag);
316:
317: if (_options.size() > 0) {
318: for (int i = 0; i < _options.size(); i++) {
319: StringBuffer href = null;
320: try {
321: if (_dsEval != null) {
322: _hrefonpick = _dsEval.evaluateRowFormat(i);
323: }
324: } catch (Exception e) {
325: }
326: // sr 12-08-2000 was getting a null pointer exception
327: if (!Util.isNull(_hrefonpick)) {
328: href = new StringBuffer(_hrefonpick);
329: int hrefLength = href.length();
330: for (int k = 0; k < hrefLength; k++) {
331: if (href.charAt(k) == ' ') {
332: href.setCharAt(k, '+');
333: }
334: }
335: }
336: String sValue = ((ThreeObjectContainer) _options
337: .elementAt(i)).getObject1().toString();
338: String sDisp = ((ThreeObjectContainer) _options
339: .elementAt(i)).getObject2().toString();
340: String sOption = "<option value=\"" + sValue + "\"";
341: if (href != null && !href.toString().trim().equals(""))
342: sOption += " onpick=\"" + href + "\"";
343: sOption += ">" + sDisp + "</option>";
344: p.print(sOption);
345: }
346: }
347: if (_wmloptions.size() > 0) {
348: for (int i = 0; i < _wmloptions.size(); i++) {
349: ((WmlOption) _wmloptions.elementAt(i)).generateHTML(p,
350: rowNo);
351: }
352: }
353: p.print("</select>");
354: }
355:
356: /**
357: * This method gets the iname for the text in the component.
358: */
359: public String getIname() {
360: return _iname;
361: }
362:
363: /**
364: * This method gets the ivalue for the component in characters.
365: */
366: public String getIvalue() {
367: return _ivalue;
368: }
369:
370: /**
371: * This method gets the title for the input.
372: */
373: public String getTitle() {
374: return _title;
375: }
376:
377: /**
378: * This method gets the tabindex of the input.
379: */
380: public int getTabIndex() {
381: return _tabindex;
382: }
383:
384: /**
385: * This method gets the multiple flag of the input.
386: */
387: public boolean getMultiple() {
388: return _multiple;
389: }
390:
391: /**
392: * This method sets the iname for the text in the component.
393: */
394: public void setIname(String iname) {
395: _iname = iname;
396: }
397:
398: /**
399: * This method sets the ivalue for the component in characters.
400: */
401: public void setIvalue(String ivalue) {
402: _ivalue = ivalue;
403: }
404:
405: /**
406: * This method sets the multiple flag for input.
407: */
408: public void setMultiple(boolean multiple) {
409: _multiple = multiple;
410: }
411:
412: /**
413: * This method sets the tabindex for input.
414: */
415: public void setTabIndex(int tabindex) {
416: _tabindex = tabindex;
417: }
418:
419: /**
420: * This method sets the title for input.
421: */
422: public void setTitle(String title) {
423: _title = title;
424: }
425:
426: /**
427: * This method gets the postfield form of this component.
428: */
429: public String getPostForm() {
430: if (getVisible() && getEnabled())
431: return "<postfield name=\"" + getName() + "\" value=\"$("
432: + getName() + ")\"/>";
433: return "";
434: }
435:
436: /**
437: *Does the binding for the component. This method is called by the framework and should not be called directly
438: */
439: public void doBinding() throws Exception {
440: for (int i = 0; i < _wmloptions.size(); i++) {
441: WmlOption wo = (WmlOption) _wmloptions.elementAt(i);
442: wo.doBinding();
443: }
444: if (_options.size() > 0) {
445: String dataSource = getDataSource();
446: String dsName = null;
447: String dsExp = null;
448:
449: if (dataSource == null)
450: return;
451:
452: int pos = dataSource.indexOf(":");
453: if (pos == -1)
454: dsName = dataSource;
455: else {
456: dsName = dataSource.substring(0, pos);
457: dsExp = dataSource.substring(pos + 1);
458: }
459:
460: DataStoreBuffer ds = ((JspController) getPage())
461: .getDataSource(dsName);
462: if (ds == null)
463: return;
464:
465: if (!ds.getAutoBind())
466: return;
467:
468: if (dsExp == null)
469: dsExp = _hrefonpick;
470: setHrefExpression(ds, ((JspController) getPage())
471: .convertExpressionOperators(dsExp));
472: }
473: }
474:
475: /**
476: * This method gets the DataStoreEvaluator being used for href expressions.
477: * @return DataStoreEvaluator
478: * @see DataStoreEvaluator
479: */
480: public DataStoreEvaluator getHrefExpression() {
481: return _dsEval;
482: }
483:
484: /**
485: * This method sets a datastore expression that will be used to compute the href for the link.
486: * @param ds com.salmonllc.sql.DataStoreBuffer
487: * @param expression The expression to evaluate
488: */
489: public void setHrefExpression(DataStoreBuffer ds,
490: DataStoreExpression expression) throws Exception {
491: try {
492: _dsEval = new DataStoreEvaluator(ds, expression);
493: } catch (Exception e) {
494: MessageLog
495: .writeErrorMessage(
496: "setHrefExpression(DataStoreBuffer ds, DataStoreExpression expression )",
497: e, this );
498: throw e;
499: }
500: }
501:
502: /**
503: * This method sets a datastore expression that will be used to compute the href for the link.
504: * @param ds com.salmonllc.sql.DataStoreBuffer
505: * @param expression java.lang.String
506: */
507: public void setHrefExpression(DataStoreBuffer ds, String expression)
508: throws Exception {
509: try {
510: _dsEval = new DataStoreEvaluator(ds, expression);
511: } catch (Exception e) {
512: MessageLog
513: .writeErrorMessage(
514: "setHrefExpression(DataStoreBuffer ds, String expression )",
515: e, this);
516: throw e;
517: }
518: }
519:
520: }
|