KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > util > ReplaceDragHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.ui.internal.presentations.util;
12
13 import org.eclipse.jface.util.Geometry;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.graphics.Rectangle;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.ui.internal.dnd.DragUtil;
18 import org.eclipse.ui.presentations.StackDropResult;
19
20 /**
21  * @since 3.0
22  */

23 public class ReplaceDragHandler extends TabDragHandler {
24
25     private final class DragCookie {
26         int insertPosition;
27
28         public DragCookie(int pos) {
29             insertPosition = pos;
30         }
31     }
32
33     private AbstractTabFolder tabFolder;
34
35     public ReplaceDragHandler(AbstractTabFolder folder) {
36         this.tabFolder = folder;
37     }
38     
39     /* (non-Javadoc)
40      * @see org.eclipse.ui.internal.presentations.util.TabDragHandler#dragOver(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.Point)
41      */

42     public StackDropResult dragOver(Control currentControl, Point location,
43             int dragStart) {
44
45         // Determine which tab we're currently dragging over
46
//Point localPos = tabFolder.getControl().toControl(location);
47

48         AbstractTabItem tabUnderPointer = tabFolder.getItem(location);
49
50         // This drop target only deals with tabs... if we're not dragging over
51
// a tab, exit.
52
if (tabUnderPointer == null) {
53             Rectangle titleArea = tabFolder.getTabArea();
54
55             // If we're dragging over the title area, treat this as a drop in the last
56
// tab position.
57
if (titleArea.contains(location) && tabFolder.getItemCount() > 0) {
58                 int dragOverIndex = tabFolder.getItemCount();
59                 AbstractTabItem lastTab = tabFolder.getItem(dragOverIndex - 1);
60
61                 // Can't drag to end unless you can see the end
62
if (!lastTab.isShowing()) {
63                     return null;
64                 }
65                 
66                 // If we are unable to compute the bounds for this tab, then ignore the drop
67
Rectangle lastTabBounds = lastTab.getBounds();
68                 if (lastTabBounds.isEmpty()) {
69                     return null;
70                 }
71
72                 if (dragStart >= 0) {
73                     dragOverIndex--;
74
75                     return new StackDropResult(lastTabBounds, new Integer JavaDoc(
76                             dragOverIndex));
77                 }
78
79                 // Make the drag-over rectangle look like a tab at the end of the tab region.
80
// We don't actually know how wide the tab will be when it's dropped, so just
81
// make it 3 times wider than it is tall.
82
Rectangle dropRectangle = titleArea;
83
84                 dropRectangle.x = lastTabBounds.x + lastTabBounds.width;
85                 dropRectangle.width = 3 * dropRectangle.height;
86                 return new StackDropResult(dropRectangle, new Integer JavaDoc(
87                         dragOverIndex));
88
89             } else {
90                 // If the closest side is the side with the tabs, consider this a stack operation.
91
// Otherwise, let the drop fall through to whatever the default behavior is
92
Rectangle displayBounds = DragUtil.getDisplayBounds(tabFolder.getControl());
93                 int closestSide = Geometry.getClosestSide(displayBounds, location);
94                 if (closestSide == tabFolder.getTabPosition()) {
95                     return new StackDropResult(displayBounds, null);
96                 }
97                 
98                 return null;
99             }
100         }
101
102         if (!tabUnderPointer.isShowing()) {
103             return null;
104         }
105         
106         Rectangle tabBounds = tabUnderPointer.getBounds();
107         
108         if (tabBounds.isEmpty()) {
109             return null;
110         }
111
112         return new StackDropResult(tabBounds, new DragCookie(tabFolder
113                 .indexOf(tabUnderPointer)));
114     }
115
116     /* (non-Javadoc)
117      * @see org.eclipse.ui.internal.presentations.util.TabDragHandler#getInsertionPosition(java.lang.Object)
118      */

119     public int getInsertionPosition(Object JavaDoc cookie) {
120         if (cookie instanceof DragCookie) {
121             return Math.min(tabFolder.getItemCount(),
122                     ((DragCookie) cookie).insertPosition);
123         }
124
125         return tabFolder.getItemCount();
126     }
127
128 }
129
Popular Tags