summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/DragDropPane.java
blob: 6296051ee2eba41322cfa216d968757d771a088b (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.vaadin.tests.dd;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.Transferable;
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.shared.MouseEventDetails;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.AbsoluteLayout.ComponentPosition;
import com.vaadin.ui.Component;
import com.vaadin.ui.DragAndDropWrapper;
import com.vaadin.ui.Html5File;
import com.vaadin.ui.Label;

/**
 * replacement for a proto class to keep tests working
 */
public class DragDropPane extends DragAndDropWrapper implements DropHandler {

    private AbsoluteLayout root;
    private AcceptCriterion crit;

    public DragDropPane() {
        super(new AbsoluteLayout());
        root = (AbsoluteLayout) getCompositionRoot();
        setDropHandler(this);
        setDragStartMode(DragStartMode.COMPONENT);
    }

    public void addComponent(Component c) {
        root.addComponent(c);
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void addComponent(Component l, String string) {
        root.addComponent(l, string);
    }

    public void setAcceptCriterion(AcceptCriterion crit) {
        this.crit = crit;
    }

    @Override
    public void drop(DragAndDropEvent event) {

        WrapperTargetDetails ed = (WrapperTargetDetails) event
                .getTargetDetails();
        Transferable ctr = event.getTransferable();
        // use "component" (from DragDropPane) if available, else take
        // the source component
        Component component = null;
        if (ctr instanceof WrapperTransferable) {
            component = ((WrapperTransferable) ctr).getDraggedComponent();
        } else if (ctr instanceof DataBoundTransferable) {
            // Item has been dragged, construct a Label from
            // Item id
            Label l = new Label();
            l.setSizeUndefined();
            l.setValue("ItemId : " + ((DataBoundTransferable) ctr).getItemId());
            component = l;
        }

        if (component != null) {

            if (component.getParent() != root) {

                root.addComponent(component);

                Integer left = ed.getAbsoluteLeft();
                Integer top = ed.getAbsoluteTop();

                MouseEventDetails eventDetails = ed.getMouseEvent();

                int clientX = eventDetails.getClientX();
                int clientY = eventDetails.getClientY();

                try {
                    root.getPosition(component).setTopValue(
                            Float.valueOf(clientY - top));
                    root.getPosition(component).setLeftValue(
                            Float.valueOf(clientX - left));
                } catch (Exception e) {
                }
            } else {
                // drag started and ended inside the this Pane

                MouseEventDetails start = ((WrapperTransferable) event
                        .getTransferable()).getMouseDownEvent();
                MouseEventDetails eventDetails = ed.getMouseEvent();

                int deltaX = eventDetails.getClientX() - start.getClientX();
                int deltaY = eventDetails.getClientY() - start.getClientY();

                ComponentPosition p = root.getPosition(component);
                p.setTopValue(p.getTopValue() + deltaY);
                p.setLeftValue(p.getLeftValue() + deltaX);

            }
        }

        else {
            // drag coming outside of Vaadin

            WrapperTransferable wtr = (WrapperTransferable) ctr;

            String object = wtr.getText();
            // String html = wtr.getHtml();
            // String url = (String) ctr.getData("Url");

            final Label l = new Label();
            l.setCaption("Generated from HTML5 drag:");
            if (object != null) {
                if (object.length() > 80) {
                    object = object.substring(0, 79);
                }
                l.setValue(object);
            } else {
                l.setValue("HTML5 dd");
            }

            Html5File[] files = wtr.getFiles();
            if (files != null) {
                for (Html5File html5File : files) {
                    l.setCaption(html5File.getFileName());
                    html5File.setStreamVariable(new StreamVariable() {
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                        @Override
                        public OutputStream getOutputStream() {
                            return byteArrayOutputStream;
                        }

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

                        @Override
                        public void onProgress(StreamingProgressEvent event) {
                        }

                        @Override
                        public void streamingStarted(StreamingStartEvent event) {
                        }

                        @Override
                        public void streamingFinished(StreamingEndEvent event) {
                            l.setValue((new String(byteArrayOutputStream
                                    .toByteArray()).substring(0, 80) + "..."));
                        }

                        @Override
                        public void streamingFailed(StreamingErrorEvent event) {
                        }

                        @Override
                        public boolean isInterrupted() {
                            return false;
                        }
                    });
                }
            }

            l.setSizeUndefined();

            root.addComponent(l);

        }
        return;
    }

    @Override
    public AcceptCriterion getAcceptCriterion() {
        return crit != null ? crit : AcceptAll.get();
    }

}