View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.ui.form.field.upload.basic;
35  
36  import static com.vaadin.shared.ui.ContentMode.HTML;
37  
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.icons.MagnoliaIcons;
40  import info.magnolia.ui.form.field.upload.UploadProgressIndicator;
41  import info.magnolia.ui.theme.ResurfaceTheme;
42  
43  import java.text.NumberFormat;
44  
45  import org.apache.commons.io.FileUtils;
46  
47  import com.vaadin.ui.Button;
48  import com.vaadin.ui.Component;
49  import com.vaadin.ui.CssLayout;
50  import com.vaadin.ui.CustomComponent;
51  import com.vaadin.ui.Label;
52  import com.vaadin.ui.ProgressBar;
53  import com.vaadin.ui.VerticalLayout;
54  
55  /**
56   * Custom Component used to create a custom display for the progress indicator.
57   * <p>
58   * This view normally contains a progress bar and a label indicating the uploaded percentage, filename...<br>
59   * Refreshing the view is done by calling {@link #refreshLayout(long, long, String)}
60   * <p>
61   * Layout composition:
62   * <ul>
63   * <li>Label.FileName
64   * <li>ProgressBar
65   * <li>Label.percentage
66   * <li>Label.UploadexOfy
67   * </ul>
68   */
69  public class ResurfaceBasicUploadProgressIndicator extends CustomComponent implements UploadProgressIndicator {
70  
71      private static final long serialVersionUID = 1L;
72      private final SimpleTranslator i18n;
73      private ProgressBar progressIndicator;
74      private Label uploadFileLocation;
75      private Label uploadFileRatio;
76      private Label uploadFileProgress;
77      private String inProgressCaption;
78      private String inProgressRatioCaption;
79      private VerticalLayout mainLayout;
80  
81      public ResurfaceBasicUploadProgressIndicator(String inProgressCaption, String inProgressRatioCaption, SimpleTranslator i18n, Button.ClickListener cancelUploadListener) {
82          this.inProgressCaption = inProgressCaption;
83          this.inProgressRatioCaption = inProgressRatioCaption;
84          this.i18n = i18n;
85  
86          uploadFileLocation = new Label();
87          uploadFileLocation.setSizeUndefined();
88          uploadFileLocation.setContentMode(HTML);
89          uploadFileLocation.addStyleName("uploading-file");
90  
91          uploadFileRatio = new Label();
92          uploadFileRatio.setSizeUndefined();
93  
94          uploadFileProgress = new Label("");
95          uploadFileProgress.setSizeUndefined();
96          uploadFileProgress.addStyleName("uploading-file-progress");
97  
98          progressIndicator = new ProgressBar();
99          progressIndicator.setVisible(false);
100         progressIndicator.setWidth("100%");
101         progressIndicator.setHeight("100%");
102 
103         Button cancelButton = new Button(MagnoliaIcons.CLOSE);
104         cancelButton.addStyleNames(ResurfaceTheme.BUTTON_ICON, "cancel-uploading");
105         cancelButton.addClickListener(cancelUploadListener);
106         cancelButton.setEnabled(true);
107 
108         mainLayout = new VerticalLayout();
109         mainLayout.setSizeFull();
110         mainLayout.setMargin(false);
111         mainLayout.setSpacing(false);
112         mainLayout.addStyleName("uploading-progress-indicator");
113 
114         CssLayout progressLayout = new CssLayout();
115         progressLayout.addStyleName("progress-layout");
116         progressLayout.addComponent(progressIndicator);
117         progressLayout.addComponent(uploadFileProgress);
118         progressLayout.addComponent(cancelButton);
119 
120         mainLayout.addComponent(uploadFileLocation);
121         mainLayout.addComponent(progressLayout);
122         mainLayout.addComponent(uploadFileRatio);
123 
124         setCompositionRoot(mainLayout);
125 
126 
127     }
128 
129     @Override
130     public void refreshLayout(long readBytes, long contentLength, String fileName) {
131         setProgress(Float.valueOf(readBytes / (float) contentLength));
132 
133         uploadFileLocation.setValue(i18n.translate(this.inProgressCaption, fileName));
134 
135         uploadFileProgress.setValue(createPercentage(readBytes, contentLength));
136 
137         String bytesRead = FileUtils.byteCountToDisplaySize(readBytes);
138         String totalBytes = FileUtils.byteCountToDisplaySize(contentLength);
139         uploadFileRatio.setValue(i18n.translate(this.inProgressRatioCaption, bytesRead, totalBytes));
140     }
141 
142     @Override
143     public void setProgress(float progress) {
144         progressIndicator.setValue(progress);
145     }
146 
147     @Override
148     public void setVisible(boolean visible) {
149         mainLayout.setVisible(visible);
150         progressIndicator.setVisible(visible);
151     }
152 
153     @Override
154     public Component asVaadinComponent() {
155         return this;
156     }
157 
158     /**
159      * Creates a percentage representation of the upload currently in progress.
160      */
161     private String createPercentage(long readBytes, long contentLength) {
162         double read = Double.valueOf(readBytes);
163         double from = Double.valueOf(contentLength);
164 
165         NumberFormat defaultFormat = NumberFormat.getPercentInstance();
166 
167         return defaultFormat.format((read / from));
168     }
169 
170 
171     protected Label getUploadFileLocation() {
172         return uploadFileLocation;
173     }
174 
175     protected Label getUploadFileRatio() {
176         return uploadFileRatio;
177     }
178 
179     protected Label getUploadFileProgress() {
180         return uploadFileProgress;
181     }
182 
183     protected VerticalLayout getMainLayout() {
184         return mainLayout;
185     }
186 
187     protected ProgressBar getProgressIndicator() {
188         return progressIndicator;
189     }
190 }