Java 需要EclipseIDE插件开发人员帮助:重命名活动编辑器TextEditor的文件资源

Java 需要EclipseIDE插件开发人员帮助:重命名活动编辑器TextEditor的文件资源,java,eclipse,eclipse-plugin,Java,Eclipse,Eclipse Plugin,从Eclipse中的一个插件中,我绑定了一个组合键,以尝试使用ProjectExplorer重构对话框重命名由TextEditor编辑的文件资源。编辑器是当时处于活动状态的选定视图。到目前为止我还没有成功 如果我将另一个键组合绑定到同一个命令ID,即“org.eclipse.ui.edit.rename”,然后在ProjectExplorer中手动选择文件资源并执行键序列,那么对话框确实会出现。这是否意味着我需要以编程方式关注ProjectExplorer 有谁能给我提供一些建议,告诉我如何通过

从Eclipse中的一个插件中,我绑定了一个组合键,以尝试使用ProjectExplorer重构对话框重命名由TextEditor编辑的文件资源。编辑器是当时处于活动状态的选定视图。到目前为止我还没有成功

如果我将另一个键组合绑定到同一个命令ID,即“org.eclipse.ui.edit.rename”,然后在ProjectExplorer中手动选择文件资源并执行键序列,那么对话框确实会出现。这是否意味着我需要以编程方式关注ProjectExplorer

有谁能给我提供一些建议,告诉我如何通过编程重构/重命名与活动编辑器关联的资源?我不想执行“另存为”,因为这会将更改提交到磁盘,我也不想强制用户执行此操作

以下是处理程序为自定义键绑定命令调用的测试代码

    public Object execute( ExecutionEvent event ) throws ExecutionException
    {
        IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked( event );
        if( window == null ) return null;

        IWorkbenchPage workbench_page = window.getActivePage();
        if( workbench_page == null ) return null;

        IEditorPart active_editor = workbench_page.getActiveEditor();
        if( !( active_editor instanceof ITextEditor ) ) return null;

        // Not used here yet...
        StyledText styled_text = (StyledText)( (ITextEditor)active_editor).getAdapter( Control.class );
        Display display = styled_text.getDisplay();

        switch( event.getCommand().getId() )
        {
            case "test.commands.sampleCommand":
            {
                IViewPart view_part  = workbench_page.findView( "org.eclipse.ui.navigator.ProjectExplorer" );
                if( ( view_part != null ) && ( view_part instanceof ProjectExplorer ) )
                {
                    // Try and select the resource. This does not seem to do anything visually...
                    IPath path = ( (IPathEditorInput)active_editor.getEditorInput() ).getPath();
                    if( path == null ) return null;
                    ( (ISetSelectionTarget)view_part ).selectReveal( new StructuredSelection( path ) );

                    ICommandService command_service = ( (ProjectExplorer)view_part ).getViewSite().getService( ICommandService.class );
                    if( command_service == null ) return null;
                    Command file_rename_command = command_service.getCommand( IWorkbenchCommandConstants.FILE_RENAME );

                    try
                    {
                        // Rename the selected resource.
                        file_rename_command.executeWithChecks( new ExecutionEvent() );
                    }
                    catch( NotDefinedException | NotEnabledException | NotHandledException exception )
                    {
                        log_error( "Did not work :", exception );
                        return null;
                    }

                    log_info( "Success :" + view_part.toString() );
                }

                return null;
            }

            default: return null;
        }
    }
以下是结果调试日志输出

!ENTRY test 1 0 2019-12-19 09:47:06.902
!MESSAGE Did not work :
!STACK 0
org.eclipse.core.commands.NotHandledException: There is no handler to execute for command org.eclipse.ui.edit.rename
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:511)
    at test.Activator.execute(Activator.java:125)
    at test.handlers.SampleHandler.execute(SampleHandler.java:18)
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:283)
    at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:95)
    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:58)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:318)
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:252)
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:173)
    at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:156)
    at org.eclipse.core.commands.Command.executeWithChecks(Command.java:498)
    at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
    at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:308)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:584)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:653)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:443)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$2(KeyBindingDispatcher.java:386)
    at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:96)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
    at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1199)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1056)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1081)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1066)
    at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1108)
    at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1104)
    at org.eclipse.swt.widgets.Widget.wmKeyDown(Widget.java:1759)
    at org.eclipse.swt.widgets.Control.WM_KEYDOWN(Control.java:5146)
    at org.eclipse.swt.widgets.Canvas.WM_KEYDOWN(Canvas.java:414)
    at org.eclipse.swt.widgets.Control.windowProc(Control.java:4793)
    at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:348)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:4820)
    at org.eclipse.swt.internal.win32.OS.DispatchMessage(Native Method)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3583)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1160)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1049)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
    at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:633)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:557)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:150)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:137)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:107)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
    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:660)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:597)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1468)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1441)
谢谢,比尔, 拉尔夫

PS,
完全不确定,我在eclipse论坛上也问了这个问题。

大多数编辑器不提供重命名处理程序。事实上,编辑器需要添加额外的代码来处理进行重命名的其他事情-您是否检查过编辑器是否确实处理在其他地方进行的重命名?@greg-449,谢谢,是的,这就是我试图完成的,即为编辑器添加重命名功能。但我对整个EclipseIDE体系结构和组件了解不够,无法实现这一点。如果我找不到答案,我将不得不开始挖掘源代码,并以这种方式学习。