Mercurial > projects > dwt-mac
comparison dwt/dnd/TreeDropTargetEffect.d @ 113:2d6116ea306e
Ported dwt.dnd.TreeDropTargetEffect
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 31 Dec 2008 15:09:06 +0100 |
parents | d8635bb48c7c |
children |
comparison
equal
deleted
inserted
replaced
112:a84f60dde47e | 113:2d6116ea306e |
---|---|
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | |
11 * Port to the D programming language: | |
12 * Jacob Carlborg <doob@me.com> | |
10 *******************************************************************************/ | 13 *******************************************************************************/ |
11 module dwt.dnd.TreeDropTargetEffect; | 14 module dwt.dnd.TreeDropTargetEffect; |
12 | 15 |
13 import dwt.dwthelper.utils; | 16 import dwt.dwthelper.utils; |
14 | 17 |
16 import dwt.graphics.Point; | 19 import dwt.graphics.Point; |
17 import dwt.graphics.Rectangle; | 20 import dwt.graphics.Rectangle; |
18 import dwt.widgets.Event; | 21 import dwt.widgets.Event; |
19 import dwt.widgets.Tree; | 22 import dwt.widgets.Tree; |
20 import dwt.widgets.TreeItem; | 23 import dwt.widgets.TreeItem; |
24 | |
25 import dwt.dnd.DND; | |
26 import dwt.dnd.DropTarget; | |
27 import dwt.dnd.DropTargetEffect; | |
28 import dwt.dnd.DropTargetEvent; | |
21 | 29 |
22 /** | 30 /** |
23 * This class provides a default drag under effect (eg. select, insert, scroll and expand) | 31 * This class provides a default drag under effect (eg. select, insert, scroll and expand) |
24 * when a drag occurs over a <code>Tree</code>. | 32 * when a drag occurs over a <code>Tree</code>. |
25 * | 33 * |
50 * @see DropTargetEvent | 58 * @see DropTargetEvent |
51 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | 59 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
52 * | 60 * |
53 * @since 3.3 | 61 * @since 3.3 |
54 */ | 62 */ |
55 public class TreeDropTargetEffect extends DropTargetEffect { | 63 public class TreeDropTargetEffect : DropTargetEffect { |
56 static final int SCROLL_HYSTERESIS = 150; // milli seconds | 64 static const int SCROLL_HYSTERESIS = 150; // milli seconds |
57 static final int EXPAND_HYSTERESIS = 1000; // milli seconds | 65 static const int EXPAND_HYSTERESIS = 1000; // milli seconds |
58 | 66 |
59 int currentEffect = DND.FEEDBACK_NONE; | 67 int currentEffect = DND.FEEDBACK_NONE; |
60 TreeItem currentItem; | 68 TreeItem currentItem; |
61 | 69 |
62 TreeItem insertItem = null; | 70 TreeItem insertItem = null; |
100 * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified | 108 * Creates a new <code>TreeDropTargetEffect</code> to handle the drag under effect on the specified |
101 * <code>Tree</code>. | 109 * <code>Tree</code>. |
102 * | 110 * |
103 * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data | 111 * @param tree the <code>Tree</code> over which the user positions the cursor to drop the data |
104 */ | 112 */ |
105 public TreeDropTargetEffect(Tree tree) { | 113 public this(Tree tree) { |
106 super(tree); | 114 super(tree); |
107 } | 115 } |
108 | 116 |
109 int checkEffect(int effect) { | 117 int checkEffect(int effect) { |
110 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified. | 118 // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified. |
155 * | 163 * |
156 * @see DropTargetAdapter | 164 * @see DropTargetAdapter |
157 * @see DropTargetEvent | 165 * @see DropTargetEvent |
158 */ | 166 */ |
159 public void dragLeave(DropTargetEvent event) { | 167 public void dragLeave(DropTargetEvent event) { |
160 Tree tree = (Tree) control; | 168 Tree tree = cast(Tree) control; |
161 if (insertItem !is null) { | 169 if (insertItem !is null) { |
162 setInsertMark(tree, null, false); | 170 setInsertMark(tree, null, false); |
163 insertItem = null; | 171 insertItem = null; |
164 } | 172 } |
165 expandBeginTime = 0; | 173 expandBeginTime = 0; |
185 * @see DND#FEEDBACK_INSERT_BEFORE | 193 * @see DND#FEEDBACK_INSERT_BEFORE |
186 * @see DND#FEEDBACK_INSERT_AFTER | 194 * @see DND#FEEDBACK_INSERT_AFTER |
187 * @see DND#FEEDBACK_SCROLL | 195 * @see DND#FEEDBACK_SCROLL |
188 */ | 196 */ |
189 public void dragOver(DropTargetEvent event) { | 197 public void dragOver(DropTargetEvent event) { |
190 Tree tree = (Tree) control; | 198 Tree tree = cast(Tree) control; |
191 TreeItem item = (TreeItem)getItem(tree, event.x, event.y); | 199 TreeItem item = cast(TreeItem)getItem(tree, event.x, event.y); |
192 int effect = checkEffect(event.feedback); | 200 int effect = checkEffect(event.feedback); |
193 if ((effect & DND.FEEDBACK_EXPAND) is 0) { | 201 if ((effect & DND.FEEDBACK_EXPAND) is 0) { |
194 expandBeginTime = 0; | 202 expandBeginTime = 0; |
195 expandItem = null; | 203 expandItem = null; |
196 } else { | 204 } else { |
197 if (item !is null && item.equals(expandItem) && expandBeginTime !is 0) { | 205 if (item !is null && item.opEquals(expandItem) && expandBeginTime !is 0) { |
198 if (System.currentTimeMillis() >= expandBeginTime) { | 206 if (System.currentTimeMillis() >= expandBeginTime) { |
199 if (item.getItemCount() > 0 && !item.getExpanded()) { | 207 if (item.getItemCount() > 0 && !item.getExpanded()) { |
200 Event e = new Event(); | 208 Event e = new Event(); |
201 e.x = event.x; | 209 e.x = event.x; |
202 e.y = event.y; | 210 e.y = event.y; |
203 e.item = item; | 211 e.item = item; |
204 e.time = (int) System.currentTimeMillis(); | 212 e.time = cast(int) System.currentTimeMillis(); |
205 tree.notifyListeners(DWT.Expand, e); | 213 tree.notifyListeners(DWT.Expand, e); |
206 if (item.isDisposed()) return; | 214 if (item.isDisposed()) return; |
207 item.setExpanded(true); | 215 item.setExpanded(true); |
208 } | 216 } |
209 expandBeginTime = 0; | 217 expandBeginTime = 0; |
217 | 225 |
218 if ((effect & DND.FEEDBACK_SCROLL) is 0) { | 226 if ((effect & DND.FEEDBACK_SCROLL) is 0) { |
219 scrollBeginTime = 0; | 227 scrollBeginTime = 0; |
220 scrollItem = null; | 228 scrollItem = null; |
221 } else { | 229 } else { |
222 if (item !is null && item.equals(scrollItem) && scrollBeginTime !is 0) { | 230 if (item !is null && item.opEquals(scrollItem) && scrollBeginTime !is 0) { |
223 if (System.currentTimeMillis() >= scrollBeginTime) { | 231 if (System.currentTimeMillis() >= scrollBeginTime) { |
224 Rectangle area = tree.getClientArea(); | 232 Rectangle area = tree.getClientArea(); |
225 int headerHeight = tree.getHeaderHeight(); | 233 int headerHeight = tree.getHeaderHeight(); |
226 int itemHeight= tree.getItemHeight(); | 234 int itemHeight= tree.getItemHeight(); |
227 Point pt = new Point(event.x, event.y); | 235 Point pt = new Point(event.x, event.y); |
228 pt = tree.getDisplay().map(null, tree, pt); | 236 pt = tree.getDisplay().map(null, tree, pt); |
229 TreeItem nextItem = null; | 237 TreeItem nextItem_ = null; |
230 if (pt.y < area.y + headerHeight + 2 * itemHeight) { | 238 if (pt.y < area.y + headerHeight + 2 * itemHeight) { |
231 nextItem = previousItem(tree, item); | 239 nextItem_ = previousItem(tree, item); |
232 } | 240 } |
233 if (pt.y > area.y + area.height - 2 * itemHeight) { | 241 if (pt.y > area.y + area.height - 2 * itemHeight) { |
234 nextItem = nextItem(tree, item); | 242 nextItem_ = nextItem(tree, item); |
235 } | 243 } |
236 if (nextItem !is null) tree.showItem(nextItem); | 244 if (nextItem_ !is null) tree.showItem(nextItem_); |
237 scrollBeginTime = 0; | 245 scrollBeginTime = 0; |
238 scrollItem = null; | 246 scrollItem = null; |
239 } | 247 } |
240 } else { | 248 } else { |
241 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; | 249 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; |
254 } | 262 } |
255 } else { | 263 } else { |
256 setInsertMark(tree, null, false); | 264 setInsertMark(tree, null, false); |
257 } | 265 } |
258 // save current effect for selection feedback | 266 // save current effect for selection feedback |
259 ((DropTarget)event.widget).feedback = effect; | 267 (cast(DropTarget)event.widget).feedback = effect; |
260 } | 268 } |
261 | 269 |
262 void setInsertMark(Tree tree, TreeItem item, bool before) { | 270 void setInsertMark(Tree tree, TreeItem item, bool before) { |
263 if (item is insertItem && before is insertBefore) return; | 271 if (item is insertItem && before is insertBefore) return; |
264 insertItem = item; | 272 insertItem = item; |