|
|
Re: How to obtain graphical Object from EditPart [message #1078654 is a reply to message #1076550] |
Sat, 03 August 2013 10:11 |
jonas b Messages: 23 Registered: July 2013 |
Junior Member |
|
|
Hello Camille,
Thanks for your reply. I think I should elaborate a bit what I am actually trying to do.
I am writing a plugin which consists of a view. In this view I want to display state machine diagrams. The view is read-only, ie the user must not be able to edit the diagram through the view. The plugin however must be able to edit the appearance of any figure in the diagram. (The purpose of the plugin is to show the active state of the state machine. It does that by increasing line width of the current state for example.)
Since the view is read-only, I do not need anything from the EditPart tree or from the Model tree. I only need the IFigure tree. In other words: I only need the right part of this structure:
The view must be able to display multiple instances of the same diagram. Therefore I need a deep copy of the entire IFigure tree starting from the root figure obtained by rootEditPart.getFigure() going down to every single leaf. Otherwise it would not be possible to add multiple instances of the same diagram to a common root figure or alter their appearance independently of each other. To achieve this deep copy, I see 2 possibilities:
1) I could write a cloning class which provides a method to recursively clone every single IFigure in the hierarchy. This approach is very tedious, since an empty state machine created by Papyrus already consists of 20+ IFigures. I would have to provide a copy method specific to every single subtype of IFigure. I took a look at the type hierarchy, it's huge.
2) Since I have the diagrams available as a combination of .di, .notation and .model files, it would be much easier to simply read these files and create any amount of instances anew. However I haven't found the place yet where this is done. Does the Papyrus project read in these files and construct its various diagrams? I suppose so, since many Elements are classes defined in Papyrus. Can you please point me to the plugin responsible for this task, so I can reuse the code for my plugin?
|
|
|
Re: How to obtain graphical Object from EditPart [message #1079908 is a reply to message #1076250] |
Mon, 05 August 2013 08:19 |
Camille Letavernier Messages: 952 Registered: February 2011 |
Senior Member |
|
|
Hi Jonas,
OK, I see what you're trying to do.
Quote:Since the view is read-only, I do not need anything from the EditPart tree or from the Model tree. I only need the IFigure tree. In other words: I only need the right part of this structure:
Yes; this includes EditParts (Not only Figures). Actually, you should never access Figures directly.
Quote:The view must be able to display multiple instances of the same diagram. Therefore I need a deep copy of the entire IFigure tree starting from the root figure obtained by rootEditPart.getFigure() going down to every single leaf. Otherwise it would not be possible to add multiple instances of the same diagram to a common root figure or alter their appearance independently of each other. To achieve this deep copy, I see 2 possibilities:
You can open the same diagram twice, programmatically, using the Papyrus IPageManager service. This will open a new tab (nested editor) on the same diagram, and duplicate the EditPart tree. They will still share the same Notation Model.
Diagram diagram = ...;
IPageManager pageManager = ServiceUtilsForEObject.getInstance().getIPageManager(diagram);
pageManager.openPage(diagram);
But then there's an issue: most EditParts have style setters, but they are protected. And Figures should not be manipulated either.
Moreover, even if you manage to change the style of an EditPart or a Figure, it will be overridden as soon as the diagram is refreshed (Because it will recompute its style from its notation Model)
There's another way to achieve this, with Diagram Stylesheets. CSS Stylesheets are used in Papyrus to dynamically compute the appearance of a Notation Model element. They also support a little bit of animation. However, as they are used on the Notation Model, you can't have multiple instances of the same diagram with different appearances (Because they share the same Notation Model). Currently, the best way to do that would be to duplicate the Diagram in the Notation Model.
So, currently, it is complicated to apply two different appearances to the same notation::Diagram. Everything else can be achieved rather easily through Stylesheets.
Regards,
Camille
Camille Letavernier
|
|
|
|
|
|
|
Re: How to obtain graphical Object from EditPart [message #1085876 is a reply to message #1080698] |
Tue, 13 August 2013 13:38 |
jonas b Messages: 23 Registered: July 2013 |
Junior Member |
|
|
I see. I followed the strategies in your test cases. To load a diagram from a file, I did the following:
private CSSDiagram init(URI uri) {
ResourceSet resourceSet = new ResourceSetImpl();
CSSHelper.installCSSSupport(resourceSet);
try {
return (CSSDiagram) EMFHelper.loadEMFModel(resourceSet, uri);
} catch (IOException ex) {}
return null;
}
@Override
public void createPartControl(Composite parent) {
URI uri = URI.createPlatformPluginURI(AnimateActivator.PLUGIN_ID + "/Ampel/model.notation", true);
Diagram diagram = init(uri);
}
Now that I have my Diagram, how can I build the whole structure including EditParts, and views without opening an editor? Because I'm extending ViewPart and eventually I want to draw onto a View, not an Editor.
I did the following:
@Override
public void createPartControl(Composite parent) {
URI uri = URI.createPlatformPluginURI(AnimateActivator.PLUGIN_ID + "/Ampel/model.notation", true);
Diagram diagram = init(uri);
EditPartViewer viewer = new DiagramGraphicalViewer();
viewer.setEditPartFactory(new UMLEditPartFactory());
viewer.setRootEditPart(new ScalableRootEditPart());
viewer.createControl(parent);
viewer.setContents(diagram);
}
but I got NPE because of missing EditingDomain. So I created the resource set from an editing domain:
private CSSDiagram init(URI uri) {
TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain();
domain.createResource(uri.toPlatformString(true));
ResourceSet resourceSet = domain.getResourceSet();
CSSHelper.installCSSSupport(resourceSet);
try {
return (CSSDiagram) EMFHelper.loadEMFModel(resourceSet, uri);
} catch (IOException ex) {}
return null;
}
Now I get a big pink rectangle inside the view, followed by an NPE and many, many messages like this:
!ENTRY org.eclipse.gmf.runtime.draw2d.ui 2 2 2013-08-13 16:13:55.351
!MESSAGE MapModeUtil#getMapMode(IFigure fig) fig parameter must be attached to a parent - using default MapMode
The NPE is caused by org.eclipse.gmf.runtime.diagram.ui.editpolicies.DiagramPopupBarEditPolicy.addPaletteListener and I presume it's because there is no host editpart present.
[Updated on: Tue, 13 August 2013 14:18] Report message to a moderator
|
|
|
Re: How to obtain graphical Object from EditPart [message #1087943 is a reply to message #1076250] |
Fri, 16 August 2013 10:23 |
jonas b Messages: 23 Registered: July 2013 |
Junior Member |
|
|
I have a solution (found it here: http://www.eclipse.org/forums/index.php?t=msg&th=58758/)
private CSSDiagram createDiagram() {
URI notationUri = URI.createPlatformPluginURI(AnimateActivator.PLUGIN_ID + "/Ampel/model.notation", true);
ResourceSet resourceSet = new ResourceSetImpl();
CSSHelper.installCSSSupport(resourceSet);
TransactionalEditingDomain domain = DiagramEditingDomainFactory.getInstance().createEditingDomain(resourceSet);
CSSDiagram diagram = null;
try {
diagram = (CSSDiagram)EMFHelper.loadEMFModel(resourceSet, notationUri);
} catch (IOException e) {
e.printStackTrace();
}
return diagram;
}
private DiagramGraphicalViewer createViewer(Diagram diagram, Composite parent) {
DiagramGraphicalViewer viewer = new DiagramGraphicalViewer();
viewer.setEditPartFactory(EditPartService.getInstance());
viewer.createControl(parent);
viewer.setRootEditPart(new DiagramRootEditPart(MeasurementUnit.PIXEL_LITERAL));
viewer.setContents(diagram);
return viewer;
}
@Override
public void createPartControl(Composite parent) {
Composite c = new Composite(parent, SWT.NONE);
c.setLayout(new RowLayout());
for (int i = 0; i < 2; i++)
createViewer(createDiagram(), c);
} With this code I get 2 diagrams drawn on my view. With the line CSSHelper.installCSSSupport(resourceset); they also have the same appearance as when I'd open them in papyrus editor. Next step is to start a transaction to modify one of the models.
[Updated on: Mon, 19 August 2013 10:41] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03851 seconds