aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/dd/DDTest5.java
blob: 43342fdc355fe61344342304c15bb2730e1c40a5 (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
package com.vaadin.tests.dd;

import java.util.Iterator;

import com.vaadin.event.dd.DragAndDropEvent;
import com.vaadin.event.dd.DropHandler;
import com.vaadin.event.dd.DropTarget;
import com.vaadin.event.dd.acceptcriteria.AcceptAll;
import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
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.DragStartMode;
import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;

public class DDTest5 extends TestBase {

    java.util.Random r = new java.util.Random(1);

    HorizontalLayout hl = new HorizontalLayout();

    private DragAndDropWrapper dragAndDropWrapper2;

    private DropHandler dh;

    private static int count;

    class WrappedLabel extends DragAndDropWrapper {

        private static final long serialVersionUID = 1L;

        public WrappedLabel(String content) {
            super(new Label(content + " c:" + ++count));
            setDragStartMode(DragStartMode.WRAPPER);
        }

        @Override
        public DropHandler getDropHandler() {
            return dh;
        }

    }

    @Override
    protected void setup() {
        UI w = getLayout().getUI();

        HorizontalSortableCssLayoutWithWrappers verticalSortableCssLayoutWithWrappers = new HorizontalSortableCssLayoutWithWrappers();
        w.addWindow(verticalSortableCssLayoutWithWrappers);
        verticalSortableCssLayoutWithWrappers.setPositionX(200);
        verticalSortableCssLayoutWithWrappers.setPositionY(40); /*
                                                                 * FIXME:
                                                                 * subwindow
                                                                 * horizontal
                                                                 * position does
                                                                 * not work if
                                                                 * only x set
                                                                 */

        Label l;

        l = new Label("Drag me");
        DragAndDropWrapper dragAndDropWrapper = new DragAndDropWrapper(l);
        dragAndDropWrapper.setDragStartMode(DragStartMode.COMPONENT);
        dragAndDropWrapper.setWidth("100px");
        dragAndDropWrapper.setHeight("100px");
        getLayout().addComponent(dragAndDropWrapper);

        l = new Label("Drag me too");
        dragAndDropWrapper = new DragAndDropWrapper(l);
        dragAndDropWrapper.setDragStartMode(DragStartMode.WRAPPER);
        dragAndDropWrapper.setWidth("100px");
        dragAndDropWrapper.setHeight("100px");
        getLayout().addComponent(dragAndDropWrapper);

        final CssLayout cssLayout = new CssLayout();
        cssLayout.setHeight("300px");

        dragAndDropWrapper2 = new DragAndDropWrapper(cssLayout);
        dragAndDropWrapper2
                .setCaption("Drop here or sort with dd (wrapper(csslayout(n*wrapper(label))))");

        dh = new DropHandler() {

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

            @Override
            public void drop(DragAndDropEvent dropEvent) {

                /*
                 * TODO wrap componentns in wrappers (so we can build reordering
                 * here)
                 */

                if (dropEvent.getTransferable() instanceof WrapperTransferable) {
                    WrapperTransferable transferable = (WrapperTransferable) dropEvent
                            .getTransferable();
                    Component sourceComponent = transferable
                            .getSourceComponent();

                    Component draggedComponent = transferable
                            .getDraggedComponent();

                    DropTarget target = dropEvent.getTargetDetails()
                            .getTarget();

                    WrappedLabel wrappedLabel = new WrappedLabel(
                            draggedComponent.toString());
                    if (target instanceof WrappedLabel) {
                        int i = 1; // add next to reference by default
                        Iterator<Component> componentIterator = cssLayout
                                .getComponentIterator();
                        Component next = componentIterator.next();
                        while (next != target && componentIterator.hasNext()) {
                            if (next != sourceComponent) {
                                // don't count on index if component is being
                                // moved
                                i++;
                            }
                            next = componentIterator.next();
                        }

                        if (sourceComponent instanceof WrappedLabel) {
                            cssLayout.removeComponent(sourceComponent);
                            wrappedLabel = (WrappedLabel) sourceComponent;
                        }
                        if (dropEvent.getTargetDetails()
                                .getData("verticalLocation").equals("TOP")) {
                            // before reference if dropped on topmost part
                            i--;
                            if (i < 0) {
                                i = 0;
                            }
                        }
                        cssLayout.addComponent(wrappedLabel, i);

                    } else {
                        cssLayout.addComponent(wrappedLabel);
                    }

                } else {
                    // no component, add label with "Text"

                    String data = (String) dropEvent.getTransferable().getData(
                            "text/plain");
                    if (data == null || "".equals(data)) {
                        data = "-- no Text --";
                    }
                    cssLayout.addComponent(new WrappedLabel(data));

                }

            }
        };

        dragAndDropWrapper2.setDropHandler(dh);

        getLayout().addComponent(dragAndDropWrapper2);

    }

    @Override
    protected String getDescription() {
        return "dd: DragAndDropWrapper to build various use cases completely on server side";
    }

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

}