|
|
|
|
|
Re: Accessing a element in Model Explorer [message #1284486 is a reply to message #1284479] |
Fri, 04 April 2014 14:40 |
Vinicius Pereira Messages: 50 Registered: March 2014 |
Member |
|
|
From what I saw in the code that get the ID (it is another program): org.eclipse.emf.ecore.xmi.XMLResource.getID(EObject eObject)
public String getUMLId() {
String id=((XMLResource) this.uml_state.eResource()).getID(uml_state);
return id;
}
And used here:
out.write(pred.getPredicateName()+","+state.getUMLId()+"\n");
In this example, using just States in State Diagram.
Remembering that I only have access to file with "someName,ID"... and this "someName" is not in the Papyrus Model.
[Updated on: Fri, 04 April 2014 14:44] Report message to a moderator
|
|
|
Re: Accessing a element in Model Explorer [message #1284501 is a reply to message #1284486] |
Fri, 04 April 2014 14:57 |
Camille Letavernier Messages: 952 Registered: February 2011 |
Senior Member |
|
|
Ok, so this looks like the XMI ID (URI Fragment). As this is only the last segment of the URI, this can only work if you have a single UML Model (Resource/File), but here's the idea:
Object[] visibleElement = commonViewer.getVisibleExpandedElements();
modelRoot = EMFHelper.getEObject(visibleElement[0]); //There are much better ways to access a model element, but I'm not sure what you have access to, at this time
Resource umlResource = modelRoot.eResource(); //The UML Resource
EObject umlElement = umlResource.getEObject(id); //If id it the EObject's URI Fragment, this will return the correct EObject
if (umlElement instanceof State){
State state = (State)umlElement;
... //Now you have the UML State element
}
The code used to retrieve the model root element is not pretty. You can probably use the ModelExplorer ViewPart to retrieve the Papyrus ServicesRegistry, then lookup the UmlModel, and retrieve the root element of this UmlModel. This is a little bit more complex, but it is also safer. However, I haven't tested, so it may contain a few errors:
ModelExplorerPageBookView view = ...;
ServicesRegistry registry = view.getAdapter(ServicesRegistry.class);
try {
ModelSet papyrusModelSet = registry.getService(ModelSet.class);
UmlModel umlModel = (UmlModel)papyrusModelSet.getModel(UmlModel.MODEL_ID);
EObject modelRoot = umlModel.lookupRoot();
... //Now you have the modelRoot
} catch (ServiceException ex){
//Do log
}
Camille Letavernier
|
|
|
Re: Accessing a element in Model Explorer [message #1284508 is a reply to message #1284501] |
Fri, 04 April 2014 15:09 |
Vinicius Pereira Messages: 50 Registered: March 2014 |
Member |
|
|
Thanks Camille, I will try
What I have are: a trace result (text file), a "mapping" file (also text file) and the Papyrus model already open in Eclipse.
I open the trace result in a view and the user clicks in the line that he wants "to analyze". Then I get the line, extract the item from this line and check the mapping, get the ID from mapping and show in Model Explorer (and later in the Editor).
And then I repeat this to every line in "Trace View" that the user clicks.
Right now, I'm trying to get the UML element in Model Explorer using the ID from "mapping".
I'm starting with State Diagram, but the model has also Sequence, Class, Object and Interactive Overview Diagrams.
Thank you for your help
[Updated on: Fri, 04 April 2014 15:16] Report message to a moderator
|
|
|
Re: Accessing a element in Model Explorer [message #1288171 is a reply to message #1284501] |
Tue, 08 April 2014 10:35 |
Vinicius Pereira Messages: 50 Registered: March 2014 |
Member |
|
|
Camille, I managed to catch the element I wanted in the Model Explorer using the two versions that you showed me! Indeed, the second option is better, especially if we consider that the first one has the getVisibleExpandedElements() method and it's not cool to expand all the tree just to find a node...
Anyway, thank you so much
One last problem (I hope): I can't show the selected UML element in the Model Explorer (open the tree to show it). I don't know what I'm missing...
Here I get the Model Explorer:
private void getModelExplorerView(String elementID) {
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
IViewPart modelExplorerViewPart;
try {
modelExplorerViewPart = window.getActivePage().showView(modelExplorerViewId);
} catch (PartInitException ex) {
ex.printStackTrace(System.out);
return;
}
ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelExplorerViewPart;
IPage currentPage = view.getCurrentPage();
ModelExplorerPage page = (ModelExplorerPage)currentPage;
IViewPart viewer = page.getViewer();
ModelExplorerView modelExplorerView = (ModelExplorerView)viewer;
CommonViewer commonViewer = modelExplorerView.getCommonViewer();
findElementInModelExplorer(commonViewer, view, elementID);
// What I have tried...
/*
EObject element = findElementInModelExplorer(commonViewer, view, elementID);
commonViewer.getControl().setFocus();
commonViewer.setSelection(new StructuredSelection(element), true);
commonViewer.reveal(element);
commonViewer.getSelection();
commonViewer.expandToLevel(element, 1);
*/
}
And here I find the UML Element:
private void findElementInModelExplorer(TreeViewer treeViewer, ModelExplorerPageBookView view, String elementID) {
ServicesRegistry registry = (ServicesRegistry) view.getAdapter(ServicesRegistry.class);
try {
ModelSet papyrusModelSet = registry.getService(ModelSet.class);
UmlModel umlModel = (UmlModel)papyrusModelSet.getModel(UmlModel.MODEL_ID);
EObject modelRoot = umlModel.lookupRoot();
//The UML Resource
Resource umlResource = modelRoot.eResource();
//If ID is the EObject's URI Fragment, this will return the correct EObject
EObject umlElement = umlResource.getEObject(elementID.trim());
if (umlElement instanceof State){
//Now I have the UML State element
State state = (State)umlElement;
// Just to see if is right
System.out.println(state.getName());
}
} catch (ServiceException | NotFoundException e) {
e.printStackTrace();
}
}
thanks
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04560 seconds