java - 尝试多个透视图时,RCP 中未加载透视图

标签 java eclipse rcp perspective

我正在创建一个 RCP 应用程序。我有一个 View 类 NewView。

public class NewView extends ViewPart {

    private DataBindingContext m_bindingContext;
    public static final String ID = "com.app.Editor.newView";
    SaveNewFileBean bean = new SaveNewFileBean();
    private StyledText text;

    public NewView() {
    }

    @Override
    public void createPartControl(Composite parent) {
        text = new StyledText(parent, SWT.BORDER);
        m_bindingContext = initDataBindings();
    }

    public String returnText(){
        String textData = bean.getText();
        return textData;
    }

    @Override
    public void setFocus() {

    }
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        IObservableValue observeTextTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(text);
        IObservableValue textBeanObserveValue = PojoProperties.value("text").observe(bean);
        bindingContext.bindValue(observeTextTextObserveWidget, textBeanObserveValue, null, null);
        //
        return bindingContext;
    }
}

当我第一次运行应用程序时,我单击“新建菜单”,一切正常。但是,当我尝试再次单击“新建”来再次加载它时,它不会给出任何错误。但是,它也不加载视角。 我的代码有问题吗?

谢谢!

编辑:新的是命令。 New 调用处理程序 NewFileHandler,后者又使用以下代码调用 NewView.java。

public class NewFileHandler extends AbstractHandler {

 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
 SwitchPerspectiveAction action = new SwitchPerspectiveAction();
 action.run(NewFilePerspective.ID);
    return null;
  }
 } 

SwitchPerspectiveASction 类

public class SwitchPerspectiveAction extends Action {

public void run(String newPerspectiveID) {
    if (PlatformUI.getWorkbench() != null) {
        try {

            PlatformUI.getWorkbench().showPerspective(newPerspectiveID,
                    PlatformUI.getWorkbench().getActiveWorkbenchWindow());
        } catch (WorkbenchException e) {
            e.printStackTrace();
        }
    }
}

}

编辑:

我尝试使用此代码。

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    try {
        page.showView(OpenFilePerspective.ID, "1" , IWorkbenchPage.VIEW_ACTIVATE);
    } catch (PartInitException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

给出错误。

org.eclipse.ui.PartInitException: Could not create view: com.app.Editor.openFileperspective:1
at org.eclipse.ui.internal.WorkbenchPage.busyShowView(WorkbenchPage.java:1275)
at org.eclipse.ui.internal.WorkbenchPage$14.run(WorkbenchPage.java:4208)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage.java:4204)
at com.app.editor.handlers.OpenHandler.execute(OpenHandler.java:66)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:294)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:229)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:149)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:825)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:701)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$6(HandledContributionItem.java:685)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:613)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at com.app.editor.Application.start(Application.java:20)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)
at org.eclipse.equinox.launcher.Main.main(Main.java:1438)

最佳答案

如果透视图已经打开,

showPerspective 不会执行任何操作。您不能使用它来打开同一 View 的多个副本。

如果您只想打开 View ,请使用:

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

IViewPart viewPart = page.showView("the view id");

请注意,这一次只会显示 View 的一个实例。

要关闭 View ,请使用:

page.hideView(viewPart);

您可以使用 View 的辅助 ID 显示 View 的多个副本:

page.showView("view id", "secondary id", IWorkbenchPage.VIEW_ACTIVATE);

不必在任何地方定义辅助 ID。它只需要对于您想要显示的每个 View 都是唯一的。

关于java - 尝试多个透视图时,RCP 中未加载透视图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095871/

相关文章:

java - 使用 Spring Boot 和 Spring Data JPA 的 CriteriaQuery 中出现空指针异常

java - HttpSolrServer 实例管理

java - 摆脱 org.eclipse.m2e :lifecycle-mapping 的 POM 未找到警告

java - 如何通过 Eclipse 获取类文件?

java - 由于 OSGi 包依赖性问题,无法启动 RCP 应用程序

java - 使用内部类在 JavaFX 中显示三角形

java - 如果我使用 Java 8 的 String.codePoints 获取一个 int codePoints 数组,数组的长度是字符数吗?

java - 合适的 Eclipse 版本吗?

java - 删除 View 上的最小化/最大化按钮

java - RCP 命令处理程序未扩展 "org.eclipse.core.commands.AbstractHandler"类