View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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 org.apache.commons.lang.StringUtils;
41  
42  /**
43   * @author Vinzenz Wyser
44   * @version 2.0
45   */
46  public class File extends ControlImpl {
47  
48      public static final String REMOVE = "remove"; //$NON-NLS-1$
49  
50      private String cssClassFileName;
51  
52      private String nodeDataTemplate;
53  
54      /**
55       * Package private constructor
56       */
57      File() {
58      }
59  
60      /**
61       * @param name
62       * @param value
63       */
64      public File(String name, String value) {
65          super(name, value);
66      }
67  
68      /**
69       * @param name
70       * @param content
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 + "\""; //$NON-NLS-1$ //$NON-NLS-2$
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\""); //$NON-NLS-1$
105         html.append(" name=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
106         html.append(" id=\"" + this.getName() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
107         html.append(" onchange=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
108         html.append(" onblur=\"mgnlControlFileSetFileName('" + this.getName() + "')\""); //$NON-NLS-1$ //$NON-NLS-2$
109         html.append(this.getHtmlCssClass());
110         html.append(" />"); //$NON-NLS-1$
111         Hidden control0 = new Hidden(this.getName() + "_" + REMOVE, StringUtils.EMPTY); //$NON-NLS-1$
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()); //$NON-NLS-1$
146         control.setSaveInfo(false);
147         if (StringUtils.isNotEmpty(this.getCssClassFileName())) {
148             control.setCssClass(this.cssClassFileName);
149         }
150 
151         control.setCssStyles("width", "45%"); //$NON-NLS-1$ //$NON-NLS-2$
152         return control.getHtml();
153     }
154 
155     public String getHtmlNodeDataTemplate() {
156         Hidden control = new Hidden(this.getName() + "_" + FileProperties.PROPERTY_TEMPLATE, this.getNodeDataTemplate()); //$NON-NLS-1$
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")); //$NON-NLS-1$
168         control1.setCssClass("mgnlControlButtonSmall"); //$NON-NLS-1$
169         control1.setOnclick(additionalOnclick + "mgnlControlFileRemove('" + this.getName() + "')"); //$NON-NLS-1$ //$NON-NLS-2$
170         return control1.getHtml();
171     }
172 
173     public String getHandle() {
174         return this.getWebsiteNode().getHandle() + "/" + this.getName(); //$NON-NLS-1$
175     }
176 
177     @Override
178     public String getPath() {
179         return getHandle() + "/" + this.getFileName() + "." + this.getExtension(); //$NON-NLS-1$ //$NON-NLS-2$
180     }
181 
182     /**
183      * Read a property from content node, check nulls.
184      * @param propertyName node data name
185      * @return property string, "" if not found
186      * @throws RepositoryException
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 }