From: Matti Tahvonen Date: Wed, 5 Aug 2009 13:34:08 +0000 (+0000) Subject: added upload feature for sampler X-Git-Tag: 6.7.0.beta1~2632 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d67ccc9c4a8ab6e986138759e0c2524e4bf317aa;p=vaadin-framework.git added upload feature for sampler svn changeset:8444/svn branch:6.0 --- diff --git a/src/com/vaadin/demo/sampler/FeatureSet.java b/src/com/vaadin/demo/sampler/FeatureSet.java index dad0a408bc..a20bbaba92 100644 --- a/src/com/vaadin/demo/sampler/FeatureSet.java +++ b/src/com/vaadin/demo/sampler/FeatureSet.java @@ -78,6 +78,8 @@ import com.vaadin.demo.sampler.features.trees.TreeActions; import com.vaadin.demo.sampler.features.trees.TreeMouseEvents; import com.vaadin.demo.sampler.features.trees.TreeMultiSelect; import com.vaadin.demo.sampler.features.trees.TreeSingleSelect; +import com.vaadin.demo.sampler.features.upload.UploadBasic; +import com.vaadin.demo.sampler.features.upload.UploadWithProgressMonitoring; import com.vaadin.demo.sampler.features.windows.NativeWindow; import com.vaadin.demo.sampler.features.windows.Subwindow; import com.vaadin.demo.sampler.features.windows.SubwindowAutoSized; @@ -139,6 +141,7 @@ public class FeatureSet extends Feature { new TextFields(), // new Trees(), // new Windows(), // + new Uploads(), // }); } } @@ -304,6 +307,20 @@ public class FeatureSet extends Feature { } } + public static class Uploads extends FeatureSet { + public Uploads() { + super( + "Uploads", + "Uploads", + "Upload is a component providing a method for clients to send files to server.", + new Feature[] { + // + new UploadBasic(), // + new UploadWithProgressMonitoring(), // + }); + } + } + public static class Windows extends FeatureSet { public Windows() { super( diff --git a/src/com/vaadin/demo/sampler/features/upload/75-UploadBasic.png b/src/com/vaadin/demo/sampler/features/upload/75-UploadBasic.png new file mode 100644 index 0000000000..d0e28771ce Binary files /dev/null and b/src/com/vaadin/demo/sampler/features/upload/75-UploadBasic.png differ diff --git a/src/com/vaadin/demo/sampler/features/upload/75-UploadWithProgressMonitoring.png b/src/com/vaadin/demo/sampler/features/upload/75-UploadWithProgressMonitoring.png new file mode 100644 index 0000000000..a1c4444d42 Binary files /dev/null and b/src/com/vaadin/demo/sampler/features/upload/75-UploadWithProgressMonitoring.png differ diff --git a/src/com/vaadin/demo/sampler/features/upload/UploadBasic.java b/src/com/vaadin/demo/sampler/features/upload/UploadBasic.java new file mode 100644 index 0000000000..4b98557370 --- /dev/null +++ b/src/com/vaadin/demo/sampler/features/upload/UploadBasic.java @@ -0,0 +1,43 @@ +package com.vaadin.demo.sampler.features.upload; + +import com.vaadin.demo.sampler.APIResource; +import com.vaadin.demo.sampler.Feature; +import com.vaadin.demo.sampler.NamedExternalResource; +import com.vaadin.ui.Upload; + +@SuppressWarnings("serial") +public class UploadBasic extends Feature { + + @Override + public String getDescription() { + return "Upload component provides a method to handle " + + "files uploaded from clients. " + + "In this example we simply be " + + "count line breaks of the uploaded file." + + "The data could just as well be saved on " + + "the server as file or inserted into a database."; + } + + @Override + public String getName() { + return "Basic upload"; + } + + @Override + public APIResource[] getRelatedAPI() { + return new APIResource[] { new APIResource(Upload.class) }; + } + + @SuppressWarnings("unchecked") + @Override + public Class[] getRelatedFeatures() { + return new Class[] { UploadWithProgressMonitoring.class }; + } + + @Override + public NamedExternalResource[] getRelatedResources() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/com/vaadin/demo/sampler/features/upload/UploadBasicExample.java b/src/com/vaadin/demo/sampler/features/upload/UploadBasicExample.java new file mode 100644 index 0000000000..1b1623ab1a --- /dev/null +++ b/src/com/vaadin/demo/sampler/features/upload/UploadBasicExample.java @@ -0,0 +1,74 @@ +package com.vaadin.demo.sampler.features.upload; + +import java.io.IOException; +import java.io.OutputStream; + +import com.vaadin.ui.Label; +import com.vaadin.ui.Upload; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Upload.FinishedEvent; +import com.vaadin.ui.Upload.Receiver; + +@SuppressWarnings("serial") +public class UploadBasicExample extends VerticalLayout { + + private Label result = new Label(); + + private LineBreakCounter counter = new LineBreakCounter(); + + private Upload upload = new Upload("Upload a file", counter); + + public UploadBasicExample() { + addComponent(upload); + addComponent(result); + upload.addListener(new Upload.FinishedListener() { + public void uploadFinished(FinishedEvent event) { + result.setValue("Uploaded file contained " + + counter.getLineBreakCount() + " linebreaks"); + } + }); + + } + + public static class LineBreakCounter implements Receiver { + + private String fileName; + private String mtype; + private int counter; + + /** + * OutputStream that simply counts lineends + */ + private OutputStream stream = new OutputStream() { + private static final int searchedByte = '\n'; + + @Override + public void write(int b) throws IOException { + if (b == searchedByte) { + counter++; + } + } + }; + + public OutputStream receiveUpload(String filename, String MIMEType) { + counter = 0; + fileName = filename; + mtype = MIMEType; + return stream; + } + + public String getFileName() { + return fileName; + } + + public String getMimeType() { + return mtype; + } + + public int getLineBreakCount() { + return counter; + } + + } + +} diff --git a/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoring.java b/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoring.java new file mode 100644 index 0000000000..8c9f04f7c6 --- /dev/null +++ b/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoring.java @@ -0,0 +1,47 @@ +package com.vaadin.demo.sampler.features.upload; + +import com.vaadin.demo.sampler.APIResource; +import com.vaadin.demo.sampler.Feature; +import com.vaadin.demo.sampler.NamedExternalResource; +import com.vaadin.ui.ProgressIndicator; +import com.vaadin.ui.Upload; + +@SuppressWarnings("serial") +public class UploadWithProgressMonitoring extends Feature { + + @Override + public String getDescription() { + return "Upload does not block the entire UI. While uploading a large" + + "file users can navigate to other views in the application." + + " Other advanced upload features used in this demo:"; + } + + @Override + public String getName() { + return "Upload with advanced features"; + } + + @Override + public APIResource[] getRelatedAPI() { + return new APIResource[] { new APIResource(Upload.class), + new APIResource(ProgressIndicator.class) }; + } + + @SuppressWarnings("unchecked") + @Override + public Class[] getRelatedFeatures() { + return new Class[] { UploadBasic.class }; + } + + @Override + public NamedExternalResource[] getRelatedResources() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoringExample.java b/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoringExample.java new file mode 100644 index 0000000000..2af9a8432f --- /dev/null +++ b/src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoringExample.java @@ -0,0 +1,194 @@ +package com.vaadin.demo.sampler.features.upload; + +import java.io.IOException; +import java.io.OutputStream; + +import com.vaadin.ui.Button; +import com.vaadin.ui.CheckBox; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Panel; +import com.vaadin.ui.ProgressIndicator; +import com.vaadin.ui.Upload; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Upload.FailedEvent; +import com.vaadin.ui.Upload.FinishedEvent; +import com.vaadin.ui.Upload.Receiver; +import com.vaadin.ui.Upload.StartedEvent; +import com.vaadin.ui.Upload.SucceededEvent; + +@SuppressWarnings("serial") +public class UploadWithProgressMonitoringExample extends VerticalLayout { + + private Label state = new Label(); + private Label result = new Label(); + private Label fileName = new Label(); + private Label textualProgress = new Label(); + + private ProgressIndicator pi = new ProgressIndicator(); + + private LineBreakCounter counter = new LineBreakCounter(); + + private Upload upload = new Upload("Upload a file", counter); + + public UploadWithProgressMonitoringExample() { + upload.setImmediate(true); // make analyzing start immediatedly when + upload.setButtonCaption("Analyze file"); + // file is selected + addComponent(upload); + + CheckBox handBrake = new CheckBox("Simulate slow server"); + handBrake.setValue(true); + counter.setSlow(true); + handBrake + .setDescription("Sleep for 100ms after each kilobyte to simulate slower processing/bandwidth. This is to show progress indicator even with rather small files."); + handBrake.addListener(new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + counter.setSlow(event.getButton().booleanValue()); + } + }); + + final Button cancelProcessing = new Button("Cancel processing"); + cancelProcessing.addListener(new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + upload.interruptUpload(); + } + }); + cancelProcessing.setEnabled(false); + + addComponent(cancelProcessing); + + handBrake.setImmediate(true); + + addComponent(handBrake); + + Panel p = new Panel("Status"); + FormLayout l = new FormLayout(); + p.setContent(l); + state.setCaption("Current state"); + state.setValue("Idle"); + l.addComponent(state); + fileName.setCaption("File name"); + l.addComponent(fileName); + result.setCaption("Line breaks counted"); + l.addComponent(result); + pi.setCaption("Progress"); + pi.setVisible(false); + l.addComponent(pi); + textualProgress.setVisible(false); + l.addComponent(textualProgress); + + addComponent(p); + + upload.addListener(new Upload.StartedListener() { + public void uploadStarted(StartedEvent event) { + // this method gets called immediatedly after upload is + // started + pi.setValue(0f); + pi.setVisible(true); + pi.setPollingInterval(500); // hit server frequantly to get + textualProgress.setVisible(true); + // updates to client + state.setValue("Uploading"); + fileName.setValue(event.getFilename()); + + cancelProcessing.setEnabled(true); + } + }); + + upload.addListener(new Upload.ProgressListener() { + public void updateProgress(long readBytes, long contentLength) { + // this method gets called several times during the update + pi.setValue(new Float(readBytes / (float) contentLength)); + textualProgress.setValue("Processed " + readBytes + + " bytes of " + contentLength); + result.setValue(counter.getLineBreakCount() + " (counting...)"); + } + + }); + + upload.addListener(new Upload.SucceededListener() { + public void uploadSucceeded(SucceededEvent event) { + result.setValue(counter.getLineBreakCount() + " (total)"); + } + }); + + upload.addListener(new Upload.FailedListener() { + public void uploadFailed(FailedEvent event) { + result.setValue(counter.getLineBreakCount() + + " (counting interrupted at " + + Math.round(100 * (Float) pi.getValue()) + "%)"); + } + }); + + upload.addListener(new Upload.FinishedListener() { + public void uploadFinished(FinishedEvent event) { + state.setValue("Idle"); + pi.setVisible(false); + textualProgress.setVisible(false); + cancelProcessing.setEnabled(false); + } + }); + + } + + public static class LineBreakCounter implements Receiver { + + private String fileName; + private String mtype; + + private int counter; + private int total; + private boolean sleep; + + /** + * OutputStream that simply counts lineends + */ + private OutputStream stream = new OutputStream() { + private static final int searchedByte = '\n'; + + @Override + public void write(int b) throws IOException { + total++; + if (b == searchedByte) { + counter++; + } + if (sleep && total % 1000 == 0) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + }; + + public OutputStream receiveUpload(String filename, String MIMEType) { + counter = 0; + total = 0; + fileName = filename; + mtype = MIMEType; + return stream; + } + + public String getFileName() { + return fileName; + } + + public String getMimeType() { + return mtype; + } + + public int getLineBreakCount() { + return counter; + } + + public void setSlow(boolean value) { + sleep = value; + } + + } + +}