KickJava   Java API By Example, From Geeks To Geeks.

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


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.graphics.*;
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.widgets.*;
16
17 /**
18  * This class provides a default drag under effect (eg. select, insert, scroll and expand)
19  * when a drag occurs over a <code>Tree</code>.
20  *
21  * <p>Classes that wish to provide their own drag under effect for a <code>Tree</code>
22  * can extend the <code>TreeDropTargetEffect</code> class and override any applicable methods
23  * in <code>TreeDropTargetEffect</code> to display their own drag under effect.</p>
24  *
25  * Subclasses that override any methods of this class must call the corresponding
26  * <code>super</code> method to get the default drag under effect implementation.
27  *
28  * <p>The feedback value is either one of the FEEDBACK constants defined in
29  * class <code>DND</code> which is applicable to instances of this class,
30  * or it must be built by <em>bitwise OR</em>'ing together
31  * (that is, using the <code>int</code> "|" operator) two or more
32  * of those <code>DND</code> effect constants.
33  * </p>
34  * <p>
35  * <dl>
36  * <dt><b>Feedback:</b></dt>
37  * <dd>FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE, FEEDBACK_INSERT_AFTER, FEEDBACK_EXPAND, FEEDBACK_SCROLL</dd>
38  * </dl>
39  * </p><p>
40  * Note: Only one of the styles FEEDBACK_SELECT, FEEDBACK_INSERT_BEFORE or
41  * FEEDBACK_INSERT_AFTER may be specified.
42  * </p>
43  *
44  * @see DropTargetAdapter
45  * @see DropTargetEvent
46  *
47  * @since 3.3
48  */

49 public class TreeDropTargetEffect extends DropTargetEffect {
50     static final int SCROLL_HYSTERESIS = 200; // milli seconds
51
static final int EXPAND_HYSTERESIS = 1000; // milli seconds
52

53     int dropIndex;
54     int scrollIndex;
55     long scrollBeginTime;
56     int expandIndex;
57     long expandBeginTime;
58     TreeItem insertItem;
59     boolean insertBefore;
60     
61     /**
62      * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified
63      * <code>Tree</code>.
64      *
65      * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data
66      */

67     public TreeDropTargetEffect(Tree tree) {
68         super(tree);
69     }
70
71     int checkEffect(int effect) {
72         // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified.
73
if ((effect & DND.FEEDBACK_SELECT) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE;
74         if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER;
75         return effect;
76     }
77
78     /**
79      * This implementation of <code>dragEnter</code> provides a default drag under effect
80      * for the feedback specified in <code>event.feedback</code>.
81      *
82      * For additional information see <code>DropTargetAdapter.dragEnter</code>.
83      *
84      * Subclasses that override this method should call <code>super.dragEnter(event)</code>
85      * to get the default drag under effect implementation.
86      *
87      * @param event the information associated with the drag enter event
88      *
89      * @see DropTargetAdapter
90      * @see DropTargetEvent
91      */

92     public void dragEnter(DropTargetEvent event) {
93         dropIndex = -1;
94         insertItem = null;
95         expandBeginTime = 0;
96         expandIndex = -1;
97         scrollBeginTime = 0;
98         scrollIndex = -1;
99     }
100     
101     /**
102      * This implementation of <code>dragLeave</code> provides a default drag under effect
103      * for the feedback specified in <code>event.feedback</code>.
104      *
105      * For additional information see <code>DropTargetAdapter.dragLeave</code>.
106      *
107      * Subclasses that override this method should call <code>super.dragLeave(event)</code>
108      * to get the default drag under effect implementation.
109      *
110      * @param event the information associated with the drag leave event
111      *
112      * @see DropTargetAdapter
113      * @see DropTargetEvent
114      */

115     public void dragLeave(DropTargetEvent event) {
116         Tree tree = (Tree) control;
117         int handle = tree.handle;
118         if (dropIndex != -1) {
119             TVITEM tvItem = new TVITEM ();
120             tvItem.hItem = dropIndex;
121             tvItem.mask = OS.TVIF_STATE;
122             tvItem.stateMask = OS.TVIS_DROPHILITED;
123             tvItem.state = 0;
124             OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
125             dropIndex = -1;
126         }
127         if (insertItem != null) {
128             tree.setInsertMark(null, false);
129             insertItem = null;
130         }
131         expandBeginTime = 0;
132         expandIndex = -1;
133         scrollBeginTime = 0;
134         scrollIndex = -1;
135     }
136
137     /**
138      * This implementation of <code>dragOver</code> provides a default drag under effect
139      * for the feedback specified in <code>event.feedback</code>.
140      *
141      * For additional information see <code>DropTargetAdapter.dragOver</code>.
142      *
143      * Subclasses that override this method should call <code>super.dragOver(event)</code>
144      * to get the default drag under effect implementation.
145      *
146      * @param event the information associated with the drag over event
147      *
148      * @see DropTargetAdapter
149      * @see DropTargetEvent
150      * @see DND#FEEDBACK_SELECT
151      * @see DND#FEEDBACK_INSERT_BEFORE
152      * @see DND#FEEDBACK_INSERT_AFTER
153      * @see DND#FEEDBACK_SCROLL
154      */

155     public void dragOver(DropTargetEvent event) {
156         Tree tree = (Tree) getControl();
157         int effect = checkEffect(event.feedback);
158         int handle = tree.handle;
159         Point coordinates = new Point(event.x, event.y);
160         coordinates = tree.toControl(coordinates);
161         TVHITTESTINFO lpht = new TVHITTESTINFO ();
162         lpht.x = coordinates.x;
163         lpht.y = coordinates.y;
164         OS.SendMessage (handle, OS.TVM_HITTEST, 0, lpht);
165         int hItem = lpht.hItem;
166         if ((effect & DND.FEEDBACK_SCROLL) == 0) {
167             scrollBeginTime = 0;
168             scrollIndex = -1;
169         } else {
170             if (hItem != -1 && scrollIndex == hItem && scrollBeginTime != 0) {
171                 if (System.currentTimeMillis() >= scrollBeginTime) {
172                     int topItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, OS.TVGN_FIRSTVISIBLE, 0);
173                     int nextItem = OS.SendMessage(handle, OS.TVM_GETNEXTITEM, hItem == topItem ? OS.TVGN_PREVIOUSVISIBLE : OS.TVGN_NEXTVISIBLE, hItem);
174                     boolean scroll = true;
175                     if (hItem == topItem) {
176                         scroll = nextItem != 0;
177                     } else {
178                         RECT itemRect = new RECT ();
179                         itemRect.left = nextItem;
180                         if (OS.SendMessage (handle, OS.TVM_GETITEMRECT, 1, itemRect) != 0) {
181                             RECT rect = new RECT ();
182                             OS.GetClientRect (handle, rect);
183                             POINT pt = new POINT ();
184                             pt.x = itemRect.left;
185                             pt.y = itemRect.top;
186                             if (OS.PtInRect (rect, pt)) {
187                                 pt.y = itemRect.bottom;
188                                 if (OS.PtInRect (rect, pt)) scroll = false;
189                             }
190                         }
191                     }
192                     if (scroll) {
193                         OS.SendMessage (handle, OS.TVM_ENSUREVISIBLE, 0, nextItem);
194                         tree.redraw();
195                     }
196                     scrollBeginTime = 0;
197                     scrollIndex = -1;
198                 }
199             } else {
200                 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
201                 scrollIndex = hItem;
202             }
203         }
204         if ((effect & DND.FEEDBACK_EXPAND) == 0) {
205             expandBeginTime = 0;
206             expandIndex = -1;
207         } else {
208             if (hItem != -1 && expandIndex == hItem && expandBeginTime != 0) {
209                 if (System.currentTimeMillis() >= expandBeginTime) {
210                     if (OS.SendMessage (handle, OS.TVM_GETNEXTITEM, OS.TVGN_CHILD, hItem) != 0) {
211                         TVITEM tvItem = new TVITEM ();
212                         tvItem.hItem = hItem;
213                         tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_STATE;
214                         OS.SendMessage (handle, OS.TVM_GETITEM, 0, tvItem);
215                         if ((tvItem.state & OS.TVIS_EXPANDED) == 0) {
216                             OS.SendMessage (handle, OS.TVM_EXPAND, OS.TVE_EXPAND, hItem);
217                             tree.redraw();
218                         }
219                     }
220                     expandBeginTime = 0;
221                     expandIndex = -1;
222                 }
223             } else {
224                 expandBeginTime = System.currentTimeMillis() + EXPAND_HYSTERESIS;
225                 expandIndex = hItem;
226             }
227         }
228         if (dropIndex != -1 && (dropIndex != hItem || (effect & DND.FEEDBACK_SELECT) == 0)) {
229             TVITEM tvItem = new TVITEM ();
230             tvItem.hItem = dropIndex;
231             tvItem.mask = OS.TVIF_STATE;
232             tvItem.stateMask = OS.TVIS_DROPHILITED;
233             tvItem.state = 0;
234             OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
235             dropIndex = -1;
236         }
237         if (hItem != -1 && hItem != dropIndex && (effect & DND.FEEDBACK_SELECT) != 0) {
238             TVITEM tvItem = new TVITEM ();
239             tvItem.hItem = hItem;
240             tvItem.mask = OS.TVIF_STATE;
241             tvItem.stateMask = OS.TVIS_DROPHILITED;
242             tvItem.state = OS.TVIS_DROPHILITED;
243             OS.SendMessage (handle, OS.TVM_SETITEM, 0, tvItem);
244             dropIndex = hItem;
245         }
246         if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0 || (effect & DND.FEEDBACK_INSERT_AFTER) != 0) {
247             boolean before = (effect & DND.FEEDBACK_INSERT_BEFORE) != 0;
248             /*
249             * Bug in Windows. When TVM_SETINSERTMARK is used to set
250             * an insert mark for a tree and an item is expanded or
251             * collapsed near the insert mark, the tree does not redraw
252             * the insert mark properly. The fix is to hide and show
253             * the insert mark whenever an item is expanded or collapsed.
254             * Since the insert mark can not be queried from the tree,
255             * use the Tree API rather than calling the OS directly.
256             */

257             TreeItem item = (TreeItem)tree.getDisplay().findWidget(tree.handle, hItem);
258             if (item != null) {
259                 if (item != insertItem || before != insertBefore) {
260                     tree.setInsertMark(item, before);
261                 }
262                 insertItem = item;
263                 insertBefore = before;
264             } else {
265                 if (insertItem != null) {
266                     tree.setInsertMark(null, false);
267                 }
268                 insertItem = null;
269             }
270         } else {
271             if (insertItem != null) {
272                 tree.setInsertMark(null, false);
273             }
274             insertItem = null;
275         }
276     }
277 }
278
Popular Tags