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.jsp;
021:
022: import com.salmonllc.html.HtmlApplet;
023: import com.salmonllc.html.HtmlComponent;
024: import com.salmonllc.html.HtmlPage;
025: import com.salmonllc.html.events.SubmitEvent;
026: import com.salmonllc.html.events.SubmitListener;
027:
028: import com.salmonllc.util.MessageLog;
029:
030: /////////////////////////
031: //$Archive: /SOFIA/SourceCode/com/salmonllc/jsp/JspForm.java $
032: //$Author: Srufle $
033: //$Revision: 14 $
034: //$Modtime: 6/11/04 2:13p $
035: /////////////////////////
036: import java.util.Hashtable;
037: import java.util.Vector;
038:
039: /**
040: * This container implements an HTML Form Tag
041: */
042: public class JspForm extends JspContainer {
043: Vector _listeners;
044: private String _action;
045: private String _encType;
046: private String _lastAction;
047: private String _method;
048: private String _onReset;
049: private String _onSubmit;
050: private String _target;
051: private boolean _autoScrollEnabled = true;
052: private boolean _lastActionFromForward;
053: private int _rowNo;
054: private int _scrollLeft;
055: private int _scrollTop;
056:
057: /**
058: * Creates a new JSP Box
059: *
060: * @param name
061: * @param p
062: */
063: public JspForm(String name, HtmlPage p) {
064: super (name, p);
065: }
066:
067: /**
068: * Sets the action for this form (The URL invoked when the form is posted)
069: *
070: * @param action
071: */
072: public void setAction(String action) {
073: _action = action;
074: }
075:
076: /**
077: * Returns the action for this form (The URL invoked when the form is posted)
078: *
079: * @return
080: */
081: public String getAction() {
082: return _action;
083: }
084:
085: /**
086: * Allows for the user to set whether they want auto scrolling enabled. defaults to true
087: *
088: * @param b
089: */
090: public void setAutoScrollEnabled(boolean b) {
091: _autoScrollEnabled = b;
092: }
093:
094: /**
095: * Allows the user to find out if auto scrolling is enabled
096: *
097: * @return boolean
098: */
099: public boolean isAutoScrollEnabled() {
100: return _autoScrollEnabled;
101: }
102:
103: /**
104: * Sets the mime type encoding of the data sent "application/x-www-form-urlencoded" (the default), is usually used if the METHOD attribute has the value POST. "multipart/form-data" is used when the form contains a file upload element (INPUT TYPE="FILE").
105: *
106: * @param encType
107: */
108: public void setEncType(String encType) {
109: _encType = encType;
110: }
111:
112: /**
113: * Gets the mime type encoding of the data sent "application/x-www-form-urlencoded" (the default), is usually used if the METHOD attribute has the value POST. "multipart/form-data" is used when the form contains a file upload element (INPUT TYPE="FILE").
114: *
115: * @return
116: */
117: public String getEncType() {
118: return _encType;
119: }
120:
121: /**
122: * Sets the method for this form (GET or POST)
123: *
124: * @param method
125: */
126: public void setMethod(String method) {
127: _method = method;
128: }
129:
130: /**
131: * Returns the method for this form (GET or POST)
132: *
133: * @return
134: */
135: public String getMethod() {
136: return _method;
137: }
138:
139: /**
140: * Sets the javascript code that executes if the user resets the form
141: *
142: * @param onReset
143: */
144: public void setOnReset(String onReset) {
145: _onReset = onReset;
146: }
147:
148: /**
149: * Returns the javascript code that executes if the user resets the form
150: *
151: * @return
152: */
153: public String getOnReset() {
154: return _onReset;
155: }
156:
157: /**
158: * Sets the javascript code that executes if the user submits the form
159: *
160: * @param onSubmit
161: */
162: public void setOnSubmit(String onSubmit) {
163: _onSubmit = onSubmit;
164: }
165:
166: /**
167: * Gets the javascript code that executes if the user submits the form
168: *
169: * @return
170: */
171: public String getOnSubmit() {
172: return _onSubmit;
173: }
174:
175: /**
176: * The form component can track the scroll position of the page when it was submitted for some browsers. This method will return whether or not the position was set for the last submit.
177: *
178: * @return
179: */
180: public boolean isScrollPositionSet() {
181: return (_scrollLeft != -1) || (_scrollTop != -1);
182: }
183:
184: public String getSubmitScript() {
185: String ret = "";
186: ret += (getFullName() + "onSubmit();");
187: ret += ("document.forms['" + getPortletNameSpace() + getName() + "'].submit();");
188:
189: return ret;
190: }
191:
192: /**
193: * Sets the window that will display the results of the submit
194: *
195: * @param target
196: */
197: public void setTarget(String target) {
198: _target = target;
199: }
200:
201: /**
202: * Gets the window that will display the results of the submit
203: *
204: * @return
205: */
206: public String getTarget() {
207: return _target;
208: }
209:
210: /**
211: * This method adds a listener the will be notified when this button causes the page to be submitted.
212: *
213: * @param l The listener to add.
214: */
215: public void addSubmitListener(SubmitListener l) {
216: if (_listeners == null) {
217: _listeners = new Vector();
218: }
219:
220: for (int i = 0; i < _listeners.size(); i++) {
221: if (((SubmitListener) _listeners.elementAt(i)) == l) {
222: return;
223: }
224: }
225:
226: _listeners.addElement(l);
227: }
228:
229: public boolean executeEvent(int type) throws Exception {
230: if (type == HtmlComponent.EVENT_OTHER) {
231: HtmlComponent h = null;
232:
233: for (int i = 0; i < getComponentCount(); i++) {
234: h = getComponent(i);
235:
236: if (!h.executeEvent(type)) {
237: return false;
238: }
239: }
240: } else if ((_submit != null)
241: && (type == HtmlComponent.EVENT_SUBMIT)) {
242: if (_listeners != null) {
243: SubmitEvent e = new SubmitEvent(getPage(), this ,
244: getName(), getFullName(), _rowNo);
245:
246: for (int i = 0; i < _listeners.size(); i++) {
247: SubmitListener l = (SubmitListener) _listeners
248: .elementAt(i);
249: e
250: .setNextListener((_listeners.size() > (i + 1)) ? _listeners
251: .elementAt(i + 1)
252: : null);
253:
254: if (!l.submitPerformed(e)) {
255: return false;
256: }
257: }
258: }
259:
260: boolean retVal = true;
261:
262: if (_submit != this ) {
263: retVal = _submit.executeEvent(type);
264: }
265:
266: _submit = null;
267:
268: return retVal;
269: }
270:
271: return true;
272: }
273:
274: /**
275: * Returns the form component that an HtmlComponent lives in or null if it isn't in a JspForm
276: *
277: * @param comp
278: *
279: * @return
280: */
281: public static JspForm findParentForm(HtmlComponent comp) {
282: HtmlComponent parent = comp.getParent();
283:
284: while (parent != null) {
285: if (parent instanceof JspForm) {
286: return (JspForm) parent;
287: }
288:
289: parent = parent.getParent();
290: }
291:
292: return null;
293: }
294:
295: /**
296: * Generates the Html for the component. This method is called by the framework and should not be called directly
297: *
298: * @param t
299: * @param content
300: *
301: * @throws java.io.IOException
302: */
303: public void generateHTML(TagWriter t, String content)
304: throws java.io.IOException {
305: StringBuffer sb = new StringBuffer("<FORM ");
306: _lastActionFromForward = ((JspController) getPage())
307: .isRequestFromForward();
308:
309: if (_action != null) {
310: sb.append("ACTION=\"");
311: sb.append(_action);
312: sb.append("\" ");
313: } else {
314: sb.append("ACTION=\"");
315:
316: if (getPage().isRequestFromPortlet()) {
317: _lastAction = getPage().getPortletInfo()
318: .getPortletActionURL();
319: sb.append(_lastAction);
320: } else {
321: String url = getPage().getCurrentRequest()
322: .getRequestURI();
323: _lastAction = url;
324:
325: String queryString = getPage().getCurrentRequest()
326: .getQueryString();
327:
328: if (queryString != null) {
329: url += ("?" + queryString);
330: }
331:
332: sb.append(encodeURL(url));
333: }
334:
335: sb.append("\" ");
336: }
337:
338: if (_encType != null) {
339: sb.append("ENCTYPE=\"");
340: sb.append(_encType);
341: sb.append("\" ");
342: }
343:
344: if (_method != null) {
345: sb.append("METHOD=\"");
346: sb.append(_method);
347: sb.append("\" ");
348: } else {
349: sb.append("METHOD=\"POST\" ");
350: }
351:
352: sb.append("NAME=\"");
353: sb.append(getPortletNameSpace() + getName());
354: sb.append("\" ");
355:
356: if (_target != null) {
357: sb.append("TARGET=\"");
358: sb.append(_target);
359: sb.append("\" ");
360: }
361:
362: sb.append("ONSUBMIT=\"");
363:
364: if ((_onSubmit != null) && _onSubmit.startsWith("return")) {
365: sb.append("return ");
366: }
367:
368: sb.append(getFullName());
369: sb.append("onSubmit();\" ");
370:
371: if (_onReset != null) {
372: sb.append("ONRESET=\"");
373: sb.append(_onReset);
374: sb.append("\" ");
375: }
376:
377: sb.setCharAt(sb.length() - 1, '>');
378:
379: if (isAutoScrollEnabled()) {
380: sb
381: .append("<INPUT TYPE=\"HIDDEN\" VALUE=\"-1\" NAME=\"FORMSCROLLLEFT");
382: sb.append(getFullName());
383: sb.append("\">\r\n");
384: sb
385: .append("<INPUT TYPE=\"HIDDEN\" VALUE=\"-1\" NAME=\"FORMSCROLLTOP");
386: sb.append(getFullName());
387: sb.append("\">\r\n");
388: }
389:
390: sb.append("<SCRIPT language=\"javascript\">\r\n");
391: sb.append("function " + getFullName() + "onSubmit() {\r\n");
392:
393: if (_onSubmit != null) {
394: sb.append(_onSubmit);
395: }
396:
397: String portletNameSpace = getPortletNameSpace();
398:
399: if (isAutoScrollEnabled()) {
400: if (browserSupportsScrollOffset()) {
401: sb.append("document.forms['");
402: sb.append(portletNameSpace);
403: sb.append(getName());
404: sb.append("'].FORMSCROLLLEFT");
405: sb.append(getFullName());
406: sb.append(".value=document.body.scrollLeft;\r\n");
407: sb.append("document.forms['");
408: sb.append(portletNameSpace);
409: sb.append(getName());
410: sb.append("'].FORMSCROLLTOP");
411: sb.append(getFullName());
412: sb.append(".value=document.body.scrollTop;\r\n");
413: } else {
414: sb.append("document.forms['");
415: sb.append(portletNameSpace);
416: sb.append(getName());
417: sb.append("'].FORMSCROLLLEFT");
418: sb.append(getFullName());
419: sb.append(".value=window.pageXOffset;\r\n");
420: sb.append("document.forms['");
421: sb.append(portletNameSpace);
422: sb.append(getName());
423: sb.append("'].FORMSCROLLTOP");
424: sb.append(getFullName());
425: sb.append(".value=window.pageYOffset;\r\n");
426: }
427: }
428:
429: Vector applets = getPage().getApplets();
430:
431: if (applets != null) {
432: for (int i = 0; i < applets.size(); i++) {
433: HtmlApplet apl = (HtmlApplet) applets.elementAt(i);
434:
435: if (isThisTheParent(apl)) {
436: if (apl.getVisible() && apl.getInteractWithForm()) {
437: sb.append("document.forms['");
438: sb.append(portletNameSpace);
439: sb.append(getName());
440: sb.append("'].");
441: sb.append(apl.getFullName());
442: sb
443: .append("HIDDENVALUE.value=document.applets['");
444: sb.append(apl.getFullName());
445: sb.append("'].getValue();\r\n ");
446:
447: //
448: sb.append("document.forms['");
449: sb.append(portletNameSpace);
450: sb.append(getName());
451: sb.append("'].");
452: sb.append(apl.getFullName());
453: sb
454: .append("HIDDENSTATE.value=document.applets['");
455: sb.append(apl.getFullName());
456: sb.append("'].getState();\r\n ");
457: }
458: }
459: }
460: }
461:
462: // sb.append("alert(document.forms['" + getName() + "'].FORMSCROLLTOP" + getFullName() + ".value);\r\n");
463: sb.append("}\r\n");
464: sb.append("</SCRIPT>\r\n");
465:
466: sb
467: .append("<INPUT TYPE=\"HIDDEN\" VALUE=\"1\" NAME=\"SALMONFORM");
468: sb.append(getFullName());
469: sb.append("\">");
470:
471: t.print(sb.toString(), TagWriter.TYPE_BEGIN_TAG);
472: t.print(content, TagWriter.TYPE_CONTENT);
473: t.print("</FORM>", TagWriter.TYPE_END_TAG);
474: }
475:
476: /**
477: * This method will process the parms from a post for every component in the container.
478: *
479: * @param parms
480: * @param rowNo
481: *
482: * @return
483: *
484: * @throws Exception
485: */
486: public boolean processParms(Hashtable parms, int rowNo)
487: throws Exception {
488: Object val = parms.get("SALMONFORM" + getFullName());
489:
490: if (val == null) {
491: return false;
492: }
493:
494: _scrollTop = -1;
495: _scrollLeft = -1;
496:
497: Object scrollTop = parms.get("FORMSCROLLTOP" + getFullName());
498: Object scrollLeft = parms.get("FORMSCROLLLEFT" + getFullName());
499:
500: try {
501: if (scrollTop != null) {
502: _scrollTop = Integer
503: .parseInt(((String[]) scrollTop)[0]);
504: }
505:
506: if (scrollLeft != null) {
507: _scrollLeft = Integer
508: .parseInt(((String[]) scrollLeft)[0]);
509: }
510: } catch (Exception ex) {
511: }
512:
513: String compName = null;
514:
515: try {
516: if (!getVisible()) {
517: return false;
518: }
519:
520: int compSize = getComponentCount();
521: HtmlComponent comp = null;
522:
523: for (int i = 0; i < compSize; i++) {
524: comp = getComponent(i);
525:
526: if (comp.processParms(parms, rowNo)) {
527: _submit = getComponent(i);
528: _rowNo = rowNo;
529: }
530: }
531:
532: if (_submit == null) {
533: _submit = this ;
534: _rowNo = rowNo;
535: }
536:
537: return true;
538: } catch (Exception e) {
539: MessageLog.writeErrorMessage("processParms for " + compName
540: + "\n", e, this );
541: throw (e);
542: }
543: }
544:
545: /**
546: * This method removes a listener from the list that will be notified if this button causes the page to be submitted.
547: *
548: * @param l The listener to remove.
549: */
550: public void removeSubmitListener(SubmitListener l) {
551: if (_listeners == null) {
552: return;
553: }
554:
555: for (int i = 0; i < _listeners.size(); i++) {
556: if (((SubmitListener) _listeners.elementAt(i)) == l) {
557: _listeners.removeElementAt(i);
558:
559: return;
560: }
561: }
562: }
563:
564: /**
565: * If the scroll position is set, scroll to the same spot on the page as it was when it was submitted
566: */
567: public void scrollToLastPosition() {
568: if (!isScrollPositionSet()) {
569: return;
570: }
571:
572: if ((_scrollLeft != 0) || (_scrollTop != 0)) {
573: if (getPage().getBrowserType() == HtmlPage.BROWSER_MICROSOFT) {
574: getPage().writeScript(
575: "document.body.scrollTop=" + _scrollTop + ";");
576: getPage()
577: .writeScript(
578: "document.body.scrollLeft="
579: + _scrollLeft + ";");
580: } else {
581: getPage().writeScript(
582: "window.scrollTo(" + _scrollLeft + ","
583: + _scrollTop + ");");
584: }
585: }
586:
587: _scrollLeft = -1;
588: _scrollTop = -1;
589: }
590:
591: String getLastAction() {
592: return _lastAction;
593: }
594:
595: boolean isLastActionFromForward() {
596: return _lastActionFromForward;
597: }
598:
599: private boolean isThisTheParent(HtmlComponent comp) {
600: HtmlComponent parent = comp.getParent();
601:
602: while (parent != null) {
603: if (parent == this ) {
604: return true;
605: }
606:
607: if (parent instanceof JspForm) {
608: return false;
609: }
610:
611: parent = parent.getParent();
612: }
613:
614: return false;
615: }
616:
617: private boolean browserSupportsScrollOffset() {
618: int type = getPage().getBrowserType();
619: int ver = getPage().getBrowserVersion();
620:
621: if (type == HtmlPage.BROWSER_NETSCAPE) {
622: if (ver >= 7) {
623: return true;
624: } else {
625: return false;
626: }
627: }
628:
629: return true;
630: }
631: }
|