我在我的应用程序中遇到了一个问题,我需要在两个透视图中显示给定的视图(添加到两个不同的透视图),如果在视图中具有相同内容的任何透视图中打开一次。这意味着如果我在任何一个透视图中打开视图,当我们切换透视图时它应该为另一个透视图打开。
问问题
573 次
1 回答
0
您描述的行为在某种程度上违反了 Eclipse UI 指南和 Eclipse UI 隐喻的想法。但是,您可以使用如下代码实现所需的行为:
public class MyViewPart extends ViewPart {
public final static String ID = "foo.bar.perspectivesync.view";
private final class PerspectiveListenerImplementation implements IPerspectiveListener4 {
final Set<String> PERSPECTIVE_IDS = new HashSet<String>(Arrays.asList("foo.bar.perspectivesync.p1",
"foo.bar.perspectivesync.p2"));
@Override
public void perspectiveActivated(final IWorkbenchPage page, final IPerspectiveDescriptor perspective) {
if (PERSPECTIVE_IDS.contains(perspective.getId())) {
if (!viewIsAlreadyInPage(MyViewPart.ID, page)) {
try {
page.showView(MyViewPart.ID);
}
catch (PartInitException e) {
// TODO log exception properly
e.printStackTrace();
}
}
}
}
private boolean viewIsAlreadyInPage(final String id, final IWorkbenchPage page) {
boolean result = false;
IViewReference[] viewReferences = page.getViewReferences();
for (IViewReference ref: viewReferences) {
if (id.equals(ref.getId())) {
result = true;
}
}
return result;
}
//@formatter:off
@Override public void perspectiveChanged(final IWorkbenchPage page, final IPerspectiveDescriptor perspective, final String changeId) {}
@Override public void perspectiveChanged(final IWorkbenchPage page, final IPerspectiveDescriptor perspective, final IWorkbenchPartReference partRef, final String changeId) {}
@Override public void perspectiveSavedAs(final IWorkbenchPage page, final IPerspectiveDescriptor oldPerspective, final IPerspectiveDescriptor newPerspective) {}
@Override public void perspectiveOpened(final IWorkbenchPage page, final IPerspectiveDescriptor perspective) {}
@Override public void perspectiveDeactivated(final IWorkbenchPage page, final IPerspectiveDescriptor perspective) {}
@Override public void perspectiveClosed(final IWorkbenchPage page, final IPerspectiveDescriptor perspective) {}
@Override public void perspectivePreDeactivate(final IWorkbenchPage page, final IPerspectiveDescriptor perspective) {}
//@formatter:on
}
private final IPerspectiveListener4 perspectiveListener = new PerspectiveListenerImplementation();
@Override
public void init(final IViewSite site) throws PartInitException {
super.init(site);
site.getWorkbenchWindow().addPerspectiveListener(perspectiveListener);
}
@Override
public void dispose() {
getSite().getWorkbenchWindow().removePerspectiveListener(perspectiveListener);
super.dispose();
}
//@formatter:off
@Override public void createPartControl(final Composite parent) {}
@Override public void setFocus(){}
// @formatter:on
}
于 2012-06-04T15:13:42.383 回答