/*
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:
*
* - string: a string to be included between the hyperlink tags (optional).
* - 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.
* - href: The URL that the hyperlink should point to.
* - pageName: The name of the WOComponent that the hyperlink should point to.
* - directActionName: The name of the direct action to call when the link is activated.
* - actionClass: The name of the WODirectAction subclass where the direct action resides.
* - anchorName: The name of the link, for anchor tags.
* - action: A pointer to a method on the component that contains this element. If the link is activated,
* the method will be called.
*
- ref: The name of the anchor to go to inside the resulting page.
*
*
* 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 string 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("");
//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("");
}
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);
}
}