KickJava   Java API By Example, From Geeks To Geeks.

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


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.internal.win32.*;
16 import org.eclipse.swt.widgets.*;
17
18 /**
19  * This class provides default implementations to display a source image
20  * when a drag is initiated from a <code>Table</code>.
21  *
22  * <p>Classes that wish to provide their own source image for a <code>Table</code> can
23  * extend the <code>TableDragSourceEffect</code> class, override the
24  * <code>TableDragSourceEffect.dragStart</code> method and set the field
25  * <code>DragSourceEvent.image</code> with their own image.</p>
26  *
27  * Subclasses that override any methods of this class must call the corresponding
28  * <code>super</code> method to get the default drag source effect implementation.
29  *
30  * @see DragSourceEffect
31  * @see DragSourceEvent
32  *
33  * @since 3.3
34  */

35 public class TableDragSourceEffect extends DragSourceEffect {
36     Image dragSourceImage = null;
37     
38     /**
39      * Creates a new <code>TableDragSourceEffect</code> to handle drag effect
40      * from the specified <code>Table</code>.
41      *
42      * @param table the <code>Table</code> that the user clicks on to initiate the drag
43      */

44     public TableDragSourceEffect(Table table) {
45         super(table);
46     }
47
48     /**
49      * This implementation of <code>dragFinished</code> disposes the image
50      * that was created in <code>TableDragSourceEffect.dragStart</code>.
51      *
52      * Subclasses that override this method should call <code>super.dragFinished(event)</code>
53      * to dispose the image in the default implementation.
54      *
55      * @param event the information associated with the drag finished event
56      */

57     public void dragFinished(DragSourceEvent event) {
58         if (dragSourceImage != null) dragSourceImage.dispose();
59         dragSourceImage = null;
60     }
61     
62     /**
63      * This implementation of <code>dragStart</code> will create a default
64      * image that will be used during the drag. The image should be disposed
65      * when the drag is completed in the <code>TableDragSourceEffect.dragFinished</code>
66      * method.
67      *
68      * Subclasses that override this method should call <code>super.dragStart(event)</code>
69      * to use the image from the default implementation.
70      *
71      * @param event the information associated with the drag start event
72      */

73     public void dragStart(DragSourceEvent event) {
74         event.image = getDragSourceImage(event);
75     }
76     
77     Image getDragSourceImage(DragSourceEvent event) {
78         if (dragSourceImage != null) dragSourceImage.dispose();
79         dragSourceImage = null;
80         Table table = (Table) control;
81         TableItem[] selection = table.getSelection();
82         if (selection.length == 0) return null;
83         int tableImageList = OS.SendMessage (table.handle, OS.LVM_GETIMAGELIST, OS.LVSIL_SMALL, 0);
84         if (tableImageList != 0) {
85             int count = Math.min(selection.length, 10);
86             Rectangle bounds = selection[0].getBounds(0);
87             for (int i = 1; i < count; i++) {
88                 bounds = bounds.union(selection[i].getBounds(0));
89             }
90             int hDC = OS.GetDC(0);
91             int hDC1 = OS.CreateCompatibleDC(hDC);
92             if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) {
93                 if ((table.getStyle() & SWT.RIGHT_TO_LEFT) != 0) {
94                     OS.SetLayout(hDC1, OS.LAYOUT_RTL | OS.LAYOUT_BITMAPORIENTATIONPRESERVED);
95                 }
96             }
97             int bitmap = OS.CreateCompatibleBitmap(hDC, bounds.width, bounds.height);
98             int hOldBitmap = OS.SelectObject(hDC1, bitmap);
99             RECT rect = new RECT();
100             rect.right = bounds.width;
101             rect.bottom = bounds.height;
102             int hBrush = OS.GetStockObject(OS.WHITE_BRUSH);
103             OS.FillRect(hDC1, rect, hBrush);
104             for (int i = 0; i < count; i++) {
105                 TableItem selected = selection[i];
106                 Rectangle cell = selected.getBounds(0);
107                 POINT pt = new POINT();
108                 int imageList = OS.SendMessage (table.handle, OS.LVM_CREATEDRAGIMAGE, table.indexOf(selected), pt);
109                 OS.ImageList_Draw(imageList, 0, hDC1, cell.x - bounds.x, cell.y - bounds.y, OS.ILD_SELECTED);
110                 OS.ImageList_Destroy(imageList);
111             }
112             OS.SelectObject(hDC1, hOldBitmap);
113             OS.DeleteDC (hDC1);
114             OS.ReleaseDC (0, hDC);
115             Display display = table.getDisplay();
116             dragSourceImage = Image.win32_new(display, SWT.BITMAP, bitmap);
117             return dragSourceImage;
118         }
119         return null;
120     }
121 }
122
Popular Tags