How to create diagram elements programatically [message #696191] |
Wed, 13 July 2011 13:09 |
Neslepaks Messages: 37 Registered: April 2011 |
Member |
|
|
Hi,
I would need to create diagram element after triggering an action and I am wondering how to do that.
I have tried to read the simplediagram example code from Papyrus SVN but I don't understand how to do that. I suppose the ComponentCreateCommand could the key to find out the mechanism but I feel it's quite complex.
Is there a simpler (more readable) way to do that?
[Updated on: Wed, 13 July 2011 13:16] Report message to a moderator
|
|
|
|
|
|
Re: How to create diagram elements programatically [message #703394 is a reply to message #701732] |
Wed, 27 July 2011 14:26 |
Neslepaks Messages: 37 Registered: April 2011 |
Member |
|
|
Ok, thanks for the links. I'll try check if can find any help from there.
Of course the knowledge of frameworks and architectures
would be useful, but at this point, any kind of relevant code snippet would
be helpful.
For me, it seems that most of the GMF tutorials concentrate on how to
generate a graphical editor. I don't need to make a whole new editor
but just to add, for example, an action to main toolbar that
creates a diagram element to opened diagram. I have also read PapyrusDevelopperTutorial_OnDiagramCreating.odt and wonder if there is any help of the GMF wizards on such a task.
The following code is what I've been able to made but I am not sure if I'm on the right path:
CreateElementRequest req = new CreateElementRequest(UMLElementTypes.Component_2001);
IProgressMonitor monitor = null;
IAdaptable info = null;
ComponentCreateCommand ccc = new ComponentCreateCommand(req);
try {
ccc.doExecuteWithResult(monitor,info);
} catch (ExecutionException e1) {
e1.printStackTrace();
}
Then I have the ComponentCreateCommand.java from org.eclipse.papyrus.example.diagram.simplediagram.edit.commands
and the most relevant part of the class is the function
public CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
Component newElement = UMLFactory.eINSTANCE.createComponent();
Package owner = (Package) getElementToEdit();
owner.getPackagedElements().add(newElement);
//doConfigure(newElement, monitor, info);
((CreateElementRequest) getRequest()).setNewElement(newElement);
return CommandResult.newOKCommandResult(newElement);
}
The problem in this, is that the line owner.getPackagedElements().add(newElement); throws a NullPointerException. Does anyone have ideas how to get rid of that or is the line even needed?
Also is it necessary to initialize IProgressMonitor and IAdaptable to something else than null as they are used in doConfigure?
|
|
|
|
|
Re: How to create diagram elements programatically [message #717730 is a reply to message #696191] |
Mon, 22 August 2011 07:55 |
Neslepaks Messages: 37 Registered: April 2011 |
Member |
|
|
I guess the example could be simpler but hopefully this is useful as well. This works with a class diagram.
Some attributes:
/** The diagram editor. */
protected UMLDiagramEditor diagramEditor=null;
/** The clazzdiagramedit part. */
protected DiagramEditPart clazzdiagrameditPart;
/** The papyrus editor. */
protected PapyrusMultiDiagramEditor papyrusEditor;
The code:
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
papyrusEditor =((PapyrusMultiDiagramEditor)editorPart);
EObject model = null;
try {
//get root element
model = UmlUtils.getUmlModel().lookupRoot();
} catch (org.eclipse.papyrus.resource.NotFoundException e) {
e.printStackTrace();
}
//it is possible to add new elements to packages
final org.eclipse.uml2.uml.Package modelpackage = (org.eclipse.uml2.uml.Package) model;
//Add new class to model
TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(model);
ted.getCommandStack().execute(new RecordingCommand(ted) {
protected void doExecute() {
modelpackage.createOwnedClass("ClassName", true);
}
});
//Make some checks and place the new class to diagram.
for(int i = 0; i < modelpackage.getOwnedElements().size(); ++i)
{
if( modelpackage.getOwnedElements().get(i) instanceof org.eclipse.uml2.uml.Class )
{
org.eclipse.uml2.uml.Class c = (org.eclipse.uml2.uml.Class) modelpackage.getOwnedElements().get(i);
if( c instanceof NamedElement)
{
NamedElement ne = c;
if (ne.getName().equals("ClassName"))
{
Object object = c;
//add the new class element to diagram
DropObjectsRequest dropObjectsRequest = new DropObjectsRequest();
ArrayList list = new ArrayList();
list.add((org.eclipse.uml2.uml.Element)object);
dropObjectsRequest.setObjects(list);
dropObjectsRequest.setLocation(new Point(20,100));
Command commandDrop = getDiagramEditPart().getCommand(dropObjectsRequest);
diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(commandDrop);
}
}
}
}
And also a function used in the code above:
protected DiagramEditPart getDiagramEditPart()
{
diagramEditor = (UmlClassDiagramForMultiEditor)papyrusEditor.getActiveEditor();
clazzdiagrameditPart = (DiagramEditPart)diagramEditor.getGraphicalViewer().getEditPartRegistry().get(diagramEditor.getDiagram());
return clazzdiagrameditPart;
}
[Updated on: Mon, 22 August 2011 12:04] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: How to create diagram elements programatically [message #837195 is a reply to message #836246] |
Thu, 05 April 2012 11:35 |
Alexandra Bardiau Messages: 21 Registered: March 2012 |
Junior Member |
|
|
Hi,
Since I've had a similar problem and spent a few hours trying to figure out how to do it (I needed to programmatically generate Interface UML elements in the model and then add them to a Class diagram), and since some of the classes used in Neslepaks's example don't exist anymore in the newer versions of Papyrus, here's to code I used to solve my problem (working, maybe needs a few adjustments like better placement of elements on the diagram, for example):
EObject model = null;
try {
// Get root element
model = UmlUtils.getUmlModel().lookupRoot();
} catch (org.eclipse.papyrus.resource.NotFoundException e) {
e.printStackTrace();
}
// Get the model package
final org.eclipse.uml2.uml.Package modelpackage = (org.eclipse.uml2.uml.Package) model;
// Add new elements to the model
TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(model);
ted.getCommandStack().execute(new RecordingCommand(ted) {
EList<Element> elementsInModel;
EList<Element> elementsInComponent;
Component c;
Port p;
Interface newItf;
Stereotype s;
protected void doExecute() {
String text = "";
elementsInModel = modelpackage.allOwnedElements();
/*
* Get all the components in the model.
*/
for (int i=0; i<elementsInModel.size(); i++) {
if (elementsInModel.get(i).eClass().getName().compareTo("Component") == 0) {
c = (Component) elementsInModel.get(i);
text = text + "Component: " + c.getName() + System.getProperty("line.separator");
/*
* For each component, get all the ports (stereotyped).
*/
elementsInComponent = c.getOwnedElements();
for (int j=0; j<elementsInComponent.size(); j++) {
if (elementsInComponent.get(j).eClass().getName().compareTo("Port") == 0) {
p = (Port) elementsInComponent.get(j);
text = text + " Port: " + p.getName() + System.getProperty("line.separator");
/*
* For each port, generate a new Interface, stereotyped with the same
* stereotype as the port.
* Rename the port from [name] to [_name].
* Associate the new interface to its corresponding port
* by typing the port with the new interface
* (port.setType(newInterface)).
*/
newItf = c.createOwnedInterface(p.getName());
p.setName("_" + p.getName());
s = p.getAppliedStereotypes().get(0);
newItf.applyStereotype(s);
p.setType(newItf);
text = text + " New inteface: " + newItf.getName() + System.getProperty("line.separator");
}
}
}
}
JOptionPane.showMessageDialog(null, text);
}
});
/*
* Generate the Class diagram containing all the newly created
* interfaces.
*/
DropObjectsRequest dropObjectsRequest = new DropObjectsRequest();
ArrayList list = new ArrayList();
EList<Element> elementsInModel = modelpackage.allOwnedElements();
for (int i=0; i<elementsInModel.size(); i++) {
if (elementsInModel.get(i).eClass().getName().compareTo("Interface") == 0)
list.add((org.eclipse.uml2.uml.Element)elementsInModel.get(i));
}
dropObjectsRequest.setObjects(list);
dropObjectsRequest.setLocation(new Point(20,100));
Command commandDrop = getDiagramEditPart().getCommand(dropObjectsRequest);
((UmlClassDiagramForMultiEditor) EditorUtils.getMultiDiagramEditor().getActiveEditor()).getDiagram();
getDiagramEditPart().getDiagramEditDomain().getDiagramCommandStack().execute(commandDrop);
Cheers!
|
|
|
Powered by
FUDForum. Page generated in 0.05392 seconds