Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Detect drop location from DND event(Is it possible to detect where an object was dropped at after a drag event?)
Detect drop location from DND event [message #716387] Wed, 17 August 2011 09:19 Go to next message
werner is currently offline werner
Messages: 6
Registered: August 2011
Junior Member
Hello,

perhaps I am just blind, but after searching the API and in the forum, I still have no clue, how I can detect where an object was dropped to from an SWT widget like a TreeViewer.

I have the following code:
// Enable Drag & Drop Support
int ops = DND.DROP_COPY;
Transfer[] transfers = new Transfer[] { TextTransfer.getInstance() };
treeViewer.addDragSupport(ops, transfers, myListener);

Where SourceDragListener is an own implementation of DragSourceListener. What I want to achieve is, that if someone drags something from the treeViewer object to the Java editor of Eclipse (and only then) some actions are invoked (like formatting the code). Therefore, I need to find out if the item was dropped to the Java Editor of the current Eclipse instance (and not to any arbitrary text editor or other widget that accepts a TextTransfer.

Is there a possibility to achieve that? In the "event" object in the DragSourceListener I can't find such an information unfortunately.

Thanks.
Re: Detect drop location from DND event [message #716691 is a reply to message #716387] Thu, 18 August 2011 06:12 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
May be this would help...
http://www.eclipse.org/articles/Article-Workbench-DND/drag_drop.html


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Detect drop location from DND event [message #720793 is a reply to message #716691] Wed, 31 August 2011 08:54 Go to previous messageGo to next message
werner is currently offline werner
Messages: 6
Registered: August 2011
Junior Member
.

[Updated on: Wed, 31 August 2011 08:56]

Report message to a moderator

(no subject) [message #720794 is a reply to message #716691] Wed, 31 August 2011 08:54 Go to previous messageGo to next message
werner is currently offline werner
Messages: 1
Registered: July 2009
Junior Member
Hi!

Thanks -- but I don't get it even with that information.

I've described my problem in particular at this point:

www.eclipse.org/forums/index.php/m/720450/?srch=compilationuniteditor+drop#msg_720450

I quote it here as well:

--- snipp ---
unfortunately this doesn't work for me on Indigo. My code is:


public void partActivated(final IWorkbenchPartReference partRef) {
if (partRef.getId().equals("org.eclipse.jdt.ui.CompilationUnitEditor")) {
IEditorPart editor = PluginUI.getActiveEditor();
Control ctrl = (Control) editor.getAdapter(Control.class);
if (ctrl != null) {
logger.debug("Add drop listener to " + partRef.getTitle());
int operations = DND.DROP_COPY;
DropTarget dropTarget = new DropTarget(ctrl, operations);
final TextTransfer textTransfer = TextTransfer.getInstance();
Transfer[] types = new Transfer[] { textTransfer };
dropTarget.setTransfer(types);
dropTarget.addDropListener(new DropTargetListener() {

@Override
public void dropAccept(DropTargetEvent event) {
// TODO Auto-generated method stub

}

@Override
public void drop(DropTargetEvent event) {
System.out.println("Dropped " + event.detail);
}

@Override
public void dragOver(DropTargetEvent event) {
// TODO Auto-generated method stub

}

@Override
public void dragOperationChanged(DropTargetEvent event) {
// TODO Auto-generated method stub

}

@Override
public void dragLeave(DropTargetEvent event) {
// TODO Auto-generated method stub

}

@Override
public void dragEnter(DropTargetEvent event) {
// TODO Auto-generated method stub

}
});
}
}


Unfortunately, this leads to the following error message right away when starting an Eclipse runtime workspace with the plugin. The runtime Eclipse crashes and stops:


DEBUG ResultView: Add drop listener to Calculator.java
org.eclipse.jface.viewers.TreeViewer@4232d08f

!ENTRY org.eclipse.osgi 4 0 2011-08-30 17:28:05.282
!MESSAGE Application error
!STACK 1
org.eclipse.swt.SWTError: Cannot initialize Drop
at org.eclipse.swt.dnd.DND.error(DND.java:266)
at org.eclipse.swt.dnd.DND.error(DND.java:227)
at org.eclipse.swt.dnd.DropTarget.<init>(DropTarget.java:135)
my class, the line with new DropTarget... --> at my.project.ResultView.partActivated(ResultView.java:268)
at org.eclipse.ui.internal.PartListenerList2$1.run(PartListenerList2.java:70)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.runtime.Platform.run(Platform.java:888)
at org.eclipse.ui.internal.PartListenerList2.fireEvent(PartListenerList2.java:55)
at org.eclipse.ui.internal.PartListenerList2.firePartActivated(PartListenerList2.java:68)
at org.eclipse.ui.internal.PartService.firePartActivated(PartService.java:192)
at org.eclipse.ui.internal.PartService.access$0(PartService.java:177)
at org.eclipse.ui.internal.PartService$PartActivated.fire(PartService.java:100)
at org.eclipse.ui.internal.PartService.processPartJobs(PartService.java:161)
at org.eclipse.ui.internal.PartService$PartListener.handleEvent(PartService.java:88)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1058)
at org.eclipse.swt.widgets.Shell.setActiveControl(Shell.java:1447)
at org.eclipse.swt.widgets.Control.sendFocusEvent(Control.java:2836)
at org.eclipse.swt.widgets.Widget.wmSetFocus(Widget.java:2403)
at org.eclipse.swt.widgets.Control.WM_SETFOCUS(Control.java:5152)
at org.eclipse.swt.widgets.Canvas.WM_SETFOCUS(Canvas.java:448)
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4598)
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4972)
at org.eclipse.swt.internal.win32.OS.SetFocus(Native Method)
at org.eclipse.swt.widgets.Control.forceFocus(Control.java:1098)
at org.eclipse.swt.widgets.Control.setSavedFocus(Control.java:3599)
at org.eclipse.swt.widgets.Decorations.restoreFocus(Decorations.java:821)
at org.eclipse.swt.widgets.Shell.open(Shell.java:1248)
at org.eclipse.jface.window.Window.open(Window.java:797)
at org.eclipse.ui.internal.WorkbenchWindow.open(WorkbenchWindow.java:818)
at org.eclipse.ui.internal.Workbench$70.runWithException(Workbench.java:3719)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
at org.eclipse.ui.internal.Workbench$33.runWithException(Workbench.java:1595)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)


Any suggestions / ideas what may be wrong?

In fact, I have a TypeDeclaration or MethodDeclaration in a view that I want to drop into the Java Editor and I need the drop action to perform code formatting after insertion and if a method is duplicate to remove the older one. Alternatively I could live with dropping it into the OutlineView but that doesn't work either.
--- snipp ---

Any clue?
Solution [message #720927 is a reply to message #720794] Wed, 31 August 2011 13:52 Go to previous messageGo to next message
werner is currently offline werner
Messages: 6
Registered: August 2011
Junior Member
Hello!

Okay -- after reading some Eclipse sourcecode and crawling through the mud I found a solution to my problem. For adding a drop listener to the active workbench editor, one must execute the following steps:

IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
Control ctrl = (Control) editor.getAdapter(Control.class);
DropTarget dropTarget = (DropTarget) ctrl.getData(DND.DROP_TARGET_KEY);


This gives access to the DropTarget object of the editor. Then one can add an own listener to drop events:

dropTarget.addDropListener(new DropTargetListener() {...}


@Eclipse/SWT Devs: Anyhow I consider that a bit dirty since it is not very obvious for a developer why this way has to be chosen. A simple editor.getDropTarget() would have been very helpful.

Cheers!
Re: Solution [message #721562 is a reply to message #720927] Fri, 02 September 2011 06:27 Go to previous messageGo to next message
Vijay Raj
Messages: 608
Registered: July 2009
Senior Member
Yes it is a dirty solution,It would not have been that straight forward due to plugin environement contraints and/or Observer pattern restrictions....

Did you check out the "Plugin drop handling" section of the link

http://www.eclipse.org/articles/Article-Workbench-DND/drag_drop.html

This would enable you to deligate the drop handling to your source(treeviwer).


---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
Re: Solution [message #721571 is a reply to message #721562] Fri, 02 September 2011 06:46 Go to previous message
werner is currently offline werner
Messages: 6
Registered: August 2011
Junior Member
Hi!

It did not work out in my case. Since I want to transfer BodyDeclarations I had to write my own Transfer type anyhow and add it to the CompilationUnitEditor; thus the solution I found works well for me now.

Cheers!
Previous Topic:Layout problem when resizing shell
Next Topic:How to use Progress-Bar widget in eclispe rcp
Goto Forum:
  


Current Time: Sun Oct 26 03:55:57 GMT 2014

Powered by FUDForum. Page generated in 0.01761 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software