尝试多个时,不在RCP中加载透视图

时间:2015-08-19 12:44:03

标签: java eclipse rcp perspective

我正在创建一个RCP应用程序。我有一个视图类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再次加载它时,它不会给出任何错误。但是,它也没有加载视角。 我的代码有问题吗?

谢谢!

编辑:新命令。 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 Class

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)

1 个答案:

答案 0 :(得分:1)

如果视角已经打开,

showPerspective什么都不做。您无法使用此功能打开同一视图的多个副本。

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

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

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

请注意,这一次只会显示一个视图实例。

要关闭视图,请使用:

page.hideView(viewPart);

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

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

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