private Button interrupt = new Button("Interrupt upload");
public TestForUpload() {
setCompositionRoot(main);
main.addComponent(new Label(
"This is a simple test for upload application. "
+ "Upload should work with big files and concurrent "
+ "requests should not be blocked. Button 'b' reads "
+ "current state into label below it. Memory receiver "
+ "streams upload contents into memory. You may track"
+ "consumption."
+ "tempfile receiver writes upload to file and "
+ "should have low memory consumption."));
main.addComponent(new Label(
"Clicking on button b updates information about upload components status or same with garbage collector."));
textField = new TextField("Test field");
textFieldValue = new Label();
main.addComponent(textField);
main.addComponent(textFieldValue);
up = new Upload("Upload", buffer);
up.setImmediate(true);
up.addListener(new Listener() {
private static final long serialVersionUID = -8319074730512324303L;
@Override
public void componentEvent(Event event) {
// print out all events fired by upload for debug purposes
System.out.println("Upload fired event | " + event);
}
});
up.addListener(new StartedListener() {
private static final long serialVersionUID = 5508883803861085154L;
@Override
public void uploadStarted(StartedEvent event) {
pi.setVisible(true);
pi2.setVisible(true);
l.setValue("Started uploading file " + event.getFilename());
textFieldValue
.setValue(" TestFields value at the upload start is:"
+ textField.getValue());
}
});
up.addListener(new Upload.FinishedListener() {
private static final long serialVersionUID = -3773034195991947371L;
@Override
public void uploadFinished(FinishedEvent event) {
pi.setVisible(false);
pi2.setVisible(false);
if (event instanceof Upload.FailedEvent) {
Exception reason = ((Upload.FailedEvent) event).getReason();
l.setValue("Finished with failure ( " + reason
+ " ), idle");
} else if (event instanceof Upload.SucceededEvent) {
l.setValue("Finished with succes, idle");
} else {
l.setValue("Finished with unknow event");
}
statusLayout.removeAllComponents();
final InputStream stream = buffer.getStream();
if (stream == null) {
statusLayout.addComponent(new Label(
"Upload finished, but output buffer is null"));
} else {
statusLayout.addComponent(new Label("<b>Name:</b> "
+ event.getFilename(), ContentMode.HTML));
statusLayout.addComponent(new Label("<b>Mimetype:</b> "
+ event.getMIMEType(), ContentMode.HTML));
statusLayout.addComponent(new Label("<b>Size:</b> "
+ event.getLength() + " bytes.", ContentMode.HTML));
statusLayout.addComponent(new Link("Download "
+ buffer.getFileName(), new StreamResource(buffer,
buffer.getFileName())));
statusLayout.setVisible(true);
}
setBuffer();
}
});
up.addListener(new Upload.ProgressListener() {
@Override
public void updateProgress(long readBytes, long contentLenght) {
pi2.setValue(new Float(readBytes / (float) contentLenght));
refreshMemUsage();
}
});
final Button b = new Button("Reed state from upload",
new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
readState();
}
});
final Button c = new Button("Force GC", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
gc();
}
});
main.addComponent(b);
main.addComponent(c);
main.addComponent(beSluggish);
main.addComponent(throwExecption);
main.addComponent(interrupt);
interrupt.addListener(new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
up.interruptUpload();
}
});
uploadBufferSelector = new Select("StreamVariable type");
uploadBufferSelector.setImmediate(true);
uploadBufferSelector.addItem("memory");
uploadBufferSelector.setValue("memory");
uploadBufferSelector.addItem("tempfile");
uploadBufferSelector
.addListener(new AbstractField.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
setBuffer();
}
});
main.addComponent(uploadBufferSelector);
main.addComponent(up);
l = new Label("Idle");
main.addComponent(l);
pi.setVisible(false);
pi.setPollingInterval(1000);
main.addComponent(pi);
pi2.setVisible(false);
pi2.setPollingInterval(1000);
main.addComponent(pi2);
memoryStatus = new Label();
main.addComponent(memoryStatus);
statusLayout.setMargin(true);
status.setVisible(false);
main.addComponent(status);