aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/AcceptFromComponent.java
blob: 99dd069142e6572e8deef428745c38aae9e61fbb (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
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.AcceptCriterion;
import com.vaadin.event.dd.acceptcriteria.ServerSideCriterion;
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.WrapperTargetDetails;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;
import com.vaadin.ui.Label;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Window;

public class AcceptFromComponent extends Window {

    private AbsoluteLayout layout;

    public AcceptFromComponent(final Tree tree1) {
        setCaption("Checks the source is tree1 on server");

        layout = new AbsoluteLayout();
        DragAndDropWrapper wrapper = new DragAndDropWrapper(layout);

        setContent(wrapper);

        wrapper.setSizeFull();
        layout.setSizeFull();
        setWidth("450px");
        setHeight("150px");

        final ServerSideCriterion serverSideCriterion = new ServerSideCriterion() {

            @Override
            public boolean accept(DragAndDropEvent dragEvent) {
                Transferable transferable = dragEvent.getTransferable();
                if (transferable instanceof TransferableImpl) {
                    TransferableImpl componentTransferrable = (TransferableImpl) transferable;
                    if (componentTransferrable.getSourceComponent() == tree1) {
                        return true;
                    }
                }
                return false;
            }
        };

        wrapper.setDropHandler(new DropHandler() {

            @Override
            public AcceptCriterion getAcceptCriterion() {
                return serverSideCriterion;
            }

            @Override
            public void drop(DragAndDropEvent event) {

                WrapperTargetDetails ed = (WrapperTargetDetails) event
                        .getTargetDetails();
                Transferable ctr = event.getTransferable();
                if (ctr.getSourceComponent() != null) {
                    // use "component" (from DragDropPane) if available, else
                    // take
                    // the source component
                    Component component = (Component) ctr.getData("component");
                    if (component == null) {
                        component = ctr.getSourceComponent();
                    }

                    if (component.getParent() != layout) {
                        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());
                            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 {
                        // drag started and ended inside the this Pane

                        DragAndDropWrapper.WrapperTransferable tr = (WrapperTransferable) event
                                .getTransferable();

                        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) ctr.getData("text/plain");

                    String content = (String) ctr.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;
            }
        });

    }
}