KickJava   Java API By Example, From Geeks To Geeks.

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


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>Tree</code>.
21  *
22  * <p>Classes that wish to provide their own source image for a <code>Tree</code> can
23  * extend <code>TreeDragSourceEffect</code> class and override the <code>TreeDragSourceEffect.dragStart</code>
24  * method and set the field <code>DragSourceEvent.image</code> with their own image.</p>
25  *
26  * Subclasses that override any methods of this class must call the corresponding
27  * <code>super</code> method to get the default drag under effect implementation.
28  *
29  * @see DragSourceEffect
30  * @see DragSourceEvent
31  *
32  * @since 3.3
33  */

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

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

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

72     public void dragStart(DragSourceEvent event) {
73         event.image = getDragSourceImage(event);
74     }
75
76     Image getDragSourceImage(DragSourceEvent event) {
77         if (dragSourceImage != null) dragSourceImage.dispose();
78         dragSourceImage = null;
79         Tree tree = (Tree) control;
80         TreeItem[] selection = tree.getSelection();
81         if (selection.length == 0) return null;
82         int treeImageList = OS.SendMessage (tree.handle, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0);
83         if (treeImageList != 0) {
84             int count = Math.min(selection.length, 10);
85             Rectangle bounds = selection[0].getBounds(0);
86             for (int i = 1; i < count; i++) {
87                 bounds = bounds.union(selection[i].getBounds(0));
88             }
89             int hDC = OS.GetDC(tree.handle);
90             int hDC1 = OS.CreateCompatibleDC(hDC);
91             int bitmap = OS.CreateCompatibleBitmap(hDC, bounds.width, bounds.height);
92             int hOldBitmap = OS.SelectObject(hDC1, bitmap);
93             RECT rect = new RECT();
94             rect.right = bounds.width;
95             rect.bottom = bounds.height;
96             int hBrush = OS.GetStockObject(OS.WHITE_BRUSH);
97             OS.FillRect(hDC1, rect, hBrush);
98             for (int i = 0; i < count; i++) {
99                 TreeItem selected = selection[i];
100                 Rectangle cell = selected.getBounds(0);
101                 int imageList = OS.SendMessage(tree.handle, OS.TVM_CREATEDRAGIMAGE, 0, selected.handle);
102                 OS.ImageList_Draw(imageList, 0, hDC1, cell.x - bounds.x, cell.y - bounds.y, OS.ILD_SELECTED);
103                 OS.ImageList_Destroy(imageList);
104             }
105             OS.SelectObject(hDC1, hOldBitmap);
106             OS.DeleteDC (hDC1);
107             OS.ReleaseDC (tree.handle, hDC);
108             Display display = tree.getDisplay();
109             dragSourceImage = Image.win32_new(display, SWT.BITMAP, bitmap);
110             return dragSourceImage;
111         }
112         return null;
113     }
114 }
115
Popular Tags