问题
I am currently building a RCP application based on Eclipse. In one of my plugins I am adding two views via code:
layout.addView("dev.asd.tableviewer.tree", IPageLayout.LEFT, 0.25f, IPageLayout.ID_EDITOR_AREA);
layout.addView("dev.asd.tableviewer.view", IPageLayout.RIGHT, 0.75f, IPageLayout.ID_EDITOR_AREA);
The first view contains a treeviewer, the second one a tableviewer. Now I want to update the tableviewer's content according to the selection of the treeviewer. My question is, how can I reference the tableviewer from within the treeviewer? Or is there an other way to solve this problem?
回答1:
In each view, define an ID. The ID is the same as what you defined in the ID field when you defined the view in the extension element details. Here's one of mine:
public static final String ID = "gov.bop.rabid.ui.views.PrefetchedInmatesView";
In your RCP plug-in, define the following method:
public static IViewPart getView(IWorkbenchWindow window, String viewId) {
IViewReference[] refs = window.getActivePage().getViewReferences();
for (IViewReference viewReference : refs) {
if (viewReference.getId().equals(viewId)) {
return viewReference.getView(true);
}
}
return null;
}
When you want to reference a view from a different view, use the following code:
PrefetchedInmatesView view = (PrefetchedInmatesView)
RabidPlugin.getView(window, PrefetchedInmatesView.ID);
Substitute the name of your view for PrefetchedInmatesView
, and the name of your plug-in for RabidPlugin
.
回答2:
Use the SelectionService for this issue. No referencing of views required, see http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html
来源:https://stackoverflow.com/questions/4832551/eclipse-rcp-let-two-views-communicate