]> source.dussan.org Git - vaadin-framework.git/commitdiff
added upload feature for sampler
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 5 Aug 2009 13:34:08 +0000 (13:34 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 5 Aug 2009 13:34:08 +0000 (13:34 +0000)
svn changeset:8444/svn branch:6.0

src/com/vaadin/demo/sampler/FeatureSet.java
src/com/vaadin/demo/sampler/features/upload/75-UploadBasic.png [new file with mode: 0644]
src/com/vaadin/demo/sampler/features/upload/75-UploadWithProgressMonitoring.png [new file with mode: 0644]
src/com/vaadin/demo/sampler/features/upload/UploadBasic.java [new file with mode: 0644]
src/com/vaadin/demo/sampler/features/upload/UploadBasicExample.java [new file with mode: 0644]
src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoring.java [new file with mode: 0644]
src/com/vaadin/demo/sampler/features/upload/UploadWithProgressMonitoringExample.java [new file with mode: 0644]

index dad0a408bcedb0b848f9b3d65bda72625173e50a..a20bbaba9227b6c976ea3fa832b63cc4c330c42e 100644 (file)
@@ -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 (file)
index 0000000..d0e2877
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 (file)
index 0000000..a1c4444
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 (file)
index 0000000..4b98557
--- /dev/null
@@ -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<? extends Feature>[] 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 (file)
index 0000000..1b1623a
--- /dev/null
@@ -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 (file)
index 0000000..8c9f04f
--- /dev/null
@@ -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:<ul>"
+                + "<li> start upload once file is selected aka \"one-click-upload\"</li>"
+                + "<li> process the file during the upload</li>"
+                + "<li> trac events that occure during the update</li>"
+                + "<li> visualize upload progress with ProgressIndicator</li>"
+                + "<li> ability to cancel the upload</li>" + "</ul>";
+    }
+
+    @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<? extends Feature>[] 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 (file)
index 0000000..2af9a84
--- /dev/null
@@ -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;
+        }
+
+    }
+
+}