]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/dnd/ViewerInputDropAdapter.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / dnd / ViewerInputDropAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.dnd;
12
13
14 import org.eclipse.swt.dnd.DND;
15 import org.eclipse.swt.dnd.DropTargetEvent;
16 import org.eclipse.swt.dnd.TransferData;
17 import org.eclipse.swt.graphics.Point;
18 import org.eclipse.swt.graphics.Rectangle;
19 import org.eclipse.swt.widgets.Item;
20
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.StructuredViewer;
23
24 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDropAdapter;
25
26 public abstract class ViewerInputDropAdapter extends SelectionTransferDropAdapter {
27
28         private static final int ITEM_MARGIN_LEFT= 40;
29         private static final int ITEM_MARGIN_RIGTH= 10;
30         private static final int OPERATION= DND.DROP_LINK;
31
32         public ViewerInputDropAdapter(StructuredViewer viewer) {
33                 super(viewer);
34         }
35
36         protected abstract Object getInputElement(ISelection selection);
37
38         protected abstract void doInputView(Object inputElement);
39
40         /**
41          * {@inheritDoc}
42          */
43         @Override
44         protected int determineOperation(Object target, int operation, TransferData transferType, int operations) {
45
46                 setSelectionFeedbackEnabled(true);
47                 setExpandEnabled(true);
48
49                 initializeSelection();
50
51                 if (target != null) {
52                         return super.determineOperation(target, operation, transferType, operations);
53                 } else if (getInputElement(getSelection()) != null) {
54                         setSelectionFeedbackEnabled(false);
55                         setExpandEnabled(false);
56                         return OPERATION;
57                 } else {
58                         return DND.DROP_NONE;
59                 }
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         public boolean performDrop(Object data) {
67                 setSelectionFeedbackEnabled(true);
68                 setExpandEnabled(true);
69
70                 if (getCurrentTarget() != null || getCurrentOperation() != OPERATION) {
71                         return super.performDrop(data);
72                 }
73
74                 Object input= getInputElement(getSelection());
75                 if (input != null) {
76                         doInputView(input);
77                         return true;
78                 }
79
80                 return false;
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         public boolean isEnabled(DropTargetEvent event) {
88                 return true;
89         }
90
91         /**
92          * {@inheritDoc}
93          */
94         @Override
95         protected Object determineTarget(DropTargetEvent event) {
96                 if (event.item == null)
97                         return super.determineTarget(event);
98
99                 Point coordinates= getViewer().getControl().toControl(new Point(event.x, event.y));
100                 Rectangle bounds= getBounds((Item) event.item);
101                 if (coordinates.x < bounds.x - ITEM_MARGIN_LEFT || coordinates.x >= bounds.x + bounds.width + ITEM_MARGIN_RIGTH) {
102                         event.item= null; // too far away
103                         return null;
104                 }
105
106                 return super.determineTarget(event);
107         }
108
109 }