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.html;
021:
022: /////////////////////////
023: //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlLink.java $
024: //$Author: Dan $
025: //$Revision: 45 $
026: //$Modtime: 9/24/04 12:19p $
027: /////////////////////////
028:
029: import com.salmonllc.sql.DataStoreBuffer;
030: import com.salmonllc.sql.DataStoreEvaluator;
031: import com.salmonllc.sql.DataStoreExpression;
032: import com.salmonllc.util.MessageLog;
033: import com.salmonllc.util.Util;
034:
035: import com.salmonllc.html.events.*;
036: import com.salmonllc.jsp.JspController;
037:
038: import java.io.PrintWriter;
039: import java.util.Hashtable;
040: import java.util.Vector;
041:
042: /**
043: * This container will construct an anchor tag with a html link (HREF) reference.
044: */
045: public class HtmlLink extends HtmlContainer {
046: private String _href;
047: private String _onClick;
048: private String _onMouseOut;
049: private String _onMouseOver;
050: private String _target;
051: private DataStoreEvaluator _dsEval;
052: private boolean _doSubmit = false;
053: private String _zoomTo = null;
054: private Vector _listeners;
055: private int _rowNo = -1;
056: private HtmlStyle _style;
057: private boolean _generateLink = true;
058: private boolean _submitAlreadyGenerated = false;
059: private Integer _tabIndex;
060: private String _accessKey;
061:
062: /**
063: * HtmlLink constructor.
064: * @param name The name of the link
065: * @param href The url for the link
066: * @param p The page the link will go in.
067: */
068:
069: public HtmlLink(String name, String href,
070: com.salmonllc.html.HtmlPage p) {
071: this (name, href, null, p);
072: }
073:
074: /**
075: * HtmlLink constructor comment.
076: * @param name The name of the link
077: * @param href The url for the link
078: * @param target The target for the link
079: * @param p The page the link will go in.
080: */
081: public HtmlLink(String name, String href, String target,
082: com.salmonllc.html.HtmlPage p) {
083: super (name, p);
084: _href = href;
085: _target = target;
086: }
087:
088: /**
089: * This method will clear all pending events from the event queue for this component and components it contains.
090: */
091: public void reset() {
092: super .reset();
093: _submitAlreadyGenerated = false;
094: }
095:
096: /**
097: * This method adds a listener the will be notified when this button causes the page to be submitted.
098: * @param l The listener to add.
099: */
100: public void addSubmitListener(SubmitListener l) {
101: if (_listeners == null)
102: _listeners = new Vector();
103: int listenersSize = _listeners.size();
104: for (int i = 0; i < listenersSize; i++) {
105: if (((SubmitListener) _listeners.elementAt(i)) == l)
106: return;
107: }
108: _listeners.addElement(l);
109: setDoSubmit(true);
110: }
111:
112: /**
113: * This method inserts a listener the will be notified when this button causes the page to be submitted.
114: * @param l The listener to add.
115: */
116: public void insertSubmitListener(SubmitListener l) {
117: if (_listeners == null)
118: _listeners = new Vector();
119: int listenersSize = _listeners.size();
120: for (int i = 0; i < listenersSize; i++) {
121: if (((SubmitListener) _listeners.elementAt(i)) == l)
122: return;
123: }
124: _listeners.insertElementAt(l, 0);
125: setDoSubmit(true);
126: }
127:
128: public boolean executeEvent(int eventType) throws Exception {
129: if (eventType == EVENT_SUBMIT && _zoomTo != null) {
130: // check listeners
131: // if any have been registered
132:
133: if (_listeners != null && _listeners.size() > 0) {
134: SubmitEvent e = new SubmitEvent(getPage(), this ,
135: getName(), getFullName(), _rowNo);
136: SubmitListener l = null;
137: int listenersSize = _listeners.size();
138: for (int i = 0; i < listenersSize; i++) {
139: l = (SubmitListener) _listeners.elementAt(i);
140: e
141: .setNextListener(_listeners.size() > (i + 1) ? _listeners
142: .elementAt(i + 1)
143: : null);
144: if (!l.submitPerformed(e))
145: return false;
146: }
147: return true;
148: } else {
149: getPage().getCurrentResponse().sendRedirect(_zoomTo);
150: _zoomTo = null;
151: }
152: } else if (!super .executeEvent(eventType)) {
153: return false;
154: }
155: return true;
156: }
157:
158: public void generateHTML(PrintWriter p, int rowNo) throws Exception {
159: if (!_visible)
160: return;
161: if (!_generateLink) {
162: super .generateHTML(p, rowNo);
163: return;
164: }
165: if (getPage() instanceof JspController
166: && !_submitAlreadyGenerated)
167: generateInitialHTML(p);
168: _zoomTo = null;
169: StringBuffer href = null;
170: if (_doSubmit) {
171: href = new StringBuffer("javascript:" + getFullName()
172: + "_submit('" + rowNo + "');");
173: } else {
174: if (_dsEval != null) {
175: if (rowNo > -1) {
176: _href = _dsEval.evaluateRowFormat(rowNo);
177: } else {
178: _href = _dsEval.evaluateRowFormat();
179: }
180: }
181: // sr 12-08-2000 was getting a null pointer exception
182: if (!Util.isNull(_href)) {
183: href = new StringBuffer(_href);
184: if (!_href.startsWith("javascript:")) {
185: int hrefLength = href.length();
186: for (int i = 0; i < hrefLength; i++) {
187: if (href.charAt(i) == ' ') {
188: href.setCharAt(i, '+');
189: }
190: }
191: }
192: }
193: }
194: String row = "";
195: if (rowNo != -1)
196: row = "_" + row + new Integer(rowNo).toString();
197: StringBuffer sbOut = new StringBuffer();
198:
199: String onClick = _onClick;
200: String valScript = HtmlValidatorText
201: .generateOnClickJavaScriptForButtons(_onClick,
202: _listeners, getFullName());
203: if (valScript != null) {
204: sbOut.append(valScript);
205: onClick = "return "
206: + HtmlValidatorText
207: .generateOnClickJavaScriptFunctionName(getFullName())
208: + ";";
209: }
210:
211: sbOut.append("<A NAME=\"");
212: sbOut.append(getFullName());
213: sbOut.append(row);
214: sbOut.append("\" HREF=\"");
215: sbOut.append(encodeURL(href));
216: sbOut.append('"');
217: // String out = "<A NAME=\"" + getFullName() + row + "\" HREF=\"" + encodeURL(href) + "\"";
218: if (_target != null) {
219: sbOut.append(" TARGET=\"");
220: sbOut.append(_target);
221: sbOut.append('"');
222: // out += " TARGET=\"" + _target + "\"";
223: }
224: if (onClick != null) {
225: sbOut.append(" ONCLICK=\"");
226: sbOut.append(onClick);
227: sbOut.append('"');
228: // out += " ONCLICK=\"" + onClick + "\"";
229: }
230: if (_onMouseOver != null) {
231: sbOut.append(" ONMOUSEOVER=\"");
232: sbOut.append(_onMouseOver);
233: sbOut.append('"');
234: // out += " ONMOUSEOVER=\"" + _onMouseOver + "\"";
235: }
236: if (_onMouseOut != null) {
237: sbOut.append(" ONMOUSEOUT=\"");
238: sbOut.append(_onMouseOut);
239: sbOut.append('"');
240: // out += " ONMOUSEOUT=\"" + _onMouseOut + "\"";
241: }
242:
243: boolean styleAdded = false;
244: if (_style != null && _style.getStyleName() != null
245: && !_style.getStyleName().trim().equals("")) {
246: sbOut.append(" CLASS=\"");
247: sbOut.append(_style.getStyleName());
248: sbOut.append('"');
249: styleAdded = true;
250: // out += " CLASS=\"" + _style.getStyleName() + "\"";
251: }
252:
253: String classname = getClassName();
254: if (Util.isFilled(classname) && !styleAdded) {
255: sbOut.append(" CLASS=\"");
256: sbOut.append(classname);
257: sbOut.append('"');
258: // out += " CLASS=\"" + _style.getStyleName() + "\"";
259: }
260:
261: if (_tabIndex != null) {
262: sbOut.append(" tabindex=\"");
263: sbOut.append(_tabIndex.toString());
264: sbOut.append("\"");
265: }
266: if (_accessKey != null) {
267: sbOut.append(" accesskey=\"");
268: sbOut.append(_accessKey);
269: sbOut.append("\"");
270: }
271:
272: sbOut.append('>');
273: // out += ">";
274: p.print(sbOut.toString());
275: super .generateHTML(p, rowNo);
276: p.print("</A>");
277: }
278:
279: public void generateHTML(PrintWriter p, int rowStart, int rowEnd)
280: throws Exception {
281: if (!_visible)
282: return;
283: if (!_generateLink) {
284: super .generateHTML(p, rowStart, rowEnd);
285: return;
286: }
287:
288: if (getPage() instanceof JspController
289: && !_submitAlreadyGenerated)
290: generateInitialHTML(p);
291: _zoomTo = null;
292: StringBuffer href = null;
293: if (_doSubmit)
294: href = new StringBuffer("javascript:" + getFullName()
295: + "_submit('" + rowStart + "');");
296: else {
297: if (_dsEval != null)
298: if (rowStart > -1)
299: _href = _dsEval.evaluateRowFormat(rowStart);
300: else
301: _href = _dsEval.evaluateRowFormat();
302:
303: // ==========================================================================
304: // BYD - 04/18/01 (If any question ask Fred or Bruno)
305: // --------------
306: // Correction introduced to fix NullPointerException when HtmlLink is used in
307: // a ControlBreakFooterComponent. Can probably be applied to other components
308: // visible even when the datastore is empty
309: if (_href == null)
310: return;
311: // ==========================================================================
312:
313: href = new StringBuffer(_href);
314: if (!_href.startsWith("javascript:")) {
315: int hrefLength = href.length();
316: for (int i = 0; i < hrefLength; i++)
317: if (href.charAt(i) == ' ')
318: href.setCharAt(i, '+');
319: }
320: }
321: String row = "";
322: if (rowStart != -1)
323: row = "_" + row + new Integer(rowStart).toString();
324: StringBuffer sbOut = new StringBuffer();
325: sbOut.append("<A NAME=\"");
326: sbOut.append(getFullName());
327: sbOut.append(row);
328: sbOut.append("\" HREF=\"");
329: sbOut.append(encodeURL(href));
330: sbOut.append('"');
331: // String out = "<A NAME=\"" + getFullName() + row + "\" HREF=\"" + encodeURL(href) + "\"";
332: if (_target != null) {
333: sbOut.append(" TARGET=\"");
334: sbOut.append(_target);
335: sbOut.append('"');
336: // out += " TARGET=\"" + _target + "\"";
337: }
338: if (_onClick != null) {
339: sbOut.append(" ONCLICK=\"");
340: sbOut.append(_onClick);
341: sbOut.append('"');
342: // out += " ONCLICK=\"" + _onClick + "\"";
343: }
344: if (_onMouseOver != null) {
345: sbOut.append(" ONMOUSEOVER=\"");
346: sbOut.append(_onMouseOver);
347: sbOut.append('"');
348: // out += " ONMOUSEOVER=\"" + _onMouseOver + "\"";
349: }
350: if (_onMouseOut != null) {
351: sbOut.append(" ONMOUSEOUT=\"");
352: sbOut.append(_onMouseOut);
353: sbOut.append('"');
354: // out += " ONMOUSEOUT=\"" + _onMouseOut + "\"";
355: }
356: if (_style != null && _style.getStyleName() != null
357: && !_style.getStyleName().trim().equals("")) {
358: sbOut.append(" CLASS=\"");
359: sbOut.append(_style.getStyleName());
360: sbOut.append('"');
361: // out += " CLASS=\"" + _style.getStyleName() + "\"";
362: }
363:
364: if (_tabIndex != null) {
365: sbOut.append(" tabindex=\"");
366: sbOut.append(_tabIndex.toString());
367: sbOut.append("\"");
368: }
369: if (_accessKey != null) {
370: sbOut.append(" accesskey=\"");
371: sbOut.append(_accessKey);
372: sbOut.append("\"");
373: }
374: sbOut.append('>');
375: // out += ">";
376: p.print(sbOut.toString());
377: super .generateHTML(p, rowStart, rowEnd);
378: p.print("</A>");
379: }
380:
381: public void generateInitialHTML(PrintWriter p) throws Exception {
382: if (_visible && _doSubmit) {
383: p.println("<INPUT TYPE=\"HIDDEN\" NAME=\"" + getFullName()
384: + "_HIDDEN\" VALUE=\"\">");
385: getPage().writeScript(
386: getFormString() + getFullName()
387: + "_HIDDEN.value = '';");
388: p.println("<SCRIPT>");
389: p.println("function " + getFullName() + "_submit(row) {");
390: p.println(getFormString() + getFullName()
391: + "_HIDDEN.value = row;");
392: p.println(getFormString() + "submit();");
393: p.println("}");
394: p.println("</SCRIPT>");
395: _submitAlreadyGenerated = true;
396: }
397: }
398:
399: /**
400: * Gets whether or not the component will submit the page before transfering control to the link page. If the link does a submit, all the user entered data will sent to the server to be recorded.
401: */
402: public boolean getDoSubmit() {
403: return _doSubmit;
404: }
405:
406: /**
407: * This method gets whether or not the anchor tag will be generated for the link. Pass true to have the a tag generated and false not to. Setting the value to false, will cause the items inside the container to be displayed but nothing will happen when the user clicks on them.
408: */
409: public boolean getGenerateLink() {
410: return _generateLink;
411: }
412:
413: /**
414: * Returns the href for the link
415: */
416: public String getHref() {
417: return _href;
418: }
419:
420: /**
421: * Use this method to get the javascript that will be executed when the user clicks on one of the components in the link.
422: */
423: public String getOnClick() {
424: return _onClick;
425: }
426:
427: /**
428: * Use this method to get the javascript that will be executed when the mouse passes over out of all the components
429: */
430: public String getOnMouseOut() {
431: return _onMouseOut;
432: }
433:
434: /**
435: * Use this method to get the javascript that will be executed when the mouse passes over any component in the link
436: */
437: public String getOnMouseOver() {
438: return _onMouseOver;
439: }
440:
441: public HtmlStyle getStyle() {
442: return _style;
443: }
444:
445: /**
446: * Returns the target for the link.
447: */
448: public String getTarget() {
449: return _target;
450: }
451:
452: /**
453: * This method will process the parms from a post for every component in the container.
454: */
455: public boolean processParms(Hashtable parms, int rowNo)
456: throws Exception {
457: if (super .processParms(parms, rowNo))
458: return true;
459:
460: String name = getFullName() + "_HIDDEN";
461: String[] p = (String[]) parms.get(name);
462:
463: if (p != null && p[0].trim().length() > 0 && _zoomTo == null) {
464: _submit = this ;
465:
466: _rowNo = Integer.parseInt(p[0].trim());
467: String href = "";
468:
469: if (_dsEval != null) {
470: if (_rowNo > -1)
471: href = _dsEval.evaluateRowFormat(_rowNo);
472: else
473: href = _dsEval.evaluateRowFormat();
474: } else
475: href = _href;
476:
477: _zoomTo = href;
478:
479: return true;
480: }
481:
482: return false;
483:
484: }
485:
486: /**
487: * This method removes a listener from the list that will be notified if this button causes the page to be submitted.
488: * @param l The listener to remove.
489: */
490: public void removeSubmitListener(SubmitListener l) {
491: if (_listeners == null)
492: return;
493: //
494: int listenersSize = _listeners.size();
495: for (int i = 0; i < listenersSize; i++) {
496: if (((SubmitListener) _listeners.elementAt(i)) == l) {
497: _listeners.removeElementAt(i);
498: return;
499: }
500: }
501: }
502:
503: /**
504: * Sets whether or not the component will submit the page before transfering control to the link page. If the link does a submit, all the user entered data will sent to the server to be recorded. If the flag is true, the target and onClick properties cannot be used.
505: */
506: public void setDoSubmit(boolean doSubmit) {
507: _doSubmit = doSubmit;
508: }
509:
510: /**
511: * This method sets whether or not the anchor tag will be generated for the link. Pass true to have the a tag generated and false not to. Setting the value to false, will cause the items inside the container to be displayed but nothing will happen when the user clicks on them.
512: */
513: public void setGenerateLink(boolean gen) {
514: _generateLink = gen;
515: }
516:
517: /**
518: * Sets the href for the link
519: * @param href java.lang.String
520: */
521: public void setHref(String href) {
522: _href = href;
523: }
524:
525: /**
526: * This method was created in VisualAge.
527: * @param ds com.salmonllc.sql.DataStoreBuffer
528: * @param expression The expression to evaluate
529: * @exception java.lang.Exception The exception description.
530: */
531: public void setHrefExpression(DataStoreBuffer ds,
532: DataStoreExpression expression) throws Exception {
533: try {
534: _dsEval = new DataStoreEvaluator(ds, expression);
535: } catch (Exception e) {
536: MessageLog
537: .writeErrorMessage(
538: "setHrefExpression(DataStoreBuffer ds, DataStoreExpression expression )",
539: e, this );
540: throw e;
541: }
542: }
543:
544: /**
545: * This method was created in VisualAge.
546: * @param ds com.salmonllc.sql.DataStoreBuffer
547: * @param expression java.lang.String
548: * @exception java.lang.Exception The exception description.
549: */
550: public void setHrefExpression(DataStoreBuffer ds, String expression)
551: throws Exception {
552: try {
553: _dsEval = new DataStoreEvaluator(ds, expression);
554: } catch (Exception e) {
555: MessageLog
556: .writeErrorMessage(
557: "setHrefExpression(DataStoreBuffer ds, String expression )",
558: e, this );
559: throw e;
560: }
561: }
562:
563: /**
564: * Use this method to set the javascript that will be executed when the user clicks on one of the components in the link.
565: */
566: public void setOnClick(String onClick) {
567: _onClick = onClick;
568: }
569:
570: /**
571: * Use this method to set the javascript that will be executed when the mouse passes over out of all the components
572: */
573: public void setOnMouseOut(String onMouseOut) {
574: _onMouseOut = onMouseOut;
575: }
576:
577: /**
578: * Use this method to set the javascript that will be executed when the mouse passes over any component in the link
579: */
580: public void setOnMouseOver(String onMouseOver) {
581: _onMouseOver = onMouseOver;
582: }
583:
584: /**
585: * This method was created in VisualAge.
586: * @param style com.salmonllc.html.HtmlStyle
587: */
588: public void setStyle(HtmlStyle style) {
589: _style = style;
590: }
591:
592: /**
593: * Sets the target for the link.<BR>
594: * _blank opens the destination document in a new unnamed window. <BR>
595: * _parent opens the destination document in the parent window of the one displaying the current document. <BR>
596: * _self opens the destination document in the same window as the one in which the link was clicked. <BR>
597: * _top opens the destination document in the full body of the current window. This value can be used to ensure that the destination document takes over the full window even if the original document was displayed in a frame. <BR>
598: */
599: public void setTarget(String target) {
600: _target = target;
601: }
602:
603: /**
604: * This method gets the DataStoreEvaluator being used for href expressions.
605: * @return DataStoreEvaluator
606: * @see DataStoreEvaluator
607: */
608: public DataStoreEvaluator getHrefExpression() {
609: return _dsEval;
610: }
611:
612: /**
613: * Clears the submit component in this container or children containers. The container stores which component inside it
614: * submitted a particular page for one invocation so it can route to the correct submit performed methods. Once that's done,
615: * the framework needs to clear out that value for the next page invocation. This method is used by the framework and should not be called directly.
616: */
617: public void clearSubmit() {
618: super .clearSubmit();
619: _submitAlreadyGenerated = false;
620: }
621:
622: /**
623: * @returns the access key html attribute
624: */
625: public String getAccessKey() {
626: return _accessKey;
627: }
628:
629: /**
630: * @returns the tab index html attribute
631: */
632: public int getTabIndex() {
633: if (_tabIndex == null)
634: return -1;
635: return _tabIndex.intValue();
636: }
637:
638: /**
639: * @param sets the access key html attribute
640: */
641: public void setAccessKey(String string) {
642: _accessKey = string;
643: }
644:
645: /**
646: * @param 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
647: */
648: public void setTabIndex(int val) {
649: if (val == -1)
650: _tabIndex = null;
651: else
652: _tabIndex = new Integer(val);
653: }
654:
655: }
|