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:
021: package com.salmonllc.wml;
022:
023: /////////////////////////
024: //$Archive: /SOFIA/SourceCode/com/salmonllc/wml/WmlSubmit.java $
025: //$Author: Dan $
026: //$Revision: 5 $
027: //$Modtime: 6/12/03 12:46p $
028: /////////////////////////
029:
030: import java.io.PrintWriter;
031: import java.util.Hashtable;
032: import java.util.Vector;
033:
034: import com.salmonllc.html.events.SubmitEvent;
035: import com.salmonllc.html.events.SubmitListener;
036: import com.salmonllc.jsp.JspContainer;
037: import com.salmonllc.jsp.JspController;
038: import com.salmonllc.jsp.tags.PageTag;
039: import com.salmonllc.sql.*;
040: import com.salmonllc.util.MessageLog;
041: import com.salmonllc.util.Util;
042:
043: /**
044: * This container will construct an anchor tag with a html link (HREF) reference.
045: */
046: public class WmlSubmit extends JspContainer {
047: private DataStoreEvaluator _dsEval;
048: private int _rowNo = -1;
049: private String _class;
050: private String _title;
051: private WmlCard _card;
052: private Vector _listeners;
053:
054: /**
055: * WmlSubmit constructor comment.
056: * @param name The name of the link
057: * @param p The page the link will go in.
058: */
059:
060: public WmlSubmit(String name, com.salmonllc.html.HtmlPage p) {
061: super (name, p);
062: }
063:
064: /**
065: *Does the binding for the component. This method is called by the framework and should not be called directly
066: */
067: public void doBinding() throws Exception {
068: String dataSource = getDataSource();
069: String dsName = null;
070: String dsExp = null;
071:
072: if (dataSource == null)
073: return;
074:
075: int pos = dataSource.indexOf(":");
076: if (pos == -1)
077: dsName = dataSource;
078: else {
079: dsName = dataSource.substring(0, pos);
080: dsExp = dataSource.substring(pos + 1);
081: }
082:
083: DataStoreBuffer ds = ((JspController) getPage())
084: .getDataSource(dsName);
085: if (ds == null)
086: return;
087:
088: if (!ds.getAutoBind())
089: return;
090:
091: setTitleExpression(ds, ((JspController) getPage())
092: .convertExpressionOperators(dsExp));
093: }
094:
095: /**
096: *Generates the Html for the component. This method is called by the framework and should not be called directly
097: */
098: public void generateHTML(PrintWriter p, int rowNo)
099: throws java.io.IOException {
100: StringBuffer sb = new StringBuffer();
101:
102: if (!_visible)
103: return;
104: StringBuffer title = null;
105: try {
106: if (_dsEval != null) {
107: if (rowNo > -1) {
108: _title = _dsEval.evaluateRowFormat(rowNo);
109: } else {
110: _title = _dsEval.evaluateRowFormat();
111: }
112: }
113: } catch (Exception e) {
114: }
115: // sr 12-08-2000 was getting a null pointer exception
116: if (!Util.isNull(_title)) {
117: title = new StringBuffer(_title);
118: int titleLength = title.length();
119: for (int i = 0; i < titleLength; i++) {
120: if (title.charAt(i) == ' ') {
121: title.setCharAt(i, '+');
122: }
123: }
124: }
125: String row = "";
126: if (rowNo != -1)
127: row = "_" + row + new Integer(rowNo).toString();
128: String href = getPage().getCurrentRequest().getRequestURI();
129: int iSlashIndex = href.lastIndexOf('/');
130: if (iSlashIndex >= 0)
131: href = href.substring(iSlashIndex + 1);
132: StringBuffer sbQuery = new StringBuffer();
133:
134: if (getPage().isWMLMaintained())
135: sbQuery.append(PageTag.getSessionIdentifier() + "="
136: + PageTag.getWmlSessId(getPage().getSession())
137: + "&");
138: if (_card != null) {
139: WmlFormComponent[] wfca = _card.getInputComponents();
140: if (wfca != null) {
141: for (int i = 0; i < wfca.length; i++) {
142: String sParameter = wfca[i].getName() + "=$("
143: + wfca[i].getName() + ")";
144: sbQuery.append(sParameter + "&");
145: }
146: }
147: }
148: sbQuery.append("method=post&");
149: sbQuery.append(getName() + row + "=1&");
150: if (sbQuery.toString().endsWith("&"))
151: sbQuery.delete(sbQuery.length() - 5, sbQuery.length());
152:
153: sb.append("<a");
154: sb.append(" id=\"" + getName() + row + "\"");
155: if (!sbQuery.toString().trim().equals(""))
156: sb
157: .append(" href=\""
158: + encodeURL(href + "?" + sbQuery.toString())
159: + "\"");
160: else
161: sb.append(" href=\"" + encodeURL(href) + "\"");
162: if (_title != null)
163: sb.append(" title=\"" + _title + "\"");
164: if (_class != null)
165: sb.append(" class=\"" + _class + "\"");
166: sb.append(">");
167: sb.append(_title);
168: sb.append("</a>");
169:
170: p.print(sb.toString());
171:
172: }
173:
174: /**
175: * This method gets the DataStoreEvaluator being used for href expressions.
176: * @return DataStoreEvaluator
177: * @see DataStoreEvaluator
178: */
179: public DataStoreEvaluator getTitleExpression() {
180: return _dsEval;
181: }
182:
183: /**
184: * Use this method to get the class of the submit link
185: */
186: public String getClassName() {
187: return _class;
188: }
189:
190: /**
191: * Use this method to get the title for the submit link
192: */
193: public String getTitle() {
194: return _title;
195: }
196:
197: /**
198: * This method sets a datastore expression that will be used to compute the href for the link.
199: * @param ds com.salmonllc.sql.DataStoreBuffer
200: * @param expression The expression to evaluate
201: */
202: public void setTitleExpression(DataStoreBuffer ds,
203: DataStoreExpression expression) throws Exception {
204: try {
205: _dsEval = new DataStoreEvaluator(ds, expression);
206: } catch (Exception e) {
207: MessageLog
208: .writeErrorMessage(
209: "setHrefExpression(DataStoreBuffer ds, DataStoreExpression expression )",
210: e, this );
211: throw e;
212: }
213: }
214:
215: /**
216: * This method sets a datastore expression that will be used to compute the href for the link.
217: * @param ds com.salmonllc.sql.DataStoreBuffer
218: * @param expression java.lang.String
219: */
220: public void setTitleExpression(DataStoreBuffer ds, String expression)
221: throws Exception {
222: try {
223: _dsEval = new DataStoreEvaluator(ds, expression);
224: } catch (Exception e) {
225: MessageLog
226: .writeErrorMessage(
227: "setHrefExpression(DataStoreBuffer ds, String expression )",
228: e, this );
229: throw e;
230: }
231: }
232:
233: /**
234: * This method sets the class for the submit link.
235: */
236: public void setClassName(String className) {
237: _class = className;
238: }
239:
240: /**
241: * This method sets the title for the submit link.
242: */
243: public void setTitle(String title) {
244: _title = title;
245: }
246:
247: /**
248: *Processes the submitted parameters. This method is called by the framework and should not be called directly
249: */
250: public boolean processParms(Hashtable parms, int rowNo)
251: throws Exception {
252: String name = getName();
253: if (rowNo > -1)
254: name += "_" + rowNo;
255:
256: if (parms.get(name) != null) {
257: _rowNo = rowNo;
258: return true;
259: }
260:
261: return false;
262: }
263:
264: /**
265: * This method sets the card the submit belongs to.
266: */
267: public void setCard(WmlCard card) {
268: _card = card;
269: }
270:
271: /**
272: * This method adds a listener the will be notified when this button causes the page to be submitted.
273: * @param l The listener to add.
274: */
275: public void addSubmitListener(SubmitListener l) {
276: if (_listeners == null)
277: _listeners = new Vector();
278:
279: for (int i = 0; i < _listeners.size(); i++) {
280: if (((SubmitListener) _listeners.elementAt(i)) == l)
281: return;
282: }
283:
284: _listeners.addElement(l);
285: }
286:
287: /**
288: *Executes any events for the component. This method is called by the framework and should not be called directly
289: */
290: public boolean executeEvent(int type) throws Exception {
291: if (type != EVENT_SUBMIT)
292: return true;
293:
294: if (_listeners == null)
295: return true;
296:
297: SubmitEvent e = new SubmitEvent(getPage(), this , getName(),
298: getFullName(), _rowNo);
299:
300: for (int i = 0; i < _listeners.size(); i++) {
301: SubmitListener l = (SubmitListener) _listeners.elementAt(i);
302: e.setNextListener(_listeners.size() > (i + 1) ? _listeners
303: .elementAt(i + 1) : null);
304: if (!l.submitPerformed(e))
305: return false;
306: }
307:
308: return true;
309: }
310:
311: /**
312: * This method removes a listener from the list that will be notified if this button causes the page to be submitted.
313: * @param l The listener to remove.
314: */
315: public void removeSubmitListener(SubmitListener l) {
316: if (_listeners == null)
317: return;
318:
319: for (int i = 0; i < _listeners.size(); i++) {
320: if (((SubmitListener) _listeners.elementAt(i)) == l) {
321: _listeners.removeElementAt(i);
322: return;
323: }
324: }
325: }
326:
327: }
|