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 info.magnolia.i18nsystem.SimpleTranslator;
37  import info.magnolia.ui.form.field.upload.UploadProgressIndicator;
38  
39  import java.text.NumberFormat;
40  import java.util.Iterator;
41  
42  import org.apache.commons.io.FileUtils;
43  
44  import com.vaadin.shared.ui.MarginInfo;
45  import com.vaadin.ui.Alignment;
46  import com.vaadin.ui.Component;
47  import com.vaadin.ui.CssLayout;
48  import com.vaadin.ui.CustomComponent;
49  import com.vaadin.v7.ui.Label;
50  import com.vaadin.v7.ui.ProgressBar;
51  import com.vaadin.v7.ui.VerticalLayout;
52  
53  /**
54   * Custom Component used to create a custom display for the progress indicator.
55   * <p>
56   * This view normally contains a progress bar and a label indicating the uploaded percentage, filename...<br>
57   * Refreshing the view is done by calling {@link #refreshLayout(long, long, String)}
58   * <p>
59   * Layout composition:
60   * <ul>
61   * <li>Label.FileName
62   * <li>ProgressBar
63   * <li>Label.percentage
64   * <li>Label.UploadexOfy
65   * </ul>
66   */
67  public class BasicUploadProgressIndicator extends CustomComponent implements UploadProgressIndicator {
68  
69      private static final long serialVersionUID = 1L;
70  
71      private ProgressBar progressIndicator;
72      private Label uploadFileLocation;
73      private Label uploadFileRatio;
74      private Label uploadFileProgress;
75      private String inProgressCaption;
76      private String inProgressRatioCaption;
77      private VerticalLayout mainLayout;
78      private final SimpleTranslator i18n;
79  
80      public BasicUploadProgressIndicator(String inProgressCaption, String inProgressRatioCaption, SimpleTranslator i18n) {
81          this.inProgressCaption = inProgressCaption;
82          this.inProgressRatioCaption = inProgressRatioCaption;
83          this.i18n = i18n;
84  
85          uploadFileLocation = new Label("");
86          uploadFileLocation.setSizeUndefined();
87          uploadFileLocation.addStyleName("uploading-file");
88  
89          uploadFileRatio = new Label("");
90          uploadFileRatio.setSizeUndefined();
91          uploadFileRatio.addStyleName("uploaded-file");
92  
93          uploadFileProgress = new Label("");
94          uploadFileProgress.setSizeUndefined();
95          uploadFileProgress.addStyleName("uploading-file-progress");
96  
97          progressIndicator = new ProgressBar();
98          progressIndicator.setVisible(false);
99          progressIndicator.setWidth("100%");
100 
101         mainLayout = new VerticalLayout();
102         mainLayout.setSizeFull();
103 
104         mainLayout.addComponent(uploadFileLocation);
105 
106         CssLayout progressLayout = new CssLayout();
107         progressLayout.addStyleName("progress-layout");
108         progressLayout.addComponent(progressIndicator);
109         progressLayout.addComponent(uploadFileProgress);
110         progressLayout.setWidth("100%");
111         mainLayout.addComponent(progressLayout);
112         mainLayout.addComponent(uploadFileRatio);
113 
114         Iterator<Component> it = mainLayout.iterator();
115         while (it.hasNext()) {
116             Component c = it.next();
117             mainLayout.setComponentAlignment(c, Alignment.MIDDLE_CENTER);
118         }
119         mainLayout.setMargin(new MarginInfo(false, true, false, true));
120 
121         setCompositionRoot(mainLayout);
122         addStyleName("uploading-progress-indicator");
123 
124     }
125 
126     @Override
127     public void refreshLayout(long readBytes, long contentLength, String fileName) {
128         progressIndicator.setValue(Float.valueOf(readBytes / (float) contentLength));
129 
130         uploadFileLocation.setValue(i18n.translate(this.inProgressCaption,fileName ));
131 
132         uploadFileProgress.setValue(createPercentage(readBytes, contentLength));
133 
134         String bytesRead = FileUtils.byteCountToDisplaySize(readBytes);
135         String totalBytes = FileUtils.byteCountToDisplaySize(contentLength);
136         uploadFileRatio.setValue(i18n.translate(this.inProgressRatioCaption,bytesRead, totalBytes));
137     }
138 
139     @Override
140     public void setProgress(float progress) {
141         progressIndicator.setValue(progress);
142     }
143 
144     /**
145      * Creates a percentage representation of the upload currently in progress.
146      */
147     private String createPercentage(long readBytes, long contentLength) {
148         double read = Double.valueOf(readBytes);
149         double from = Double.valueOf(contentLength);
150 
151         NumberFormat defaultFormat = NumberFormat.getPercentInstance();
152 
153         return defaultFormat.format((read / from));
154     }
155 
156     @Override
157     public void setVisible(boolean visible) {
158         mainLayout.setVisible(visible);
159         progressIndicator.setVisible(visible);
160     }
161 
162     @Override
163     public Component asVaadinComponent() {
164         return this;
165     }
166 }