|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 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 |
|
| 12 |
package org.eclipse.e4.ui.workbench.addons.dndaddon; |
| 13 |
|
| 14 |
import org.eclipse.e4.ui.model.application.ui.MUIElement; |
| 15 |
import org.eclipse.e4.ui.model.application.ui.SideValue; |
| 16 |
import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar; |
| 17 |
import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement; |
| 18 |
import org.eclipse.e4.ui.model.application.ui.menu.MToolBar; |
| 19 |
import org.eclipse.swt.SWT; |
| 20 |
import org.eclipse.swt.events.DisposeEvent; |
| 21 |
import org.eclipse.swt.events.DisposeListener; |
| 22 |
import org.eclipse.swt.graphics.Cursor; |
| 23 |
import org.eclipse.swt.graphics.Point; |
| 24 |
import org.eclipse.swt.graphics.Rectangle; |
| 25 |
import org.eclipse.swt.layout.FillLayout; |
| 26 |
import org.eclipse.swt.widgets.Composite; |
| 27 |
import org.eclipse.swt.widgets.Control; |
| 28 |
import org.eclipse.swt.widgets.Display; |
| 29 |
import org.eclipse.swt.widgets.Shell; |
| 30 |
|
| 31 |
/** |
| 32 |
* |
| 33 |
*/ |
| 34 |
public class ToolbarDropAgent extends DropAgent { |
| 35 |
private Shell dragShell; |
| 36 |
|
| 37 |
public ToolbarDropAgent() { |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public boolean canDrop(MUIElement dragElement, CursorInfo info) { |
| 42 |
if (!(dragElement instanceof MToolBar) || info.curElement == null) |
| 43 |
return false; |
| 44 |
|
| 45 |
// We can only drop TB's onto a trim area |
| 46 |
if (info.curElement instanceof MTrimBar) |
| 47 |
return true; |
| 48 |
|
| 49 |
MUIElement parentElement = info.curElement.getParent(); |
| 50 |
if (parentElement instanceof MTrimBar) |
| 51 |
return true; |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public boolean drop(MUIElement dragElement, CursorInfo info) { |
| 58 |
MTrimBar theBar = (MTrimBar) ((info.curElement instanceof MTrimBar) ? info.curElement |
| 59 |
: info.curElement.getParent()); |
| 60 |
|
| 61 |
// If we're over another trim element should we go before or after it ? |
| 62 |
int index = -1; |
| 63 |
if (info.curElement != theBar) { |
| 64 |
index = info.curElement.getParent().getChildren().indexOf(info.curElement); |
| 65 |
if (after(info)) |
| 66 |
index++; |
| 67 |
} |
| 68 |
|
| 69 |
dragElement.getParent().getChildren().remove(dragElement); |
| 70 |
dragElement.setVisible(true); |
| 71 |
if (index == -1 || index > theBar.getChildren().size() - 1) |
| 72 |
theBar.getChildren().add((MTrimElement) dragElement); |
| 73 |
else |
| 74 |
theBar.getChildren().add(index, (MTrimElement) dragElement); |
| 75 |
|
| 76 |
if (dragShell != null) |
| 77 |
dragShell.dispose(); |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
private boolean after(CursorInfo info) { |
| 83 |
if (info.curElement instanceof MTrimBar) |
| 84 |
return false; |
| 85 |
|
| 86 |
MTrimBar theBar = (MTrimBar) ((MUIElement) info.curElement.getParent()); |
| 87 |
boolean isHorizontal = theBar.getSide() == SideValue.TOP |
| 88 |
|| theBar.getSide() == SideValue.BOTTOM; |
| 89 |
|
| 90 |
MUIElement overElement = info.curElement; |
| 91 |
Control ctrl = (Control) overElement.getWidget(); |
| 92 |
Rectangle bounds = ctrl.getBounds(); |
| 93 |
bounds = ctrl.getDisplay().map(ctrl.getParent(), null, bounds); |
| 94 |
|
| 95 |
if (isHorizontal |
| 96 |
&& info.cursorPos.x - bounds.x >= (bounds.x + bounds.width) - info.cursorPos.x) |
| 97 |
return true; |
| 98 |
else if (!isHorizontal |
| 99 |
&& info.cursorPos.y - bounds.y >= (bounds.y + bounds.height) - info.cursorPos.y) |
| 100 |
return true; |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public Rectangle getRectangle(MUIElement dragElement, CursorInfo info) { |
| 107 |
boolean needsOpen = false; |
| 108 |
if (dragShell == null) { |
| 109 |
dragShell = new Shell(SWT.NO_TRIM); |
| 110 |
dragShell.setAlpha(150); |
| 111 |
dragShell.setLayout(new FillLayout()); |
| 112 |
|
| 113 |
Composite comp = new Composite(dragShell, SWT.BORDER); |
| 114 |
comp.setLayout(new FillLayout()); |
| 115 |
|
| 116 |
dragShell.addDisposeListener(new DisposeListener() { |
| 117 |
public void widgetDisposed(DisposeEvent e) { |
| 118 |
dragShell = null; |
| 119 |
} |
| 120 |
}); |
| 121 |
Control ctrl = (Control) dragElement.getWidget(); |
| 122 |
ctrl.setParent(comp); |
| 123 |
dragShell.pack(); |
| 124 |
needsOpen = true; |
| 125 |
} |
| 126 |
|
| 127 |
Point newLoc = new Point(info.cursorPos.x + 4, info.cursorPos.y + 4); |
| 128 |
dragShell.setLocation(newLoc); |
| 129 |
|
| 130 |
if (needsOpen) |
| 131 |
dragShell.open(); |
| 132 |
|
| 133 |
MUIElement overElement = info.curElement; |
| 134 |
Control ctrl = (Control) overElement.getWidget(); |
| 135 |
Rectangle bounds = ctrl.getBounds(); |
| 136 |
bounds = ctrl.getDisplay().map(ctrl.getParent(), null, bounds); |
| 137 |
bounds.x = after(info) ? bounds.x + bounds.width : bounds.x; |
| 138 |
bounds.width = 5; |
| 139 |
bounds.x -= bounds.width / 2; |
| 140 |
|
| 141 |
return bounds; |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public Cursor getCursor(Display display, MUIElement dragElement, CursorInfo info) { |
| 146 |
return display.getSystemCursor(SWT.CURSOR_HAND); |
| 147 |
} |
| 148 |
} |