KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > control > File


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-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.control;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.NodeData;
17 import info.magnolia.cms.gui.misc.FileProperties;
18 import info.magnolia.cms.i18n.MessagesManager;
19
20 import javax.jcr.PathNotFoundException;
21 import javax.jcr.RepositoryException;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.log4j.Logger;
25
26
27 /**
28  * @author Vinzenz Wyser
29  * @version 2.0
30  */

31 public class File extends ControlSuper {
32
33     public static final String JavaDoc REMOVE = "remove"; //$NON-NLS-1$
34

35     /**
36      * Logger.
37      */

38     private static Logger log = Logger.getLogger(File.class);
39
40     private String JavaDoc cssClassFileName;
41
42     private String JavaDoc nodeDataTemplate;
43
44     public File() {
45     }
46
47     public File(String JavaDoc name, String JavaDoc value) {
48         super(name, value);
49     }
50
51     public File(String JavaDoc name, Content websiteNode) {
52         super(name, websiteNode);
53     }
54
55     public void setCssClassFileName(String JavaDoc s) {
56         this.cssClassFileName = s;
57     }
58
59     public String JavaDoc getCssClassFileName() {
60         return this.cssClassFileName;
61     }
62
63     public String JavaDoc getHtmlCssClassFileName() {
64         if (StringUtils.isNotEmpty(this.cssClassFileName)) {
65             return " class=\"" + this.cssClassFileName + "\""; //$NON-NLS-1$ //$NON-NLS-2$
66
}
67
68         return StringUtils.EMPTY;
69     }
70
71     public String JavaDoc getHtml() {
72         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
73         html.append(this.getHtmlBrowse());
74         html.append(this.getHtmlFileName());
75         html.append(this.getHtmlNodeDataTemplate());
76         html.append(this.getHtmlRemove());
77         return html.toString();
78     }
79
80     public String JavaDoc getHtmlBrowse() {
81         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
82         html.append("<input type=\"file\""); //$NON-NLS-1$
83
html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
84
html.append(" id=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
85
html.append(" onchange=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
86
html.append(" onblur=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
87
html.append(this.getHtmlCssClass());
88         html.append(" />"); //$NON-NLS-1$
89
Hidden control0 = new Hidden(this.getName() + "_" + REMOVE, StringUtils.EMPTY); //$NON-NLS-1$
90
control0.setSaveInfo(false);
91         html.append(control0.getHtml());
92         if (this.getSaveInfo()) {
93             html.append(this.getHtmlSaveInfo());
94         }
95         return html.toString();
96     }
97
98     public String JavaDoc getFileName() {
99         String JavaDoc fileName = StringUtils.EMPTY;
100         try {
101             fileName = getPropertyString(FileProperties.PROPERTY_FILENAME);
102         }
103         catch (PathNotFoundException e) {
104             if (log.isDebugEnabled()) {
105                 log.debug("Data not found: " + e.getMessage()); //$NON-NLS-1$
106
}
107         }
108         catch (RepositoryException e) {
109             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
110
}
111         return fileName;
112     }
113
114     public void setNodeDataTemplate(String JavaDoc s) {
115         this.nodeDataTemplate = s;
116     }
117
118     public String JavaDoc getNodeDataTemplate() {
119         String JavaDoc template = this.nodeDataTemplate;
120         if (template == null) {
121             try {
122                 template = getPropertyString(FileProperties.PROPERTY_TEMPLATE);
123             }
124             catch (PathNotFoundException e) {
125                 if (log.isDebugEnabled()) {
126                     log.debug("Data not found: " + e.getMessage()); //$NON-NLS-1$
127
}
128             }
129             catch (RepositoryException e) {
130                 log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
131
}
132         }
133         return template;
134     }
135
136     public String JavaDoc getExtension() {
137         String JavaDoc ext = StringUtils.EMPTY;
138         try {
139             ext = getPropertyString(FileProperties.PROPERTY_EXTENSION);
140         }
141         catch (PathNotFoundException e) {
142             if (log.isDebugEnabled()) {
143                 log.debug("Data not found: " + e.getMessage()); //$NON-NLS-1$
144
}
145         }
146         catch (RepositoryException e) {
147             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
148
}
149         return ext;
150     }
151
152     public String JavaDoc getHtmlFileName() {
153         Edit control = new Edit(this.getName() + "_" + FileProperties.PROPERTY_FILENAME, this.getFileName()); //$NON-NLS-1$
154
control.setSaveInfo(false);
155         if (StringUtils.isNotEmpty(this.getCssClassFileName())) {
156             control.setCssClass(this.cssClassFileName);
157         }
158
159         control.setCssStyles("width", "45%"); //$NON-NLS-1$ //$NON-NLS-2$
160
return control.getHtml();
161     }
162
163     public String JavaDoc getHtmlNodeDataTemplate() {
164         Hidden control = new Hidden(this.getName() + "_" + FileProperties.PROPERTY_TEMPLATE, this.getNodeDataTemplate()); //$NON-NLS-1$
165
control.setSaveInfo(false);
166         return control.getHtml();
167     }
168
169     public String JavaDoc getHtmlRemove() {
170         return getHtmlRemove(StringUtils.EMPTY);
171     }
172
173     public String JavaDoc getHtmlRemove(String JavaDoc additionalOnclick) {
174         Button control1 = new Button();
175         control1.setLabel(MessagesManager.get(getRequest(), "dialog.file.remove")); //$NON-NLS-1$
176
control1.setCssClass("mgnlControlButtonSmall"); //$NON-NLS-1$
177
control1.setOnclick(additionalOnclick + "mgnlControlFileRemove('" + this.getName() + "')"); //$NON-NLS-1$ //$NON-NLS-2$
178
return control1.getHtml();
179     }
180
181     public String JavaDoc getHandle() {
182         return this.getWebsiteNode().getHandle() + "/" + this.getName(); //$NON-NLS-1$
183
}
184
185     public String JavaDoc getPath() {
186         return getHandle() + "/" + this.getFileName() + "." + this.getExtension(); //$NON-NLS-1$ //$NON-NLS-2$
187
}
188
189     /**
190      * Read a property from content node, check nulls.
191      * @param propertyName node data name
192      * @return property string, "" if not found
193      * @throws RepositoryException
194      */

195     protected String JavaDoc getPropertyString(String JavaDoc propertyName) throws RepositoryException {
196         if (this.getWebsiteNode() != null) {
197             Content contentNode = getPropertyNode();
198             if (contentNode != null) {
199                 NodeData nodeData = contentNode.getNodeData(propertyName);
200                 if (nodeData != null) {
201                     return nodeData.getString();
202                 }
203             }
204         }
205
206         return StringUtils.EMPTY;
207     }
208
209     protected Content getPropertyNode() throws RepositoryException {
210         return this.getWebsiteNode().getContent(this.getName() + "_" + FileProperties.PROPERTIES_CONTENTNODE); //$NON-NLS-1$
211
}
212 }
213
Popular Tags