1 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 31 public class File extends ControlSuper { 32 33 public static final String REMOVE = "remove"; 35 38 private static Logger log = Logger.getLogger(File.class); 39 40 private String cssClassFileName; 41 42 private String nodeDataTemplate; 43 44 public File() { 45 } 46 47 public File(String name, String value) { 48 super(name, value); 49 } 50 51 public File(String name, Content websiteNode) { 52 super(name, websiteNode); 53 } 54 55 public void setCssClassFileName(String s) { 56 this.cssClassFileName = s; 57 } 58 59 public String getCssClassFileName() { 60 return this.cssClassFileName; 61 } 62 63 public String getHtmlCssClassFileName() { 64 if (StringUtils.isNotEmpty(this.cssClassFileName)) { 65 return " class=\"" + this.cssClassFileName + "\""; } 67 68 return StringUtils.EMPTY; 69 } 70 71 public String getHtml() { 72 StringBuffer html = new StringBuffer (); 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 getHtmlBrowse() { 81 StringBuffer html = new StringBuffer (); 82 html.append("<input type=\"file\""); html.append(" name=\"" + this.getName() + "\""); html.append(" id=\"" + this.getName() + "\""); html.append(" onchange=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); html.append(" onblur=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); html.append(this.getHtmlCssClass()); 88 html.append(" />"); Hidden control0 = new Hidden(this.getName() + "_" + REMOVE, StringUtils.EMPTY); 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 getFileName() { 99 String 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()); } 107 } 108 catch (RepositoryException e) { 109 log.debug("Exception caught: " + e.getMessage(), e); } 111 return fileName; 112 } 113 114 public void setNodeDataTemplate(String s) { 115 this.nodeDataTemplate = s; 116 } 117 118 public String getNodeDataTemplate() { 119 String 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()); } 128 } 129 catch (RepositoryException e) { 130 log.debug("Exception caught: " + e.getMessage(), e); } 132 } 133 return template; 134 } 135 136 public String getExtension() { 137 String 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()); } 145 } 146 catch (RepositoryException e) { 147 log.debug("Exception caught: " + e.getMessage(), e); } 149 return ext; 150 } 151 152 public String getHtmlFileName() { 153 Edit control = new Edit(this.getName() + "_" + FileProperties.PROPERTY_FILENAME, this.getFileName()); control.setSaveInfo(false); 155 if (StringUtils.isNotEmpty(this.getCssClassFileName())) { 156 control.setCssClass(this.cssClassFileName); 157 } 158 159 control.setCssStyles("width", "45%"); return control.getHtml(); 161 } 162 163 public String getHtmlNodeDataTemplate() { 164 Hidden control = new Hidden(this.getName() + "_" + FileProperties.PROPERTY_TEMPLATE, this.getNodeDataTemplate()); control.setSaveInfo(false); 166 return control.getHtml(); 167 } 168 169 public String getHtmlRemove() { 170 return getHtmlRemove(StringUtils.EMPTY); 171 } 172 173 public String getHtmlRemove(String additionalOnclick) { 174 Button control1 = new Button(); 175 control1.setLabel(MessagesManager.get(getRequest(), "dialog.file.remove")); control1.setCssClass("mgnlControlButtonSmall"); control1.setOnclick(additionalOnclick + "mgnlControlFileRemove('" + this.getName() + "')"); return control1.getHtml(); 179 } 180 181 public String getHandle() { 182 return this.getWebsiteNode().getHandle() + "/" + this.getName(); } 184 185 public String getPath() { 186 return getHandle() + "/" + this.getFileName() + "." + this.getExtension(); } 188 189 195 protected String getPropertyString(String 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); } 212 } 213 | Popular Tags |