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