KickJava   Java API By Example, From Geeks To Geeks.

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


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 and scroll)
19  * when a drag occurs over a <code>Table</code>.
20  *
21  * <p>Classes that wish to provide their own drag under effect for a <code>Table</code>
22  * can extend the <code>TableDropTargetEffect</code> and override any applicable methods
23  * in <code>TableDropTargetEffect</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_SCROLL</dd>
38  * </dl>
39  * </p>
40  *
41  * @see DropTargetAdapter
42  * @see DropTargetEvent
43  *
44  * @since 3.3
45  */

46 public class TableDropTargetEffect extends DropTargetEffect {
47     static final int SCROLL_HYSTERESIS = 200; // milli seconds
48

49     int scrollIndex = -1;
50     long scrollBeginTime;
51     TableItem dropHighlight;
52
53     /**
54      * Creates a new <code>TableDropTargetEffect</code> to handle the drag under effect on the specified
55      * <code>Table</code>.
56      *
57      * @param table the <code>Table</code> over which the user positions the cursor to drop the data
58      */

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

84     public void dragEnter(DropTargetEvent event) {
85         scrollBeginTime = 0;
86         scrollIndex = -1;
87         dropHighlight = null;
88     }
89     
90     /**
91      * This implementation of <code>dragLeave</code> provides a default drag under effect
92      * for the feedback specified in <code>event.feedback</code>.
93      *
94      * For additional information see <code>DropTargetAdapter.dragLeave</code>.
95      *
96      * Subclasses that override this method should call <code>super.dragLeave(event)</code>
97      * to get the default drag under effect implementation.
98      *
99      * @param event the information associated with the drag leave event
100      *
101      * @see DropTargetAdapter
102      * @see DropTargetEvent
103      */

104     public void dragLeave(DropTargetEvent event) {
105         Table table = (Table) control;
106         int handle = table.handle;
107         if (dropHighlight != null) {
108             LVITEM lvItem = new LVITEM ();
109             lvItem.stateMask = OS.LVIS_DROPHILITED;
110             OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
111             dropHighlight = null;
112         }
113         scrollBeginTime = 0;
114         scrollIndex = -1;
115     }
116
117     /**
118      * This implementation of <code>dragOver</code> provides a default drag under effect
119      * for the feedback specified in <code>event.feedback</code>. The class description
120      * lists the FEEDBACK constants that are applicable to the class.
121      *
122      * For additional information see <code>DropTargetAdapter.dragOver</code>.
123      *
124      * Subclasses that override this method should call <code>super.dragOver(event)</code>
125      * to get the default drag under effect implementation.
126      *
127      * @param event the information associated with the drag over event
128      *
129      * @see DropTargetAdapter
130      * @see DropTargetEvent
131      * @see DND#FEEDBACK_SELECT
132      * @see DND#FEEDBACK_SCROLL
133      */

134     public void dragOver(DropTargetEvent event) {
135         Table table = (Table) getControl();
136         int effect = checkEffect(event.feedback);
137         int handle = table.handle;
138         Point coordinates = new Point(event.x, event.y);
139         coordinates = table.toControl(coordinates);
140         LVHITTESTINFO pinfo = new LVHITTESTINFO();
141         pinfo.x = coordinates.x;
142         pinfo.y = coordinates.y;
143         OS.SendMessage(handle, OS.LVM_HITTEST, 0, pinfo);
144         if ((effect & DND.FEEDBACK_SCROLL) == 0) {
145             scrollBeginTime = 0;
146             scrollIndex = -1;
147         } else {
148             if (pinfo.iItem != -1 && scrollIndex == pinfo.iItem && scrollBeginTime != 0) {
149                 if (System.currentTimeMillis() >= scrollBeginTime) {
150                     int top = Math.max (0, OS.SendMessage (handle, OS.LVM_GETTOPINDEX, 0, 0));
151                     int count = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0);
152                     int index = (scrollIndex - 1 < top) ? Math.max(0, scrollIndex - 1) : Math.min(count - 1, scrollIndex + 1);
153                     boolean scroll = true;
154                     if (pinfo.iItem == top) {
155                         scroll = pinfo.iItem != index;
156                     } else {
157                         RECT itemRect = new RECT ();
158                         itemRect.left = OS.LVIR_BOUNDS;
159                         if (OS.SendMessage (handle, OS.LVM_GETITEMRECT, pinfo.iItem, itemRect) != 0) {
160                             RECT rect = new RECT ();
161                             OS.GetClientRect (handle, rect);
162                             POINT pt = new POINT ();
163                             pt.x = itemRect.left;
164                             pt.y = itemRect.top;
165                             if (OS.PtInRect (rect, pt)) {
166                                 pt.y = itemRect.bottom;
167                                 if (OS.PtInRect (rect, pt)) scroll = false;
168                             }
169                         }
170                     }
171                     if (scroll) {
172                         OS.SendMessage (handle, OS.LVM_ENSUREVISIBLE, index, 0);
173                         table.redraw();
174                     }
175                     scrollBeginTime = 0;
176                     scrollIndex = -1;
177                 }
178             } else {
179                 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
180                 scrollIndex = pinfo.iItem;
181             }
182         }
183         
184         if (pinfo.iItem != -1 && (effect & DND.FEEDBACK_SELECT) != 0) {
185             TableItem item = table.getItem(pinfo.iItem);
186             if (dropHighlight != item) {
187                 LVITEM lvItem = new LVITEM();
188                 lvItem.stateMask = OS.LVIS_DROPHILITED;
189                 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
190                 lvItem.state = OS.LVIS_DROPHILITED;
191                 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, pinfo.iItem, lvItem);
192                 dropHighlight = item;
193             }
194         } else {
195             if (dropHighlight != null) {
196                 LVITEM lvItem = new LVITEM ();
197                 lvItem.stateMask = OS.LVIS_DROPHILITED;
198                 OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
199                 dropHighlight = null;
200             }
201         }
202     }
203 }
204
Popular Tags