Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Organize Imports?
Organize Imports? [message #85243] Wed, 13 August 2003 08:36 Go to next message
Michael Taege is currently offline Michael TaegeFriend
Messages: 29
Registered: July 2009
Junior Member
Hello,

I've got a little problem. I want to organize the imports of an
ICompilationUnit with a plugin automatically. I know there is an
OrganizeImportsAction class that organizes the imports if the user calls
the action, but the constructor of OrganizeImportsAction expects an
IWorkbenchSite or an org.eclipse.jdt.internal.ui.javaeditor.JavaEditor
(for internal use). All what I have when I want to call the action is an
ICompilationUnit. I've tried to open an editor for the unit with the
fallowing code:

IEditorPart javaEditor = JavaUI.openInEditor(iCompilationUnit);
OrganizeImportsAction organize =
new OrganizeImportsAction(javaEditor.getSite());
organize.run();

but JavaUI returns null (couldn't open the editor).

Is there a way to get an IWorkbenchSite from an ICompilationUnit?

Someone told me that I have to implement IObjectActionDelegate but I
don't want to implement an own action for the popupMenus that the user
can call. I just want to call the OrganizeImportsAction from my plugin.

Thanks in advance,

Michael
Re: Organize Imports? [message #85687 is a reply to message #85243] Thu, 14 August 2003 07:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mbleyh.de.ibm.com

Hi Michael,

I had the same problem, and I figured out this:

// only need this if you do not already have a ICompilatioinUnit
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);

String[] prefOrder =
ImportOrganizePreferencePage.getImportOrderPreference();
int threshold =
ImportOrganizePreferencePage.getImportNumberThreshold();
boolean ignoreLowerCaseNames =
ImportOrganizePreferencePage.doIgnoreLowerCaseNames();

OrganizeImportsOperation op =
new OrganizeImportsOperation( cu
, prefOrder
, threshold
, ignoreLowerCaseNames
, !cu.isWorkingCopy()
, true
, null
);

BusyIndicatorRunnableContext context =
new BusyIndicatorRunnableContext();
try {
context.run(false, true, new WorkbenchRunnableAdapter(op));
}
catch (Exception e) {
e.printStackTrace();
}


That works so far.

Michael Täge wrote:

> Hello,

> I've got a little problem. I want to organize the imports of an
> ICompilationUnit with a plugin automatically. I know there is an
> OrganizeImportsAction class that organizes the imports if the user calls
> the action, but the constructor of OrganizeImportsAction expects an
> IWorkbenchSite or an org.eclipse.jdt.internal.ui.javaeditor.JavaEditor
> (for internal use). All what I have when I want to call the action is an
> ICompilationUnit. I've tried to open an editor for the unit with the
> fallowing code:

> IEditorPart javaEditor = JavaUI.openInEditor(iCompilationUnit);
> OrganizeImportsAction organize =
> new OrganizeImportsAction(javaEditor.getSite());
> organize.run();

> but JavaUI returns null (couldn't open the editor).

> Is there a way to get an IWorkbenchSite from an ICompilationUnit?

> Someone told me that I have to implement IObjectActionDelegate but I
> don't want to implement an own action for the popupMenus that the user
> can call. I just want to call the OrganizeImportsAction from my plugin.

> Thanks in advance,

> Michael
Re: Organize Imports? [message #86118 is a reply to message #85687] Mon, 18 August 2003 10:26 Go to previous message
Michael Taege is currently offline Michael TaegeFriend
Messages: 29
Registered: July 2009
Junior Member
Hello,

I found another problem with the OrganizeImportsOperation.
If the ICompilationUnit is opened in an editor the imports will be
organized (you can see it in the editor), but after the operation the
ICompilationUnit seems to be updated with the old content of the editor,
so the imports are in a state as before the operation.

Any hints?

Thanks in advance

Michael

Mirko Bleyh wrote:

> Hi Michael,
>
> I had the same problem, and I figured out this:
>
> // only need this if you do not already have a ICompilatioinUnit
> ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
>
> String[] prefOrder =
> ImportOrganizePreferencePage.getImportOrderPreference();
> int threshold =
> ImportOrganizePreferencePage.getImportNumberThreshold();
> boolean ignoreLowerCaseNames =
> ImportOrganizePreferencePage.doIgnoreLowerCaseNames();
>
> OrganizeImportsOperation op =
> new OrganizeImportsOperation( cu
> , prefOrder
> , threshold
> , ignoreLowerCaseNames
> , !cu.isWorkingCopy()
> , true
> , null
> );
>
> BusyIndicatorRunnableContext context =
> new BusyIndicatorRunnableContext();
> try {
> context.run(false, true, new WorkbenchRunnableAdapter(op));
> }
> catch (Exception e) {
> e.printStackTrace();
> }
>
>
> That works so far.
>
> Michael Täge wrote:
>
>
>>Hello,
>
>
>>I've got a little problem. I want to organize the imports of an
>>ICompilationUnit with a plugin automatically. I know there is an
>>OrganizeImportsAction class that organizes the imports if the user calls
>>the action, but the constructor of OrganizeImportsAction expects an
>>IWorkbenchSite or an org.eclipse.jdt.internal.ui.javaeditor.JavaEditor
>>(for internal use). All what I have when I want to call the action is an
>>ICompilationUnit. I've tried to open an editor for the unit with the
>>fallowing code:
>
>
>>IEditorPart javaEditor = JavaUI.openInEditor(iCompilationUnit);
>>OrganizeImportsAction organize =
>> new OrganizeImportsAction(javaEditor.getSite());
>>organize.run();
>
>
>>but JavaUI returns null (couldn't open the editor).
>
>
>>Is there a way to get an IWorkbenchSite from an ICompilationUnit?
>
>
>>Someone told me that I have to implement IObjectActionDelegate but I
>>don't want to implement an own action for the popupMenus that the user
>>can call. I just want to call the OrganizeImportsAction from my plugin.
>
>
>>Thanks in advance,
>
>
>>Michael
>
>
>
Previous Topic:How to set the button to be pressed by default.
Next Topic:Re: Compiler errors no longer in Tasks list?
Goto Forum:
  


Current Time: Sat Dec 20 18:07:12 GMT 2014

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

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