1 /*******************************************************************************
2 * Copyright (c) 2007, 2008 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.dnd;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.*;
22 * This class provides a default drag under effect during a drag and drop.
23 * The current implementation does not provide any visual feedback.
25 * <p>The drop target effect has the same API as the
26 * <code>DropTargetAdapter</code> so that it can provide custom visual
27 * feedback when a <code>DropTargetEvent</code> occurs.
30 * <p>Classes that wish to provide their own drag under effect
31 * can extend the <code>DropTargetEffect</code> and override any applicable methods
32 * in <code>DropTargetAdapter</code> to display their own drag under effect.</p>
34 * <p>The feedback value is either one of the FEEDBACK constants defined in
35 * class <code>DND</code> which is applicable to instances of this class,
36 * or it must be built by <em>bitwise OR</em>'ing together
37 * (that is, using the <code>int</code> "|" operator) two or more
38 * of those <code>DND</code> effect constants.
41 * <dt><b>Feedback:</b></dt>
42 * <dd>FEEDBACK_EXPAND, FEEDBACK_INSERT_AFTER, FEEDBACK_INSERT_BEFORE,
43 * FEEDBACK_NONE, FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
46 * @see DropTargetAdapter
47 * @see DropTargetEvent
48 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
52 public class DropTargetEffect extends DropTargetAdapter {
56 * Creates a new <code>DropTargetEffect</code> to handle the drag under effect on the specified
57 * <code>Control</code>.
59 * @param control the <code>Control</code> over which the user positions the cursor to drop the data
61 * @exception IllegalArgumentException <ul>
62 * <li>ERROR_NULL_ARGUMENT - if the control is null</li>
65 public DropTargetEffect(Control control) {
66 if (control == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
67 this.control = control;
71 * Returns the Control which is registered for this DropTargetEffect. This is the control over which the
72 * user positions the cursor to drop the data.
74 * @return the Control which is registered for this DropTargetEffect
76 public Control getControl() {
81 * Returns the item at the given x-y coordinate in the receiver
82 * or null if no such item exists. The x-y coordinate is in the
83 * display relative coordinates.
85 * @param x the x coordinate used to locate the item
86 * @param y the y coordinate used to locate the item
87 * @return the item at the given x-y coordinate, or null if the coordinate is not in a selectable item
89 public Widget getItem(int x, int y) {
90 if (control instanceof Table) {
91 return getItem((Table) control, x, y);
93 if (control instanceof Tree) {
94 return getItem((Tree) control, x, y);
99 Widget getItem(Table table, int x, int y) {
100 Point coordinates = new Point(x, y);
101 coordinates = table.toControl(coordinates);
102 TableItem item = table.getItem(coordinates);
103 if (item != null) return item;
104 Rectangle area = table.getClientArea();
105 int tableBottom = area.y + area.height;
106 int itemCount = table.getItemCount();
107 for (int i=table.getTopIndex(); i<itemCount; i++) {
108 item = table.getItem(i);
109 Rectangle rect = item.getBounds();
111 rect.width = area.width;
112 if (rect.contains(coordinates)) return item;
113 if (rect.y > tableBottom) break;
118 Widget getItem(Tree tree, int x, int y) {
119 Point point = new Point(x, y);
120 point = tree.toControl(point);
121 TreeItem item = tree.getItem(point);
123 Rectangle area = tree.getClientArea();
124 if (area.contains(point)) {
125 int treeBottom = area.y + area.height;
126 item = tree.getTopItem();
127 while (item != null) {
128 Rectangle rect = item.getBounds();
129 int itemBottom = rect.y + rect.height;
130 if (rect.y <= point.y && point.y < itemBottom) return item;
131 if (itemBottom > treeBottom) break;
132 item = nextItem(tree, item);
140 TreeItem nextItem(Tree tree, TreeItem item) {
141 if (item == null) return null;
142 if (item.getExpanded() && item.getItemCount() > 0) return item.getItem(0);
143 TreeItem childItem = item;
144 TreeItem parentItem = childItem.getParentItem();
145 int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
146 int count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
148 if (index + 1 < count) return parentItem == null ? tree.getItem(index + 1) : parentItem.getItem(index + 1);
149 if (parentItem == null) return null;
150 childItem = parentItem;
151 parentItem = childItem.getParentItem();
152 index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
153 count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
157 TreeItem previousItem(Tree tree, TreeItem item) {
158 if (item == null) return null;
159 TreeItem childItem = item;
160 TreeItem parentItem = childItem.getParentItem();
161 int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
162 if (index == 0) return parentItem;
163 TreeItem nextItem = parentItem == null ? tree.getItem(index-1) : parentItem.getItem(index-1);
164 int count = nextItem.getItemCount();
165 while (count > 0 && nextItem.getExpanded()) {
166 nextItem = nextItem.getItem(count - 1);
167 count = nextItem.getItemCount();