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