简体   繁体   中英

Perspective not loading in RCP when tried more than one

I am creating a RCP application. I have a view class 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;
    }
}

When I am running the application first time, I click on New Menu and everything everything is working fine. But, When I am trying to load it again by clicking on New again, it doesn't give any error. But, it doesn't load the perspective also. Is there some issue with my code?

Thanks!

edit: New is command. New calls a handler NewFileHandler which in turn calls NewView.java using the below code.

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();
        }
    }
}

}

edit:

I tried with this code.

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();
    }

GIves error.

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 doesn't do anything if the perspective is already open. You can't use this to open multiple copies of the same view.

If you just want to open a view use:

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

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

Note that this will only show one instance of the view at a time.

To close a view use:

page.hideView(viewPart);

You can show multiple copies of a view by using a secondary id for the view:

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

The secondary id does not have to be defined anywhere. It just needs to be unique for each view you want to show.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM