KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.win32.*;
16 import org.eclipse.swt.widgets.*;
17
18 class TableDragAndDropEffect extends DragAndDropEffect {
19     Table table;
20     int scrollIndex;
21     long scrollBeginTime;
22     TableItem dropHighlight;
23
24     static final int SCROLL_HYSTERESIS = 200; // milli seconds
25

26 TableDragAndDropEffect(Table table) {
27     this.table = table;
28 }
29
30 int checkEffect(int effect) {
31     // Some effects are mutually exclusive. Make sure that only one of the mutually exclusive effects has been specified.
32
if ((effect & DND.FEEDBACK_SELECT) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER & ~DND.FEEDBACK_INSERT_BEFORE;
33     if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0) effect = effect & ~DND.FEEDBACK_INSERT_AFTER;
34     return effect;
35 }
36
37 Widget getItem(int x, int y) {
38     Point coordinates = new Point(x, y);
39     coordinates = table.toControl(coordinates);
40     TableItem item = table.getItem(coordinates);
41     if (item == null) {
42         Rectangle area = table.getClientArea();
43         if (area.contains(coordinates)) {
44             // Scan across the width of the table.
45
for (int x1 = area.x; x1 < area.x + area.width; x1++) {
46                 Point pt = new Point(x1, coordinates.y);
47                 item = table.getItem(pt);
48                 if (item != null) {
49                     break;
50                 }
51             }
52         }
53     }
54     return item;
55 }
56
57 ImageData getDragSourceImage(int x, int y) {
58     TableItem[] selection = table.getSelection();
59     if (selection.length == 0) return null;
60     int tableImageList = OS.SendMessage (table.handle, OS.LVM_GETIMAGELIST, OS.LVSIL_SMALL, 0);
61     if (tableImageList != 0) {
62         int count = Math.min(selection.length, 10);
63         Rectangle bounds = selection[0].getBounds(0);
64         for (int i = 1; i < count; i++) {
65             bounds = bounds.union(selection[i].getBounds(0));
66         }
67         int hDC = OS.GetDC(0);
68         int hDC1 = OS.CreateCompatibleDC(hDC);
69         int bitmap = OS.CreateCompatibleBitmap(hDC, bounds.width, bounds.height);
70         int hOldBitmap = OS.SelectObject(hDC1, bitmap);
71         RECT rect = new RECT();
72         rect.right = bounds.width;
73         rect.bottom = bounds.height;
74         int hBrush = OS.GetStockObject(OS.WHITE_BRUSH);
75         OS.FillRect(hDC1, rect, hBrush);
76         for (int i = 0; i < count; i++) {
77             TableItem selected = selection[i];
78             Rectangle cell = selected.getBounds(0);
79             POINT pt = new POINT();
80             int imageList = OS.SendMessage (table.handle, OS.LVM_CREATEDRAGIMAGE, table.indexOf(selected), pt);
81             OS.ImageList_Draw(imageList, 0, hDC1, cell.x - bounds.x, cell.y - bounds.y, OS.ILD_SELECTED);
82             OS.ImageList_Destroy(imageList);
83         }
84         OS.SelectObject(hDC1, hOldBitmap);
85         OS.DeleteDC (hDC1);
86         OS.ReleaseDC (0, hDC);
87         Display display = table.getDisplay();
88         Image image = Image.win32_new(display, SWT.BITMAP, bitmap);
89         ImageData imageData = image.getImageData();
90         image.dispose();
91         return imageData;
92     }
93     return null;
94 }
95
96 void showDropTargetEffect(int effect, int x, int y) {
97     effect = checkEffect(effect);
98     int handle = table.handle;
99     Point coordinates = new Point(x, y);
100     coordinates = table.toControl(coordinates);
101     LVHITTESTINFO pinfo = new LVHITTESTINFO();
102     pinfo.x = coordinates.x;
103     pinfo.y = coordinates.y;
104     OS.SendMessage(handle, OS.LVM_HITTEST, 0, pinfo);
105     if ((effect & DND.FEEDBACK_SCROLL) == 0) {
106         scrollBeginTime = 0;
107         scrollIndex = -1;
108     } else {
109         if (pinfo.iItem != -1 && scrollIndex == pinfo.iItem && scrollBeginTime != 0) {
110             if (System.currentTimeMillis() >= scrollBeginTime) {
111                 int top = Math.max (0, OS.SendMessage (handle, OS.LVM_GETTOPINDEX, 0, 0));
112                 int count = OS.SendMessage (handle, OS.LVM_GETITEMCOUNT, 0, 0);
113                 int index = (scrollIndex - 1 < top) ? Math.max(0, scrollIndex - 1) : Math.min(count - 1, scrollIndex + 1);
114                 boolean scroll = true;
115                 if (pinfo.iItem == top) {
116                     scroll = pinfo.iItem != index;
117                 } else {
118                     RECT itemRect = new RECT ();
119                     itemRect.left = OS.LVIR_BOUNDS;
120                     if (OS.SendMessage (handle, OS.LVM_GETITEMRECT, pinfo.iItem, itemRect) != 0) {
121                         RECT rect = new RECT ();
122                         OS.GetClientRect (handle, rect);
123                         POINT pt = new POINT ();
124                         pt.x = itemRect.left;
125                         pt.y = itemRect.top;
126                         if (OS.PtInRect (rect, pt)) {
127                             pt.y = itemRect.bottom;
128                             if (OS.PtInRect (rect, pt)) scroll = false;
129                         }
130                     }
131                 }
132                 if (scroll) {
133                     OS.SendMessage (handle, OS.LVM_ENSUREVISIBLE, index, 0);
134                     table.redraw();
135                 }
136                 scrollBeginTime = 0;
137                 scrollIndex = -1;
138             }
139         } else {
140             scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS;
141             scrollIndex = pinfo.iItem;
142         }
143     }
144     
145     if (pinfo.iItem != -1 && (effect & DND.FEEDBACK_SELECT) != 0) {
146         TableItem item = table.getItem(pinfo.iItem);
147         if (dropHighlight != item) {
148             LVITEM lvItem = new LVITEM();
149             lvItem.stateMask = OS.LVIS_DROPHILITED;
150             OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
151             lvItem.state = OS.LVIS_DROPHILITED;
152             OS.SendMessage(handle, OS.LVM_SETITEMSTATE, pinfo.iItem, lvItem);
153             dropHighlight = item;
154         }
155     } else {
156         if (dropHighlight != null) {
157             LVITEM lvItem = new LVITEM ();
158             lvItem.stateMask = OS.LVIS_DROPHILITED;
159             OS.SendMessage(handle, OS.LVM_SETITEMSTATE, -1, lvItem);
160             dropHighlight = null;
161         }
162     }
163
164     //Insert mark only supported on Windows XP with manifest
165
// if (OS.COMCTL32_MAJOR >= 6) {
166
// if ((effect & DND.FEEDBACK_INSERT_BEFORE) != 0 || (effect & DND.FEEDBACK_INSERT_AFTER) != 0) {
167
// LVINSERTMARK lvinsertmark = new LVINSERTMARK();
168
// lvinsertmark.cbSize = LVINSERTMARK.sizeof;
169
// lvinsertmark.dwFlags = (effect & DND.FEEDBACK_INSERT_BEFORE) != 0 ? 0 : OS.LVIM_AFTER;
170
// lvinsertmark.iItem = pinfo.iItem == -1 ? 0 : pinfo.iItem;
171
// int hItem = pinfo.iItem;
172
// OS.SendMessage (handle, OS.LVM_SETINSERTMARK, 0, lvinsertmark);
173
// } else {
174
// OS.SendMessage (handle, OS.LVM_SETINSERTMARK, 0, 0);
175
// }
176
// }
177
return;
178 }
179 }
180
Popular Tags