View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Vinzenz Wyser
48   * @version 2.0
49   */
50  public class File extends ControlImpl {
51  
52      public static final String REMOVE = "remove"; //$NON-NLS-1$
53  
54      private String cssClassFileName;
55  
56      private String nodeDataTemplate;
57  
58      /**
59       * Package private constructor
60       */
61      File() {
62      }
63  
64      /**
65       * @param name
66       * @param value
67       */
68      public File(String name, String value) {
69          super(name, value);
70      }
71  
72      /**
73       * @param name
74       * @param content
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 + "\""; //$NON-NLS-1$ //$NON-NLS-2$
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\""); //$NON-NLS-1$
110         html.append(" name=\"" + name + "\""); //$NON-NLS-1$ //$NON-NLS-2$
111         html.append(" id=\"" + name + "\""); //$NON-NLS-1$ //$NON-NLS-2$
112         html.append(" onchange=\"mgnlControlFileSetFileName('" + name + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
113         html.append(" onblur=\"mgnlControlFileSetFileName('" + name + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
114         html.append(this.getHtmlCssClass());
115         html.append(" />"); //$NON-NLS-1$
116         Hidden control0 = new Hidden(name + "_" + REMOVE, StringUtils.EMPTY); //$NON-NLS-1$
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%"); //$NON-NLS-1$ //$NON-NLS-2$
157         return control.getHtml();
158     }
159 
160     public String getHtmlNodeDataTemplate() {
161         Hidden control = new Hidden(this.getName() + "_" + FileProperties.PROPERTY_TEMPLATE, this.getNodeDataTemplate()); //$NON-NLS-1$
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")); //$NON-NLS-1$
173         control1.setCssClass("mgnlControlButtonSmall"); //$NON-NLS-1$
174         control1.setOnclick(additionalOnclick + "mgnlControlFileRemove('" + this.getName() + "')");
175         return control1.getHtml();
176     }
177 
178     public String getHandle() {
179         return this.getWebsiteNode().getHandle() + "/" + this.getName(); //$NON-NLS-1$
180     }
181 
182     @Override
183     public String getPath() {
184         return getHandle() + "/" + this.getFileName() + "." + this.getExtension(); //$NON-NLS-1$ //$NON-NLS-2$
185     }
186 
187     /**
188      * Read a property from content node, check nulls.
189      * @param propertyName node data name
190      * @return property string, "" if not found
191      * @throws RepositoryException
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 }