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);
}

@Override
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();
}

@Override
protected Request createTargetRequest() {
DropObjectsRequest req = new DropObjectsRequest();
req.setObjects(getObjectsBeingDropped());
req.setAllowedDetail(getCurrentEvent().operations);
return req;
}

@Override
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:

@Override
protected void initializeGraphicalViewer() {
super.initializeGraphicalViewer();
getDiagramGraphicalViewer().addDropTargetListener(new
DropTargetListener(getDiagramGraphicalViewer(),
LocalSelectionTransfer.getTransfer()) {

@Override
protected Object getJavaObject(TransferData data) {
return LocalSelectionTransfer.getTransfer().nativeToJava(data);
}
});
getDiagramGraphicalViewer().addDropTargetListener(new
DropTargetListener(getDiagramGraphicalViewer(),
LocalTransfer.getInstance()) {

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

3. I create a DnD policy:


public class IntentionDragDropEditPolicy extends DiagramDragDropEditPolicy {

@Override
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>();
//System.out.println(request.getObjects().size());
Object element = objects.get(0);
//System.out.println(element.getClass().getName());
if (element != null && !getHostView().getElement().equals(element))
descriptors.add(new CreateViewRequest.ViewDescriptor(new
EObjectAdapter((EObject)element), Node.class, null,
getDiagramPreferencesHint()));
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();
}

@SuppressWarnings("unchecked")
protected Command createViewsAndRestoreRelatedLinks(DropObjectsRequest
request, List<ViewDescriptor> descriptors)
{
CreateViewRequest viewRequest = new CreateViewRequest(descriptors);
viewRequest.setLocation(request.getLocation());
Command res = getHost().getCommand(viewRequest);
if (res != null)
{
List l = (List) (viewRequest.getNewObject());
request.setResult(l);
res.chain(new ICommandProxy(new
RestoreRelatedLinksCommand(getDiagramEditPart(), (List<?>)
viewRequest.getNewObject())));
ArrangeRequest ar = new
ArrangeRequest(RequestConstants.REQ_ARRANGE_DEFERRED);
ar.setViewAdaptersToArrange(l);
res.chain(getHost().getCommand(ar));
}
return res;
}

}


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

protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(
EditPolicyRoles.SEMANTIC_ROLE,
new
fr.mopcom.modal.diagram.intentionDiagram.edit.policies.Disci plineItemSemanticEditPolicy());
installEditPolicy(
EditPolicyRoles.CANONICAL_ROLE,
new
fr.mopcom.modal.diagram.intentionDiagram.edit.policies.Disci plineCanonicalEditPolicy());
//DnD
installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE,
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