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