Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Changing a shape to another shape at run time
Changing a shape to another shape at run time [message #199575] Wed, 30 July 2008 15:00 Go to next message
Toyin is currently offline ToyinFriend
Messages: 35
Registered: July 2009
Member
Hi Everyone,
This is what Im trying to achieve:
I want to create an editor where I can right-click on a rectangular shape
and a popup menu comes with options to change the rectangular shape to
another shape (say ellipse), while the new ellipse shape retains its link
(connection) with other shapes. Can anyone suggest ways to achieve this?

Thanks

David
Re: Changing a shape to another shape at run time [message #199996 is a reply to message #199575] Mon, 04 August 2008 05:33 Go to previous messageGo to next message
Jevon  is currently offline Jevon Friend
Messages: 164
Registered: July 2009
Senior Member
The easiest option within GMF is to have two different classes in the
metamodel, with each model object represented by a different visual
class. If this visual notation isn't stored in the model, how will GMF
know which representation to display when re-opening the model?

Once you have two different element types, you could use a series of
commands to create a new element with its original children, and delete
the original one. Have a look at the GMF tutorial's "Insert subtopic"
code for how to create elements programatically:



David wrote:
> Hi Everyone,
> This is what Im trying to achieve:
> I want to create an editor where I can right-click on a rectangular
> shape and a popup menu comes with options to change the rectangular
> shape to another shape (say ellipse), while the new ellipse shape
> retains its link (connection) with other shapes. Can anyone suggest ways
> to achieve this?
>
> Thanks
>
> David
Re: Changing a shape to another shape at run time [message #200004 is a reply to message #199996] Mon, 04 August 2008 05:36 Go to previous messageGo to next message
Jevon  is currently offline Jevon Friend
Messages: 164
Registered: July 2009
Senior Member
Tutorial link: http://wiki.eclipse.org/GMF_Tutorial_Part_3

To delete an element, create a new DestroyElementRequest:

DestroyElementRequest request = new DestroyElementRequest(someElement,
false);
Command deleteLinkCmd = link.getCommand( new
EditCommandRequestWrapper(request) );
cmd.add(deleteLinkCmd);

Hope this helps.

Jevon

Jevon Wright wrote:
> The easiest option within GMF is to have two different classes in the
> metamodel, with each model object represented by a different visual
> class. If this visual notation isn't stored in the model, how will GMF
> know which representation to display when re-opening the model?
>
> Once you have two different element types, you could use a series of
> commands to create a new element with its original children, and delete
> the original one. Have a look at the GMF tutorial's "Insert subtopic"
> code for how to create elements programatically:
>
>
>
> David wrote:
>> Hi Everyone,
>> This is what Im trying to achieve:
>> I want to create an editor where I can right-click on a rectangular
>> shape and a popup menu comes with options to change the rectangular
>> shape to another shape (say ellipse), while the new ellipse shape
>> retains its link (connection) with other shapes. Can anyone suggest
>> ways to achieve this?
>>
>> Thanks
>>
>> David
Re: Changing a shape to another shape at run time [message #200473 is a reply to message #200004] Tue, 05 August 2008 12:10 Go to previous messageGo to next message
Toyin is currently offline ToyinFriend
Messages: 35
Registered: July 2009
Member
Hi Jevon,
Many thanks for your help.
I intend to replace a selected element with another without loosing the
link to other elements. Now I can right-click on the element and then
select another element to replace it, the problem now is this; the new
element just sits on top of the selected element without replacing it. Im
trying to include the "DestroyElementrequest" code you sent. Please, if
you have more suggestions, they would be highly appreciated.

Thanks
David
Re: Changing a shape to another shape at run time [message #200959 is a reply to message #200473] Thu, 07 August 2008 03:12 Go to previous messageGo to next message
Jevon  is currently offline Jevon Friend
Messages: 164
Registered: July 2009
Senior Member
Hi David,

Sure, here are some snippets of code that I was using. In particular it
is some code to create an element, and then some similar code to delete
an element.

// we will combine all of these commands together
CompoundCommand cc = new CompoundCommand("A compound command");

// create a command to create an element
CreateViewRequest leftRequest =
CreateViewRequestFactory.getCreateShapeRequest(ThreeElementT ypes.LeftObject_1002,
part.getDiagramPreferencesHint());
ContainerEditPart mapEditPart = (ContainerEditPart) part.getParent();
Command createLeftCmd = mapEditPart.getCommand(leftRequest);

// add it to the compound command
cc.add(createLeftCmd);

// create a command to delete an element
DestroyElementRequest request2 = new
DestroyElementRequest(sublink.resolveSemanticElement(), false);
Command deleteLinkCmd = sublink.getCommand( new
EditCommandRequestWrapper(request2) );
cc.add(deleteLinkCmd);

// you can then execute the command
selectedElement.getDiagramEditDomain().getDiagramCommandStac k().execute(cc);

Hope this helps.

Jevon

David wrote:
> Hi Jevon,
> Many thanks for your help.
> I intend to replace a selected element with another without loosing the
> link to other elements. Now I can right-click on the element and then
> select another element to replace it, the problem now is this; the new
> element just sits on top of the selected element without replacing it.
> Im trying to include the "DestroyElementrequest" code you sent. Please,
> if you have more suggestions, they would be highly appreciated.
>
> Thanks
> David
>
Re: Changing a shape to another shape at run time [message #201117 is a reply to message #200959] Fri, 08 August 2008 10:17 Go to previous messageGo to next message
Toyin is currently offline ToyinFriend
Messages: 35
Registered: July 2009
Member
Hi Jevon,
Thanks for your code. I have included your code in mine and it worked.
However, the only the short-come is that all the elements in the model
become static. Did you encounter such problem? Here is my code. I don't
know what to change anymore. What do you think is wrong with my code?

Thanks

David

package org.eclipse.scrapbook.custom.popup.actions;

import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import
org.eclipse.gmf.runtime.diagram.ui.commands.DeferredCreateCo nnectionViewAndElementCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import
org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnection ViewAndElementRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewReques t;
import
org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewReques tFactory;
import
org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandReque stWrapper;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants ;
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.emf.type.core.requests.DestroyElemen tRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import Scrapbook.diagram.edit.parts.ScrapItemEditPart;
import Scrapbook.diagram.edit.parts.ScrapBookDiagramEditPart;
import Scrapbook.diagram.edit.parts.FolderEditPart;
import Scrapbook.diagram.edit.parts.SolidConnectionEditPart;
import Scrapbook.diagram.providers.ScrapbookElementTypes;

public class CreateShape implements IObjectActionDelegate {

public final static String ID = "org.eclipse.scrapbook.custom.newAction";

private ScrapItemEditPart selectedElement;


public void run(IAction action) {
CompoundCommand cc = new CompoundCommand("Create another shape");

// Create the new shape.
CreateViewRequest shapeRequest =
CreateViewRequestFactory.getCreateShapeRequest(ScrapbookElem entTypes.Folder_1001,
selectedElement.getDiagramPreferencesHint());

Point p =
selectedElement.getFigure().getBounds().getTopRight().getCop y();
selectedElement.getFigure().translateToAbsolute(p);
int edgeCount =
selectedElement.getNotationView().getSourceEdges().size();
// Code to set the position of the new shape
int offset = (edgeCount * 0);

shapeRequest.setLocation(p.translate(-45, offset));

ScrapBookDiagramEditPart scrapbookmodelEditPart =
(ScrapBookDiagramEditPart) selectedElement.getParent();
Command createShapeCmd = scrapbookmodelEditPart.getCommand(shapeRequest);
IAdaptable topicViewAdapter = (IAdaptable) ((List)
shapeRequest.getNewObject()).get(0);


cc.add(createShapeCmd);

// Code to destroy element
DestroyElementRequest request2 = new
DestroyElementRequest(selectedElement.resolveSemanticElement (), false);
Command deleteLinkCmd = selectedElement.getCommand( new
EditCommandRequestWrapper(request2) );
cc.add(deleteLinkCmd);


selectedElement.getDiagramEditDomain().getDiagramCommandStac k().execute(cc);

// put the new shape in edit mode
final EditPartViewer viewer = selectedElement.getViewer();
final EditPart elementPart = (EditPart)
viewer.getEditPartRegistry().get(topicViewAdapter.getAdapter (View.class));
if (elementPart != null) {
Display.getCurrent().asyncExec(new Runnable() {

public void run() {
viewer.setSelection(new StructuredSelection(elementPart));
Request der = new Request(RequestConstants.REQ_DIRECT_EDIT);
elementPart.performRequest(der);
}
});
}
}

public void selectionChanged(IAction action, ISelection selection) {
selectedElement = null;
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection)
selection;
if (structuredSelection.getFirstElement() instanceof ScrapItemEditPart)
{
selectedElement = (ScrapItemEditPart)
structuredSelection.getFirstElement();
}
}
}

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}


}
Re: Changing a shape to another shape at run time [message #201990 is a reply to message #201117] Thu, 14 August 2008 13:18 Go to previous message
Toyin is currently offline ToyinFriend
Messages: 35
Registered: July 2009
Member
Hi Jevon,
Thanks for your code. I have included your code in mine and it worked.
However, the only the short-come is that all the elements in the model
become static. Did you encounter such problem? Here is my code. I don't
know what to change anymore. What do you think is wrong with my code?

Thanks

David

package org.eclipse.scrapbook.custom.popup.actions;

import java.util.List;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import
org.eclipse.gmf.runtime.diagram.ui.commands.DeferredCreateCo nnectionViewAndElementCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import
org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnection ViewAndElementRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewReques t;
import
org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewReques tFactory;
import
org.eclipse.gmf.runtime.diagram.ui.requests.EditCommandReque stWrapper;
import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants ;
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.emf.type.core.requests.DestroyElemen tRequest;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import Scrapbook.diagram.edit.parts.ScrapItemEditPart;
import Scrapbook.diagram.edit.parts.ScrapBookDiagramEditPart;
import Scrapbook.diagram.edit.parts.FolderEditPart;
import Scrapbook.diagram.edit.parts.SolidConnectionEditPart;
import Scrapbook.diagram.providers.ScrapbookElementTypes;

public class CreateShape implements IObjectActionDelegate {

public final static String ID = "org.eclipse.scrapbook.custom.newAction";

private ScrapItemEditPart selectedElement;


public void run(IAction action) {
CompoundCommand cc = new CompoundCommand("Create another shape");

// Create the new shape.
CreateViewRequest shapeRequest =
CreateViewRequestFactory.getCreateShapeRequest(ScrapbookElem entTypes.Folder_1001,
selectedElement.getDiagramPreferencesHint());

Point p = selectedElement.getFigure().getBounds().getTopRight().getCop y();
selectedElement.getFigure().translateToAbsolute(p);
int edgeCount = selectedElement.getNotationView().getSourceEdges().size();
// Code to set the position of the new shape
int offset = (edgeCount * 0);

shapeRequest.setLocation(p.translate(-45, offset));

ScrapBookDiagramEditPart scrapbookmodelEditPart =
(ScrapBookDiagramEditPart) selectedElement.getParent();
Command createShapeCmd = scrapbookmodelEditPart.getCommand(shapeRequest);
IAdaptable topicViewAdapter = (IAdaptable) ((List)
shapeRequest.getNewObject()).get(0);


cc.add(createShapeCmd);

// Code to destroy element
DestroyElementRequest request2 = new
DestroyElementRequest(selectedElement.resolveSemanticElement (), false);
Command deleteLinkCmd = selectedElement.getCommand( new
EditCommandRequestWrapper(request2) );
cc.add(deleteLinkCmd);


selectedElement.getDiagramEditDomain().getDiagramCommandStac k().execute(cc);

// put the new shape in edit mode
final EditPartViewer viewer = selectedElement.getViewer();
final EditPart elementPart = (EditPart)
viewer.getEditPartRegistry().get(topicViewAdapter.getAdapter (View.class));
if (elementPart != null) {
Display.getCurrent().asyncExec(new Runnable() {

public void run() {
viewer.setSelection(new StructuredSelection(elementPart));
Request der = new Request(RequestConstants.REQ_DIRECT_EDIT);
elementPart.performRequest(der);
}
});
}
}

public void selectionChanged(IAction action, ISelection selection) {
selectedElement = null;
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection)
selection;
if (structuredSelection.getFirstElement() instanceof ScrapItemEditPart) {
selectedElement = (ScrapItemEditPart)
structuredSelection.getFirstElement();
}
}
}

public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}


}
Previous Topic:Generic Tool creation
Next Topic:file path location
Goto Forum:
  


Current Time: Fri Apr 26 10:51:21 GMT 2024

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

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

Back to the top