001: package com.salmonllc.examples.example2;
002:
003: //The Salmon Open Framework for Internet Applications (SOFIA)
004: //Copyright (C) 1999 - 2002, Salmon LLC
005: //
006: //This program is free software; you can redistribute it and/or
007: //modify it under the terms of the GNU General Public License version 2
008: //as published by the Free Software Foundation;
009: //
010: //This program is distributed in the hope that it will be useful,
011: //but WITHOUT ANY WARRANTY; without even the implied warranty of
012: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: //GNU General Public License for more details.
014: //
015: //You should have received a copy of the GNU General Public License
016: //along with this program; if not, write to the Free Software
017: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: //
019: //For more information please visit http://www.salmonllc.com
020:
021: import com.salmonllc.html.*;
022: import com.salmonllc.html.events.PageListener;
023: import com.salmonllc.html.events.PageEvent;
024: import com.salmonllc.sql.DataDictionary;
025: import com.salmonllc.sql.ColumnDefinition;
026:
027: import java.util.Vector;
028:
029: /**
030: * This class implements a combination component.
031: * It's a drop down list of database tables and a list box of columns in a table.
032: * When the user chooses a table, it automatically submits the page. The component
033: * figures out which table the user selected and populates the list box with the
034: * appropriate columns.
035: *
036: * This component uses the com.salmonllc.sql.DataDictionary component to query the database engine for the tables and columns.
037: */
038: public class DBSelector extends HtmlTable implements PageListener {
039: DataDictionary _dd;
040: HtmlDropDownList _tables;
041: HtmlListBox _columns;
042: HtmlHiddenField _hidden;
043:
044: /**
045: * Create a new DBSelector component
046: */
047: public DBSelector(String name, com.salmonllc.html.HtmlPage p) {
048: super (name, p);
049:
050: //set some properties of the table
051: setBorder(0);
052: setCellSpacing(1);
053: setCellPadding(1);
054:
055: //create the components to display on the screen
056: _tables = new HtmlDropDownList("tables", p);
057: _columns = new HtmlListBox("columns", p);
058: setComponentAt(0, 0, _tables);
059: setComponentAt(1, 0, _columns);
060:
061: //populate the tables component
062: _dd = new DataDictionary(p.getApplicationName());
063: Vector v = _dd.getTableNames();
064: for (int i = 0; i < v.size(); i++)
065: _tables.addOption((String) v.elementAt(i), (String) v
066: .elementAt(i));
067: if (v.size() > 0) {
068: _tables.setSelectedIndex(0);
069: populateColumns();
070: }
071:
072: //add a hidden field and some javascript to the component
073: //so it submits the page when the user changes it.
074: //This hidden field is used so the submit performed event can test to
075: //make sure that it was this component that submitted the page and not
076: //a different one.
077: _hidden = new HtmlHiddenField("hidden", "0", p);
078: setComponentAt(2, 0, _hidden);
079:
080: //add a page listener so this page will get notified when the page containing the component is submitted
081: p.addPageListener(this );
082: }
083:
084: /**
085: * This method is populates the column list box, based on what the user selected in the table drop down
086: */
087: private void populateColumns() {
088: String table = _tables.getValue();
089: _columns.resetOptions();
090: if (table == null)
091: return;
092: Vector v = _dd.getColumns(table);
093: for (int i = 0; i < v.size(); i++) {
094: ColumnDefinition colDef = (ColumnDefinition) v.elementAt(i);
095: _columns.addOption(colDef.getColumnName(), colDef
096: .getColumnName(), true);
097: }
098: }
099:
100: /**
101: * This method occurs each time a page is submitted.
102: * The hidden field in this component is checked to see if this is the comp that submitted the page.
103: */
104: public void pageSubmitEnd(PageEvent p) {
105: //if the hidden field has a value of one, then this component submitted the page
106: if (_hidden.getValue() != null
107: && _hidden.getValue().equals("1")) {
108: populateColumns();
109: _hidden.setValue("0");
110: }
111: }
112:
113: /**
114: * This method returns an array of column definition objects, one for each column selected in the list box.
115: */
116: public ColumnDefinition[] getSelectedColumns() {
117: Vector v = new Vector();
118: String tableName = _tables.getValue() + ".";
119: for (int i = 0; i < _columns.getOptionCount(); i++) {
120: if (_columns.getOptionSelected(i)) {
121: ColumnDefinition def = _dd.getColumnDefintion(tableName
122: + _columns.getOptionKey(i));
123: v.add(def);
124: }
125: }
126: ColumnDefinition ret[] = new ColumnDefinition[v.size()];
127: v.copyInto(ret);
128: return ret;
129: }
130:
131: /**
132: * This event is part of the PageListener interface. It is only implemented here to fill out the interface, but the component doesn't use it.
133: */
134: public void pageRequested(PageEvent p) throws Exception {
135: }
136:
137: /**
138: * This event is part of the PageListener interface. It is only implemented here to fill out the interface, but the component doesn't use it.
139: */
140: public void pageRequestEnd(PageEvent p) throws Exception {
141: }
142:
143: /**
144: * This event is part of the PageListener interface. It is only implemented here to fill out the interface, but the component doesn't use it.
145: */
146: public void pageSubmitted(PageEvent p) {
147: }
148:
149: /**
150: * We can't set the submit javascript until we know what form this component is in and we don't know that until the component is added to the container. So we can override setParent and set the data there
151: */
152: public void setParent(HtmlComponent parent) {
153: super .setParent(parent);
154: _tables.setOnChange(getFormString() + _hidden.getFullName()
155: + ".value=1;" + getFormString() + "submit();");
156: }
157:
158: }
|