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