Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » How to drag-n-drop from Project Explorer into diagram?
How to drag-n-drop from Project Explorer into diagram? [message #512658] Fri, 05 February 2010 20:34 Go to next message
Christoph Wienands is currently offline Christoph WienandsFriend
Messages: 55
Registered: July 2009
Member
Hey all,

I have a diagram with non-canonical behavior, so it might only show a subset
of the elements contained in the underlying Ecore model. Now I would like to
be able to drag and drop model elements from the Project Explorer in
Resource perspective onto a diagram, e.g. when I start with an empty one.
Essentially, I drill down into Ecore models within the Project Explorer and
would like to add these elements to the diagram.

The problem is that I am missing a few pieces to get this working. When I
drag a model element from the Project Explorer, the cursor keeps indicating
that a drop onto the diagram is not allowed ('forbidden' symbol)...

In the XXXDiagramEditor I found that two DropTargetListeners are added to
the DiagramGraphicalViewer. I did some debugging and among others, the
isEnabled method on those listeners is invoked. Unfortunately it returns
false and I don't know how to properly make it return true. During the
execution of isEnabled, the method getObjectsBeingDropped is called to what
it seems translate the dropped object to something the diagram can
understand. Just as a test I hardcoded isEnabled to return true, and the
cursor during the drag operation did indeed change to 'allowed' while
hovering over the diagram but dropping would still not work (not that I
expected it).

I found two relevant postings:
http://dev.eclipse.org/newslists/news.eclipse.technology.gmf /msg01297.html
and
http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/m sg17244.html

In the first one, the author says if EObjects are returned by the
getObjectsBeingDropped method, this should work out of the box. This is
unfortunately not true, as the result list contains an XXXImpl instance,
which implements the EObject interface. So just returning an EObject will
not enable dropping model elements onto a diagram.

I then tried installing my own DiagramDragDropEditPolicy as described in the
second post in the XXXEditPart for the diagram element:

/**
* @generated NOT
*/
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(
EditPolicyRoles.SEMANTIC_ROLE,
new Simplest.diagram.edit.policies.DiagramItemSemanticEditPolicy ());
//
removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpoli cies.EditPolicyRoles.POPUPBAR_ROLE);

installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, new
MyDiagramDragDropEditPolicy());
}

During debugging I saw that the overridden method
MyDiagramDragDropEditPolicy.getDropObjectsCommand method gets invoked (I
simply call super.getDropObjectsCommand at this time) but just as before the
drag-drop cursor still showed the
'forbidden' symbol when moving it over the diagram (still the forbidden
sign).

Can anybody spot what I am missing here, or tell what the exact steps and
locations are to enable drag-n-drop support from the Project Explorer? Maybe
it is something as simple as a setting in gmfgen?

Thanks a lot, Christoph
Re: How to drag-n-drop from Project Explorer into diagram? [message #512699 is a reply to message #512658] Sat, 06 February 2010 14:20 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: kalin.nakov.gmail.com

Hi,

I had the same problem some time ago, see the original post:
http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/m sg15585.html

After lots of debugging I've realized that the drag & drop edit policies
work fine, and the fix has to be made on a different place. The problem was
that the current diagram drop target listener did not include the selected
objects from the tree viewer. Here is the implementation of my
XXXDiagramEditor.configureGraphicalViewer() method which solves the problem.

/**
* Add the tree selection to the dragged objects. Without this patch
dragging does not work
* @generated NOT
*/
protected void configureGraphicalViewer() {
super.configureGraphicalViewer();
DiagramEditorContextMenuProvider provider = new
DiagramEditorContextMenuProvider(
this, getDiagramGraphicalViewer());
getDiagramGraphicalViewer().setContextMenu(provider);
getSite().registerContextMenu(ActionIds.DIAGRAM_EDITOR_CONTE XT_MENU,
provider, getDiagramGraphicalViewer());

getDiagramGraphicalViewer().addDropTargetListener(
new DiagramDropTargetListener(getDiagramGraphicalViewer(),
LocalSelectionTransfer.getTransfer()) {
protected List getObjectsBeingDropped() {
LinkedList<EObject> objects = new LinkedList<EObject>();
for (Iterator<?> it = ((TreeSelection) LocalSelectionTransfer
.getTransfer().getSelection()).iterator(); it
.hasNext();) {
EObject obj = (EObject) ((PlatformObject) it.next())
.getAdapter(EObject.class);
objects.add(obj);
}

return objects;
}

});
}

Cheers,
Kalin

"Christoph Wienands" <cwienands@gmx.net> wrote in message
news:hkhvcc$kc1$1@build.eclipse.org...
> Hey all,
>
> I have a diagram with non-canonical behavior, so it might only show a
> subset of the elements contained in the underlying Ecore model. Now I
> would like to be able to drag and drop model elements from the Project
> Explorer in Resource perspective onto a diagram, e.g. when I start with an
> empty one. Essentially, I drill down into Ecore models within the Project
> Explorer and would like to add these elements to the diagram.
>
> The problem is that I am missing a few pieces to get this working. When I
> drag a model element from the Project Explorer, the cursor keeps
> indicating that a drop onto the diagram is not allowed ('forbidden'
> symbol)...
>
> In the XXXDiagramEditor I found that two DropTargetListeners are added to
> the DiagramGraphicalViewer. I did some debugging and among others, the
> isEnabled method on those listeners is invoked. Unfortunately it returns
> false and I don't know how to properly make it return true. During the
> execution of isEnabled, the method getObjectsBeingDropped is called to
> what it seems translate the dropped object to something the diagram can
> understand. Just as a test I hardcoded isEnabled to return true, and the
> cursor during the drag operation did indeed change to 'allowed' while
> hovering over the diagram but dropping would still not work (not that I
> expected it).
>
> I found two relevant postings:
> http://dev.eclipse.org/newslists/news.eclipse.technology.gmf /msg01297.html
> and
> http://dev.eclipse.org/newslists/news.eclipse.modeling.gmf/m sg17244.html
>
> In the first one, the author says if EObjects are returned by the
> getObjectsBeingDropped method, this should work out of the box. This is
> unfortunately not true, as the result list contains an XXXImpl instance,
> which implements the EObject interface. So just returning an EObject will
> not enable dropping model elements onto a diagram.
>
> I then tried installing my own DiagramDragDropEditPolicy as described in
> the second post in the XXXEditPart for the diagram element:
>
> /**
> * @generated NOT
> */
> protected void createDefaultEditPolicies() {
> super.createDefaultEditPolicies();
> installEditPolicy(
> EditPolicyRoles.SEMANTIC_ROLE,
> new Simplest.diagram.edit.policies.DiagramItemSemanticEditPolicy ());
> //
> removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpoli cies.EditPolicyRoles.POPUPBAR_ROLE);
>
> installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE, new
> MyDiagramDragDropEditPolicy());
> }
>
> During debugging I saw that the overridden method
> MyDiagramDragDropEditPolicy.getDropObjectsCommand method gets invoked (I
> simply call super.getDropObjectsCommand at this time) but just as before
> the drag-drop cursor still showed the
> 'forbidden' symbol when moving it over the diagram (still the forbidden
> sign).
>
> Can anybody spot what I am missing here, or tell what the exact steps and
> locations are to enable drag-n-drop support from the Project Explorer?
> Maybe it is something as simple as a setting in gmfgen?
>
> Thanks a lot, Christoph
>
>
Re: How to drag-n-drop from Project Explorer into diagram? [message #512820 is a reply to message #512699] Mon, 08 February 2010 03:33 Go to previous messageGo to next message
Christoph Wienands is currently offline Christoph WienandsFriend
Messages: 55
Registered: July 2009
Member
Hello Kalin,

> After lots of debugging I've realized that the drag & drop edit policies
> work fine, and the fix has to be made on a different place. The problem
> was that the current diagram drop target listener did not include the
> selected objects from the tree viewer. Here is the implementation of my
> XXXDiagramEditor.configureGraphicalViewer() method which solves the
> problem.

Thanks a lot for your suggestion. Unfortunately, it did not solve the
problem. During debugging I confirmed that your DiagramDropTargetListener
does return an XXXImpl object.

However, in the generated GMF code, two DiagramDropTargetListeners get added
in 'initializeGraphicalViewer' as well, and one of them returns exactly the
same result as your listener implementation. So it does not seem to be the
problem that you had.

In the meantime I tried something else, too. I configured the gmfgen fields
'contains shortcuts to' and 'shortcuts provided for' to the same file
extension as the model files for my non-canonical diagrams. Interestingly
enough, drag-drop does not even work in the case of shortcuts. And just as
before, one of the two DiagramDropTargetListeners from
'initializeGraphicalViewer' returns a list with one XXXImpl object. So the
problem truly does not seem to be related to extracting an EObject from the
drag operation, but something with deciding that this object may be dropped
onto the diagram.

Anybody else any suggestions?

Thanks, Christoph
Re: How to drag-n-drop from Project Explorer into diagram? [message #513021 is a reply to message #512820] Mon, 08 February 2010 15:29 Go to previous messageGo to next message
Christoph Wienands is currently offline Christoph WienandsFriend
Messages: 55
Registered: July 2009
Member
I think I'm getting closer to the issue. I just created a minimal GMF
language from scratch and was able to get the shortcut feature working. I
configured the following settings:
-gmfgen -> Diagram -> Diagram -> Contains Shortcuts To = [model file name]
-gmfgen -> Diagram -> Diagram -> Shortcuts Provided For = [model file name]
-gmfgen -> Navigator -> Generate Domain Model Navigator = true

In my 'real' project I am using the GmfTools (from itemis,
http://code.google.com/p/gmftools/) with dynamic templates and some other
customizations, so something must have messed up the shortcut feature.

I'll now try to do a behavioral 'diff' between the two. Assuming I get the
shortcut feature working in my other project, the remaining question will
be:

When I drag-drop elements from the underlying model file onto a
non-canonical diagram, I don't want to add those elements as shortcuts but
as regular elements. For example, problems arise when I use 'Delete from
diagram' on an element, and add it afterwards again, this time as a
shortcut. Then the 'Delete from model' feature does not work anymore and it
only deletes the shortcut from the diagram, but not the element from the
underlying model. So how can I make GMF recognize that a drag-dropped
element does not come from another model but from the underlying model, and
have it add the element as a regular element instead of as a shortcut?

Thanks a lot, Christoph
Re: How to drag-n-drop from Project Explorer into diagram? [message #513230 is a reply to message #513021] Tue, 09 February 2010 12:56 Go to previous messageGo to next message
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------030905090905050207080406
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Hi Christoph,

I had the same problems and also had no answers. By the way, after few
nights debugging, I solved my problem (see http://vimeo.com/8928764). I
am now able to drag and drop ecore elements from the project explorer
and automatically recreate links between notational elements. You 'll
find as attached document the java code source of my diagram edit part
as example. Let me know if there's something that you don't understand.

Cheers.

Le 08/02/2010 16:29, Christoph Wienands a
Re: How to drag-n-drop from Project Explorer into diagram? [message #513376 is a reply to message #513021] Tue, 09 February 2010 21:43 Go to previous messageGo to next message
Christoph Wienands is currently offline Christoph WienandsFriend
Messages: 55
Registered: July 2009
Member
So with more debugging and trying I discovered an awefully strange behavior.
For this test I started with a GMF project/model/etc. from the very scratch
and made a minimalistic example. When I started the test Eclipse app,
drag-n-drop worked wonderfully. However after restarting the test Eclipse
app, drag-n-drop would not work anymore.

I later found out that drag-n-drop will start working again after making a
change to the diagram that affects the underlying model file, such as adding
an element. For the same reason, DnD probably worked at the very beginning
when I had just created diagram and model files.

Ironically, when I went back to my own project and performed a
model-affecting change to the diagram, lo and behold DnD all of a sudden
worked. Seems like all the time I was just so focused on that part that I
never played around with the diagrams any further.

I filed a GMF bug report so that this could be checked out. Unfortunately,
I'm not deep enough in EMF/GMF to figure out a patch.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=302186

Christoph
Re: How to drag-n-drop from Project Explorer into diagram? [message #523801 is a reply to message #513230] Mon, 29 March 2010 12:54 Go to previous messageGo to next message
Ralf is currently offline RalfFriend
Messages: 25
Registered: March 2010
Junior Member
Ali Koudri wrote on Tue, 09 February 2010 07:56
You 'll find as attached document the java code source of my diagram edit part as example. Let me know if there's something that you don't understand.

Cheers.


Hi Ali,

Sadly your attached source is missing in the Eclipse GMF forum. I would also be very interested in your working example. Could you please repost your solution ?

Best regards

Ralf.
Re: How to drag-n-drop from Project Explorer into diagram? [message #523860 is a reply to message #523801] Mon, 29 March 2010 16:07 Go to previous messageGo to next message
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040101070009020500030104
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

You'll find it as attached document.
Cheers.

Le 29/03/2010 14:54, Ralf a écrit :
> Ali Koudri wrote on Tue, 09 February 2010 07:56
>> You 'll find as attached document the java code source of my diagram
>> edit part as example. Let me know if there's something that you don't
>> understand.
>>
>> Cheers.
>
>
> Hi Ali,
>
> Sadly your attached source is missing in the Eclipse GMF forum. I would
> also be very interested in your working example. Could you please repost
> your solution ?
>
> Best regards
>
> Ralf.


--------------040101070009020500030104
Content-Type: text/x-java;
name="DisciplineEditPart.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="DisciplineEditPart.java"

package fr.mopcom.modal.intentionDiagram.edit.parts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.workspace.AbstractEMFOperation;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.util.StringStatics;
import org.eclipse.gmf.runtime.diagram.core.preferences.Preferences Hint;
import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionNodeE ditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart ;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditP art;
import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPa rt;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.DiagramDragD ropEditPolicy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRo les;
import org.eclipse.gmf.runtime.diagram.ui.requests.ArrangeRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnection ViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewReques t;
import org.eclipse.gmf.runtime.diagram.ui.requests.DropObjectsReque st;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants ;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTr ansactionalCommand;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.IHintedType;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.Shape;
import org.eclipse.gmf.runtime.notation.View;

import fr.mopcom.modal.Intention;
import fr.mopcom.modal.IntentionAssociation;
import fr.mopcom.modal.IntentionUsage;
import fr.mopcom.modal.MethodologyRule;
import fr.mopcom.modal.Role;
import fr.mopcom.modal.SatisfactionLink;
import fr.mopcom.modal.ToolDefinition;

/**
* @generated
*/
public class DisciplineEditPart extends DiagramEditPart {

/**
* @generated
*/
public final static String MODEL_ID = "Intention Diagram"; //$NON-NLS-1$

/**
* @generated
*/
public static final int VISUAL_ID = 1000;

/**
* @generated
*/
public DisciplineEditPart(View view) {
super(view);
}

/**
* @generated
*/
protected void createDefaultEditPolicies() {
super.createDefaultEditPolicies();
installEditPolicy(
EditPolicyRoles.SEMANTIC_ROLE,
new fr.mopcom.modal.intentionDiagram.edit.policies.DisciplineIte mSemanticEditPolicy());
installEditPolicy(
EditPolicyRoles.CANONICAL_ROLE,
new fr.mopcom.modal.intentionDiagram.edit.policies.DisciplineCan onicalEditPolicy());

// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpoli cies.EditPolicyRoles.POPUPBAR_ROLE);
//DnD
installEditPolicy(EditPolicyRoles.DRAG_DROP_ROLE,
new IntentionDiagramDnDEditPolicy());
}

/**
* @generated
*/
protected void refreshChildren() {
int i;
EditPart editPart;
Object model;

Map modelToEditPart = new HashMap();
List children = getChildren();

for (i = 0; i < children.size(); i++) {
editPart = (EditPart) children.get(i);
modelToEditPart.put(editPart.getModel(), editPart);
}

List modelObjects = getModelChildren();

for (i = 0; i < modelObjects.size(); i++) {
model = modelObjects.get(i);

//Do a quick check to see if editPart[i] == model[i]
if (i < children.size()
&& ((EditPart) children.get(i)).getModel() == model)
continue;

//Look to see if the EditPart is already around but in the wrong location
editPart = (EditPart) modelToEditPart.get(model);

if (editPart != null)
reorderChild(editPart, i);
else {
//An editpart for this model doesn't exist yet. Create and insert one if it
//references at least one element
if (!(model instanceof Shape) || ((Shape) model).isSetElement()) {
editPart = createChild(model);
addChild(editPart, i);
}
}
}
List trash = new ArrayList();
for (; i < children.size(); i++)
trash.add(children.get(i));
for (i = 0; i < trash.size(); i++) {
EditPart ep = (EditPart) trash.get(i);
removeChild(ep);
}
}

/**
* @generated
*/
private class IntentionDiagramDnDEditPolicy extends
DiagramDragDropEditPolicy {
public Command getDropObjectsCommand(DropObjectsRequest request) {
List<?> objects = request.getObjects();
if (objects.size() != 1)
return UnexecutableCommand.INSTANCE;
Object element = objects.get(0);
if (element == null || getHostView().getElement().equals(element))
return UnexecutableCommand.INSTANCE;
//TODO: check element in not already in view: Already handled
//TODO: enable several notational element for the same semantic element
List<CreateViewRequest.ViewDescriptor> descriptors = new ArrayList<CreateViewRequest.ViewDescriptor>();
CreateViewRequest.ViewDescriptor viewDescriptor = new CreateViewRequest.ViewDescriptor(
new EObjectAdapter((EObject) element), Node.class,
getSemanticHintForEObject((EObject) element),
getDiagramPreferencesHint());
viewDescriptor.setPersisted(true);
descriptors.add(viewDescriptor);
Command res = null;
if (!descriptors.isEmpty()) {
res = createViewsAndRestoreRelatedLinks(request, descriptors);
}
if (res == null)
return UnexecutableCommand.INSTANCE;
res.setLabel("Create a new Modal Diagram Element");
return res;
}

private String getSemanticHintForEObject(EObject element) {

if (element instanceof Role) {
return ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. RoleEditPart.VISUAL_ID))
.getSemanticHint();
}

if (element instanceof Intention) {
return ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. IntentionEditPart.VISUAL_ID))
.getSemanticHint();
}

if (element instanceof MethodologyRule) {
return ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. MethodologyRuleEditPart.VISUAL_ID))
.getSemanticHint();
}

if (element instanceof ToolDefinition) {
return ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. ToolDefinitionEditPart.VISUAL_ID))
.getSemanticHint();
}

return null;
}

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 Command createViewsAndRestoreRelatedLinks(
DropObjectsRequest dropRequest,
List<CreateViewRequest.ViewDescriptor> viewDescriptors) {
CreateViewRequest createViewRequest = new CreateViewRequest(
viewDescriptors);
createViewRequest.setLocation(dropRequest.getLocation());
Command createCommand = getHost().getCommand(createViewRequest);

if (createCommand != null) {
List result = (List) createViewRequest.getNewObject();
dropRequest.setResult(result);

createCommand.chain(new ICommandProxy(
new RestoreRelatedLinksCommand(
(DiagramEditPart) getHost(), result)));

ArrangeRequest arrangeRequest = new ArrangeRequest(
RequestConstants.REQ_ARRANGE_DEFERRED);
arrangeRequest.setViewAdaptersToArrange(result);
createCommand.chain(getHost().getCommand(arrangeRequest));
}
return createCommand;
}
}

/**
* @generated
*/
private class DiagramObjectAdapter implements IAdaptable {
private EObject modelElement;
private IElementType elementType;

public DiagramObjectAdapter(EObject modelElement) {
this.modelElement = modelElement;
processElementType();
}

private void processElementType() {

if (modelElement instanceof IntentionAssociation)
elementType = fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. IntentionAssociationEditPart.VISUAL_ID);

if (modelElement instanceof IntentionUsage)
elementType = fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. IntentionUsageEditPart.VISUAL_ID);

if (modelElement instanceof SatisfactionLink)
elementType = fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. SatisfactionLinkEditPart.VISUAL_ID);

}

@Override
public Object getAdapter(Class adapter) {
if (IElementType.class.equals(adapter))
return elementType;
return null;
}

}

private class RestoreRelatedLinksCommand extends
AbstractTransactionalCommand {
private DiagramEditPart diagramEditPart;
private List<?> adapters;

public RestoreRelatedLinksCommand(DiagramEditPart diagramEditPart,
List<?> selection) {
super(diagramEditPart.getEditingDomain(),
"Related Link Restoration", null);
this.diagramEditPart = diagramEditPart;
this.adapters = selection;
}

@Override
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
for (Object object : adapters) {
if (object instanceof IAdaptable) {
View view = (View) ((IAdaptable) object)
.getAdapter(View.class);
if (view != null)
refreshRelatedLinks(view);
} else if (object instanceof View)
refreshRelatedLinks((View) object);
}
return CommandResult.newOKCommandResult();
}

private void refreshRelatedLinks(View view) {
EObject model = view.getElement();
List<EObject> possibleRelatedElements = new ArrayList<EObject>();
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ShapeNodeEditPart)
possibleRelatedElements.add(((ShapeNodeEditPart) child)
.resolveSemanticElement());
}
createRelatedLinks(view.getElement(), possibleRelatedElements);
}

private void createRelatedLinks(EObject element,
List<EObject> possibleRelatedElements) {
//create missing edges between notational elements
Resource r = element.eResource();

if (element instanceof Role)
processRole((Role) element, r);

if (element instanceof Intention)
processIntention((Intention) element, r);

if (element instanceof MethodologyRule)
processMethodologyRule((MethodologyRule) element, r);

if (element instanceof ToolDefinition)
processToolDefinition((ToolDefinition) element, r);

}

private void processRole(Role element, Resource r) {

//process IntentionAssociation links
List<IntentionAssociation> listIntentionAssociation1 = new ArrayList<IntentionAssociation>();
TreeIterator<EObject> itIntentionAssociation = EcoreUtil
.getAllContents(r, false);
while (itIntentionAssociation.hasNext()) {
EObject o = itIntentionAssociation.next();
if (o instanceof IntentionAssociation)
listIntentionAssociation1.add((IntentionAssociation) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof IntentionAssociation)
listIntentionAssociation1.remove(o);
}
}

List<IntentionAssociation> listIntentionAssociation2 = new ArrayList<IntentionAssociation>();
for (IntentionAssociation e : listIntentionAssociation1)
if (e.getSource() == element || e.getTarget() == element)
listIntentionAssociation2.add(e);
listIntentionAssociation1.clear();
listIntentionAssociation1.addAll(listIntentionAssociation2);
for (IntentionAssociation link : listIntentionAssociation1)
createViewForIntentionAssociation(link);

}

private void processIntention(Intention element, Resource r) {

//process IntentionAssociation links
List<IntentionAssociation> listIntentionAssociation1 = new ArrayList<IntentionAssociation>();
TreeIterator<EObject> itIntentionAssociation = EcoreUtil
.getAllContents(r, false);
while (itIntentionAssociation.hasNext()) {
EObject o = itIntentionAssociation.next();
if (o instanceof IntentionAssociation)
listIntentionAssociation1.add((IntentionAssociation) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof IntentionAssociation)
listIntentionAssociation1.remove(o);
}
}

List<IntentionAssociation> listIntentionAssociation2 = new ArrayList<IntentionAssociation>();
for (IntentionAssociation e : listIntentionAssociation1)
if (e.getSource() == element || e.getTarget() == element)
listIntentionAssociation2.add(e);
listIntentionAssociation1.clear();
listIntentionAssociation1.addAll(listIntentionAssociation2);
for (IntentionAssociation link : listIntentionAssociation1)
createViewForIntentionAssociation(link);

//process IntentionUsage links
List<IntentionUsage> listIntentionUsage1 = new ArrayList<IntentionUsage>();
TreeIterator<EObject> itIntentionUsage = EcoreUtil.getAllContents(
r, false);
while (itIntentionUsage.hasNext()) {
EObject o = itIntentionUsage.next();
if (o instanceof IntentionUsage)
listIntentionUsage1.add((IntentionUsage) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof IntentionUsage)
listIntentionUsage1.remove(o);
}
}

List<IntentionUsage> listIntentionUsage2 = new ArrayList<IntentionUsage>();
for (IntentionUsage e : listIntentionUsage1)
if (e.getSource() == element || e.getTarget() == element)
listIntentionUsage2.add(e);
listIntentionUsage1.clear();
listIntentionUsage1.addAll(listIntentionUsage2);
for (IntentionUsage link : listIntentionUsage1)
createViewForIntentionUsage(link);

//process SatisfactionLink links
List<SatisfactionLink> listSatisfactionLink1 = new ArrayList<SatisfactionLink>();
TreeIterator<EObject> itSatisfactionLink = EcoreUtil
.getAllContents(r, false);
while (itSatisfactionLink.hasNext()) {
EObject o = itSatisfactionLink.next();
if (o instanceof SatisfactionLink)
listSatisfactionLink1.add((SatisfactionLink) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof SatisfactionLink)
listSatisfactionLink1.remove(o);
}
}

List<SatisfactionLink> listSatisfactionLink2 = new ArrayList<SatisfactionLink>();
for (SatisfactionLink e : listSatisfactionLink1)
if (e.getSource() == element || e.getTarget() == element)
listSatisfactionLink2.add(e);
listSatisfactionLink1.clear();
listSatisfactionLink1.addAll(listSatisfactionLink2);
for (SatisfactionLink link : listSatisfactionLink1)
createViewForSatisfactionLink(link);

}

private void processMethodologyRule(MethodologyRule element, Resource r) {

//process IntentionUsage links
List<IntentionUsage> listIntentionUsage1 = new ArrayList<IntentionUsage>();
TreeIterator<EObject> itIntentionUsage = EcoreUtil.getAllContents(
r, false);
while (itIntentionUsage.hasNext()) {
EObject o = itIntentionUsage.next();
if (o instanceof IntentionUsage)
listIntentionUsage1.add((IntentionUsage) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof IntentionUsage)
listIntentionUsage1.remove(o);
}
}

List<IntentionUsage> listIntentionUsage2 = new ArrayList<IntentionUsage>();
for (IntentionUsage e : listIntentionUsage1)
if (e.getSource() == element || e.getTarget() == element)
listIntentionUsage2.add(e);
listIntentionUsage1.clear();
listIntentionUsage1.addAll(listIntentionUsage2);
for (IntentionUsage link : listIntentionUsage1)
createViewForIntentionUsage(link);

}

private void processToolDefinition(ToolDefinition element, Resource r) {

//process IntentionUsage links
List<IntentionUsage> listIntentionUsage1 = new ArrayList<IntentionUsage>();
TreeIterator<EObject> itIntentionUsage = EcoreUtil.getAllContents(
r, false);
while (itIntentionUsage.hasNext()) {
EObject o = itIntentionUsage.next();
if (o instanceof IntentionUsage)
listIntentionUsage1.add((IntentionUsage) o);
}
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ConnectionNodeEditPart) {
EObject o = ((ConnectionNodeEditPart) child)
.resolveSemanticElement();
if (o instanceof IntentionUsage)
listIntentionUsage1.remove(o);
}
}

List<IntentionUsage> listIntentionUsage2 = new ArrayList<IntentionUsage>();
for (IntentionUsage e : listIntentionUsage1)
if (e.getSource() == element || e.getTarget() == element)
listIntentionUsage2.add(e);
listIntentionUsage1.clear();
listIntentionUsage1.addAll(listIntentionUsage2);
for (IntentionUsage link : listIntentionUsage1)
createViewForIntentionUsage(link);

}

public void createViewForIntentionAssociation(IntentionAssociation link) {
//retrieve source and target edit parts
EditPart sourceEditPart = retrieveEditPartForEObject((EObject) link
.getSource());
EditPart targetEditPart = retrieveEditPartForEObject((EObject) link
.getTarget());
//continue if none of them are empty
if (sourceEditPart == null || targetEditPart == null)
return;
//Create a connection view request using connection view descriptor
String semanticHint = ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. IntentionAssociationEditPart.VISUAL_ID))
.getSemanticHint();
CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor(
new DiagramObjectAdapter(link), semanticHint,
ViewUtil.APPEND, true, diagramEditPart
.getDiagramPreferencesHint());
CreateConnectionViewRequest request = new CreateConnectionViewRequest(
descriptor);
request.setType(RequestConstants.REQ_CONNECTION_START);
request.setSourceEditPart(sourceEditPart);
sourceEditPart.getCommand(request);
request.setType(RequestConstants.REQ_CONNECTION_END);
request.setTargetEditPart(targetEditPart);
//retrieve corresponding command and execute it
final Command cmd = targetEditPart.getCommand(request);
if (cmd != null && cmd.canExecute()) {
AbstractEMFOperation op = new AbstractEMFOperation(
diagramEditPart.getEditingDomain(),
StringStatics.BLANK, null) {

@Override
protected IStatus doExecute(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
cmd.execute();
return Status.OK_STATUS;
}
};
try {
op.execute(new NullProgressMonitor(), null);
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
}

public void createViewForIntentionUsage(IntentionUsage link) {
//retrieve source and target edit parts
EditPart sourceEditPart = retrieveEditPartForEObject((EObject) link
.getSource());
EditPart targetEditPart = retrieveEditPartForEObject((EObject) link
.getTarget());
//continue if none of them are empty
if (sourceEditPart == null || targetEditPart == null)
return;
//Create a connection view request using connection view descriptor
String semanticHint = ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. IntentionUsageEditPart.VISUAL_ID))
.getSemanticHint();
CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor(
new DiagramObjectAdapter(link), semanticHint,
ViewUtil.APPEND, true, diagramEditPart
.getDiagramPreferencesHint());
CreateConnectionViewRequest request = new CreateConnectionViewRequest(
descriptor);
request.setType(RequestConstants.REQ_CONNECTION_START);
request.setSourceEditPart(sourceEditPart);
sourceEditPart.getCommand(request);
request.setType(RequestConstants.REQ_CONNECTION_END);
request.setTargetEditPart(targetEditPart);
//retrieve corresponding command and execute it
final Command cmd = targetEditPart.getCommand(request);
if (cmd != null && cmd.canExecute()) {
AbstractEMFOperation op = new AbstractEMFOperation(
diagramEditPart.getEditingDomain(),
StringStatics.BLANK, null) {

@Override
protected IStatus doExecute(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
cmd.execute();
return Status.OK_STATUS;
}
};
try {
op.execute(new NullProgressMonitor(), null);
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
}

public void createViewForSatisfactionLink(SatisfactionLink link) {
//retrieve source and target edit parts
EditPart sourceEditPart = retrieveEditPartForEObject((EObject) link
.getSource());
EditPart targetEditPart = retrieveEditPartForEObject((EObject) link
.getTarget());
//continue if none of them are empty
if (sourceEditPart == null || targetEditPart == null)
return;
//Create a connection view request using connection view descriptor
String semanticHint = ((IHintedType) fr.mopcom.modal.intentionDiagram.providers.ModalElementTypes
.getElementType(fr.mopcom.modal.intentionDiagram.edit.parts. SatisfactionLinkEditPart.VISUAL_ID))
.getSemanticHint();
CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor(
new DiagramObjectAdapter(link), semanticHint,
ViewUtil.APPEND, true, diagramEditPart
.getDiagramPreferencesHint());
CreateConnectionViewRequest request = new CreateConnectionViewRequest(
descriptor);
request.setType(RequestConstants.REQ_CONNECTION_START);
request.setSourceEditPart(sourceEditPart);
sourceEditPart.getCommand(request);
request.setType(RequestConstants.REQ_CONNECTION_END);
request.setTargetEditPart(targetEditPart);
//retrieve corresponding command and execute it
final Command cmd = targetEditPart.getCommand(request);
if (cmd != null && cmd.canExecute()) {
AbstractEMFOperation op = new AbstractEMFOperation(
diagramEditPart.getEditingDomain(),
StringStatics.BLANK, null) {

@Override
protected IStatus doExecute(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
cmd.execute();
return Status.OK_STATUS;
}
};
try {
op.execute(new NullProgressMonitor(), null);
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
}

private EditPart retrieveEditPartForEObject(EObject element) {
for (Object child : diagramEditPart.getChildren()) {
if (child instanceof ShapeNodeEditPart) {
if (((ShapeNodeEditPart) child).resolveSemanticElement() == element)
return (EditPart) child;
}
}
return null;
}

}

}

--------------040101070009020500030104--
Re: How to drag-n-drop from Project Explorer into diagram? [message #523888 is a reply to message #523860] Mon, 29 March 2010 18:06 Go to previous message
Ralf is currently offline RalfFriend
Messages: 25
Registered: March 2010
Junior Member
Ali Koudri wrote on Mon, 29 March 2010 12:07

You'll find it as attached document.
Cheers.


Thanks for your quick response, Ali! I hope your source will enlight me. Right now I'm quite frustrated about the drag-and-drop stuff Sad I just want to drag a valid domain-model object from a custom treeviewer into the canvas.


Re: How to drag-n-drop from Project Explorer into diagram? [message #523891 is a reply to message #523888] Mon, 29 March 2010 13:20 Go to previous message
Ali Koudri is currently offline Ali KoudriFriend
Messages: 118
Registered: July 2009
Senior Member
My code is not documented at all. So if there anything you don't
understand, please let me know.

Regards.

Le 29/03/2010 20:06, Ralf a écrit :
> Ali Koudri wrote on Mon, 29 March 2010 12:07
>> You'll find it as attached document.
>> Cheers.
>
>
> Thanks for your quick response, Ali! I hope your source will enlight me.
> Right now I'm quite frustrated about the drag-and-drop stuff :( I just
> want to drag a valid domain-model object from a custom treeviewer into
> the canvas.
>
>
>
Previous Topic:labeled container
Next Topic:Change linestyle
Goto Forum:
  


Current Time: Wed Sep 30 07:07:14 GMT 2020

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

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

Back to the top