Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » DnD
DnD [message #501135] Tue, 01 December 2009 08:54
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
Hi all,

I have mixed CNF and GMF to make a multi-view editor and I am still
looking for a way to use DnD. I tried to reproduce the same approach as
in the ecoretool editor but it does'nt work for me. Plus, I am not able
to use debug because at the moment I use DnD during debug, my eclipse
freezes. I have already read other related posts but I have not found
any satisfying solution. Here is the way I have proceeded:

1. I create a private inner class extending DiagramDropTargetListener in
my editor:

private abstract class DropTargetListener extends DiagramDropTargetListener

public DropTargetListener(EditPartViewer viewer, Transfer xfer) {
super(viewer, xfer);

protected List<?> getObjectsBeingDropped() {
TransferData data = getCurrentEvent().currentDataType;
Object transferedObject = getJavaObject(data);
if (!(transferedObject instanceof IStructuredSelection) ||
((IStructuredSelection)transferedObject).size() != 1)
return Collections.EMPTY_LIST;
IStructuredSelection selection = (IStructuredSelection)transferedObject;
Object element = selection.getFirstElement();
if (!(element instanceof Element)) return Collections.EMPTY_LIST;
return selection.toList();

protected Request createTargetRequest() {
DropObjectsRequest req = new DropObjectsRequest();
return req;

public boolean isEnabled(DropTargetEvent event) {
Object o = getJavaObject(event.currentDataType);
if (o instanceof IStructuredSelection &&
((IStructuredSelection)o).getFirstElement() instanceof Element)
return true;
return false;

protected abstract Object getJavaObject(TransferData data);


2. I add Target Listeners in the initializeGraphicalViewer method of my
graphical editor:

protected void initializeGraphicalViewer() {
LocalSelectionTransfer.getTransfer()) {

protected Object getJavaObject(TransferData data) {
return LocalSelectionTransfer.getTransfer().nativeToJava(data);
LocalTransfer.getInstance()) {

protected Object getJavaObject(TransferData data) {
return LocalTransfer.getInstance().nativeToJava(data);

3. I create a DnD policy:

public class IntentionDragDropEditPolicy extends DiagramDragDropEditPolicy {

public Command getDropObjectsCommand(DropObjectsRequest request) {
List<?> objects = request.getObjects();
if (objects.size() != 1) return UnexecutableCommand.INSTANCE;
//TODO: check element in not already in view
List<ViewDescriptor> descriptors = new ArrayList<ViewDescriptor>();
Object element = objects.get(0);
if (element != null && !getHostView().getElement().equals(element))
descriptors.add(new CreateViewRequest.ViewDescriptor(new
EObjectAdapter((EObject)element), Node.class, null,
Command res = null;
if (!descriptors.isEmpty())
res = createViewsAndRestoreRelatedLinks(request, descriptors);
res.chain(new ICommandProxy(new
UpdateEditPartCommand(getEditingDomain(), getHost())));
if (res == null)
return UnexecutableCommand.INSTANCE;
return res;

private TransactionalEditingDomain getEditingDomain()
if (getHost() instanceof IGraphicalEditPart) {
return ((IGraphicalEditPart) getHost()).getEditingDomain();
return null;

private View getHostView()
return (View)(getHost().getModel());

private PreferencesHint getDiagramPreferencesHint()
return ((IGraphicalEditPart)getHost()).getDiagramPreferencesHint();

private DiagramEditPart getDiagramEditPart()
if (getHost() instanceof DiagramEditPart) {
return (DiagramEditPart) getHost();
IEditorPart editorPart =
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActi vePage().getActiveEditor();
if (false == editorPart instanceof DiagramEditor) {
return null;
return ((DiagramEditor) editorPart).getDiagramEditPart();

protected Command createViewsAndRestoreRelatedLinks(DropObjectsRequest
request, List<ViewDescriptor> descriptors)
CreateViewRequest viewRequest = new CreateViewRequest(descriptors);
Command res = getHost().getCommand(viewRequest);
if (res != null)
List l = (List) (viewRequest.getNewObject());
res.chain(new ICommandProxy(new
RestoreRelatedLinksCommand(getDiagramEditPart(), (List<?>)
ArrangeRequest ar = new
return res;


4. I add my DnD policy in the root edit part:

protected void createDefaultEditPolicies() {
fr.mopcom.modal.diagram.intentionDiagram.edit.policies.Disci plineItemSemanticEditPolicy());
fr.mopcom.modal.diagram.intentionDiagram.edit.policies.Disci plineCanonicalEditPolicy());
new IntentionDragDropEditPolicy());
//installEditPolicy(EditPolicy.LAYOUT_ROLE, new XYLayoutEditPolicy());
removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpoli cies.EditPolicyRoles.POPUPBAR_ROLE);

Still, I don't know what's going wrong. Have I missed something ?
Thx for your help,
Best regards.
Previous Topic:Modify creation tool: Create element in conteiner of its container
Next Topic:Add a SubElement to an new created Element
Goto Forum:

Current Time: Mon Apr 19 01:01:56 GMT 2021

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

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

Back to the top