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.control;
35
36 import info.magnolia.cms.beans.runtime.FileProperties;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.core.NodeData;
39 import info.magnolia.cms.i18n.MessagesManager;
40 import info.magnolia.util.EscapeUtil;
41
42 import javax.jcr.RepositoryException;
43
44 import org.apache.commons.lang.StringUtils;
45
46
47
48
49
50 public class File extends ControlImpl {
51
52 public static final String REMOVE = "remove";
53
54 private String cssClassFileName;
55
56 private String nodeDataTemplate;
57
58
59
60
61 File() {
62 }
63
64
65
66
67
68 public File(String name, String value) {
69 super(name, value);
70 }
71
72
73
74
75
76 public File(String name, Content content) {
77 super(name, content);
78 }
79
80 public void setCssClassFileName(String s) {
81 this.cssClassFileName = s;
82 }
83
84 public String getCssClassFileName() {
85 return this.cssClassFileName;
86 }
87
88 public String getHtmlCssClassFileName() {
89 if (StringUtils.isNotEmpty(this.cssClassFileName)) {
90 return " class=\"" + this.cssClassFileName + "\"";
91 }
92
93 return StringUtils.EMPTY;
94 }
95
96 @Override
97 public String getHtml() {
98 StringBuffer html = new StringBuffer();
99 html.append(this.getHtmlBrowse());
100 html.append(this.getHtmlFileName());
101 html.append(this.getHtmlNodeDataTemplate());
102 html.append(this.getHtmlRemove());
103 return html.toString();
104 }
105
106 public String getHtmlBrowse() {
107 final String name = this.getName();
108 StringBuffer html = new StringBuffer();
109 html.append("<input type=\"file\"");
110 html.append(" name=\"" + name + "\"");
111 html.append(" id=\"" + name + "\"");
112 html.append(" onchange=\"mgnlControlFileSetFileName('" + name + "')\"");
113 html.append(" onblur=\"mgnlControlFileSetFileName('" + name + "')\"");
114 html.append(this.getHtmlCssClass());
115 html.append(" />");
116 Hidden control0 = new Hidden(name + "_" + REMOVE, StringUtils.EMPTY);
117 control0.setSaveInfo(false);
118 html.append(control0.getHtml());
119 if (this.getSaveInfo()) {
120 html.append(this.getHtmlSaveInfo());
121 }
122 return html.toString();
123 }
124
125 public String getFileName() {
126 return EscapeUtil.escapeXss(getPropertyString(FileProperties.PROPERTY_FILENAME));
127 }
128
129 public String getImageWidth() {
130 return getPropertyString(FileProperties.PROPERTY_WIDTH);
131 }
132
133 public String getImageHeight() {
134 return getPropertyString(FileProperties.PROPERTY_HEIGHT);
135 }
136
137 public void setNodeDataTemplate(String s) {
138 this.nodeDataTemplate = s;
139 }
140
141 public String getNodeDataTemplate() {
142 return this.nodeDataTemplate;
143 }
144
145 public String getExtension() {
146 return EscapeUtil.escapeXss(getPropertyString(FileProperties.PROPERTY_EXTENSION));
147 }
148
149 public String getHtmlFileName() {
150 Edit control = new Edit(this.getName() + "_" + FileProperties.PROPERTY_FILENAME, this.getFileName());
151 control.setSaveInfo(false);
152 if (StringUtils.isNotEmpty(this.getCssClassFileName())) {
153 control.setCssClass(this.cssClassFileName);
154 }
155
156 control.setCssStyles("width", "45%");
157 return control.getHtml();
158 }
159
160 public String getHtmlNodeDataTemplate() {
161 Hidden control = new Hidden(this.getName() + "_" + FileProperties.PROPERTY_TEMPLATE, this.getNodeDataTemplate());
162 control.setSaveInfo(false);
163 return control.getHtml();
164 }
165
166 public String getHtmlRemove() {
167 return getHtmlRemove(StringUtils.EMPTY);
168 }
169
170 public String getHtmlRemove(String additionalOnclick) {
171 Button control1 = new Button();
172 control1.setLabel(MessagesManager.get("dialog.file.remove"));
173 control1.setCssClass("mgnlControlButtonSmall");
174 control1.setOnclick(additionalOnclick + "mgnlControlFileRemove('" + this.getName() + "')");
175 return control1.getHtml();
176 }
177
178 public String getHandle() {
179 return this.getWebsiteNode().getHandle() + "/" + this.getName();
180 }
181
182 @Override
183 public String getPath() {
184 return getHandle() + "/" + this.getFileName() + "." + this.getExtension();
185 }
186
187
188
189
190
191
192
193 protected String getPropertyString(String propertyName) {
194 if (this.getWebsiteNode() != null) {
195 NodeData nodeData = getPropertyNode();
196 if (nodeData != null) {
197 return nodeData.getAttribute(propertyName);
198 }
199 }
200
201 return StringUtils.EMPTY;
202 }
203
204 protected NodeData getPropertyNode() {
205 return this.getWebsiteNode().getNodeData(this.getName());
206 }
207 }