Opening model explorer programmatically and selecting content in it [message #654905] |
Thu, 17 February 2011 10:05  |
Eclipse User |
|
|
|
Hi!
Im trying to open the model explorer and selecting a model element in it using the XMI-ID.
Here is the code that I have so far:
public void showTracePoint(TracePoint tp) {
// create editor input
FileEditorInput fileEditorInput = createFileEditorInput(tp.getResourceID());
// open editor
PapyrusMultiDiagramEditor papyrusMultiDiagramEditor = null;
try {
papyrusMultiDiagramEditor = (PapyrusMultiDiagramEditor) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.openEditor(fileEditorInput, EDITOR_ID);
} catch (PartInitException e) {
Activator.logError(e);
}
// open explorer
ModelExplorerPageBookView modelExplorerPageBookView = null;
try {
modelExplorerPageBookView = (ModelExplorerPageBookView) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.showView(EXPLORER_ID);
} catch (PartInitException e) {
Activator.logError(e);
}
// obtain model element
EObject model = null;
try {
model = UmlUtils.getUmlModel().lookupRoot();
} catch (NotFoundException e) {
Activator.logError(e);
}
if (model != null){
EObject element = null;
Iterator<EObject> it = model.eAllContents();
while(it.hasNext()) {
EObject current = it.next();
if(current.eResource().getURIFragment(current).equals(tp.getPositionID())) {
element = current;
break;
}
}
// TODO stuck here
}
}
My first opinion was to obtain the corresponding ModelElementItem, create a StructuredSelection and set the selection.
If I understand it right the method ModelExplorerPageBookView.findElementForEObject(TreeViewer treeViewer, EObject eObjectToFind) should give me the ModelElementItem.
I need the TreeViewer to use this method and to set the selection.
But I can't obtain the TreeViewer. There are access restriction while doing this:
((ModelExplorerPage)modelExplorerPageBookView.getCurrentPage()).getViewer()
I tried a lot of other things without success.
Am I on the right way? If yes, how to proceed? If no, which is the best way to implement such use case?
Best regards,
guersoy
|
|
|
Re: Opening model explorer programmatically and selecting content in it [message #657582 is a reply to message #654905] |
Thu, 03 March 2011 06:13  |
Eclipse User |
|
|
|
I solved it. Here is the complete code:
public void showTracePoint(TracePoint tp) throws TracingException {
// create editor input
FileEditorInput fileEditorInput = createFileEditorInput(tp.getResourceID());
// open editor
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.openEditor(fileEditorInput,EDITOR_ID);
} catch (PartInitException e) {
throw new TracingException("Could not open model editor.", e);
}
// open explorer
ModelExplorerPageBookView modelExplorerPageBookView = null;
try {
modelExplorerPageBookView = (ModelExplorerPageBookView) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.showView(EXPLORER_ID);
} catch (PartInitException e) {
throw new TracingException("Could not open model explorer.", e);
}
// obtain model element
EObject model = null;
try {
model = UmlUtils.getUmlModel().lookupRoot();
} catch (NotFoundException e) {
throw new TracingException("Could not obtain model root.", e);
}
if (model != null){
EObject element = null;
Iterator<EObject> it = model.eAllContents();
while(it.hasNext()) {
EObject current = it.next();
if(current.eResource().getURIFragment(current).equals(tp.getPositionID())) {
element = current;
break;
}
}
if (element == null)
throw new TracingException("Element does not exist.");
// select in view
IPage page = modelExplorerPageBookView.getCurrentPage();
IViewPart viewPart = (IViewPart) ((IAdaptable) page).getAdapter(IViewPart.class);
if (viewPart instanceof ModelExplorerView) {
ModelExplorerView modelExplorerView = (ModelExplorerView) viewPart;
if (element != null) {
CommonViewer treeViewer = modelExplorerView.getCommonViewer();
Object modelElementItem = modelExplorerPageBookView.findElementForEObject (treeViewer, element);
if (modelElementItem != null) {
TreePath treePath = new TreePath(new Object[] {
modelElementItem
});
EObject parent = element.eContainer();
if (parent != null) {
Object parentElement = modelExplorerPageBookView.findElementForEObject (treeViewer, parent);
treeViewer.expandToLevel(parentElement, 1);
}
treeViewer.setSelection(new TreeSelection (treePath), true);
}
}
}
}
}
guersoy
|
|
|
Powered by
FUDForum. Page generated in 0.04138 seconds