0001: //** Copyright Statement ***************************************************
0002: //The Salmon Open Framework for Internet Applications (SOFIA)
0003: // Copyright (C) 1999 - 2002, Salmon LLC
0004: //
0005: // This program is free software; you can redistribute it and/or
0006: // modify it under the terms of the GNU General Public License version 2
0007: // as published by the Free Software Foundation;
0008: //
0009: // This program is distributed in the hope that it will be useful,
0010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0012: // GNU General Public License for more details.
0013: //
0014: // You should have received a copy of the GNU General Public License
0015: // along with this program; if not, write to the Free Software
0016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0017: //
0018: // For more information please visit http://www.salmonllc.com
0019: //** End Copyright Statement ***************************************************
0020: package com.salmonllc.html;
0021:
0022: /////////////////////////
0023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlDropDownList.java $
0024: //$Author: Srufle $
0025: //$Revision: 49 $
0026: //$Modtime: 8/06/04 3:11p $
0027: /////////////////////////
0028: import com.salmonllc.html.events.*;
0029:
0030: import com.salmonllc.properties.*;
0031:
0032: import com.salmonllc.sql.*;
0033:
0034: import com.salmonllc.util.*;
0035:
0036: import java.util.*;
0037:
0038: /**
0039: * This class is used for text input in a page in the form of a drop down list box of options.
0040: */
0041: public class HtmlDropDownList extends HtmlFormComponent implements
0042: PageListener {
0043: private Integer _tabIndex;
0044:
0045: /*Claudio Pi (4-01-2003) Changed to allow options sorting*/
0046: private OptionsSort _optionsVec = new OptionsSort();
0047: private String _criteria;
0048: private String _dispColumn;
0049: private String _fontTagEnd;
0050: private String _fontTagStart;
0051: private String _keyColumn;
0052: private String _onChange;
0053: private String _onClick;
0054: private String _onFocus;
0055: private String _onLoseFocus;
0056: private String _style;
0057: private String _table;
0058: private boolean _inputVersion;
0059: private boolean _reloadDropDownInEveryPageRequest;
0060: private boolean _toUpper;
0061: private boolean _trimResults;
0062:
0063: /**
0064: * Constructs a new HTMLDropDownList component.
0065: *
0066: * @param name The name of the component
0067: * @param p The page the component will be placed in.
0068: */
0069: public HtmlDropDownList(String name, com.salmonllc.html.HtmlPage p) {
0070: this (name, null, p);
0071: }
0072:
0073: /**
0074: * Constructs a new HTMLDropDownList component.
0075: *
0076: * @param name The name of the component
0077: * @param theme The theme to use for loading properties.
0078: * @param p The page the component will be placed in.
0079: */
0080: public HtmlDropDownList(String name, String theme,
0081: com.salmonllc.html.HtmlPage p) {
0082: super (name, theme, p);
0083: }
0084:
0085: /**
0086: * Creates a drop-down list based on a table with an integer primary key column (typically an id) and a string column. A simplifying assumption is that each of the following is the same: <BR> - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0087: *
0088: * @param name DOCUMENT ME!
0089: * @param theme DOCUMENT ME!
0090: * @param page com.salmonllc.html.HtmlPage The page hold the new component
0091: * @param table - name of table to look up keys and displays from
0092: * @param keyColumn - column to get key values from
0093: * @param dispColumn - column to get display values from
0094: * @param criteria - optional selection criteria
0095: * @param inputVersion - optional value that allows for a null to be placed at the top
0096: */
0097: public HtmlDropDownList(String name, String theme, HtmlPage page,
0098: String table, String keyColumn, String dispColumn,
0099: String criteria, boolean inputVersion) {
0100: super (name, theme, page);
0101: initialize(table, keyColumn, dispColumn, criteria, inputVersion);
0102: }
0103:
0104: /**
0105: * Creates a drop-down list based on a table with an integer primary key column (typically an id) and a string column. A simplifying assumption is that each of the following is the same: - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0106: *
0107: * @param name DOCUMENT ME!
0108: * @param theme DOCUMENT ME!
0109: * @param page com.salmonllc.html.HtmlPage The page hold the new component
0110: * @param table - name of table to look up keys and displays from
0111: * @param keyColumn - column to get key values from
0112: * @param dispColumn - column to get display values from
0113: * @param inputVersion - optional value that allows for a null to be placed at the top
0114: */
0115: public HtmlDropDownList(String name, String theme, HtmlPage page,
0116: String table, String keyColumn, String dispColumn,
0117: boolean inputVersion) {
0118: super (name, theme, page);
0119: initialize(table, keyColumn, dispColumn, null, inputVersion);
0120: }
0121:
0122: /**
0123: * Sets the criteria
0124: *
0125: * @param criteria
0126: */
0127: public void setCriteria(String criteria) {
0128: this ._criteria = criteria;
0129: }
0130:
0131: /**
0132: * Returns the criteria
0133: *
0134: * @return
0135: */
0136: public String getCriteria() {
0137: return _criteria;
0138: }
0139:
0140: /**
0141: * This method sets the end font tag for the component.
0142: *
0143: * @param value DOCUMENT ME!
0144: */
0145: public void setFontEndTag(String value) {
0146: _fontTagEnd = value;
0147: }
0148:
0149: /**
0150: * This method gets the end font tag for the component.
0151: *
0152: * @return DOCUMENT ME!
0153: */
0154: public String getFontEndTag() {
0155: return _fontTagEnd;
0156: }
0157:
0158: /**
0159: * This method sets the start font tag for the component.
0160: *
0161: * @param value DOCUMENT ME!
0162: */
0163: public void setFontStartTag(String value) {
0164: _fontTagStart = value;
0165: }
0166:
0167: /**
0168: * This method gets the start font tag for the component.
0169: *
0170: * @return DOCUMENT ME!
0171: */
0172: public String getFontStartTag() {
0173: return _fontTagStart;
0174: }
0175:
0176: public void setInputVersion(boolean _inputVersion) {
0177: this ._inputVersion = _inputVersion;
0178: }
0179:
0180: public boolean isInputVersion() {
0181: return _inputVersion;
0182: }
0183:
0184: /**
0185: * This method sets the javascript to be executed when the value of the text in the component changes.
0186: *
0187: * @param value DOCUMENT ME!
0188: */
0189: public void setOnChange(String value) {
0190: _onChange = value;
0191: }
0192:
0193: /**
0194: * This method gets the javascript to be executed when the value of the text in the component changes.
0195: *
0196: * @return DOCUMENT ME!
0197: */
0198: public String getOnChange() {
0199: return _onChange;
0200: }
0201:
0202: /**
0203: * This method sets the javascript to be executed when the component is clicked on.
0204: *
0205: * @param value DOCUMENT ME!
0206: */
0207: public void setOnClick(String value) {
0208: _onClick = value;
0209: }
0210:
0211: /**
0212: * This method gets the javascript to be executed when the component is clicked on.
0213: *
0214: * @return DOCUMENT ME!
0215: */
0216: public String getOnClick() {
0217: return _onClick;
0218: }
0219:
0220: /**
0221: * This method sets the javascript to be executed when the component gains focus.
0222: *
0223: * @param value DOCUMENT ME!
0224: */
0225: public void setOnFocus(String value) {
0226: _onFocus = value;
0227: }
0228:
0229: /**
0230: * This method gets the javascript to be executed when the component gets focus.
0231: *
0232: * @return DOCUMENT ME!
0233: */
0234: public String getOnFocus() {
0235: return _onFocus;
0236: }
0237:
0238: /**
0239: * This method sets the javascript to be executed when the component loses focus.
0240: *
0241: * @param value DOCUMENT ME!
0242: */
0243: public void setOnLoseFocus(String value) {
0244: _onLoseFocus = value;
0245: }
0246:
0247: /**
0248: * This method gets the javascript to be executed when the component loses focus.
0249: *
0250: * @return DOCUMENT ME!
0251: */
0252: public String getOnLoseFocus() {
0253: return _onLoseFocus;
0254: }
0255:
0256: /**
0257: * This method returns the number of options in the component.
0258: *
0259: * @return DOCUMENT ME!
0260: */
0261: public int getOptionCount() {
0262: return _optionsVec.size();
0263: }
0264:
0265: /**
0266: * Use this method to set the key of the option at index.
0267: *
0268: * @param index DOCUMENT ME!
0269: * @param key DOCUMENT ME!
0270: */
0271: public void setOptionKey(int index, String key) {
0272: if ((index < 0) || (index >= _optionsVec.size())) {
0273: return;
0274: }
0275:
0276: HtmlOption opt = (HtmlOption) _optionsVec.elementAt(index);
0277: opt.setKey(key);
0278: }
0279:
0280: /**
0281: * Use this method get the value of the key at index.
0282: *
0283: * @param index DOCUMENT ME!
0284: *
0285: * @return DOCUMENT ME!
0286: */
0287: public String getOptionKey(int index) {
0288: if ((index < 0) || (index >= _optionsVec.size())) {
0289: return null;
0290: }
0291:
0292: HtmlOption opt = (HtmlOption) _optionsVec.elementAt(index);
0293:
0294: return (String) opt.getKey();
0295: }
0296:
0297: /**
0298: * Use this method to set the value of the option at index.
0299: *
0300: * @param index DOCUMENT ME!
0301: * @param value DOCUMENT ME!
0302: */
0303: public void setOptionValue(int index, String value) {
0304: if ((index < 0) || (index >= _optionsVec.size())) {
0305: return;
0306: }
0307:
0308: HtmlOption opt = (HtmlOption) _optionsVec.elementAt(index);
0309: opt.setDisplay(value);
0310: }
0311:
0312: /**
0313: * Use this method get the value of the option at index.
0314: *
0315: * @param index DOCUMENT ME!
0316: *
0317: * @return DOCUMENT ME!
0318: */
0319: public String getOptionValue(int index) {
0320: if ((index < 0) || (index >= _optionsVec.size())) {
0321: return null;
0322: }
0323:
0324: HtmlOption opt = (HtmlOption) _optionsVec.elementAt(index);
0325:
0326: return (String) opt.getDisplay();
0327: }
0328:
0329: /**
0330: * This method adds the options in the list. This method can be used in a property expression. To use this in a property expression ; 1- Create a DataStoreExpression class which will extend DataStoreExpression. in the evaluateExpression() method handle the row value and create a list of HtmlOption objects and return all of them in a ArrayList. 2- In the controller add the property expression
0331: * to the datatable. Example; MyDSExpressionThatExtendsDataStoreExpression dsSourceExp = new MyDSExpressionThatExtendsDataStoreExpression(); _myDataTable.addPropertyExpression(_ddlMyDropDownList, "Options", dsSourceExp); //TODO Add some examples for this method.
0332: *
0333: * @param options
0334: */
0335: public void setOptions(ArrayList options) {
0336: if (options == null) {
0337: return;
0338: }
0339:
0340: resetOptions();
0341:
0342: int iSize = options.size();
0343:
0344: for (int i = 0; i < iSize; i++) {
0345: HtmlOption option = (HtmlOption) options.get(i);
0346: addOption(option);
0347: }
0348: }
0349:
0350: public OptionsSort getOptionsVector()
0351: throws CloneNotSupportedException {
0352: OptionsSort retVal = new OptionsSort();
0353: int iSize = _optionsVec.size();
0354: HtmlOption opt = null;
0355:
0356: for (int i = 0; i < iSize; i++) {
0357: opt = (HtmlOption) ((HtmlOption) _optionsVec.elementAt(i))
0358: .clone();
0359: retVal.add(opt);
0360: }
0361:
0362: return retVal;
0363: }
0364:
0365: /**
0366: * This methods returns the ReloadDropDownInEveryPageRequest attribute
0367: *
0368: * @return
0369: */
0370: public boolean getReloadDropDownInEveryPageRequest() {
0371: return _reloadDropDownInEveryPageRequest;
0372: }
0373:
0374: /**
0375: * Sets the value of the selected index.
0376: *
0377: * @param index DOCUMENT ME!
0378: */
0379: public void setSelectedIndex(int index) {
0380: setSelectedIndex(index, -1);
0381: }
0382:
0383: /**
0384: * Sets the value of the selected index.
0385: *
0386: * @param index DOCUMENT ME!
0387: * @param row DOCUMENT ME!
0388: */
0389: public void setSelectedIndex(int index, int row) {
0390: if ((index < 0) || (index >= _optionsVec.size())) {
0391: return;
0392: }
0393:
0394: HtmlOption opt = (HtmlOption) _optionsVec.elementAt(index);
0395:
0396: if (opt.getKey() == null) {
0397: setValue(null, row);
0398: } else {
0399: setValue(opt.getKey().toString(), row);
0400: }
0401: }
0402:
0403: /**
0404: * Returns the index of the selected option.
0405: *
0406: * @return DOCUMENT ME!
0407: */
0408: public int getSelectedIndex() {
0409: String option = getValue();
0410:
0411: return findOptionIndexOf(option);
0412: }
0413:
0414: /**
0415: * Returns the index of the selected option.
0416: *
0417: * @param rowNo DOCUMENT ME!
0418: *
0419: * @return DOCUMENT ME!
0420: */
0421: public int getSelectedIndex(int rowNo) {
0422: String option = getValue(rowNo);
0423:
0424: return findOptionIndexOf(option);
0425: }
0426:
0427: /**
0428: * This method sets the property style (allows for the inlining of style attributes like color:#666666;) for the component.
0429: *
0430: * @param style - The style text to use.
0431: */
0432: public void setStyle(String style) {
0433: _style = style;
0434: }
0435:
0436: /**
0437: * This method gets the property style (allows for the inlining of style attributes like color:#666666;) for the component.
0438: *
0439: * @return DOCUMENT ME!
0440: */
0441: public String getStyle() {
0442: return _style;
0443: }
0444:
0445: /**
0446: * 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
0447: *
0448: * @param val
0449: */
0450: public void setTabIndex(int val) {
0451: if (val == -1) {
0452: _tabIndex = null;
0453: } else {
0454: _tabIndex = new Integer(val);
0455: }
0456: }
0457:
0458: /**
0459: * Returns the tab index html attribute
0460: *
0461: * @return int
0462: */
0463: public int getTabIndex() {
0464: if (_tabIndex == null) {
0465: return -1;
0466: }
0467:
0468: return _tabIndex.intValue();
0469: }
0470:
0471: /**
0472: * This method sets the property theme for the component.
0473: *
0474: * @param theme The theme to use.
0475: */
0476: public void setTheme(String theme) {
0477: super .setTheme(theme);
0478:
0479: Props props = getPage().getPageProperties();
0480:
0481: _fontTagStart = props.getThemeProperty(theme, Props.FONT_DDLB
0482: + Props.TAG_START);
0483: _fontTagEnd = props.getThemeProperty(theme, Props.FONT_DDLB
0484: + Props.TAG_END);
0485: }
0486:
0487: /**
0488: * Flag to convert the options to the upper case
0489: *
0490: * @param toUpper
0491: */
0492: public void setToUpper(boolean toUpper) {
0493: this ._toUpper = toUpper;
0494: }
0495:
0496: /**
0497: * Flag to convert the options to the upper case
0498: *
0499: * @return
0500: */
0501: public boolean isToUpper() {
0502: return _toUpper;
0503: }
0504:
0505: /**
0506: * Flag to trim the options
0507: *
0508: * @param trimResults
0509: */
0510: public void setTrimResults(boolean trimResults) {
0511: this ._trimResults = trimResults;
0512: }
0513:
0514: /**
0515: * Flag to trim the options
0516: *
0517: * @return
0518: */
0519: public boolean isTrimResults() {
0520: return _trimResults;
0521: }
0522:
0523: /**
0524: * Use this method to add new choices to the list.
0525: *
0526: * @param key The internal name of the item (must be unique)
0527: * @param disp The value to be displayed on the list.
0528: */
0529: public void addOption(String key, String disp) {
0530: HtmlOption opt = new HtmlOption(key, disp);
0531: _optionsVec.addElement(opt);
0532: }
0533:
0534: /**
0535: * Use this method to add new choices to the list.
0536: *
0537: * @param opt - This allows for full control of the option before adding
0538: */
0539: public void addOption(HtmlOption opt) {
0540: _optionsVec.addElement(opt);
0541: }
0542:
0543: /**
0544: * This method returns the index of the option with the specified key.
0545: *
0546: * @param key DOCUMENT ME!
0547: *
0548: * @return The index of the key or -1 if not found.
0549: */
0550: public int findOption2IndexOf(String key) {
0551: int optionsSize = _optionsVec.size();
0552: HtmlOption opt = null;
0553:
0554: for (int i = 0; i < optionsSize; i++) {
0555: opt = (HtmlOption) _optionsVec.elementAt(i);
0556:
0557: if (key == null) {
0558: if (opt.getDisplay() == null) {
0559: return i;
0560: }
0561: } else if (key.equals(opt.getDisplay())) {
0562: return i;
0563: }
0564: }
0565:
0566: return -1;
0567: }
0568:
0569: /**
0570: * This method returns the index of the option with the specified key.
0571: *
0572: * @param key DOCUMENT ME!
0573: *
0574: * @return The index of the key or -1 if not found.
0575: */
0576: public int findOptionIndexOf(String key) {
0577: int optionsSize = _optionsVec.size();
0578: HtmlOption opt = null;
0579:
0580: for (int i = 0; i < optionsSize; i++) {
0581: opt = (HtmlOption) _optionsVec.elementAt(i);
0582:
0583: if (key == null) {
0584: if (opt.getKey() == null) {
0585: return i;
0586: }
0587: } else if (key.equals(opt.getKey())) {
0588: return i;
0589: }
0590: }
0591:
0592: return -1;
0593: }
0594:
0595: public void generateHTML(java.io.PrintWriter p, int rowNo)
0596: throws Exception {
0597: if (!_visible) {
0598: return;
0599: }
0600:
0601: String name = getFullName();
0602:
0603: if (rowNo > -1) {
0604: name += ("_" + rowNo);
0605: }
0606:
0607: StringBuffer tag = new StringBuffer("<SELECT NAME=\"" + name
0608: + "\"");
0609:
0610: if (!_enabled) {
0611: if (useDisabledAttribute()) {
0612: tag.append(" disabled=\"true\"");
0613: } else {
0614: String out = getOptionValue(findOptionIndexOf(getValue(
0615: rowNo, true)));
0616:
0617: if (out == null) {
0618: out = "";
0619: }
0620:
0621: out += " ";
0622:
0623: if (_disabledFontStartTag != null) {
0624: p.print(_disabledFontStartTag + out
0625: + _disabledFontEndTag);
0626: } else {
0627: p.print(out);
0628: }
0629:
0630: return;
0631: }
0632: }
0633:
0634: if (_onChange != null) {
0635: tag.append(" onChange=\"" + _onChange + "\"");
0636: }
0637:
0638: if (_onFocus != null) {
0639: tag.append(" onFocus=\"" + _onFocus + "\"");
0640: }
0641:
0642: if (_onLoseFocus != null) {
0643: tag.append(" onBlur=\"" + _onLoseFocus + "\"");
0644: }
0645:
0646: if (_onClick != null) {
0647: tag.append(" onClick=\"" + _onClick + "\"");
0648: }
0649:
0650: if (getClassName() != null) {
0651: tag.append(" class=\"" + getClassName() + "\"");
0652: }
0653:
0654: if (Util.isFilled(getStyle())) {
0655: tag.append(" style=\"" + getStyle() + "\"");
0656: }
0657:
0658: if (_tabIndex != null) {
0659: tag.append(" tabindex=\"" + _tabIndex + "\"");
0660: }
0661:
0662: tag.append(">");
0663:
0664: String value = getValue(rowNo, true);
0665:
0666: int selectedIndex = findOptionIndexOf(value);
0667:
0668: //
0669: int optionsSize = _optionsVec.size();
0670: HtmlOption opt = null;
0671:
0672: for (int i = 0; i < optionsSize; i++) {
0673: opt = (HtmlOption) _optionsVec.elementAt(i);
0674:
0675: tag.append("<OPTION");
0676:
0677: if (Util.isFilled(opt.getClassname())) {
0678: tag.append(" CLASS=\"");
0679: tag.append(opt.getClassname());
0680: tag.append("\"");
0681: }
0682:
0683: if (Util.isFilled(opt.getStyle())) {
0684: tag.append(" STYLE=\"");
0685: tag.append(opt.getStyle());
0686: tag.append("\"");
0687: }
0688:
0689: if (opt.getKey() == null) {
0690: tag.append(" VALUE=\"\"");
0691: } else {
0692: tag.append(" VALUE=\"");
0693: tag.append(opt.getKey());
0694: tag.append("\"");
0695: }
0696:
0697: if (selectedIndex == i) {
0698: tag.append(" SELECTED");
0699: }
0700:
0701: tag.append(">" + (String) opt.getDisplay() + "</OPTION>");
0702: }
0703:
0704: tag.append("</SELECT>");
0705:
0706: if (_fontTagStart != null) {
0707: tag.insert(0, _fontTagStart);
0708: tag.append(_fontTagEnd);
0709: }
0710:
0711: if (_generateNewline) {
0712: p.println(tag.toString());
0713: } else {
0714: p.print(tag.toString());
0715: }
0716:
0717: writeFocusScript(p, rowNo);
0718: }
0719:
0720: /**
0721: * Creates a drop-down list based on a table with an <BR> integer primary key column (typically an id) and a string column.<BR> A simplifying assumption is that each of the following <BR> is the same: <BR> - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0722: *
0723: * @param table - name of table to look up keys and displays from
0724: * @param keyColumn - column to get key values from
0725: * @param dispColumn - column to get display values from
0726: * @param criteria - optional selection criteria
0727: * @param inputVersion - optional value that allows for a null to be placed at the top
0728: */
0729: public void initialize(String table, String keyColumn,
0730: String dispColumn, String criteria, boolean inputVersion) {
0731: initialize(table, keyColumn, dispColumn, criteria,
0732: inputVersion, false);
0733: }
0734:
0735: /**
0736: * Creates a drop-down list based on a table with an <BR> integer primary key column (typically an id) and a string column.<BR> A simplifying assumption is that each of the following <BR> is the same: <BR> - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0737: *
0738: * @param table - name of table to look up keys and displays from
0739: * @param keyColumn - column to get key values from
0740: * @param dispColumn - column to get display values from
0741: * @param criteria - optional selection criteria
0742: * @param inputVersion - optional value that allows for a null to be placed at the top
0743: * @param trimResults - optional value that trims the rtesults before adding the options
0744: */
0745: public void initialize(String table, String keyColumn,
0746: String dispColumn, String criteria, boolean inputVersion,
0747: boolean trimResults) {
0748: initialize(table, keyColumn, dispColumn, criteria,
0749: inputVersion, trimResults, false);
0750: }
0751:
0752: /**
0753: * Creates a drop-down list based on a table with an <BR> integer primary key column (typically an id) and a string column.<BR> A simplifying assumption is that each of the following <BR> is the same: <BR> - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0754: *
0755: * @param table - name of table to look up keys and displays from
0756: * @param keyColumn - column to get key values from
0757: * @param dispColumn - column to get display values from
0758: * @param criteria - optional selection criteria
0759: * @param inputVersion - optional value that allows for a null to be placed at the top
0760: * @param trimResults - optional value that trims the rtesults before adding the options
0761: * @param toUpper - optional value that makes the option's kays and display values all upper case
0762: */
0763: public void initialize(String table, String keyColumn,
0764: String dispColumn, String criteria, boolean inputVersion,
0765: boolean trimResults, boolean toUpper) {
0766: initialize(table, keyColumn, dispColumn, criteria,
0767: inputVersion, trimResults, toUpper, false);
0768: }
0769:
0770: /**
0771: * Creates a drop-down list based on a table with an <BR> integer primary key column (typically an id) and a string column.<BR> A simplifying assumption is that each of the following <BR> is the same: <BR> - name of column in the main table which refers to the simple table <BR> - name of integer column in simple table <BR>
0772: *
0773: * @param table - name of table to look up keys and displays from
0774: * @param keyColumn - column to get key values from
0775: * @param dispColumn - column to get display values from
0776: * @param criteria - optional selection criteria
0777: * @param inputVersion - optional value that allows for a null to be placed at the top
0778: * @param trimResults - optional value that trims the rtesults before adding the options
0779: * @param toUpper - optional value that makes the option's kays and display values all upper case
0780: * @param reloadOptionsEveryPageRequest - If this is set and the options are bound to the database, than options will be refereshed in every page request. This will increase trhe database trafic.
0781: */
0782: public void initialize(String table, String keyColumn,
0783: String dispColumn, String criteria, boolean inputVersion,
0784: boolean trimResults, boolean toUpper,
0785: boolean reloadOptionsEveryPageRequest) {
0786: resetOptions();
0787:
0788: if (inputVersion) {
0789: addOption(null, "");
0790: }
0791:
0792: _table = table;
0793: _keyColumn = keyColumn;
0794: _dispColumn = dispColumn;
0795: _reloadDropDownInEveryPageRequest = reloadOptionsEveryPageRequest;
0796: _criteria = criteria;
0797: _trimResults = trimResults;
0798: _toUpper = toUpper;
0799: _inputVersion = inputVersion;
0800:
0801: populateDropdownOptions();
0802: }
0803:
0804: public void pageRequestEnd(PageEvent p) throws Exception {
0805: }
0806:
0807: /**
0808: * This method refreshes the dropdown if the attribute is set.
0809: *
0810: * @param p
0811: *
0812: * @throws Exception
0813: */
0814: public void pageRequested(PageEvent p) throws Exception {
0815: if (getReloadDropDownInEveryPageRequest()
0816: && !getPage().isReferredByCurrentPage()) {
0817: resetOptions();
0818:
0819: if (isInputVersion()) {
0820: addOption(null, "");
0821: }
0822:
0823: populateDropdownOptions();
0824: }
0825: }
0826:
0827: public void pageSubmitEnd(PageEvent p) {
0828: }
0829:
0830: public void pageSubmitted(PageEvent p) {
0831: }
0832:
0833: /**
0834: * This method populates the dropdown list options
0835: */
0836: public void populateDropdownOptions() {
0837: DBConnection connection = null;
0838:
0839: try {
0840: connection = DBConnection.getConnection(getPage()
0841: .getApplicationName());
0842:
0843: java.sql.Statement s = connection.createStatement();
0844: String query = null;
0845:
0846: //fc 06/24/04 Added the following code contributed by ilev.
0847:
0848: /** added by ilev 2004/03 */
0849: String alias = "";
0850:
0851: if (_keyColumn.equals(_dispColumn)) {
0852: alias = " AS DISPCOL";
0853: }
0854:
0855: /**
0856: * end added
0857: */
0858: if (getCriteria() != null) {
0859: //fc 06/24/04 Added the following code contributed by ilev.
0860:
0861: /**
0862: * alias added by ilev 2004/03
0863: */
0864: query = "SELECT " + _keyColumn + "," + _dispColumn
0865: + alias + " FROM " + _table + " WHERE "
0866: + getCriteria() + " ORDER BY ";
0867:
0868: if (isToUpper()) {
0869: query = query + "UPPER(" + _dispColumn + ")";
0870: } else {
0871: query += _dispColumn;
0872: }
0873: } else {
0874: //fc 06/24/04 Added the following code contributed by ilev.
0875:
0876: /**
0877: * alias added by ilev 2004/03
0878: */
0879: query = "SELECT " + _keyColumn + "," + _dispColumn
0880: + alias + " FROM " + _table + " ORDER BY ";
0881:
0882: if (isToUpper()) {
0883: query = query + "UPPER(" + _dispColumn + ")";
0884: } else {
0885: query += _dispColumn;
0886: }
0887: }
0888:
0889: java.sql.ResultSet r = s.executeQuery(query);
0890:
0891: int cols = r.getMetaData().getColumnCount();
0892: String key;
0893: String display;
0894:
0895: if (r.next()) {
0896: do {
0897: key = "";
0898: display = "";
0899:
0900: if (isTrimResults()) {
0901: key = r.getObject(1).toString().trim();
0902:
0903: for (int i = 2; i <= cols; i++) {
0904: display = display
0905: + r.getObject(i).toString().trim()
0906: + " ";
0907: }
0908:
0909: addOption(key, display);
0910: } else {
0911: key = r.getObject(1).toString();
0912:
0913: for (int i = 2; i <= cols; i++) {
0914: display = display
0915: + r.getObject(i).toString() + " ";
0916: }
0917:
0918: addOption(key, display);
0919: }
0920: } while (r.next());
0921: }
0922:
0923: r.close();
0924: s.close();
0925: } catch (java.sql.SQLException se) {
0926: MessageLog.writeErrorMessage("initialize", se, null);
0927: } catch (Exception e) {
0928: MessageLog.writeErrorMessage("initialize", e, this );
0929: } finally {
0930: if (connection != null) {
0931: connection.freeConnection();
0932: }
0933: }
0934: }
0935:
0936: public boolean processParms(Hashtable parms, int rowNo)
0937: throws Exception {
0938: // fc: 07/17/02 Commented out the below lines as they are no longer required,
0939: // since a better approach is to check to see if the field is contained in
0940: // the form when submitted. see other change below
0941: // if (!getVisible() || !getEnabled())
0942: // return false;
0943: Object oldValue = _value;
0944:
0945: String name = getFullName();
0946:
0947: if (rowNo > -1) {
0948: name += ("_" + rowNo);
0949:
0950: if (_dsBuff != null) {
0951: try {
0952: oldValue = _dsBuff.getAny(rowNo, _dsColNo);
0953: } catch (Exception e) {
0954: oldValue = null;
0955: }
0956: }
0957: } else {
0958: if (_dsBuff != null) {
0959: try {
0960: oldValue = _dsBuff.getAny(_dsColNo);
0961: } catch (Exception e) {
0962: oldValue = null;
0963: }
0964: }
0965: }
0966:
0967: //fc: 07/17/02 Added check to see if component exists in form if not
0968: // then do not process.
0969: if (parms.containsKey(name)) {
0970: String val[] = (String[]) parms.get(name);
0971:
0972: if (val == null) {
0973: _value = null;
0974: } else {
0975: _value = val[0];
0976:
0977: if (_value.equals("")) {
0978: _value = null;
0979: }
0980: }
0981:
0982: if (!valuesEqual(oldValue, _value)) {
0983: String s = null;
0984:
0985: if (oldValue != null) {
0986: s = oldValue.toString();
0987: }
0988:
0989: ValueChangedEvent e = new ValueChangedEvent(getPage(),
0990: this , getName(), getFullName(), s, _value,
0991: rowNo, _dsColNo, _dsBuff);
0992: addEvent(e);
0993: }
0994: }
0995:
0996: return false;
0997: }
0998:
0999: /**
1000: * Use this method to remove an option from the list.
1001: *
1002: * @param index The index of the option to remove.
1003: */
1004: public void removeOption(int index) {
1005: if ((index < 0) || (index >= _optionsVec.size())) {
1006: return;
1007: }
1008:
1009: _optionsVec.removeElementAt(index);
1010: }
1011:
1012: public void removeOption(String optionText) {
1013: HtmlOption temp = null;
1014: String textTemp = null;
1015:
1016: for (int i = 0; i < _optionsVec.size(); i++) {
1017: temp = (HtmlOption) _optionsVec.elementAt(i);
1018: textTemp = (String) temp.getDisplay();
1019:
1020: if (optionText.equals(textTemp)) {
1021: _optionsVec.removeElementAt(i);
1022:
1023: break;
1024: }
1025: }
1026: }
1027:
1028: /**
1029: * This method removes all options from the component.
1030: */
1031: public void resetOptions() {
1032: _optionsVec.removeAllElements();
1033: }
1034:
1035: /**
1036: * This method sets the ReloadDropDownInEveryPageRequest attribute
1037: *
1038: * @param reloadDropDownInEveryPageRequest
1039: */
1040: public void set_reloadDropDownInEveryPageRequest(
1041: boolean reloadDropDownInEveryPageRequest) {
1042: this ._reloadDropDownInEveryPageRequest = reloadDropDownInEveryPageRequest;
1043: }
1044:
1045: /**
1046: * Claudio Pi (4-01-2003) This method sorts the dropdown list based on its options values in the default order direction.
1047: */
1048: public void sort() {
1049: _optionsVec.sort();
1050: }
1051:
1052: /**
1053: * Claudio Pi (4-01-2003) This method sorts the dropdown list based on its options values in the given order direction.
1054: *
1055: * @param dir direction in which the options will be sorted (0=Ascending or 1=Descending order)
1056: */
1057: public void sort(int dir) {
1058: _optionsVec.setSortDir(dir);
1059: _optionsVec.sort();
1060: }
1061: }
|