Hello !
After numerous tours and bends, I found approximately how to open a sub-diagram since a pull-down menu.
But I have some concerns to open several sub-diagrams different since a contextual menu and since the same element.
To explain better my problem, I am going to show you at first how I open a sub-diagram since a contextual menu:
Open diagram with pop up menu
The selection parameter in selectionChanged method from MainEditorOpenXXXDiagramAction indicates the element selected in the editor. We need to cast this attribute in IStructuredSelection to access to the contained element:
@Override
public void selectionChanged(IAction action, ISelection selection) {
IStructuredSelection select = (IStructuredSelection) selection;
if(select.getFirstElement() instanceof MyElement_TypeEditPart){
selectedElement = (MyElement_TypeEditPart) select.getFirstElement();
}
}
With this example, we recover the element selected in an attributes selectedElement which. Now we need to initialize the new diagram with the OpenXXXDiagramEditPolicy. The method getOpenCommand allow creating the command that open the diagram, but the generated method have a Request parameter. If we look at the code, we see that the editPart element is extracted from the request parameter. We can overload the method with an editPart in replacement the Request:
/**
* @generated NOT
*/
public Command getOpenCommand(EditPart request) {
EditPart targetEditPart = request;
if (false == targetEditPart.getModel() instanceof View) {
return null;
}
View view = (View) targetEditPart.getModel();
Style link = view.getStyle(NotationPackage.eINSTANCE
.getHintedDiagramLinkStyle());
if (false == link instanceof HintedDiagramLinkStyle) {
return null;
}
return new ICommandProxy(new OpenXXXDiagramCommand(
(HintedDiagramLinkStyle) link));
}
Now in my MainEditorOpenXXXDiagramAction class, I can call the getOpenCommand with my selectedElement who are an EditPart:
@Override
public void run(IAction action) {
OpenIBDiagramEditPolicy od = new OpenIBDiagramEditPolicy();
Command cmd = od.getOpenCommand(aswcEP);
if(cmd != null && cmd.canExecute())
cmd.execute();
}
To open the diagram, we need just to execute the command.
Open several diagram from the same selected element
The OpenXXXDiagramEditPolicy class allows opening a diagram which has a designed element as root in the Meta model.
If we try to apply the next solution to open several diagram, we saw that the same diagram is always open. I have a solution to bypass them.
If we look at the class OpenXXXDiagramCommand (in OpenXXXDiagramEditPolicy), the method doExecuteWithResult test if they are already a sub-diagram initialized from the selected element. Obviously, a diagram initialized from an element is unique and I have not found for the moment the good solution:
/**
* @generated NOT
*/
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
try {
// bypass the backup of old diagram
// Diagram diagram = getDiagramToOpen();
// if (diagram == null) {
Diagram diagram = intializeNewDiagram();
// }
URI uri = EcoreUtil.getURI(diagram);
String editorName = uri.lastSegment()
+ "#" + diagram.eResource().getContents().indexOf(diagram); //$NON-NLS-1$
IEditorInput editorInput = new URIEditorInput(uri, editorName);
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
page.openEditor(editorInput, getEditorID());
return CommandResult.newOKCommandResult();
} catch (Exception ex) {
throw new ExecutionException("Can't open diagram", ex);
}
}
In this case, I force the initialization of a new diagram to each call of the method. They are some disadvantage with this solution. First, we don't control the memory stack, and two we can save the arrangement of element.
You will see that I cheated and that I initialize a new diagram in every opening of a sub-diagram. I am conscious that this solution is not optimal and can be problematic.
I look for a solution who shall allow to avoid the tip which I used. If somebody has an idea to allow how to have some sub-diagram for a selected element with a better method...
Thank you in advance