How to call organizing imports? [message #498891] |
Wed, 18 November 2009 22:15  |
Eclipse User |
|
|
|
Hi guys,
I'm using jdt to format the generated java codes. now i have a problem when organizing imports. I searched with good and find a solution like this:
IWorkbenchSite site = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPartService()
.getActivePart().getSite();
List<IFile> javaResources = getAllJavaFile(project.members());
ICompilationUnit[] units = new ICompilationUnit[javaResources.size()];
for (int i=0;i<javaResources.size();i++ ) {
units[i] = JavaCore.createCompilationUnitFrom(javaResources.get(i));
}
OrganizeImportsAction organizeImports = new OrganizeImportsAction(site);
organizeImports.runOnMultiple(units);
You know that we only can getActiveWorkBenWindow() in a UI thread, or you always get null from that. ok, that my problem, is there any solution using the organizing api in non-UI thread?
|
|
|
|
Re: How to call organizing imports? [message #505678 is a reply to message #498923] |
Mon, 04 January 2010 03:00   |
Eclipse User |
|
|
|
Hi Daniel,
I have the same question as Kyle but I am afraid that your reference to the ImportRewrite is not that clear to me. After
studying the ImportRewrite it seems that I have to call it explicitly with the qualified class names present in the code.
I am looking for something which can be run in eclipse headless, with an api in which I can pass in a source (string,
file or anything) and get a result with resolved imports back. The solution by Kyle seems close to this but I do not
really see how ImportRewrite answers his question....
I hope you can give some more info, thanks!
gr. Martin
Daniel Megert wrote:
> Kyle wrote:
>> Hi guys,
>> I'm using jdt to format the generated java codes. now i have a problem
>> when organizing imports. I searched with good and find a solution like
>> this:
>>
>> IWorkbenchSite site =
>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPart Service()
>> .getActivePart().getSite();
>> List<IFile> javaResources = getAllJavaFile(project.members());
>> ICompilationUnit[] units = new
>> ICompilationUnit[javaResources.size()];
>> for (int i=0;i<javaResources.size();i++ ) {
>> units[i] =
>> JavaCore.createCompilationUnitFrom(javaResources.get(i));
>> }
>> OrganizeImportsAction organizeImports = new
>> OrganizeImportsAction(site);
>> organizeImports.runOnMultiple(units);
>>
>>
>> You know that we only can getActiveWorkBenWindow() in a UI thread, or
>> you always get null from that. ok, that my problem, is there any
>> solution using the organizing api in non-UI thread?
> See: org.eclipse.jdt.core.dom.rewrite.ImportRewrite
>
> Dani
--
With Regards, Martin Taal
Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
|
|
|
Re: How to call organizing imports? [message #505845 is a reply to message #505678] |
Tue, 05 January 2010 05:33  |
Eclipse User |
|
|
|
Martin Taal wrote:
> Hi Daniel,
> I have the same question as Kyle but I am afraid that your reference
> to the ImportRewrite is not that clear to me. After studying the
> ImportRewrite it seems that I have to call it explicitly with the
> qualified class names present in the code.
>
> I am looking for something which can be run in eclipse headless, with
> an api in which I can pass in a source (string, file or anything) and
> get a result with resolved imports back. The solution by Kyle seems
> close to this but I do not really see how ImportRewrite answers his
> question....
>
> I hope you can give some more info, thanks!
The full code is in
org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImp ortsOperation.
To use it headless you have to copy it.
Dani
>
> gr. Martin
>
> Daniel Megert wrote:
>> Kyle wrote:
>>> Hi guys,
>>> I'm using jdt to format the generated java codes. now i have a
>>> problem when organizing imports. I searched with good and find a
>>> solution like this:
>>>
>>> IWorkbenchSite site =
>>> PlatformUI.getWorkbench().getActiveWorkbenchWindow().getPart Service()
>>> .getActivePart().getSite();
>>> List<IFile> javaResources = getAllJavaFile(project.members());
>>> ICompilationUnit[] units = new
>>> ICompilationUnit[javaResources.size()];
>>> for (int i=0;i<javaResources.size();i++ ) {
>>> units[i] =
>>> JavaCore.createCompilationUnitFrom(javaResources.get(i));
>>> }
>>> OrganizeImportsAction organizeImports = new
>>> OrganizeImportsAction(site);
>>> organizeImports.runOnMultiple(units);
>>>
>>>
>>> You know that we only can getActiveWorkBenWindow() in a UI thread,
>>> or you always get null from that. ok, that my problem, is there any
>>> solution using the organizing api in non-UI thread?
>> See: org.eclipse.jdt.core.dom.rewrite.ImportRewrite
>>
>> Dani
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.04794 seconds