001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.presentations.util;
011:
012: import org.eclipse.jface.util.Geometry;
013: import org.eclipse.swt.graphics.Point;
014: import org.eclipse.swt.graphics.Rectangle;
015: import org.eclipse.swt.widgets.Control;
016: import org.eclipse.ui.internal.dnd.DragUtil;
017: import org.eclipse.ui.presentations.StackDropResult;
018:
019: /**
020: * @since 3.0
021: */
022: public class ReplaceDragHandler extends TabDragHandler {
023:
024: private final class DragCookie {
025: int insertPosition;
026:
027: public DragCookie(int pos) {
028: insertPosition = pos;
029: }
030: }
031:
032: private AbstractTabFolder tabFolder;
033:
034: public ReplaceDragHandler(AbstractTabFolder folder) {
035: this .tabFolder = folder;
036: }
037:
038: /* (non-Javadoc)
039: * @see org.eclipse.ui.internal.presentations.util.TabDragHandler#dragOver(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.Point)
040: */
041: public StackDropResult dragOver(Control currentControl,
042: Point location, int dragStart) {
043:
044: // Determine which tab we're currently dragging over
045: //Point localPos = tabFolder.getControl().toControl(location);
046:
047: AbstractTabItem tabUnderPointer = tabFolder.getItem(location);
048:
049: // This drop target only deals with tabs... if we're not dragging over
050: // a tab, exit.
051: if (tabUnderPointer == null) {
052: Rectangle titleArea = tabFolder.getTabArea();
053:
054: // If we're dragging over the title area, treat this as a drop in the last
055: // tab position.
056: if (titleArea.contains(location)
057: && tabFolder.getItemCount() > 0) {
058: int dragOverIndex = tabFolder.getItemCount();
059: AbstractTabItem lastTab = tabFolder
060: .getItem(dragOverIndex - 1);
061:
062: // Can't drag to end unless you can see the end
063: if (!lastTab.isShowing()) {
064: return null;
065: }
066:
067: // If we are unable to compute the bounds for this tab, then ignore the drop
068: Rectangle lastTabBounds = lastTab.getBounds();
069: if (lastTabBounds.isEmpty()) {
070: return null;
071: }
072:
073: if (dragStart >= 0) {
074: dragOverIndex--;
075:
076: return new StackDropResult(lastTabBounds,
077: new Integer(dragOverIndex));
078: }
079:
080: // Make the drag-over rectangle look like a tab at the end of the tab region.
081: // We don't actually know how wide the tab will be when it's dropped, so just
082: // make it 3 times wider than it is tall.
083: Rectangle dropRectangle = titleArea;
084:
085: dropRectangle.x = lastTabBounds.x + lastTabBounds.width;
086: dropRectangle.width = 3 * dropRectangle.height;
087: return new StackDropResult(dropRectangle, new Integer(
088: dragOverIndex));
089:
090: } else {
091: // If the closest side is the side with the tabs, consider this a stack operation.
092: // Otherwise, let the drop fall through to whatever the default behavior is
093: Rectangle displayBounds = DragUtil
094: .getDisplayBounds(tabFolder.getControl());
095: int closestSide = Geometry.getClosestSide(
096: displayBounds, location);
097: if (closestSide == tabFolder.getTabPosition()) {
098: return new StackDropResult(displayBounds, null);
099: }
100:
101: return null;
102: }
103: }
104:
105: if (!tabUnderPointer.isShowing()) {
106: return null;
107: }
108:
109: Rectangle tabBounds = tabUnderPointer.getBounds();
110:
111: if (tabBounds.isEmpty()) {
112: return null;
113: }
114:
115: return new StackDropResult(tabBounds, new DragCookie(tabFolder
116: .indexOf(tabUnderPointer)));
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.ui.internal.presentations.util.TabDragHandler#getInsertionPosition(java.lang.Object)
121: */
122: public int getInsertionPosition(Object cookie) {
123: if (cookie instanceof DragCookie) {
124: return Math.min(tabFolder.getItemCount(),
125: ((DragCookie) cookie).insertPosition);
126: }
127:
128: return tabFolder.getItemCount();
129: }
130:
131: }
|