Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Plugin Development Environment (PDE) » how to organize imports via command
icon5.gif  how to organize imports via command [message #804614] Wed, 22 February 2012 20:33 Go to next message
Andrej B is currently offline Andrej BFriend
Messages: 2
Registered: February 2012
Junior Member
Hey,

I'm creating a plugin where I often generate code into active editor. This code of course contains classes, so I would like to organise imports after I generate this code (shortcut ctrl+shift+o).

Has anyone done this before? I've been searchin the web for answer but either haven't found anything or it was not working.

So far I've tried this:
IServiceLocator locator = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow(); 
IHandlerService handlerServ = (IHandlerService)locator.getService(IHandlerService.class);
handlerServ.executeCommand("org.eclipse.jdt.ui.edit.text.java.organize.imports", null);

but nothing happens after execute. Although when I used different command (org.eclipse.ui.edit.selectAll) it worked and it selected all nodes in my tree viewer. Which means these few lines of code works, but not with organize.imports command. Would be cool if anyone knew why. My guess is, that command is executed at my view and therefore it doesnt work, because it is tryin to organize imports in view. But I have no idea to make it work at my active editor.

I also tried playing with this Class:
org.eclipse.jdt.ui.actions.OrganizeImportsAction

but I just got Exception java.lang.NoClassDefFoundError which I could not get rid of.

This code I posted I'm calling in my tree viewer class in Action which is in viewer contextmenu.
Re: how to organize imports via command [message #822914 is a reply to message #804614] Sat, 17 March 2012 13:42 Go to previous messageGo to next message
Andrej B is currently offline Andrej BFriend
Messages: 2
Registered: February 2012
Junior Member
So I was right Smile. Execution of import command wasn't working because editor wasn't active. I tried calling command in menu (because when you select items in menu, editor is still active) and it organized my imports.

For those who generate some code... also this command is quite useful: "org.eclipse.jdt.ui.edit.text.java.format" which calls the formatter (ctrl+shift+f)

//edit: if you still want to call command from view (or anywhere except editor) just activate him and then use executeCommand service Smile

IEditorPart editorPart = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(editorPart);


Case closed. Have a nice day everyone.

[Updated on: Sat, 17 March 2012 13:58]

Report message to a moderator

Re: how to organize imports via command [message #909414 is a reply to message #822914] Fri, 07 September 2012 05:33 Go to previous message
Jakob Braeuchi is currently offline Jakob BraeuchiFriend
Messages: 69
Registered: July 2009
Location: Switzerland
Member
hi,

we would like to execute the OrganizeImportAction on the whole Project, without any Editors beeing open.
How can this be done ?

thanks
jakob

Previous Topic:Plugin Activation
Next Topic:Measuring file load time
Goto Forum:
  


Current Time: Thu Apr 25 09:34:01 GMT 2024

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

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

Back to the top