KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > dnd > DropTargetEffect


1 /*******************************************************************************
2  * Copyright (c) 2007 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.swt.dnd;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.widgets.*;
16
17
18 /**
19  * This class provides a default drag under effect during a drag and drop.
20  * The current implementation does not provide any visual feedback.
21  *
22  * <p>The drop target effect has the same API as the
23  * <code>DropTargetAdapter</code> so that it can provide custom visual
24  * feedback when a <code>DropTargetEvent</code> occurs.
25  * </p>
26  *
27  * <p>Classes that wish to provide their own drag under effect
28  * can extend the <code>DropTargetEffect</code> and override any applicable methods
29  * in <code>DropTargetAdapter</code> to display their own drag under effect.</p>
30  *
31  * <p>The feedback value is either one of the FEEDBACK constants defined in
32  * class <code>DND</code> which is applicable to instances of this class,
33  * or it must be built by <em>bitwise OR</em>'ing together
34  * (that is, using the <code>int</code> "|" operator) two or more
35  * of those <code>DND</code> effect constants.
36  * </p>
37  * <p>
38  * <dl>
39  * <dt><b>Feedback:</b></dt>
40  * <dd>FEEDBACK_EXPAND, FEEDBACK_INSERT_AFTER, FEEDBACK_INSERT_BEFORE,
41  * FEEDBACK_NONE, FEEDBACK_SELECT, FEEDBACK_SCROLL</dd>
42  * </dl>
43  * </p>
44  *
45  * @see DropTargetAdapter
46  * @see DropTargetEvent
47  *
48  * @since 3.3
49  */

50 public class DropTargetEffect extends DropTargetAdapter {
51     Control control;
52
53     /**
54      * Creates a new <code>DropTargetEffect</code> to handle the drag under effect on the specified
55      * <code>Control</code>.
56      *
57      * @param control the <code>Control</code> over which the user positions the cursor to drop the data
58      *
59      * @exception IllegalArgumentException <ul>
60      * <li>ERROR_NULL_ARGUMENT - if the control is null</li>
61      * </ul>
62      */

63     public DropTargetEffect(Control control) {
64         if (control == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
65         this.control = control;
66     }
67
68     /**
69      * Returns the Control which is registered for this DropTargetEffect. This is the control over which the
70      * user positions the cursor to drop the data.
71      *
72      * @return the Control which is registered for this DropTargetEffect
73      */

74     public Control getControl() {
75         return control;
76     }
77     
78     /**
79      * Returns the item at the given x-y coordinate in the receiver
80      * or null if no such item exists. The x-y coordinate is in the
81      * display relative coordinates.
82      *
83      * @param x the x coordinate used to locate the item
84      * @param y the y coordinate used to locate the item
85      * @return the item at the given x-y coordinate, or null if the coordinate is not in a selectable item
86      */

87     public Widget getItem(int x, int y) {
88         if (control instanceof Table) {
89             return getItem((Table) control, x, y);
90         }
91         if (control instanceof Tree) {
92             return getItem((Tree) control, x, y);
93         }
94         return null;
95     }
96     
97     Widget getItem(Table table, int x, int y) {
98         Point coordinates = new Point(x, y);
99         coordinates = table.toControl(coordinates);
100         Item item = table.getItem(coordinates);
101         if (item == null) {
102             Rectangle area = table.getClientArea();
103             if (area.contains(coordinates)) {
104                 // Scan across the width of the table.
105
for (int x1 = area.x; x1 < area.x + area.width; x1++) {
106                     Point pt = new Point(x1, coordinates.y);
107                     item = table.getItem(pt);
108                     if (item != null) {
109                         break;
110                     }
111                 }
112             }
113         }
114         return item;
115     }
116     
117     Widget getItem(Tree tree, int x, int y) {
118         Point coordinates = new Point(x, y);
119         coordinates = tree.toControl(coordinates);
120         Item item = tree.getItem(coordinates);
121         if (item == null) {
122             Rectangle area = tree.getClientArea();
123             if (area.contains(coordinates)) {
124                 // Scan across the width of the tree.
125
for (int x1 = area.x; x1 < area.x + area.width; x1++) {
126                     Point pt = new Point(x1, coordinates.y);
127                     item = tree.getItem(pt);
128                     if (item != null) {
129                         break;
130                     }
131                 }
132             }
133         }
134         return item;
135     }
136     
137     TreeItem nextItem(Tree tree, TreeItem item) {
138         if (item == null) return null;
139         if (item.getExpanded()) return item.getItem(0);
140         TreeItem childItem = item;
141         TreeItem parentItem = childItem.getParentItem();
142         int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
143         int count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
144         while (true) {
145             if (index + 1 < count) return parentItem == null ? tree.getItem(index + 1) : parentItem.getItem(index + 1);
146             if (parentItem == null) return null;
147             childItem = parentItem;
148             parentItem = childItem.getParentItem();
149             index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
150             count = parentItem == null ? tree.getItemCount() : parentItem.getItemCount();
151         }
152     }
153     
154     TreeItem previousItem(Tree tree, TreeItem item) {
155         if (item == null) return null;
156         TreeItem childItem = item;
157         TreeItem parentItem = childItem.getParentItem();
158         int index = parentItem == null ? tree.indexOf(childItem) : parentItem.indexOf(childItem);
159         if (index == 0) return parentItem;
160         TreeItem nextItem = parentItem == null ? tree.getItem(index-1) : parentItem.getItem(index-1);
161         int count = nextItem.getItemCount();
162         while (count > 0 && nextItem.getExpanded()) {
163             nextItem = nextItem.getItem(count - 1);
164             count = nextItem.getItemCount();
165         }
166         return nextItem;
167     }
168 }
169
Popular Tags