diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
| commit | aedc34d55462a75e329bbf342251ff6504cd117e (patch) | |
| tree | bcc8f1f2352582717b484df302aeea6696b8f000 /projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java | |
Initial import from SVN
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java new file mode 100644 index 0000000..d0f3ff7 --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOHyperlink.java @@ -0,0 +1,249 @@ +/* + Wotonomy: OpenStep design patterns for pure Java applications. + Copyright (C) 2000 Blacksmith, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, see http://www.gnu.org + */ + +package net.wotonomy.web; + +import java.util.Iterator; +import java.util.Map; + +import net.wotonomy.foundation.NSArray; +import net.wotonomy.foundation.NSDictionary; + +/** +* WOHyperlink renders a dynamically generated hyperlink in the output. + * Bindings are: + * <ul> + * <li>string: a string to be included between the hyperlink tags (optional).</li> + * <li>escapeHTML: a property returning a value convertable to a Boolean + * indicating whether the any html characters in the output should be + * escaped so they are shown as html characters rather than interpreted + * as html.</li> + * <li>href: The URL that the hyperlink should point to.</li> + * <li>pageName: The name of the WOComponent that the hyperlink should point to.</li> + * <li>directActionName: The name of the direct action to call when the link is activated.</li> + * <li>actionClass: The name of the WODirectAction subclass where the direct action resides.</li> + * <li>anchorName: The name of the link, for anchor tags.</li> + * <li>action: A pointer to a method on the component that contains this element. If the link is activated, + * the method will be called. + * <li>ref: The name of the anchor to go to inside the resulting page.</li> + * </ul> + * + * The href, pageName and directActionName/actionClass and name properties are mutually exclusive and you should + * only use at most one of them simultaneously. + * + * @author ezamudio@nasoft.com + * @author $Author: cgruber $ + * @version $Revision: 905 $ + */ +public class WOHyperlink extends WODynamicElement { + + protected String string; + protected String href; + protected String pageName; + protected String directActionName; + protected String actionClass; + protected String action; + protected boolean escapeHTML; + protected String anchorName; + protected String ref; + + protected WOHyperlink() { + super(); + } + + public WOHyperlink(String aName, NSDictionary aMap, WOElement aRootElement) { + super(aName, aMap, aRootElement); + escapeHTML = true; + } + + public void setString(String value) { + string = value; + } + public String string() { + return string; + } + + public void setHref(String value) { + href = value; + } + public String href() { + return href; + } + + public void setAnchorName(String value) { + anchorName = value; + } + public String anchorName() { + return anchorName; + } + + public void setPageName(String value) { + pageName = value; + } + public String pageName() { + return pageName; + } + + public void setDirectActionName(String value) { + directActionName = value; + } + public String directActionName() { + return directActionName; + } + + public void setActionClass(String value) { + actionClass = value; + } + public String actionClass() { + return actionClass; + } + + /** Sets the escapeHTML property. */ + public void setEscapeHTML(boolean escape) { + escapeHTML = escape; + } + + /** If true, inserts escape codes in to the <B>string</B> string so + * that HTML special characters (greater-than, less-than, etc.) + * appear correctly. If false, those characters will get + * interpreted by the browser. Defaults to true. + */ + public boolean escapeHTML() { + return escapeHTML; + } + + public String actionURL(WOContext c) { + //Check if the href property is set + if (href() != null) { + return href(); + } else if (pageName() != null || associations.objectForKey("action") != null) { //write this component's URL + StringBuffer retval = new StringBuffer(c.componentActionURL()); + Map addFields = urlFields(c.component()); + if (addFields.size() > 0) { + Iterator enumeration = addFields.keySet().iterator(); + retval.append('?'); + while (enumeration.hasNext()) { + String encoding = c.response() != null ? c.response().contentEncoding() : c.request().contentEncoding(); + String key = (String)enumeration.next(); + try { + retval.append(java.net.URLEncoder.encode(key, encoding)); + } catch (java.io.UnsupportedEncodingException ex) { + retval.append(key); + } + retval.append("="); + try { + retval.append(java.net.URLEncoder.encode(addFields.get(key).toString(), encoding)); + } catch (java.io.UnsupportedEncodingException e) { + retval.append(addFields.get(key).toString()); + } + if (enumeration.hasNext()) + retval.append('&'); + } + } + return retval.toString(); + } else if (directActionName() != null) { //compose the direct action URL + String fullActionName = null; + if (actionClass() != null ) + fullActionName = actionClass() + "/" + directActionName(); + else + fullActionName = directActionName(); + return c.directActionURLForActionNamed(fullActionName, urlFields(c.component())); + } + return null; + } + + protected void pullValuesFromParent(WOComponent c) { + string = stringForProperty("string", c); + href = stringForProperty("href", c); + pageName = stringForProperty("pageName", c); + directActionName = stringForProperty("directActionName", c); + actionClass = stringForProperty("actionClass", c); + //action = stringForProperty("action", c); + escapeHTML = booleanForProperty("escapeHTML", c); + anchorName = stringForProperty("anchorName", c); + ref = stringForProperty("ref", c); + } + + public void appendToResponse(WOResponse r, WOContext c) { + pullValuesFromParent( c.component() ); + r.appendContentString("<A"); + boolean closeQuotes = false; + //Check if the href property is set + String _href = actionURL(c); + if (_href != null) { + closeQuotes = true; + r.appendContentString(" HREF=\""); + r.appendContentString(_href); + } else if (anchorName() != null) { + r.appendContentString(" NAME=\""); + r.appendContentString(anchorName()); + closeQuotes = true; + } + if (ref != null) { + if (!closeQuotes) { + r.appendContentString(" HREF=\"#"); + closeQuotes = true; + } else + r.appendContentString("#"); + r.appendContentString(ref); + } + if (closeQuotes) + r.appendContentString("\""); + r.appendContentString(additionalHTMLProperties(c.component(), new NSArray(new Object[]{ + "name", "href", "pageName", "action", "directActionName", "actionClass", "anchorName", + "escapeHTML", "string" }))); + r.appendContentString(">"); + //Append the string if present + if (string() != null) { + if (escapeHTML()) + r.appendContentHTMLString(string()); + else + r.appendContentString(string()); + } + //If there is a template, call appendToResponse on it + if (rootElement != null) { + rootElement.appendToResponse(r, c); + } + //Close the tag + r.appendContentString("</A>"); + } + + public WOActionResults invokeAction(WORequest r, WOContext c) { + System.out.println("invoke action with elementID=" + c.elementID() + " senderID=" + c.senderID()); + //Check if this element is the target + if (c.senderID().equals(c.elementID())) { + if (pageName() != null) + { + return WOApplication.application().pageWithName(pageName(), r); + } + else + { + WOAssociation ass = (WOAssociation) associations.objectForKey("action"); + if ( ass != null && ass.path != null ) //?? + return (WOActionResults)c.component().performAction( ass.path ); + } + } + return null; + } + + public void takeValuesFromRequest(WORequest r, WOContext c) { + System.out.println("takeValuesFromRequest elementID=" + c.elementID() + " senderID=" + c.senderID()); + super.takeValuesFromRequest(r, c); + } + +} |
