Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » How to call organizing imports?
How to call organizing imports? [message #498891] Wed, 18 November 2009 22:15 Go to next message
Eclipse UserFriend
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 #498923 is a reply to message #498891] Thu, 19 November 2009 03:40 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: How to call organizing imports? [message #505678 is a reply to message #498923] Mon, 04 January 2010 03:00 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous message
Eclipse UserFriend
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
>
>
Previous Topic:Navigate history with mouse buttons
Next Topic:Running Plug-in Project form Command Line
Goto Forum:
  


Current Time: Sun Mar 16 06:37:28 EDT 2025

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

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

Back to the top