1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.gui.dialog;
35
36 import info.magnolia.cms.beans.config.MIMEMapping;
37 import info.magnolia.cms.beans.runtime.Document;
38 import info.magnolia.cms.beans.runtime.MultipartForm;
39 import info.magnolia.cms.core.Content;
40 import info.magnolia.cms.gui.control.File;
41 import info.magnolia.cms.gui.misc.CssConstants;
42 import info.magnolia.cms.gui.misc.Spacer;
43
44 import java.io.IOException;
45 import java.io.Writer;
46 import java.util.ArrayList;
47 import java.util.List;
48
49 import javax.jcr.PropertyType;
50 import javax.jcr.RepositoryException;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53
54 import org.apache.commons.lang.StringUtils;
55
56
57
58
59
60
61 public class DialogFile extends DialogBox {
62
63 private List imageExtensions = new ArrayList();
64
65
66
67
68 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
69 throws RepositoryException {
70 super.init(request, response, websiteNode, configNode);
71 initImageExtensions();
72 }
73
74 public List getImageExtensions() {
75 return this.imageExtensions;
76 }
77
78 public void setImageExtensions(List l) {
79 this.imageExtensions = l;
80 }
81
82 public void initImageExtensions() {
83 this.getImageExtensions().add("jpg");
84 this.getImageExtensions().add("jpeg");
85 this.getImageExtensions().add("gif");
86 this.getImageExtensions().add("png");
87 this.getImageExtensions().add("bmp");
88 this.getImageExtensions().add("swf");
89 }
90
91 public void drawHtml(Writer out) throws IOException {
92 File control = getFileControl();
93 control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING));
94 control.setSaveInfo(false);
95 control.setCssClass(CssConstants.CSSCLASS_FILE);
96 control.setCssClassFileName(CssConstants.CSSCLASS_EDIT);
97 control.setCssStyles("width", this.getConfigValue("width", "100%"));
98
99 this.drawHtmlPre(out);
100
101 String width = this.getConfigValue("width", "100%");
102
103
104 final boolean preview = Boolean.valueOf(getConfigValue("preview", "true")).booleanValue();
105 final boolean extensionIsDisplayableImage = this.getImageExtensions().contains(control.getExtension().toLowerCase());
106 final boolean showImage = extensionIsDisplayableImage && preview;
107
108 StringBuffer htmlControlFileName = getHtmlControlFileName(control);
109 String htmlContentEmpty = control.getHtmlBrowse() + Spacer.getHtml(0, 0) + htmlControlFileName;
110 out.write("<div id=\"" + this.getName() + "_contentDiv\" style=\"width:100%;\">");
111 boolean exists = false;
112
113 if (this.getStorageNode() != null) {
114 exists = this.getStorageNode().getNodeData(this.getName()).isExist();
115 }
116
117 if (!exists) {
118 out.write(htmlContentEmpty);
119 out.write("</div>");
120 }
121 else {
122 String link = getLink(control);
123
124 if (showImage) {
125
126 out.write("\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">");
127 out.write("<tr><td class=\"" + CssConstants.CSSCLASS_FILEIMAGE + "\">");
128
129 if ("swf".equals(control.getExtension().toLowerCase())) {
130
131
132 out.write("<object type=\"application/x-shockwave-flash\" data=\"");
133 out.write(link);
134 out.write("\" title=\"");
135 out.write(control.getFileName());
136 out.write("\" ");
137 out.write("width=\"150\" ");
138 out.write("height=\"100\" ");
139 out.write(">");
140
141 out.write("<param name=\"movie\" value=\"");
142 out.write(link);
143 out.write("\"/>");
144
145 out.write("</object>\n");
146
147 }
148 else {
149
150
151
152
153
154
155
156
157 int imgwidth = 150;
158 int imgheight = 150;
159
160 try{
161 imgwidth = Integer.parseInt(control.getImageWidth());
162 imgheight = Integer.parseInt(control.getImageHeight());
163 }
164 catch(NumberFormatException e){
165
166 }
167
168
169 if(imgwidth > imgheight && imgwidth > 150){
170 imgheight = (int)(150.0/imgwidth * imgheight);
171 imgwidth = 150;
172 }
173 else if(imgheight > imgwidth && imgheight > 150){
174 imgwidth = (int)(150.0/imgheight * imgwidth);
175 imgheight = 150;
176 }
177
178 out.write("<img width=\"" + imgwidth + "\" height=\"" + imgheight + "\"src=\"");
179 out.write(link);
180 out.write("\" class=\"");
181 out.write(CssConstants.CSSCLASS_FILEIMAGE);
182 out.write("\" alt=\"");
183 out.write(control.getFileName());
184 out.write("\" title=\"");
185 out.write(control.getFileName());
186 out.write("\" />\n");
187
188 if (StringUtils.isNotEmpty(control.getImageWidth())) {
189 out.write("<em style='white-space:nowrap'>");
190
191 out.write("width: ");
192 out.write(control.getImageWidth());
193
194 out.write(" height: ");
195 out.write(control.getImageHeight());
196
197 out.write("</em>\n");
198 }
199
200 }
201
202 out.write("</td><td>");
203 }
204 writeInnerHtml(out, showImage, control, htmlControlFileName, link);
205
206 out.write(Spacer.getHtml(12, 12));
207 out.write(control.getHtmlRemove("mgnlDialogFileRemove('" + this.getName() + "');"));
208 if (showImage) {
209 out.write("</td></tr></table>");
210 }
211 out.write("</div>\n");
212 out.write("<div style=\"position:absolute;top:-500px;left:-500px;visibility:hidden;\">\n<textarea id=\"");
213 out.write(this.getName());
214 out.write("_contentEmpty\">");
215 out.write(htmlContentEmpty);
216
217
218
219 out.write("</textarea>\n</div>\n");
220 }
221 control.setSaveInfo(true);
222 out.write(control.getHtmlSaveInfo());
223 control.setNodeDataTemplate(this.getConfigValue("nodeDataTemplate", null));
224 out.write(control.getHtmlNodeDataTemplate());
225 this.drawHtmlPost(out);
226 }
227
228 protected String getLink(File control) {
229 String link = this.getRequest().getContextPath() + getFileURI(control);
230 if (!StringUtils.isEmpty(control.getExtension())) {
231 link += "." + control.getExtension();
232 }
233 return link;
234 }
235
236 protected void writeInnerHtml(Writer out, final boolean showImage, File control, StringBuffer htmlControlFileName, String link) throws IOException {
237 out.write(htmlControlFileName.toString());
238 if (!showImage) {
239 String iconPath = MIMEMapping.getMIMETypeIcon(control.getExtension());
240
241 out.write(Spacer.getHtml(0, 0));
242
243 out.write("<a href=");
244 out.write(link);
245 out.write(" target=\"_blank\">");
246
247 out.write("<img src=\""
248 + this.getRequest().getContextPath()
249 + iconPath
250 + "\" class=\""
251 + CssConstants.CSSCLASS_FILEICON
252 + "\" border=\"0\">");
253 out.write(control.getFileName() + "." + control.getExtension() + "</a>");
254 }
255 }
256
257 protected StringBuffer getHtmlControlFileName(File control) {
258 StringBuffer htmlControlFileName = new StringBuffer();
259 htmlControlFileName.append("<span class=\""
260 + CssConstants.CSSCLASS_DESCRIPTION
261 + "\">"
262 + getMessage("dialog.file.filename")
263 + "</span>");
264 htmlControlFileName.append(Spacer.getHtml(1, 1));
265 htmlControlFileName.append(control.getHtmlFileName() + "<span id=\""
266 + this.getName()
267 + "_fileNameExtension\">."
268 + control.getExtension()
269 + "</span>");
270 return htmlControlFileName;
271 }
272
273 public boolean validate() {
274 if (isRequired()) {
275
276 MultipartForm form = (MultipartForm) getRequest().getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME);
277 if (form != null) {
278 Document doc = form.getDocument(getName());
279 if (doc != null) {
280 return true;
281 }
282
283
284
285 if (form.getParameter(getName() + "_" + File.REMOVE) != null) {
286 setValidationMessage("dialogs.validation.required");
287 return false;
288 }
289 }
290
291
292 if(this.getStorageNode() == null || !getStorageNode().getNodeData(getName()).isExist()){
293 setValidationMessage("dialogs.validation.required");
294 return false;
295 }
296 }
297 return true;
298 }
299
300
301
302
303
304
305 protected String getFileURI(File control) {
306 return control.getHandle();
307 }
308
309
310
311
312 protected File getFileControl() {
313 File control = new File(this.getName(), this.getStorageNode());
314 return control;
315 }
316 }