1
2 package net.wotonomy.web;
3
4 import net.wotonomy.foundation.NSArray;
5 import net.wotonomy.foundation.NSData;
6 import net.wotonomy.foundation.NSDictionary;
7
8 /***
9 * WOImage renders a dynamically generated IMG tag. The URL for the image SRC can be
10 * static, or it can be generated dynamically to return a NSData object (with a mime-type)
11 * or even return the contents of a file (not implemented yet).
12 *
13 * Bindings are:
14 * <UL><LI>src: A static URL for the image source.</li>
15 * <li>data: A NSData object with the image content. Must be used with mimeType.</li>
16 * <li>mimeType: The MIME type for the image data. Can be used with filename or data bindings.</li>
17 * <li>filename: The path to a file containing an image.</li>
18 * <li>framework: The optional framework from whence the image should be retrieved (used in conjunction with filename).</li>
19 *
20 * @author ezamudio@nasoft.com
21 * @author $Author: cgruber $
22 * @version $Revision: 905 $
23 */
24 public class WOImage extends WODynamicElement {
25
26 protected String src;
27 protected String filename;
28 protected String framework;
29 protected NSData data;
30 protected String mimeType;
31
32 protected WOImage() {
33 super();
34 }
35
36 public WOImage(String aName, NSDictionary aMap, WOElement template) {
37 super(aName, aMap, template);
38 }
39
40 public void setSrc(String value) {
41 src = value;
42 }
43 public String src() {
44 return src;
45 }
46
47 public void setFilename(String value) {
48 filename = value;
49 }
50
51 public String filename() {
52 return filename;
53 }
54
55 public void setFramework(String value) {
56 framework = value;
57 }
58 public String framework() {
59 return framework;
60 }
61
62 public void setData(NSData value) {
63 data = value;
64 }
65 public NSData data() {
66 return data;
67 }
68
69 public void setMimeType(String value) {
70 mimeType = value;
71 }
72 public String mimeType() {
73 return mimeType();
74 }
75
76 public String sourceURL(WOContext c) {
77 if (associations.objectForKey("src") != null)
78 return (String)valueForProperty("src", c.component());
79 if (associations.objectForKey("data") != null) {
80 return c.componentActionURL();
81 }
82 if (associations.objectForKey("filename") != null) {
83 WOComponent component = c.component();
84
85 String framework = stringForProperty("framework", component);
86 String filename = stringForProperty( "filename", component );
87 if ( filename != null && framework == null )
88 {
89 if ( filename.startsWith("/" ) )
90 {
91 int i = filename.lastIndexOf( "/" );
92 if ( i > 0 )
93 {
94 framework = filename.substring( 0, i );
95 if ( i < filename.length() )
96 {
97 filename = filename.substring( i+1 );
98 }
99 }
100 }
101 else
102 {
103
104 framework = component.frameworkName();
105 if ( framework != null )
106 {
107 framework = framework + '/' + component.name() + ".wo";
108 }
109 else
110 {
111 framework = '/' + component.name() + ".wo";
112 }
113 }
114 }
115 return WOApplication.application().resourceManager().urlForResourceNamed(
116 filename, framework, c.request().browserLanguages(), c.request() );
117 }
118 return "NO SOURCE";
119 }
120
121 public void appendToResponse(WOResponse r, WOContext c) {
122 r.appendContentString("<IMG SRC=\"");
123 r.appendContentString(sourceURL(c));
124 r.appendContentString("\"");
125 r.appendContentString(additionalHTMLProperties(c.component(), new NSArray(new Object[]{
126 "src", "filename", "framework", "data", "mimeType" })));
127 r.appendContentString(">");
128 }
129
130 public WOActionResults invokeAction(WORequest r, WOContext c) {
131 if (c.senderID().equals(c.elementID())) {
132 Object data = valueForProperty("data", c.component());
133 if (data instanceof byte[]) data = new NSData( (byte[]) data );
134 if (data instanceof NSData) {
135 String mt = stringForProperty("mimeType", c.component());
136 if (mt == null)
137 throw new IllegalArgumentException("WOImage: No mimeType specified for data.");
138 WOResponse img = new WOResponse();
139 img.setContent((NSData)data);
140 img.setHeader(mt, "content-type");
141 return img;
142 } else if (filename() != null) {
143
144
145 }
146 }
147 return null;
148 }
149
150 }