Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Edge creation
Edge creation [message #179293] Thu, 27 March 2008 03:36
Ben Monroe is currently offline Ben MonroeFriend
Messages: 3
Registered: July 2009
Junior Member
Greetings.

I know my way around standard Eclipse plugins but am just getting started with
GMF. The documentation and tutorials have been useful to a point, but I need
some assistance.

At present, I am trying to create an action that will perform the following task:

1) Click on an element
2) Check the source and target edges
3) Remove the selected element
4) If edge directions are the same, re-adjust the edge (ideally create a new
one) appropriately

More specifically, imagine the following four simplified cases:

Before: (element) <--- (element) <--- (element)
After: (element) <------------------ (element)

Before: (element) ---> (element) ---> (element)
After: (element) ------------------> (element)

Before: (element) <--- (element) ---> (element)
After: (element) (element)

Before: (element) ---> (element) <--- (element)
After: (element) (element)

The diagram has both default and several custom edges. Please assume for
simplicity that the above edges are of the same type, whatever that may be.

I have come up with the following solution that partially works.
However, it is flawed in several areas. I adjust an existing edge and then
delete the selected shape with DestroyElementCommand. This command leaves
existing edges, so they must be destroyed manually. I would rather utilize the
default deletion behavior (what is the command name?) that also removes linked
edges. Also, it seems to ruin the model (partially out of synch?) and element
placement in some situations; I'm still investigating that one.

Thus, rather than using an existing edge, it would make more sense to create a
new one. I have tried various incarnations of ViewService.createEdge. However,
my IViewProvider does not know about the default edges in DiagramViewProvider so
often returns null on getEdgeViewClass. I'd like the solution to work for Edge
types in general, so perhaps another method is required.

Any suggestions or advice would be greatly appreciated.

Code:
---------------------
package ...

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.common.ui.action.AbstractActionDeleg ate;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditP art;
import org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack ;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramEditDomain;
import org.eclipse.gmf.runtime.emf.type.core.commands.DestroyElemen tCommand;
import org.eclipse.gmf.runtime.emf.type.core.commands.SetValueComma nd;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElemen tRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.SetRequest;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;

public class LinkedRemovalAction extends AbstractActionDelegate implements
IObjectActionDelegate
{
@Override
protected void doRun(IProgressMonitor progressMonitor)
{
final IStructuredSelection structuredSelection = getStructuredSelection();
final Object selection = structuredSelection.getFirstElement();

if (!(selection instanceof IGraphicalEditPart))
{
throw new IllegalArgumentException();
}

final IGraphicalEditPart container = (IGraphicalEditPart) selection;
final View primaryView = container.getPrimaryView();

if (primaryView != null)
{
final CompoundCommand compoundCommand = new CompoundCommand();

final EList<?> sourceEdges = primaryView.getSourceEdges();
final EList<?> targetEdges = primaryView.getTargetEdges();

for (final Object objSource : sourceEdges)
{
if (objSource instanceof Edge)
{
final Edge sourceEdge = (Edge) objSource;
final View targetView = sourceEdge.getTarget();

for (final Object objTarget : targetEdges)
{
if (objTarget instanceof Edge)
{
final Edge targetEdge = ((Edge) objTarget);
final EClass eClass = targetEdge.eClass();

final EStructuralFeature sourceFeature = eClass
.getEStructuralFeature("target"); //$NON-NLS-1$
final TransactionalEditingDomain ted = TransactionUtil
.getEditingDomain(targetEdge);

final SetRequest reqSet = new SetRequest(ted,
targetEdge,
sourceFeature, targetView);
final SetValueCommand operation = new
SetValueCommand(reqSet);
final Command command = new ICommandProxy(operation);

compoundCommand.add(command);
}
}

final Command destroyEdgeCommand =
getDestroyCommand(sourceEdge);
compoundCommand.add(destroyEdgeCommand);
}
}

final Command destorySelectionCommand = getDestroyCommand(primaryView);
compoundCommand.add(destorySelectionCommand);

final IDiagramEditDomain editDomain = container.getDiagramEditDomain();
final DiagramCommandStack commandStack =
editDomain.getDiagramCommandStack();
commandStack.execute(compoundCommand);
}
}

private Command getDestroyCommand(final View view)
{
final DestroyElementRequest der = new DestroyElementRequest(view, false);
final DestroyElementCommand destroyCommand = new
DestroyElementCommand(der);

return new ICommandProxy(destroyCommand);
}
}


Regards,
Ben Monroe
Previous Topic:GMF-based diagram editor using an extension of uml2 meta-model.
Next Topic:Containment=true breaks element creation using DND from palette
Goto Forum:
  


Current Time: Thu Apr 25 01:38:08 GMT 2024

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

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

Back to the top