1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
55
56
57
58
59
60
61
62
63
64
65
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
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 }