aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/DragAndDropFiles.java
blob: 4ecabee617ec66c02e82709d5f33f9922013d699 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.vaadin.tests.dd;

import java.io.OutputStream;

import org.apache.commons.io.output.NullOutputStream;

import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
import com.vaadin.server.StreamVariable;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;
import com.vaadin.ui.Html5File;
import com.vaadin.ui.Label;

public class DragAndDropFiles extends TestBase {

    @Override
    protected void setup() {
        CssLayout cssLayout = new CssLayout() {
            @Override
            protected String getCss(Component c) {
                return "display: block; padding:20px; border: 2px dotted black; background: #aaa;";
            }
        };
        Component l = new Label("Drag file on me");
        l.setSizeUndefined();
        cssLayout.addComponent(l);
        DragAndDropWrapper dragAndDropWrapper = new DragAndDropWrapper(
                cssLayout);
        dragAndDropWrapper.setSizeUndefined();
        dragAndDropWrapper.setDropHandler(new DropHandler() {

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return AcceptAll.get();
            }

            @Override
            public void drop(DragAndDropEvent event) {
                WrapperTransferable transferable = (WrapperTransferable) event
                        .getTransferable();
                Html5File[] files = transferable.getFiles();
                if (files != null) {

                    for (int i = 0; i < files.length; i++) {
                        Html5File file = files[i];
                        // Max 1 MB files are uploaded
                        if (file.getFileSize() > 1024 * 1024) {
                            getMainWindow()
                                    .showNotification(
                                            "File "
                                                    + file.getFileName()
                                                    + " was too large, not transferred to the server side.");
                            continue;
                        }

                        StreamVariable streamVariable = new StreamVariable() {

                            @Override
                            public OutputStream getOutputStream() {
                                return new NullOutputStream();
                            }

                            @Override
                            public boolean listenProgress() {
                                return true;
                            }

                            @Override
                            public void onProgress(StreamingProgressEvent event) {
                                System.err.println("Progress"
                                        + event.getBytesReceived());
                            }

                            @Override
                            public void streamingStarted(
                                    StreamingStartEvent event) {
                                getMainWindow().showNotification(
                                        "Started uploading "
                                                + event.getFileName());
                            }

                            @Override
                            public void streamingFinished(
                                    StreamingEndEvent event) {
                                getMainWindow().showNotification(
                                        "Finished uploading "
                                                + event.getFileName());
                            }

                            @Override
                            public void streamingFailed(
                                    StreamingErrorEvent event) {
                                getMainWindow().showNotification(
                                        "Failed uploading "
                                                + event.getFileName());
                            }

                            @Override
                            public boolean isInterrupted() {
                                return false;
                            }
                        };
                        file.setStreamVariable(streamVariable);
                    }
                }

            }
        });

        addComponent(dragAndDropWrapper);
    }

    /*
     * TODO implement 'handbrake' for testing, progresss listener, interrupting.
     */
    @Override
    protected String getDescription() {
        return "Should work. Over 1 MB files will not be posted. TODO implement 'handbrake' for testing, progresss listener, interrupting.";
    }

    @Override
    protected Integer getTicketNumber() {
        return null;
    }

}