summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/AcceptAnythingWindow.java
blob: 5d57de388cfe7e4b75a00c9341acc0ea413ed962 (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
package com.vaadin.tests.dd;

import com.vaadin.event.DataBoundTransferable;
import com.vaadin.event.Transferable;
import com.vaadin.event.TransferableImpl;
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.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.DragAndDropWrapper.DragStartMode;
import com.vaadin.ui.DragAndDropWrapper.WrapperTargetDetails;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class AcceptAnythingWindow extends Window {

    private AbsoluteLayout layout = new AbsoluteLayout();

    public AcceptAnythingWindow() {
        setCaption("Drop anything here");

        final DragAndDropWrapper wrapper = new DragAndDropWrapper(layout);
        wrapper.setDropHandler(new DropHandler() {

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

            @Override
            public void drop(DragAndDropEvent event) {
                WrapperTargetDetails ed = (WrapperTargetDetails) event
                        .getTargetDetails();
                Transferable transferable = event.getTransferable();
                TransferableImpl ctr = (TransferableImpl) transferable;
                Component component = ctr.getSourceComponent();
                if (component == wrapper) {
                    // if the drag source was wrapper we are interested about
                    // the dragged component
                    WrapperTransferable tr = (WrapperTransferable) transferable;
                    component = tr.getDraggedComponent();
                    // html5 drag created by wrapper set component null
                }

                if (component != null) {

                    if (component.getParent() != layout) {
                        if (transferable instanceof DataBoundTransferable) {
                            // Item has been dragged, construct a Label from
                            // Item id
                            Label l = new Label();
                            l.setSizeUndefined();
                            l.setValue("ItemId : "
                                    + ((DataBoundTransferable) transferable)
                                            .getItemId());
                            layout.addComponent(l);
                            component = l;

                        } else {
                            // we have a component that is been dragged, add
                            // it to this
                            layout.addComponent(component);
                        }

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

                        MouseEventDetails eventDetails = ed.getMouseEvent();

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

                        try {
                            layout.getPosition(component).setTopValue(
                                    Float.valueOf(clientY - top));
                            layout.getPosition(component).setLeftValue(
                                    Float.valueOf(clientX - left));
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    } else {

                        WrapperTransferable tr = (WrapperTransferable) transferable;
                        // drag ended inside the this Pane

                        MouseEventDetails start = tr.getMouseDownEvent();
                        MouseEventDetails eventDetails = ed.getMouseEvent();

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

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

                    }

                } else {
                    // drag coming outside of Vaadin
                    String object = (String) transferable.getData("Text");

                    String content = (String) transferable
                            .getData("fileContents");

                    Label l = new Label();
                    l.setCaption("Generated from HTML5 drag:");
                    if (object != null) {
                        l.setValue(object);
                    } else {
                        l.setValue("HTML5 dd");
                    }

                    l.setDescription(content);
                    l.setSizeUndefined();

                    layout.addComponent(l);

                }
                return;
            }
        });

        wrapper.setDragStartMode(DragStartMode.COMPONENT);
        wrapper.setSizeFull();
        setContent(wrapper);

        setWidth("250px");
        setHeight("100px");
    }
}