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.
I have write a small documentation to explain how I made, see below. (Sorry for my low level English)
In the last part of this documentation, 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 bend which I used. If somebody has an idea to allow how to have some sub-diagram for a selected element.
Thank you in
advance
_____________________________________________________________________________________________________________________________
How to open a sub-diagram from a pop-up menu in a editor generated with
GMF?
Problem
I want to open different sub-diagrams from an
action initialized with a pop up (or contextual) menu in a main editor.
I have two objectives:
- I want to open a sub-diagram with the menu,
- I want to open several sub-diagrams from a
selected element in the main diagram that show different kinds of information.
I need to know how to create a pop up menu; I
found this solution in the tutorial http://wiki.eclipse.org/GMF_Tutorial_Part_3#Custom_Actions.
After implementing this tutorial, I have created a
new class in my MainEditor.diagram Eclipse project that represent the fonction
call when option is selected in the pop up menu. This class implements
IObjectActionDelegate and have three method unimplemented:
public class MainEditorOpenXXXDiagramAction implements IObjectActionDelegate{
@Override
public void
setActivePart(IAction action, IWorkbenchPart targetPart) {
}
@Override
public void
run(IAction action) {
}
@Override
public void
selectionChanged(IAction action, ISelection selection) {
}
}
The method run
is called when the option is selected in pop up menu. Now I want to open a
diagram from this method.
I have the class OpenXXXDiagramEditPolicy
generated with the feature that allow to open a diagram with a double click on
an element in my diagram (http://www.jevon.org/wiki/GMF_Diagram_Partitioning).
How to access to the element selected in the
editor from MainEditorOpenXXXDiagramAction?
How to open the wished editor from this class?
Solutions found
Open diagram with pop up menu
The selectionChanged 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 {
// 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. I looking for always...
______________________________________________________________________________________________________________________________
DEWAS Albert
Etudiant à l'IUP Systèmes Intelligents en Master 2
Stagiaire chez Continental Toulouse - Albert.Dewas@xxxxxxxxxxxxxxxxxxxxxxxxxxx
Trésorier de l'association Kojima - www.kojima-asso.fr
Tél : 06 87 74 53 89
Email : b0b_ChoK@xxxxxxxx / a.dewas@xxxxxxxxxxx
De : b0b_ChoK <b0b_chok@xxxxxxxx>
À : gmf-dev@xxxxxxxxxxx
Envoyé le : Ven 23 avril 2010, 9h 42min 41s
Objet : [gmf-dev] Run a Edit Policy to Open a sub-diagram with pop-up menu
Hello everybody!
I work on a modelling tool based on GMF
allowing to connect Software Component between them. I would like to
open for each Software Component two sub-diagram representing the
internal architecture and the temporal behavior.
I realized my
main editor and my two sub-editor. I can reach directly in one of my
sub-editor in double clicking a Software Component in my main editor,
but not to the second.
I shall like having a pop up menu on my Software Component which allows me to open the sub-editor of my choice.
I
managed to add to the main editor new options in the pop-up menu (or
contextual menu), but I do not know how to implement the method run
which is executed when the entry of the pop-up menu is selected to open
the corresponding sub-diagram.
So I have my classes
OpenXXXDiagramEditPolicy to open every sub-diagram and my classes
OpenXXXDiagramAction which correspond to every option of the pop-up
menu.
Every class OpenXXXDiagramAction has a method
run(IAction action) to implement, but I don't know how to use the class OpenXXXDiagramEditPolicy which allows to open sub-diagram.
Could anybody help me by saying to me how to open by programming the sub-diagram defines by the policy OpenXXXDiagramEditPolicy?
Thank you in advance!
DEWAS Albert
Etudiant à l'IUP Systèmes Intelligents en Master 2
Stagiaire chez Continental Toulouse - Albert.Dewas@xxxxxxxxxxxxxxxxxxxxxxxxxxx
Trésorier de l'association Kojima - www.kojima-asso.fr
Tél : 06 87 74 53 89
Email : b0b_ChoK@xxxxxxxx / a.dewas@xxxxxxxxxxx