Organize Imports? [message #85243] |
Wed, 13 August 2003 04:36  |
Eclipse User |
|
|
|
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 03:26   |
Eclipse User |
|
|
|
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 06:26  |
Eclipse User |
|
|
|
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
>
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.39938 seconds