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 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
57
58
59
60
61
62
63
64
65
66
67
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
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 }