Home | History | Annotate | Download | only in gle2
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
     17 
     18 import org.eclipse.jface.viewers.TreeViewer;
     19 import org.eclipse.swt.dnd.DND;
     20 import org.eclipse.swt.dnd.DragSourceEvent;
     21 import org.eclipse.swt.dnd.DragSourceListener;
     22 import org.eclipse.swt.dnd.TextTransfer;
     23 import org.eclipse.swt.graphics.Point;
     24 import org.eclipse.swt.widgets.Tree;
     25 import org.eclipse.swt.widgets.TreeItem;
     26 
     27 import java.util.ArrayList;
     28 
     29 /** Drag listener for the outline page */
     30 /* package */ class OutlineDragListener implements DragSourceListener {
     31     private TreeViewer mTreeViewer;
     32     private OutlinePage mOutlinePage;
     33     private final ArrayList<SelectionItem> mDragSelection = new ArrayList<SelectionItem>();
     34     private SimpleElement[] mDragElements;
     35 
     36     public OutlineDragListener(OutlinePage outlinePage, TreeViewer treeViewer) {
     37         super();
     38         mOutlinePage = outlinePage;
     39         mTreeViewer = treeViewer;
     40     }
     41 
     42     @Override
     43     public void dragStart(DragSourceEvent e) {
     44         Tree tree = mTreeViewer.getTree();
     45 
     46         TreeItem overTreeItem = tree.getItem(new Point(e.x, e.y));
     47         if (overTreeItem == null) {
     48             // Not dragging over a tree item
     49             e.doit = false;
     50             return;
     51         }
     52         CanvasViewInfo over = getViewInfo(overTreeItem);
     53         if (over == null) {
     54             e.doit = false;
     55             return;
     56         }
     57 
     58         // The selection logic for the outline is much simpler than in the canvas,
     59         // because for one thing, the tree selection is updated synchronously on mouse
     60         // down, so it's not possible to start dragging a non-selected item.
     61         // We also don't deliberately disallow root-element dragging since you can
     62         // drag it into another form.
     63         final LayoutCanvas canvas = mOutlinePage.getEditor().getCanvasControl();
     64         SelectionManager selectionManager = canvas.getSelectionManager();
     65         TreeItem[] treeSelection = tree.getSelection();
     66         mDragSelection.clear();
     67         for (TreeItem item : treeSelection) {
     68             CanvasViewInfo viewInfo = getViewInfo(item);
     69             if (viewInfo != null) {
     70                 mDragSelection.add(selectionManager.createSelection(viewInfo));
     71             }
     72         }
     73         SelectionManager.sanitize(mDragSelection);
     74 
     75         e.doit = !mDragSelection.isEmpty();
     76         int imageCount = mDragSelection.size();
     77         if (e.doit) {
     78             mDragElements = SelectionItem.getAsElements(mDragSelection);
     79             GlobalCanvasDragInfo.getInstance().startDrag(mDragElements,
     80                     mDragSelection.toArray(new SelectionItem[imageCount]),
     81                     canvas, new Runnable() {
     82                         @Override
     83                         public void run() {
     84                             canvas.getClipboardSupport().deleteSelection("Remove",
     85                                     mDragSelection);
     86                         }
     87                     });
     88             return;
     89         }
     90 
     91         e.detail = DND.DROP_NONE;
     92     }
     93 
     94     @Override
     95     public void dragSetData(DragSourceEvent e) {
     96         if (TextTransfer.getInstance().isSupportedType(e.dataType)) {
     97             LayoutCanvas canvas = mOutlinePage.getEditor().getCanvasControl();
     98             e.data = SelectionItem.getAsText(canvas, mDragSelection);
     99             return;
    100         }
    101 
    102         if (SimpleXmlTransfer.getInstance().isSupportedType(e.dataType)) {
    103             e.data = mDragElements;
    104             return;
    105         }
    106 
    107         // otherwise we failed
    108         e.detail = DND.DROP_NONE;
    109         e.doit = false;
    110     }
    111 
    112     @Override
    113     public void dragFinished(DragSourceEvent e) {
    114         // Unregister the dragged data.
    115         // Clear the selection
    116         mDragSelection.clear();
    117         mDragElements = null;
    118         GlobalCanvasDragInfo.getInstance().stopDrag();
    119     }
    120 
    121     private CanvasViewInfo getViewInfo(TreeItem item) {
    122         Object data = item.getData();
    123         if (data != null) {
    124             return OutlinePage.getViewInfo(data);
    125         }
    126 
    127         return null;
    128     }
    129 }