aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/GestureManager.java
blob: 98bc25e37b79ff68dfb96f0257cf947f30e4ca9c (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.layout.gle2;

import com.android.SdkConstants;
import com.android.ide.common.api.DropFeedback;
import com.android.ide.common.api.IViewRule;
import com.android.ide.common.api.Rect;
import com.android.ide.common.api.SegmentType;
import com.android.ide.eclipse.adt.internal.editors.layout.gre.NodeProxy;
import com.android.utils.Pair;

import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.TypedEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorSite;

import java.util.ArrayList;
import java.util.List;

/**
 * The {@link GestureManager} is is the central manager of gestures; it is responsible
 * for recognizing when particular gestures should begin and terminate. It
 * listens to the drag, mouse and keyboard systems to find out when to start
 * gestures and in order to update the gestures along the way.
 */
public class GestureManager {
    /** The canvas which owns this GestureManager. */
    private final LayoutCanvas mCanvas;

    /** The currently executing gesture, or null. */
    private Gesture mCurrentGesture;

    /** A listener for drop target events. */
    private final DropTargetListener mDropListener = new CanvasDropListener();

    /** A listener for drag source events. */
    private final DragSourceListener mDragSourceListener = new CanvasDragSourceListener();

    /** Tooltip shown during the gesture, or null */
    private GestureToolTip mTooltip;

    /**
     * The list of overlays associated with {@link #mCurrentGesture}. Will be
     * null before it has been initialized lazily by the paint routine (the
     * initialized value can never be null, but it can be an empty collection).
     */
    private List<Overlay> mOverlays;

    /**
     * Most recently seen mouse position (x coordinate). We keep a copy of this
     * value since we sometimes need to know it when we aren't told about the
     * mouse position (such as when a keystroke is received, such as an arrow
     * key in order to tweak the current drop position)
     */
    protected int mLastMouseX;

    /**
     * Most recently seen mouse position (y coordinate). We keep a copy of this
     * value since we sometimes need to know it when we aren't told about the
     * mouse position (such as when a keystroke is received, such as an arrow
     * key in order to tweak the current drop position)
     */
    protected int mLastMouseY;

    /**
     * Most recently seen mouse mask. We keep a copy of this since in some
     * scenarios (such as on a drag gesture) we don't get access to it.
     */
    protected int mLastStateMask;

    /**
     * Listener for mouse motion, click and keyboard events.
     */
    private Listener mListener;

    /**
     * When we the drag leaves, we don't know if that's the last we'll see of
     * this drag or if it's just temporarily outside the canvas and it will
     * return. We want to restore it if it comes back. This is also necessary
     * because even on a drop we'll receive a
     * {@link DropTargetListener#dragLeave} right before the drop, and we need
     * to restore it in the drop. Therefore, when we lose a {@link DropGesture}
     * to a {@link DropTargetListener#dragLeave}, we store a reference to the
     * current gesture as a {@link #mZombieGesture}, since the gesture is dead
     * but might be brought back to life if we see a subsequent
     * {@link DropTargetListener#dragEnter} before another gesture begins.
     */
    private DropGesture mZombieGesture;

    /**
     * Flag tracking whether we've set a message or error message on the global status
     * line (since we only want to clear that message if we have set it ourselves).
     * This is the actual message rather than a boolean such that (if we can get our
     * hands on the global message) we can check to see if the current message is the
     * one we set and only in that case clear it when it is no longer applicable.
     */
    private String mDisplayingMessage;

    /**
     * Constructs a new {@link GestureManager} for the given
     * {@link LayoutCanvas}.
     *
     * @param canvas The canvas which controls this {@link GestureManager}
     */
    public GestureManager(LayoutCanvas canvas) {
        mCanvas = canvas;
    }

    /**
     * Returns the canvas associated with this GestureManager.
     *
     * @return The {@link LayoutCanvas} associated with this GestureManager.
     *         Never null.
     */
    public LayoutCanvas getCanvas() {
        return mCanvas;
    }

    /**
     * Returns the current gesture, if one is in progress, and otherwise returns
     * null.
     *
     * @return The current gesture or null.
     */
    public Gesture getCurrentGesture() {
        return mCurrentGesture;
    }

    /**
     * Paints the overlays associated with the current gesture, if any.
     *
     * @param gc The graphics object to paint into.
     */
    public void paint(GC gc) {
        if (mCurrentGesture == null) {
            return;
        }

        if (mOverlays == null) {
            mOverlays = mCurrentGesture.createOverlays();
            Device device = gc.getDevice();
            for (Overlay overlay : mOverlays) {
                overlay.create(device);
            }
        }
        for (Overlay overlay : mOverlays) {
            overlay.paint(gc);
        }
    }

    /**
     * Registers all the listeners needed by the {@link GestureManager}.
     *
     * @param dragSource The drag source in the {@link LayoutCanvas} to listen
     *            to.
     * @param dropTarget The drop target in the {@link LayoutCanvas} to listen
     *            to.
     */
    public void registerListeners(DragSource dragSource, DropTarget dropTarget) {
        assert mListener == null;
        mListener = new Listener();
        mCanvas.addMouseMoveListener(mListener);
        mCanvas.addMouseListener(mListener);
        mCanvas.addKeyListener(mListener);

        if (dragSource != null) {
            dragSource.addDragListener(mDragSourceListener);
        }
        if (dropTarget != null) {
            dropTarget.addDropListener(mDropListener);
        }
    }

    /**
     * Unregisters all the listeners previously registered by
     * {@link #registerListeners}.
     *
     * @param dragSource The drag source in the {@link LayoutCanvas} to stop
     *            listening to.
     * @param dropTarget The drop target in the {@link LayoutCanvas} to stop
     *            listening to.
     */
    public void unregisterListeners(DragSource dragSource, DropTarget dropTarget) {
        if (mCanvas.isDisposed()) {
            // If the LayoutCanvas is already disposed, we shouldn't try to unregister
            // the listeners; they are already not active and an attempt to remove the
            // listener will throw a widget-is-disposed exception.
            mListener = null;
            return;
        }

        if (mListener != null) {
            mCanvas.removeMouseMoveListener(mListener);
            mCanvas.removeMouseListener(mListener);
            mCanvas.removeKeyListener(mListener);
            mListener = null;
        }

        if (dragSource != null) {
            dragSource.removeDragListener(mDragSourceListener);
        }
        if (dropTarget != null) {
            dropTarget.removeDropListener(mDropListener);
        }
    }

    /**
     * Starts the given gesture.
     *
     * @param mousePos The most recent mouse coordinate applicable to the new
     *            gesture, in control coordinates.
     * @param gesture The gesture to initiate
     */
    private void startGesture(ControlPoint mousePos, Gesture gesture, int mask) {
        if (mCurrentGesture != null) {
            finishGesture(mousePos, true);
            assert mCurrentGesture == null;
        }

        if (gesture != null) {
            mCurrentGesture = gesture;
            mCurrentGesture.begin(mousePos, mask);
        }
    }

    /**
     * Updates the current gesture, if any, for the given event.
     *
     * @param mousePos The most recent mouse coordinate applicable to the new
     *            gesture, in control coordinates.
     * @param event The event corresponding to this update. May be null. Don't
     *            make any assumptions about the type of this event - for
     *            example, it may not always be a MouseEvent, it could be a
     *            DragSourceEvent, etc.
     */
    private void updateMouse(ControlPoint mousePos, TypedEvent event) {
        if (mCurrentGesture != null) {
            mCurrentGesture.update(mousePos);
        }
    }

    /**
     * Finish the given gesture, either from successful completion or from
     * cancellation.
     *
     * @param mousePos The most recent mouse coordinate applicable to the new
     *            gesture, in control coordinates.
     * @param canceled True if and only if the gesture was canceled.
     */
    private void finishGesture(ControlPoint mousePos, boolean canceled) {
        if (mCurrentGesture != null) {
            mCurrentGesture.end(mousePos, canceled);
            if (mOverlays != null) {
                for (Overlay overlay : mOverlays) {
                    overlay.dispose();
                }
                mOverlays = null;
            }
            mCurrentGesture = null;
            mZombieGesture = null;
            mLastStateMask = 0;
            updateMessage(null);
            updateCursor(mousePos);
            mCanvas.redraw();
        }
    }

    /**
     * Update the cursor to show the type of operation we expect on a mouse press:
     * <ul>
     * <li>Over a selection handle, show a directional cursor depending on the position of
     * the selection handle
     * <li>Over a widget, show a move (hand) cursor
     * <li>Otherwise, show the default arrow cursor
     * </ul>
     */
    void updateCursor(ControlPoint controlPoint) {
        // We don't hover on the root since it's not a widget per see and it is always there.
        SelectionManager selectionManager = mCanvas.getSelectionManager();

        if (!selectionManager.isEmpty()) {
            Display display = mCanvas.getDisplay();
            Pair<SelectionItem, SelectionHandle> handlePair =
                selectionManager.findHandle(controlPoint);
            if (handlePair != null) {
                SelectionHandle handle = handlePair.getSecond();
                int cursorType = handle.getSwtCursorType();
                Cursor cursor = display.getSystemCursor(cursorType);
                if (cursor != mCanvas.getCursor()) {
                    mCanvas.setCursor(cursor);
                }
                return;
            }

            // See if it's over a selected view
            LayoutPoint layoutPoint = controlPoint.toLayout();
            for (SelectionItem item : selectionManager.getSelections()) {
                if (item.getRect().contains(layoutPoint.x, layoutPoint.y)
                        && !item.isRoot()) {
                    Cursor cursor = display.getSystemCursor(SWT.CURSOR_HAND);
                    if (cursor != mCanvas.getCursor()) {
                        mCanvas.setCursor(cursor);
                    }
                    return;
                }
            }
        }

        if (mCanvas.getCursor() != null) {
            mCanvas.setCursor(null);
        }
    }

    /**
     * Update the Eclipse status message with any feedback messages from the given
     * {@link DropFeedback} object, or clean up if there is no more feedback to process
     * @param feedback the feedback whose message we want to display, or null to clear the
     *            message if previously set
     */
    void updateMessage(DropFeedback feedback) {
        IEditorSite editorSite = mCanvas.getEditorDelegate().getEditor().getEditorSite();
        IStatusLineManager status = editorSite.getActionBars().getStatusLineManager();
        if (feedback == null) {
            if (mDisplayingMessage != null) {
                status.setMessage(null);
                status.setErrorMessage(null);
                mDisplayingMessage = null;
            }
        } else if (feedback.errorMessage != null) {
            if (!feedback.errorMessage.equals(mDisplayingMessage)) {
                mDisplayingMessage = feedback.errorMessage;
                status.setErrorMessage(mDisplayingMessage);
            }
        } else if (feedback.message != null) {
            if (!feedback.message.equals(mDisplayingMessage)) {
                mDisplayingMessage = feedback.message;
                status.setMessage(mDisplayingMessage);
            }
        } else if (mDisplayingMessage != null) {
            // TODO: Can we check the existing message and only clear it if it's the
            // same as the one we set?
            mDisplayingMessage = null;
            status.setMessage(null);
            status.setErrorMessage(null);
        }

        // Tooltip
        if (feedback != null && feedback.tooltip != null) {
            Pair<Boolean,Boolean> position = mCurrentGesture.getTooltipPosition();
            boolean below = position.getFirst();
            if (feedback.tooltipY != null) {
                below = feedback.tooltipY == SegmentType.BOTTOM;
            }
            boolean toRightOf = position.getSecond();
            if (feedback.tooltipX != null) {
                toRightOf = feedback.tooltipX == SegmentType.RIGHT;
            }
            if (mTooltip == null) {
                mTooltip = new GestureToolTip(mCanvas, below, toRightOf);
            }
            mTooltip.update(feedback.tooltip, below, toRightOf);
        } else if (mTooltip != null) {
            mTooltip.dispose();
            mTooltip = null;
        }
    }

    /**
     * Returns the current mouse position as a {@link ControlPoint}
     *
     * @return the current mouse position as a {@link ControlPoint}
     */
    public ControlPoint getCurrentControlPoint() {
        return ControlPoint.create(mCanvas, mLastMouseX, mLastMouseY);
    }

    /**
     * Returns the current SWT modifier key mask as an {@link IViewRule} modifier mask
     *
     * @return the current SWT modifier key mask as an {@link IViewRule} modifier mask
     */
    public int getRuleModifierMask() {
        int swtMask = mLastStateMask;
        int modifierMask = 0;
        if ((swtMask & SWT.MOD1) != 0) {
            modifierMask |= DropFeedback.MODIFIER1;
        }
        if ((swtMask & SWT.MOD2) != 0) {
            modifierMask |= DropFeedback.MODIFIER2;
        }
        if ((swtMask & SWT.MOD3) != 0) {
            modifierMask |= DropFeedback.MODIFIER3;
        }
        return modifierMask;
    }

    /**
     * Helper class which implements the {@link MouseMoveListener},
     * {@link MouseListener} and {@link KeyListener} interfaces.
     */
    private class Listener implements MouseMoveListener, MouseListener, MouseTrackListener,
            KeyListener {

        // --- MouseMoveListener ---

        @Override
        public void mouseMove(MouseEvent e) {
            mLastMouseX = e.x;
            mLastMouseY = e.y;
            mLastStateMask = e.stateMask;

            ControlPoint controlPoint = ControlPoint.create(mCanvas, e);
            if ((e.stateMask & SWT.BUTTON_MASK) != 0) {
                if (mCurrentGesture != null) {
                    updateMouse(controlPoint, e);
                    mCanvas.redraw();
                }
            } else {
                updateCursor(controlPoint);
                mCanvas.hover(e);
                mCanvas.getPreviewManager().moved(controlPoint);
            }
        }

        // --- MouseListener ---

        @Override
        public void mouseUp(MouseEvent e) {
            ControlPoint mousePos = ControlPoint.create(mCanvas, e);

            if (mCurrentGesture == null) {
                // If clicking on a configuration preview, just process it there
                if (mCanvas.getPreviewManager().click(mousePos)) {
                    return;
                }

                // Just a click, select
                Pair<SelectionItem, SelectionHandle> handlePair =
                    mCanvas.getSelectionManager().findHandle(mousePos);
                if (handlePair == null) {
                    mCanvas.getSelectionManager().select(e);
                }
            }
            if (mCurrentGesture == null) {
                updateCursor(mousePos);
            } else if (mCurrentGesture instanceof DropGesture) {
                // Mouse Up shouldn't be delivered in the middle of a drag & drop -
                // but this can happen on some versions of Linux
                // (see http://code.google.com/p/android/issues/detail?id=19057 )
                // and if we process the mouseUp it will abort the remainder of
                // the drag & drop operation, so ignore this event!
            } else {
                finishGesture(mousePos, false);
            }
            mCanvas.redraw();
        }

        @Override
        public void mouseDown(MouseEvent e) {
            mLastMouseX = e.x;
            mLastMouseY = e.y;
            mLastStateMask = e.stateMask;

            // Not yet used. Should be, for Mac and Linux.
        }

        @Override
        public void mouseDoubleClick(MouseEvent e) {
            // SWT delivers a double click event even if you click two different buttons
            // in rapid succession. In any case, we only want to let you double click the
            // first button to warp to XML:
            if (e.button == 1) {
                // Warp to the text editor and show the corresponding XML for the
                // double-clicked widget
                LayoutPoint p = ControlPoint.create(mCanvas, e).toLayout();
                CanvasViewInfo vi = mCanvas.getViewHierarchy().findViewInfoAt(p);
                if (vi != null) {
                    mCanvas.show(vi);
                }
            }
        }

        // --- MouseTrackListener ---

        @Override
        public void mouseEnter(MouseEvent e) {
            ControlPoint mousePos = ControlPoint.create(mCanvas, e);
            mCanvas.getPreviewManager().enter(mousePos);
        }

        @Override
        public void mouseExit(MouseEvent e) {
            ControlPoint mousePos = ControlPoint.create(mCanvas, e);
            mCanvas.getPreviewManager().exit(mousePos);
        }

        @Override
        public void mouseHover(MouseEvent e) {
        }

        // --- KeyListener ---

        @Override
        public void keyPressed(KeyEvent e) {
            mLastStateMask = e.stateMask;
            // Workaround for the fact that in keyPressed the current state
            // mask is not yet updated
            if (e.keyCode == SWT.SHIFT) {
                mLastStateMask |= SWT.MOD2;
            }
            if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN) {
                if (e.keyCode == SWT.COMMAND) {
                    mLastStateMask |= SWT.MOD1;
                }
            } else {
                if (e.keyCode == SWT.CTRL) {
                    mLastStateMask |= SWT.MOD1;
                }
            }

            // Give gestures a first chance to see and consume the key press
            if (mCurrentGesture != null) {
                // unless it's "Escape", which cancels the gesture
                if (e.keyCode == SWT.ESC) {
                    ControlPoint controlPoint = ControlPoint.create(mCanvas,
                            mLastMouseX, mLastMouseY);
                    finishGesture(controlPoint, true);
                    return;
                }

                if (mCurrentGesture.keyPressed(e)) {
                    return;
                }
            }

            // Fall back to canvas actions for the key press
            mCanvas.handleKeyPressed(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            mLastStateMask = e.stateMask;
            // Workaround for the fact that in keyPressed the current state
            // mask is not yet updated
            if (e.keyCode == SWT.SHIFT) {
                mLastStateMask &= ~SWT.MOD2;
            }
            if (SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN) {
                if (e.keyCode == SWT.COMMAND) {
                    mLastStateMask &= ~SWT.MOD1;
                }
            } else {
                if (e.keyCode == SWT.CTRL) {
                    mLastStateMask &= ~SWT.MOD1;
                }
            }

            if (mCurrentGesture != null) {
                mCurrentGesture.keyReleased(e);
            }
        }
    }

    /** Listener for Drag &amp; Drop events. */
    private class CanvasDropListener implements DropTargetListener {
        public CanvasDropListener() {
        }

        /**
         * The cursor has entered the drop target boundaries. {@inheritDoc}
         */
        @Override
        public void dragEnter(DropTargetEvent event) {
            mCanvas.showInvisibleViews(true);
            mCanvas.getEditorDelegate().getGraphicalEditor().dismissHoverPalette();

            if (mCurrentGesture == null) {
                Gesture newGesture = mZombieGesture;
                if (newGesture == null) {
                    newGesture = new MoveGesture(mCanvas);
                } else {
                    mZombieGesture = null;
                }
                startGesture(ControlPoint.create(mCanvas, event),
                        newGesture, 0);
            }

            if (mCurrentGesture instanceof DropGesture) {
                ((DropGesture) mCurrentGesture).dragEnter(event);
            }
        }

        /**
         * The cursor is moving over the drop target. {@inheritDoc}
         */
        @Override
        public void dragOver(DropTargetEvent event) {
            if (mCurrentGesture instanceof DropGesture) {
                ((DropGesture) mCurrentGesture).dragOver(event);
            }
        }

        /**
         * The cursor has left the drop target boundaries OR data is about to be
         * dropped. {@inheritDoc}
         */
        @Override
        public void dragLeave(DropTargetEvent event) {
            if (mCurrentGesture instanceof DropGesture) {
                DropGesture dropGesture = (DropGesture) mCurrentGesture;
                dropGesture.dragLeave(event);
                finishGesture(ControlPoint.create(mCanvas, event), true);
                mZombieGesture = dropGesture;
            }

            mCanvas.showInvisibleViews(false);
        }

        /**
         * The drop is about to be performed. The drop target is given a last
         * chance to change the nature of the drop. {@inheritDoc}
         */
        @Override
        public void dropAccept(DropTargetEvent event) {
            Gesture gesture = mCurrentGesture != null ? mCurrentGesture : mZombieGesture;
            if (gesture instanceof DropGesture) {
                ((DropGesture) gesture).dropAccept(event);
            }
        }

        /**
         * The data is being dropped. {@inheritDoc}
         */
        @Override
        public void drop(final DropTargetEvent event) {
            // See if we had a gesture just prior to the drop (we receive a dragLeave
            // right before the drop which we don't know whether means the cursor has
            // left the canvas for good or just before a drop)
            Gesture gesture = mCurrentGesture != null ? mCurrentGesture : mZombieGesture;
            mZombieGesture = null;

            if (gesture instanceof DropGesture) {
                ((DropGesture) gesture).drop(event);

                finishGesture(ControlPoint.create(mCanvas, event), true);
            }
        }

        /**
         * The operation being performed has changed (e.g. modifier key).
         * {@inheritDoc}
         */
        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (mCurrentGesture instanceof DropGesture) {
                ((DropGesture) mCurrentGesture).dragOperationChanged(event);
            }
        }
    }

    /**
     * Our canvas {@link DragSourceListener}. Handles drag being started and
     * finished and generating the drag data.
     */
    private class CanvasDragSourceListener implements DragSourceListener {

        /**
         * The current selection being dragged. This may be a subset of the
         * canvas selection due to the "sanitize" pass. Can be empty but never
         * null.
         */
        private final ArrayList<SelectionItem> mDragSelection = new ArrayList<SelectionItem>();

        private SimpleElement[] mDragElements;

        /**
         * The user has begun the actions required to drag the widget.
         * <p/>
         * Initiate a drag only if there is one or more item selected. If
         * there's none, try to auto-select the one under the cursor.
         * {@inheritDoc}
         */
        @Override
        public void dragStart(DragSourceEvent e) {
            LayoutPoint p = LayoutPoint.create(mCanvas, e);
            ControlPoint controlPoint = ControlPoint.create(mCanvas, e);
            SelectionManager selectionManager = mCanvas.getSelectionManager();

            // See if the mouse is over a selection handle; if so, start a resizing
            // gesture.
            Pair<SelectionItem, SelectionHandle> handle =
                selectionManager.findHandle(controlPoint);
            if (handle != null) {
                startGesture(controlPoint, new ResizeGesture(mCanvas, handle.getFirst(),
                        handle.getSecond()), mLastStateMask);
                e.detail = DND.DROP_NONE;
                e.doit = false;
                mCanvas.redraw();
                return;
            }

            // We need a selection (simple or multiple) to do any transfer.
            // If there's a selection *and* the cursor is over this selection,
            // use all the currently selected elements.
            // If there is no selection or the cursor is not over a selected
            // element, *change* the selection to match the element under the
            // cursor and use that. If nothing can be selected, abort the drag
            // operation.
            List<SelectionItem> selections = selectionManager.getSelections();
            mDragSelection.clear();
            SelectionItem primary = null;

            if (!selections.isEmpty()) {
                // Is the cursor on top of a selected element?
                boolean insideSelection = false;

                for (SelectionItem cs : selections) {
                    if (!cs.isRoot() && cs.getRect().contains(p.x, p.y)) {
                        primary = cs;
                        insideSelection = true;
                        break;
                    }
                }

                if (!insideSelection) {
                    CanvasViewInfo vi = mCanvas.getViewHierarchy().findViewInfoAt(p);
                    if (vi != null && !vi.isRoot() && !vi.isHidden()) {
                        primary = selectionManager.selectSingle(vi);
                        insideSelection = true;
                    }
                }

                if (insideSelection) {
                    // We should now have a proper selection that matches the
                    // cursor. Let's use this one. We make a copy of it since
                    // the "sanitize" pass below might remove some of the
                    // selected objects.
                    if (selections.size() == 1) {
                        // You are dragging just one element - this might or
                        // might not be the root, but if it's the root that is
                        // fine since we will let you drag the root if it is the
                        // only thing you are dragging.
                        mDragSelection.addAll(selections);
                    } else {
                        // Only drag non-root items.
                        for (SelectionItem cs : selections) {
                            if (!cs.isRoot() && !cs.isHidden()) {
                                mDragSelection.add(cs);
                            } else if (cs == primary) {
                                primary = null;
                            }
                        }
                    }
                }
            }

            // If you are dragging a non-selected item, select it
            if (mDragSelection.isEmpty()) {
                CanvasViewInfo vi = mCanvas.getViewHierarchy().findViewInfoAt(p);
                if (vi != null && !vi.isRoot() && !vi.isHidden()) {
                    primary = selectionManager.selectSingle(vi);
                    mDragSelection.addAll(selections);
                }
            }

            SelectionManager.sanitize(mDragSelection);

            e.doit = !mDragSelection.isEmpty();
            int imageCount = mDragSelection.size();
            if (e.doit) {
                mDragElements = SelectionItem.getAsElements(mDragSelection, primary);
                GlobalCanvasDragInfo.getInstance().startDrag(mDragElements,
                        mDragSelection.toArray(new SelectionItem[imageCount]),
                        mCanvas, new Runnable() {
                            @Override
                            public void run() {
                                mCanvas.getClipboardSupport().deleteSelection("Remove",
                                        mDragSelection);
                            }
                        });
            }

            // If you drag on the -background-, we make that into a marquee
            // selection
            if (!e.doit || (imageCount == 1
                    && (mDragSelection.get(0).isRoot() || mDragSelection.get(0).isHidden()))) {
                boolean toggle = (mLastStateMask & (SWT.CTRL | SWT.SHIFT | SWT.COMMAND)) != 0;
                startGesture(controlPoint,
                        new MarqueeGesture(mCanvas, toggle), mLastStateMask);
                e.detail = DND.DROP_NONE;
                e.doit = false;
            } else {
                // Otherwise, the drag means you are moving something
                mCanvas.showInvisibleViews(true);
                startGesture(controlPoint, new MoveGesture(mCanvas), 0);

                // Render drag-images: Copy portions of the full screen render.
                Image image = mCanvas.getImageOverlay().getImage();
                if (image != null) {
                    /**
                     * Transparency of the dragged image ([0-255]). We're using 30%
                     * translucency to make the image faint and not obscure the drag
                     * feedback below it.
                     */
                    final byte DRAG_TRANSPARENCY = (byte) (0.3 * 255);

                    List<Rectangle> rectangles = new ArrayList<Rectangle>(imageCount);
                    if (imageCount > 0) {
                        ImageData data = image.getImageData();
                        Rectangle imageRectangle = new Rectangle(0, 0, data.width, data.height);
                        for (SelectionItem item : mDragSelection) {
                            Rectangle bounds = item.getRect();
                            // Some bounds can be outside the rendered rectangle (for
                            // example, in an absolute layout, you can have negative
                            // coordinates), so create the intersection of these bounds.
                            Rectangle clippedBounds = imageRectangle.intersection(bounds);
                            rectangles.add(clippedBounds);
                        }
                        Rectangle boundingBox = ImageUtils.getBoundingRectangle(rectangles);
                        double scale = mCanvas.getHorizontalTransform().getScale();
                        e.image = SwtUtils.drawRectangles(image, rectangles, boundingBox, scale,
                                DRAG_TRANSPARENCY);

                        // Set the image offset such that we preserve the relative
                        // distance between the mouse pointer and the top left corner of
                        // the dragged view
                        int deltaX = (int) (scale * (boundingBox.x - p.x));
                        int deltaY = (int) (scale * (boundingBox.y - p.y));
                        e.offsetX = -deltaX;
                        e.offsetY = -deltaY;

                        // View rules may need to know it as well
                        GlobalCanvasDragInfo dragInfo = GlobalCanvasDragInfo.getInstance();
                        Rect dragBounds = null;
                        int width = (int) (scale * boundingBox.width);
                        int height = (int) (scale * boundingBox.height);
                        dragBounds = new Rect(deltaX, deltaY, width, height);
                        dragInfo.setDragBounds(dragBounds);

                        // Record the baseline such that we can perform baseline alignment
                        // on the node as it's dragged around
                        NodeProxy firstNode =
                            mCanvas.getNodeFactory().create(mDragSelection.get(0).getViewInfo());
                        dragInfo.setDragBaseline(firstNode.getBaseline());
                    }
                }
            }

            // No hover during drag (since no mouse over events are delivered
            // during a drag to keep the hovers up to date anyway)
            mCanvas.clearHover();

            mCanvas.redraw();
        }

        /**
         * Callback invoked when data is needed for the event, typically right
         * before drop. The drop side decides what type of transfer to use and
         * this side must now provide the adequate data. {@inheritDoc}
         */
        @Override
        public void dragSetData(DragSourceEvent e) {
            if (TextTransfer.getInstance().isSupportedType(e.dataType)) {
                e.data = SelectionItem.getAsText(mCanvas, mDragSelection);
                return;
            }

            if (SimpleXmlTransfer.getInstance().isSupportedType(e.dataType)) {
                e.data = mDragElements;
                return;
            }

            // otherwise we failed
            e.detail = DND.DROP_NONE;
            e.doit = false;
        }

        /**
         * Callback invoked when the drop has been finished either way. On a
         * successful move, remove the originating elements.
         */
        @Override
        public void dragFinished(DragSourceEvent e) {
            // Clear the selection
            mDragSelection.clear();
            mDragElements = null;
            GlobalCanvasDragInfo.getInstance().stopDrag();

            finishGesture(ControlPoint.create(mCanvas, e), e.detail == DND.DROP_NONE);
            mCanvas.showInvisibleViews(false);
            mCanvas.redraw();
        }
    }
}