/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
*/
public void handleEvent(Event event) {
ToolBar toolbar = perspectiveBar.getControl();
ToolItem item = toolbar.getItem(new Point(event.x, event.y));
if (item != null) {
//ignore the first item, which remains in position Zero
if (item.getData() instanceof PerspectiveBarNewContributionItem) {
return;
}
Rectangle bounds = item.getBounds();
Rectangle parentBounds = toolbar.getBounds();
bounds.x += parentBounds.x;
bounds.y += parentBounds.y;
startDragging(item.getData(), toolbar.getDisplay().map(toolbar, null, bounds));
} else {
//startDragging(toolbar, toolbar.getDisplay().map(toolbar, null, toolbar.getBounds()));
}
}
private void startDragging(Object widget, Rectangle bounds) {
if(!DragUtil.performDrag(widget, bounds, new Point(bounds.x, bounds.y), true)) {
//currently do nothing on a failed drag
}
}
};
dragTarget = new IDragOverListener() {
protected PerspectiveDropTarget perspectiveDropTarget;
class PerspectiveDropTarget extends AbstractDropTarget {
private PerspectiveBarContributionItem perspective;
private Point location;
/**
* @param location
* @param draggedObject
*/
public PerspectiveDropTarget(Object draggedObject,
Point location) {
update(draggedObject, location);
}
/**
*
* @param draggedObject
* @param location
*/
private void update(Object draggedObject, Point location) {
this.location = location;
this.perspective = (PerspectiveBarContributionItem) draggedObject;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.internal.dnd.IDropTarget#drop()
*/
public void drop() {
ToolBar toolBar = perspectiveBar.getControl();
ToolItem item = toolBar.getItem(toolBar.getDisplay().map(
null, toolBar, location));
if (toolBar.getItem(0) == item) {
return;
}
ToolItem[] items = toolBar.getItems();
ToolItem droppedItem = null;
int dropIndex = -1;
for (int i = 0; i < items.length; i++) {
if (item == items[i]) {
dropIndex = i;
}
if (items[i].getData() == perspective) {
droppedItem = items[i];
}
}
if (dropIndex != -1 && droppedItem != null && (droppedItem != item)) {
PerspectiveBarContributionItem barItem = (PerspectiveBarContributionItem) droppedItem.getData();
// policy is to insert at the beginning so mirror the value when indicating a
// new position for the perspective
if (reorderListener != null) {
reorderListener.reorder(barItem.getPerspective(), Math.abs(dropIndex - (items.length - 1)));
}
perspectiveBar.relocate(barItem, dropIndex);
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.internal.dnd.IDropTarget#getCursor()
*/
public Cursor getCursor() {
return DragCursors.getCursor(DragCursors.CENTER);
}
boolean sameShell() {
return perspective.getToolItem().getParent().getShell().equals(perspectiveBar.getControl().getShell());
}
public Rectangle getSnapRectangle() {
ToolBar toolBar = perspectiveBar.getControl();
ToolItem item = toolBar.getItem(toolBar.getDisplay().map(
null, toolBar, location));
Rectangle bounds;
if (item != null && item != toolBar.getItem(0)) {
bounds = item.getBounds();
} else {
// it should not be possible to start a drag with item 0
return null;
}
return toolBar.getDisplay().map(toolBar, null, bounds);