如何将keyListener添加到ViewPart

时间:2017-10-15 07:45:26

标签: eclipse-plugin eclipse-rcp

我尝试在ESC按钮上添加关闭视图的可能性。我有一个视图NewsContentView并尝试通过这种方式添加键侦听器

this.addListenerObject(new KeyListener() {

   @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.keyCode == SWT.ESC){
                IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                activePage.hideView(activePage.findView(ID));
            }
        }
    });

但得到错误

  

java.lang.ClassCastException:com.forexgame.ui.views.NewsContentView $ 1无法强制转换为org.eclipse.ui.IPropertyListener       在org.eclipse.ui.part.WorkbenchPart.firePropertyChange(WorkbenchPart.java:127)       在org.eclipse.ui.part.WorkbenchPart.internalSetPartName(WorkbenchPart.java:466)       在org.eclipse.ui.part.WorkbenchPart.setPartName(WorkbenchPart.java:384)       at org.eclipse.ui.part.ViewPart.setPartName(ViewPart.java:129)       在com.forexgame.ui.views.NewsContentView.setName(NewsContentView.java:88)       在com.forexgame.ui.views.NewsView.showNewsContentView(NewsView.java:133)       在com.forexgame.ui.views.NewsView.access $ 0(NewsView.java:121)       在com.forexgame.ui.views.NewsView $ 1.doubleClick(NewsView.java:92)       在org.eclipse.jface.viewers.StructuredViewer $ 1.run(StructuredViewer.java:832)       在org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)       在org.eclipse.ui.internal.JFaceUtil $ 1.run(JFaceUtil.java:50)       在org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:173)       在org.eclipse.jface.viewers.StructuredViewer.fireDoubleClick(StructuredViewer.java:829)       在org.eclipse.jface.viewers.StructuredViewer.handleDoubleSelect(StructuredViewer.java:1150)       在org.eclipse.jface.viewers.StructuredViewer $ 4.widgetDefaultSelected(StructuredViewer.java:1263)       在org.eclipse.jface.util.OpenStrategy.fireDefaultSelectionEvent(OpenStrategy.java:252)       在org.eclipse.jface.util.OpenStrategy.access $ 0(OpenStrategy.java:249)       在org.eclipse.jface.util.OpenStrategy $ 1.handleEvent(OpenStrategy.java:311)       在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)       在org.eclipse.swt.widgets.Display.sendEvent(Display.java:4362)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1113)       在org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4180)       在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3769)       at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 4.run(PartRenderingEngine.java:1127)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)       在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)       在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)       在org.eclipse.ui.internal.Workbench $ 5.run(Workbench.java:694)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)       在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:606)       在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)       在com.forexgame.application.Application.start(Application.java:20)       在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)       在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)       在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)       在org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)       在org.eclipse.equinox.launcher.Main.run(Main.java:1515)       在org.eclipse.equinox.launcher.Main.main(Main.java:1488)

此外,我寻找机会将Key Listener添加到活动页面,但IWorkBenchPage接口没有这样的方法,只有方法

                    activePage.addPartListener(IPartListener listener);
                activePage.addPartListener(IPartListener2 listener);
                activePage.addPostSelectionListener(ISelectionListener listener);
                activePage.addPostSelectionListener(String partId, ISelectionListener listener);
                activePage.addSelectionListener(ISelectionListener listener);
                activePage.addSelectionListener(String partId, ISelectionListener listener);
                activePage.addPropertyChangeListener(IPropertyChangeListener listener);

为什么会出现此错误以及是否有不同的方法在视图上添加关键侦听器?

0 个答案:

没有答案