Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Accessing a element in Model Explorer
Accessing a element in Model Explorer [message #1283571] Thu, 03 April 2014 15:19 Go to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Hi everyone.

I want to find a element in Model Explorer, but first I need to access the Model Explorer content.

I did the code above... the commented System.out returns "Model Explorer", so I think I'm in the right way.

I see in Papyrus SDK that ModelExplorerPageBookView contains one ModelExplorerView. Then, ModelExplorerView has a getCommonViewer() method and I guess that I can use this to access the TreeView in Model Explorer View.

But how can I "link" ModelExplorerPageBookView to ModelExplorerView??

Thanks

private void verifiesModelExplorerView() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		
		IViewReference[] views = window.getActivePage().getViewReferences();
		
		IViewPart viewPart = null;
		
		for (IViewReference view : views) {
			if (view.getTitle().equals("Model Explorer")) {
				// viewPart receives a ModelExplorerPageBookView view
				viewPart = view.getView(true);
				
				// Get out from the for statement
				break;
			}
		}
		
		ModelExplorerPageBookView modelExplorerPageBookView = new ModelExplorerPageBookView();
		
		if (viewPart instanceof ModelExplorerPageBookView) {
			modelExplorerPageBookView = (ModelExplorerPageBookView) viewPart;
		}
		else {
			System.out.println("viewPart should be a instance of ModelExplorerPageBooView.");
		}
		
		//System.out.println(modelExplorerPageBookView.getTitle());
		
		// link ModelExplorerPageBookView with ModelExplorerView
		
		// and through CommonViewer
		//CommonViewer commonViewer = modelExplorerView.getCommonViewer();
		
		//modelExplorerView.getControl().setFocus();
	}
Re: Accessing a element in Model Explorer [message #1283589 is a reply to message #1283571] Thu, 03 April 2014 15:40 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 932
Registered: February 2011
Senior Member
Hi,


In the ModelExplorer test case, we use this code to initialize all intermediate elements:

// we look for the modelexplorer
IViewPart modelexplorer;
try {
	modelexplorer = activeWorkbenchWindow.getActivePage().showView(AbstractHandlerTest.viewId);
} catch (PartInitException ex) {
	ex.printStackTrace(System.out);
	setStatus(new Status(IStatus.ERROR, bundle.getSymbolicName(), ex.getMessage()));
	return;
}
ModelExplorerPageBookView view = (ModelExplorerPageBookView)modelexplorer;
IPage currentPage = view.getCurrentPage();
ModelExplorerPage page = (ModelExplorerPage)currentPage;
IViewPart viewer = page.getViewer();
modelExplorerView = (ModelExplorerView)viewer;
modelExplorerView.setFocus();

// we look for the common viewer
commonViewer = modelExplorerView.getCommonViewer();


However, you can access directly to the Workbench SelectionService:

selectionService = activeWorkbenchWindow.getSelectionService();
ISelection selection = selectionService.getSelection();


(The ModelExplorer View must be the active view)

If you want to set the selection, it is also simpler:

modelExplorerView.selectReveal(selection);


You can see some examples in the ModelExplorer tests plug-in:

http://git.eclipse.org/c/papyrus/org.eclipse.papyrus.git/tree/tests/junit/plugins/views/modelexplorer/org.eclipse.papyrus.views.modelexplorer.tests


Regards,
Camille


Camille Letavernier
Re: Accessing a element in Model Explorer [message #1283601 is a reply to message #1283589] Thu, 03 April 2014 15:56 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Thanks Camille!

I will see this Smile
Re: Accessing a element in Model Explorer [message #1284347 is a reply to message #1283589] Fri, 04 April 2014 11:16 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Now I can get the Model Explorer View, thanks!!

But where is the ID of the elements?
For example, I have the ID for a state in one State Diagram. How I can do something like:

// Or commonViewer
node = treeViewer.getNode(ID);

// To select the node in Model Explorer
modelExplorerView.selectReveal(node);


The ID for me is a String. I have a file with several "someName,IdOfEachState". So I have someName, get the ID and then need to find this ID in the Model Explorer View.
Re: Accessing a element in Model Explorer [message #1284479 is a reply to message #1284347] Fri, 04 April 2014 14:27 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 932
Registered: February 2011
Senior Member
What is an "ID" in your model?

Is that the XMI ID of the UML Element, as serialized by EMF?

Is it the name of the UML NamedElement?

Is it something else?


Camille Letavernier
Re: Accessing a element in Model Explorer [message #1284486 is a reply to message #1284479] Fri, 04 April 2014 14:40 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
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 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 932
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 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Thanks Camille, I will try Smile

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". Smile


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 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
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 Smile


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
Re: Accessing a element in Model Explorer [message #1288203 is a reply to message #1288171] Tue, 08 April 2014 11:06 Go to previous messageGo to next message
Camille Letavernier is currently offline Camille LetavernierFriend
Messages: 932
Registered: February 2011
Senior Member
Hi,


You can use this code:

modelExplorerView.revealSemanticElement(List);


IRevealSemanticElement#revealSemanticElement is used for synchronizing the selection in the ModelExplorer and Diagrams. It's higher level than CommonNavigation#selectReveal (Which I erroneously recommended in my first reply)


Camille


Camille Letavernier
Re: Accessing a element in Model Explorer [message #1288267 is a reply to message #1288203] Tue, 08 April 2014 12:20 Go to previous message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Thanks Camille!! Works Smile
Previous Topic:ProR ReqIF to Papyrus
Next Topic:Find UML element in Editor
Goto Forum:
  


Current Time: Tue Nov 24 18:51:01 GMT 2020

Powered by FUDForum. Page generated in 0.02518 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top