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