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

						// 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 :
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(
	at test.Activator.execute(
	at test.handlers.SampleHandler.execute(
	at org.eclipse.ui.internal.handlers.HandlerProxy.execute(
	at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(
	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(
	at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(
	at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(
	at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(
	at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(
	at org.eclipse.core.commands.Command.executeWithChecks(
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(
	at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$2(
	at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(
	at org.eclipse.swt.widgets.EventTable.sendEvent(
	at org.eclipse.swt.widgets.Display.filterEvent(
	at org.eclipse.swt.widgets.Widget.sendEvent(
	at org.eclipse.swt.widgets.Widget.sendEvent(
	at org.eclipse.swt.widgets.Widget.sendEvent(
	at org.eclipse.swt.widgets.Widget.sendKeyEvent(
	at org.eclipse.swt.widgets.Widget.sendKeyEvent(
	at org.eclipse.swt.widgets.Widget.wmKeyDown(
	at org.eclipse.swt.widgets.Control.WM_KEYDOWN(
	at org.eclipse.swt.widgets.Canvas.WM_KEYDOWN(
	at org.eclipse.swt.widgets.Control.windowProc(
	at org.eclipse.swt.widgets.Canvas.windowProc(
	at org.eclipse.swt.widgets.Display.windowProc(
	at org.eclipse.swt.internal.win32.OS.DispatchMessage(Native Method)
	at org.eclipse.swt.widgets.Display.readAndDispatch(
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(
	at org.eclipse.ui.internal.Workbench.lambda$3(
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(
	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(
	at org.eclipse.equinox.launcher.Main.basicRun(
	at org.eclipse.equinox.launcher.Main.main(

Thanks and BR,
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: 33075
Registered: July 2009
Senior Member
I suppose asking on the Eclipse Platform forum would be more specific:

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

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

Current Time: Sat Feb 24 13:26:17 GMT 2024

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

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

Back to the top