diff options
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOImage.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOImage.java | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOImage.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOImage.java new file mode 100644 index 0000000..2673cd1 --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOImage.java @@ -0,0 +1,150 @@ + +package net.wotonomy.web; + +import net.wotonomy.foundation.NSArray; +import net.wotonomy.foundation.NSData; +import net.wotonomy.foundation.NSDictionary; + +/** +* WOImage renders a dynamically generated IMG tag. The URL for the image SRC can be +* static, or it can be generated dynamically to return a NSData object (with a mime-type) +* or even return the contents of a file (not implemented yet). +* +* Bindings are: +* <UL><LI>src: A static URL for the image source.</li> +* <li>data: A NSData object with the image content. Must be used with mimeType.</li> +* <li>mimeType: The MIME type for the image data. Can be used with filename or data bindings.</li> +* <li>filename: The path to a file containing an image.</li> +* <li>framework: The optional framework from whence the image should be retrieved (used in conjunction with filename).</li> +* +* @author ezamudio@nasoft.com +* @author $Author: cgruber $ +* @version $Revision: 905 $ +*/ +public class WOImage extends WODynamicElement { + + protected String src; + protected String filename; + protected String framework; + protected NSData data; + protected String mimeType; + + protected WOImage() { + super(); + } + + public WOImage(String aName, NSDictionary aMap, WOElement template) { + super(aName, aMap, template); + } + + public void setSrc(String value) { + src = value; + } + public String src() { + return src; + } + + public void setFilename(String value) { + filename = value; + } + + public String filename() { + return filename; + } + + public void setFramework(String value) { + framework = value; + } + public String framework() { + return framework; + } + + public void setData(NSData value) { + data = value; + } + public NSData data() { + return data; + } + + public void setMimeType(String value) { + mimeType = value; + } + public String mimeType() { + return mimeType(); + } + + public String sourceURL(WOContext c) { + if (associations.objectForKey("src") != null) + return (String)valueForProperty("src", c.component()); + if (associations.objectForKey("data") != null) { + return c.componentActionURL(); + } + if (associations.objectForKey("filename") != null) { + WOComponent component = c.component(); + + String framework = stringForProperty("framework", component); + String filename = stringForProperty( "filename", component ); + if ( filename != null && framework == null ) + { + if ( filename.startsWith("/" ) ) + { + int i = filename.lastIndexOf( "/" ); + if ( i > 0 ) + { + framework = filename.substring( 0, i ); + if ( i < filename.length() ) + { + filename = filename.substring( i+1 ); + } + } + } + else + { + // just until we figure out how we're handling bundles/localization + framework = component.frameworkName(); + if ( framework != null ) + { + framework = framework + '/' + component.name() + ".wo"; + } + else + { + framework = '/' + component.name() + ".wo"; + } + } + } + return WOApplication.application().resourceManager().urlForResourceNamed( + filename, framework, c.request().browserLanguages(), c.request() ); + } + return "NO SOURCE"; + } + + public void appendToResponse(WOResponse r, WOContext c) { + r.appendContentString("<IMG SRC=\""); + r.appendContentString(sourceURL(c)); + r.appendContentString("\""); + r.appendContentString(additionalHTMLProperties(c.component(), new NSArray(new Object[]{ + "src", "filename", "framework", "data", "mimeType" }))); + r.appendContentString(">"); + } + + public WOActionResults invokeAction(WORequest r, WOContext c) { + if (c.senderID().equals(c.elementID())) { + Object data = valueForProperty("data", c.component()); + if (data instanceof byte[]) data = new NSData( (byte[]) data ); + if (data instanceof NSData) { + String mt = stringForProperty("mimeType", c.component()); + if (mt == null) + throw new IllegalArgumentException("WOImage: No mimeType specified for data."); + WOResponse img = new WOResponse(); + img.setContent((NSData)data); + img.setHeader(mt, "content-type"); + return img; + } else if (filename() != null) { + //will this thing use frameworks, or regular JAR files with resources? + // mp: both/either, depending on which resource manager implementation + } + } + return null; + } + +} |
