I am Trying to add a dropListener so I can Drop and text into a draw2d Label ,in GEf Editor , Can anyone help how Can I do that. An example will be great.
1 Answers
To respond to drop events on a GEF edit part viewer you have to install on the viewer itself an implementation of org.eclipse.jface.util.TransferDropTargetListener
that understands transfers of type org.eclipse.swt.dnd.TextTransfer
and that creates some kind of org.eclipse.gef.Request
that can be handled by an org.eclipse.gef.EditPolicy
installed on the target org.eclipse.gef.EditPart
.
You have to understand that both the Request
and the EditPolicy
allow you to customize the drop behavior on a EditPart
basis. As a consequence, I can show you an example that is actually fully functional, but feel free to customize it to your real needs.
First create the TransferDropTargetListener
:
public class TextTransferDropTargetListener extends AbstractTransferDropTargetListener {
public TextTransferDropTargetListener(EditPartViewer viewer) {
super(viewer, TextTransfer.getInstance());
}
@Override
protected void handleDragOver() {
getCurrentEvent().feedback = DND.FEEDBACK_SCROLL | DND.FEEDBACK_EXPAND;
super.handleDragOver();
}
@Override
protected Request createTargetRequest() {
return new ChangeBoundsRequest(REQ_ADD);
}
@Override
protected void updateTargetRequest() {
ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
request.setEditParts(Collections.EMPTY_LIST);
request.setLocation(getDropLocation());
}
@Override
protected void handleDrop() {
super.handleDrop();
if (getCurrentEvent().detail != DND.DROP_NONE) {
getViewer().setSelection(StructuredSelection.EMPTY);
getViewer().getControl().setFocus();
}
}
@Override
protected Command getCommand() {
String text = (String) getCurrentEvent().data;
List<IEntityPart> editParts = new ArrayList<IEntityPart>();
//
// using the 'text' variable you have to create
// a new EditPart that would eventually replace the old one.
//
editParts.add(createNewLabelPart());
ChangeBoundsRequest request = (ChangeBoundsRequest) getTargetRequest();
request.setEditParts(editParts);
return super.getCommand();
}
}
then install the listener in the graphical viewer constructor using the following statement:
addDropTargetListener(new TextTransferDropTargetListener(this));
finally ensure that an EditPolicy
that understands requests of type REQ_ADD
(maybe you already added one that extends LayoutEditPolicy
or ContainerEditPolicy
) is installed on the target EditPart
, which is usually done in the AbstractEditPart.createEditPolicies()
.
To better understand the chain of responsibilities, I suggest you to have a look at the super implementation of the TransferDropTargetListener.getCommand()
method.

- 176
- 1
- 3