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.forms;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/forms/ListForm.java $
024: //$Author: Dan $
025: //$Revision: 73 $
026: //$Modtime: 6/11/03 4:27p $
027: /////////////////////////
028:
029: import java.util.Vector;
030:
031: import com.salmonllc.html.HtmlComponent;
032: import com.salmonllc.html.HtmlPage;
033: import com.salmonllc.html.events.SubmitEvent;
034: import com.salmonllc.sql.DataStore;
035: import com.salmonllc.util.MessageLog;
036:
037: /**
038: *
039: * Implements Search/List form.
040: * At the top will be a display box with a table for search criteria and a "Search" button.
041: * Following will be another display box with a datatable to display the results from the
042: * search. This box contains a "Add" button which requests the detail page (name supplied)
043: * with the parameters "mode=add", and "<primary_key>=<value>", the second repeated for
044: * as many primary keys as there are.
045: * The client of this class will add data-store columns, specifying whether they go in
046: * the search box, list box, or both. <BR>
047: * The form is implemented as a container to go within an instance of (a subclass of)
048: * HtmlPage. Specify as many data store columns as you want using the addColumn* methods
049: * and indicate which goes in the search box and which goes in the list box. <BR>
050: * To hook key activities such as the "Add" button, implement the ListFormListener interface
051: * and call addListener. <BR>
052: * Example: <BR>
053: <PRE>
054: ListForm form = new ListForm(this, "UserDetailPage");
055: add(form);
056: form.addColumn(_table, "user_id", "ID#", DataStoreBuffer.DATATYPE_INT, form.PRIMARY_KEY,
057: "UserDetailPage?mode=update");
058: // add other columns ...
059: </PRE>
060: */
061: public class ListForm extends BaseListForm {
062: // Other
063: protected String _detailPageName;
064:
065: /**
066: * Implements standard Search/List form.
067: * Default data store is created. Standard add button is included.
068: * @param page Page containing this form as a component.
069: * @param detailPageName Name of associated detail page.
070: */
071: public ListForm(HtmlPage page, String detailPageName) {
072: this (page, detailPageName, null, 0);
073: }
074:
075: /**
076: * Implements standard Search/List form.
077: * Standard add button is included.
078: * @param page HtmlPage Page containing this form as a component.
079: * @param detailPageName String Name of corresponding detail page to use as link destination.
080: * @param ds DataStore Data store object to use; if null then create one.
081: */
082: public ListForm(HtmlPage page, String detailPageName, DataStore ds) {
083: this (page, detailPageName, ds, 0);
084: }
085:
086: /**
087: * Implements standard Search/List form.
088: * @param page Page containing this form as a component.
089: * @param detailPageName Name of corresponding detail page to use as link destination.
090: * @param ds Data store object to use; if null then create one.
091: * @param flags Bitwise-OR combination of INIT_NO_SEARCH_BUTTON, etc.
092: */
093: public ListForm(HtmlPage page, String detailPageName, DataStore ds,
094: int flags) {
095: super (page, ds, flags);
096: _detailPageName = detailPageName;
097:
098: // As a favor to client classes, add page as listener if appropriate
099: if (page instanceof ListFormListener)
100: addListener((ListFormListener) page);
101: }
102:
103: /**
104: * Inherited abstract method.
105: * @return boolean
106: * @param e com.salmonllc.html.events.SubmitEvent
107: */
108: public boolean submitPerformed(SubmitEvent e) throws Exception {
109: super .submitPerformed(e);
110: //
111: MessageLog.writeDebugMessage(" submitPerformed(SubmitEvent e)",
112: this );
113: HtmlComponent c = e.getComponent();
114: HtmlPage page = getPage();
115: if (c == _btnAddListForm) {
116: //
117: int listenersSize = _listeners.size();
118: for (int i = 0; i < listenersSize; i++) {
119: if (!((ListFormListener) _listeners.elementAt(i))
120: .preListAdd()) {
121: return false;
122: }
123: }
124: //
125: if (_detailPageName == null) {
126: return false;
127: }
128:
129: if (_detailPageName != null
130: && _detailPageName.indexOf("?") == -1)
131: _detailPageName += "?mode=add";
132: else
133: _detailPageName += "&mode=add";
134:
135: StringBuffer URL = new StringBuffer(_detailPageName);
136: // get all the params and put them on the end of the redirect URL
137: int paramKeysSize = _pageParamsKeys.size();
138: String param = null;
139: Vector valVec = null;
140: for (int keyIndex = 0; keyIndex < paramKeysSize; keyIndex++) {
141: param = (_pageParamsKeys.elementAt(keyIndex))
142: .toString();
143: valVec = (Vector) _pageParamsValues.elementAt(keyIndex);
144: for (int valIndex = 0; valIndex < valVec.size(); valIndex++) {
145: URL.append("&").append(param).append("=").append(
146: valVec.elementAt(valIndex).toString());
147: }
148: }
149: page.getCurrentResponse().sendRedirect(URL.toString());
150: for (int i = 0; i < listenersSize; i++)
151: ((ListFormListener) _listeners.elementAt(i))
152: .postListAdd();
153: }
154: return true;
155: }
156:
157: /**
158: * This method returns the name of the detail page for this list
159: */
160: public String getDetailPageName() {
161: return _detailPageName;
162: }
163:
164: /**
165: * This method sets the name of the detail page for this list
166: */
167: public void setDetailPageName(String newPageName) {
168: _detailPageName = newPageName;
169: }
170: }
|