KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > dialog > DialogFile


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.dialog;
14
15 import info.magnolia.cms.beans.config.MIMEMapping;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.gui.control.File;
18 import info.magnolia.cms.gui.misc.CssConstants;
19 import info.magnolia.cms.gui.misc.Spacer;
20
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.jcr.PropertyType;
27 import javax.jcr.RepositoryException;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30
31 import org.apache.commons.lang.StringUtils;
32
33
34 /**
35  * @author Vinzenz Wyser
36  * @version 2.0
37  */

38 public class DialogFile extends DialogBox {
39
40     private List JavaDoc imageExtensions = new ArrayList JavaDoc();
41
42     /**
43      * Empty constructor should only be used by DialogFactory.
44      */

45     protected DialogFile() {
46     }
47
48     /**
49      * @see info.magnolia.cms.gui.dialog.DialogControl#init(HttpServletRequest, HttpServletResponse, Content, Content)
50      */

51     public void init(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Content websiteNode, Content configNode)
52         throws RepositoryException {
53         super.init(request, response, websiteNode, configNode);
54         initImageExtensions();
55     }
56
57     public List JavaDoc getImageExtensions() {
58         return this.imageExtensions;
59     }
60
61     public void setImageExtensions(List JavaDoc l) {
62         this.imageExtensions = l;
63     }
64
65     public void initImageExtensions() {
66         this.getImageExtensions().add("jpg"); //$NON-NLS-1$
67
this.getImageExtensions().add("jpeg"); //$NON-NLS-1$
68
this.getImageExtensions().add("gif"); //$NON-NLS-1$
69
this.getImageExtensions().add("png"); //$NON-NLS-1$
70
this.getImageExtensions().add("bpm"); //$NON-NLS-1$
71
this.getImageExtensions().add("swf"); //$NON-NLS-1$
72
}
73
74     /**
75      * @see info.magnolia.cms.gui.dialog.DialogControl#drawHtml(Writer)
76      */

77     public void drawHtml(Writer JavaDoc out) throws IOException JavaDoc {
78         File control = getFileControl();
79         control.setType(this.getConfigValue("type", PropertyType.TYPENAME_STRING)); //$NON-NLS-1$
80
control.setSaveInfo(false); // set manualy below
81
control.setCssClass(CssConstants.CSSCLASS_FILE);
82         control.setCssClassFileName(CssConstants.CSSCLASS_EDIT);
83         control.setCssStyles("width", this.getConfigValue("width", "100%")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
84

85         this.drawHtmlPre(out);
86
87         String JavaDoc width = this.getConfigValue("width", "100%"); //$NON-NLS-1$ //$NON-NLS-2$
88
boolean showImage = this.getImageExtensions().contains(control.getExtension().toLowerCase());
89
90         String JavaDoc htmlControlBrowse = control.getHtmlBrowse();
91         StringBuffer JavaDoc htmlControlFileName = new StringBuffer JavaDoc();
92         htmlControlFileName.append("<span class=\"" //$NON-NLS-1$
93
+ CssConstants.CSSCLASS_DESCRIPTION
94             + "\">" //$NON-NLS-1$
95
+ getMessage("dialog.file.filename") //$NON-NLS-1$
96
+ "</span>"); //$NON-NLS-1$
97
htmlControlFileName.append(Spacer.getHtml(1, 1));
98         htmlControlFileName.append(control.getHtmlFileName() + "<span id=\"" //$NON-NLS-1$
99
+ this.getName()
100             + "_fileNameExtension\">." //$NON-NLS-1$
101
+ control.getExtension()
102             + "</span>"); //$NON-NLS-1$
103
String JavaDoc htmlContentEmpty = htmlControlBrowse + Spacer.getHtml(0, 0) + htmlControlFileName;
104         out.write("<div id=\"" + this.getName() + "_contentDiv\" style=\"width:100%;\">"); //$NON-NLS-1$ //$NON-NLS-2$
105
boolean exists = false;
106
107         if (this.getWebsiteNode() != null) {
108             exists = this.getWebsiteNode().getNodeData(this.getName()).isExist();
109         }
110
111         if (!exists) {
112             out.write(htmlContentEmpty);
113             out.write("</div>"); //$NON-NLS-1$
114
}
115         else {
116             if (showImage) {
117
118                 out.write("\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"" + width + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
119
out.write("<tr><td class=\"" + CssConstants.CSSCLASS_FILEIMAGE + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
120

121                 if ("swf".equals(control.getExtension().toLowerCase())) {
122
123                     // flash movie
124
out.write("<object type=\"application/x-shockwave-flash\" data=\"");
125                     out.write(this.getRequest().getContextPath());
126                     out.write(getFileURI(control));
127                     out.write("\" title=\"");
128                     out.write(control.getFileName());
129                     out.write("\" ");
130                     out.write("width=\"150\" ");
131                     out.write("height=\"100\" ");
132                     out.write(">");
133
134                     out.write("<param name=\"movie\" value=\"");
135                     out.write(this.getRequest().getContextPath());
136                     out.write(getFileURI(control));
137                     out.write("\"/>");
138
139                     out.write("</object>\n");
140
141                 }
142                 else {
143                     // standard image
144

145                     // todo: image thumbnail template
146
// out.write("<img SRC=\""+ this.getRequest().getContextPath()
147
// +THUMB_PATH+"?src="+control.getHandle()+"\"
148
// class=\""+CSSCLASS_FILEIMAGE+"\">");
149
// tmp workaround: resize in html ...
150

151                     int imgwidth = 150;
152                     int imgheight = 150;
153
154                     try{
155                         imgwidth = Integer.parseInt(control.getImageWidth());
156                         imgheight = Integer.parseInt(control.getImageHeight());
157                     }
158                     catch(NumberFormatException JavaDoc e){
159                         // ignore (is 150)
160
}
161                     int bigger = Math.max(imgwidth,imgheight);
162                     if(bigger > 150){
163                         imgwidth=150;
164                     }
165
166                     out.write("<img width=\"" + imgwidth + "\" SRC=\""); //$NON-NLS-1$
167
out.write(this.getRequest().getContextPath());
168                     out.write(getFileURI(control));
169                     out.write("\" class=\""); //$NON-NLS-1$
170
out.write(CssConstants.CSSCLASS_FILEIMAGE);
171                     out.write("\" alt=\""); //$NON-NLS-1$
172
out.write(control.getFileName());
173                     out.write("\" title=\""); //$NON-NLS-1$
174
out.write(control.getFileName());
175                     out.write("\" />\n"); //$NON-NLS-1$
176

177
178                     if (StringUtils.isNotEmpty(control.getImageWidth())) {
179                         out.write("<em style='white-space:nowrap'>"); //$NON-NLS-1$
180

181                         out.write("width: "); //$NON-NLS-1$
182
out.write(control.getImageWidth());
183
184                         out.write(" height: "); //$NON-NLS-1$
185
out.write(control.getImageHeight());
186
187                         out.write("</em>\n"); //$NON-NLS-1$
188
}
189
190                 }
191
192                 out.write("</td><td>"); //$NON-NLS-1$
193
}
194             out.write(htmlControlFileName.toString());
195             if (!showImage) {
196                 String JavaDoc iconPath = MIMEMapping.getMIMETypeIcon(control.getExtension());
197
198                 out.write(Spacer.getHtml(0, 0));
199                 out.write("<a HREF=" + this.getRequest().getContextPath() + control.getPath() + " target=\"_blank\">"); //$NON-NLS-1$ //$NON-NLS-2$
200
out.write("<img SRC=\"" //$NON-NLS-1$
201
+ this.getRequest().getContextPath()
202                     + iconPath
203                     + "\" class=\"" //$NON-NLS-1$
204
+ CssConstants.CSSCLASS_FILEICON
205                     + "\" border=\"0\">"); //$NON-NLS-1$
206
out.write(control.getFileName() + "." + control.getExtension() + "</a>"); //$NON-NLS-1$ //$NON-NLS-2$
207
}
208             out.write(Spacer.getHtml(12, 12));
209             out.write(control.getHtmlRemove("mgnlDialogFileRemove('" + this.getName() + "');")); //$NON-NLS-1$ //$NON-NLS-2$
210
if (showImage) {
211                 out.write("</td></tr></table>"); //$NON-NLS-1$
212
}
213             out.write("</div>\n"); //$NON-NLS-1$
214
out.write("<div style=\"position:absolute;top:-500px;left:-500px;visibility:hidden;\">\n<textarea id=\""); //$NON-NLS-1$
215
out.write(this.getName());
216             out.write("_contentEmpty\">");//$NON-NLS-1$
217
out.write(htmlContentEmpty);
218
219             // @todo should be escaped, but we need to test it
220
// out.write(StringEscapeUtils.escapeXml(htmlContentEmpty));
221
out.write("</textarea>\n</div>\n"); //$NON-NLS-1$
222
}
223         control.setSaveInfo(true);
224         out.write(control.getHtmlSaveInfo());
225         control.setNodeDataTemplate(this.getConfigValue("nodeDataTemplate", null)); //$NON-NLS-1$
226
out.write(control.getHtmlNodeDataTemplate());
227         this.drawHtmlPost(out);
228     }
229
230     /**
231      * Get the uri of the file (used to show images)
232      * @param control
233      * @return
234      */

235     protected String JavaDoc getFileURI(File control) {
236         return control.getHandle();
237     }
238
239     /**
240      * Configures the inner file upload control
241      */

242     protected File getFileControl() {
243         File control = new File(this.getName(), this.getWebsiteNode());
244         return control;
245     }
246 }
247
Popular Tags