Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Eclipse IDE Plugin Dev Help: Rename File Resource of Active Editor(Working on a plugin which one command renames file resource of active editor which is a TextEditor)
Eclipse IDE Plugin Dev Help: Rename File Resource of Active Editor [message #1818565] Thu, 19 December 2019 17:29 Go to next message
Ralph Williamson is currently offline Ralph WilliamsonFriend
Messages: 1
Registered: December 2019
Junior Member
First question would be where is the most appropriate location in this forum to ask plugin development questions? I did not see a heading/topic for that.

Second, the actual issue I am having.
From a plug-in in Eclipse, I have bound a key combination to attempt to use the Project Explorer refactor dialog to rename a file resource that is being edited by a TextEditor. I am unsuccessful.

If I bind another key combination to the same command ID, that is "org.eclipse.ui.edit.rename", Then manually select the file resource and do the key sequence, the dialog does indeed present itself. Does this imply that I need to programatically give the Project Explorer focus?

Can anyone offer suggestions how I can programatically refactor/rename the active editor? I don't want to do "Save As" as this commits the change to disk and I don't want to force the user to do that yet.

The following is my test code.

	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;

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


The following is the resultant debug output.

!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)


Thanks and BR,
Ralph
Re: Eclipse IDE Plugin Dev Help: Rename File Resource of Active Editor [message #1818577 is a reply to message #1818565] Fri, 20 December 2019 05:44 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30820
Registered: July 2009
Senior Member
I suppose asking on the Eclipse Platform forum would be more specific:

https://www.eclipse.org/forums/index.php/f/11/

I suspect what's going on here is that invoking the command will compute information for/from the execution environment so likely it will consider which view currently is active (has focus) to determine the handler. If your editor has focus then likely it won't find the handler associated with the Project Explorer (and of course it might be the Package Explorer that's actually visible instead). I'm really not sure the best way to ensure this would always work. Likely activating the view first will help. Note that I expect that a rename will also commit the rename change to disk as well, and often to do a rename, you'll be asked to save dirty editors first (it appears the Project Explore does not force the same, but the Package Explorer does).

Previous Topic: Go doesn't compile "hello world"
Next Topic:Some Help With Changing to Java 13
Goto Forum:
  


Current Time: Mon Jan 27 08:52:58 GMT 2020

Powered by FUDForum. Page generated in 0.03047 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top