Home » Language IDEs » Java Development Tools (JDT) » How to insert new file into project
How to insert new file into project [message #242673] |
Tue, 10 April 2007 17:07  |
Eclipse User |
|
|
|
Originally posted by: myawn.ebay.com
I'm not sure whether this is a JDT question, or a core question, but since
it's a Java file I'll start here.
I'm writing a wizard that will generate a Java source file. So I've got a
string representing the package name, and a string representing the class
name. Now I just want to create the file and insert it into the project.
This is supposed to be the easy part :-).
My head is about to explode from trying to navigate the object hierarchy
that may or may not be involved in this -- IJavaElement, IFile, IResource,
IContainer, IWorkspaceRoot, IJavaProject, IPackageFragement. I've probably
tried two dozen different permutations of things that look like they ought
to work, but in most cases I just get an exception thrown.
What do I actually need to call to do this?
- Take the package name, find it in my workspace. If it doesn't exist,
create it.
- Take the class name, create a.java source file for it in the package (and
if I can point to a template to be used to create the file content, even
better)
Thanks .
Mike
|
|
|
Re: How to insert new file into project [message #242702 is a reply to message #242673] |
Wed, 11 April 2007 05:03   |
Eclipse User |
|
|
|
Here is the simple code for creating a file in project.
IProject project;
....
// assume you want to create a file (src/mypackage/MyClass.java)
IFolder srcFolder = project.getFolder("src");
if (!srcFolder.exists())
srcFolder.create(true, true, null);
IFolder packageFolder = srcFolder.getFolder("mypackage");
if (!packageFolder.exists())
packageFolder.create(true, true, null);
IFile classFile = packageFolder.getFile("MyClass.java");
ByteArrayInputStream content = new ByteArrayInputStream("class content
here".getBytes());
classFile.create(content, true, null);
Mike Yawn wrote:
> I'm not sure whether this is a JDT question, or a core question, but since
> it's a Java file I'll start here.
>
> I'm writing a wizard that will generate a Java source file. So I've got a
> string representing the package name, and a string representing the class
> name. Now I just want to create the file and insert it into the project.
> This is supposed to be the easy part :-).
>
> My head is about to explode from trying to navigate the object hierarchy
> that may or may not be involved in this -- IJavaElement, IFile, IResource,
> IContainer, IWorkspaceRoot, IJavaProject, IPackageFragement. I've probably
> tried two dozen different permutations of things that look like they ought
> to work, but in most cases I just get an exception thrown.
>
> What do I actually need to call to do this?
>
> - Take the package name, find it in my workspace. If it doesn't exist,
> create it.
> - Take the class name, create a.java source file for it in the package (and
> if I can point to a template to be used to create the file content, even
> better)
>
> Thanks .
> Mike
>
>
|
|
| | | |
Re: Create package fragment? (was: How to insert new file into project) [message #242776 is a reply to message #242766] |
Thu, 12 April 2007 11:12   |
Eclipse User |
|
|
|
Originally posted by: myawn.ebay.com
Thanks for your help on this ... still trying to work out a last detail or
two.
I don't want to be dependent on their being a folder called 'src' -- in my
example project, for example, there isn't one, and packages are rooted
directly under the project, rather than under a src folder.
I'm not sure if there is a reliable way to get the 'root' folder -- either
the project folder, or a 'src' or 'source' or whatever the user may have
called it.
I have an IResource (IFile) of another Java file that is in the project, but
which isn't in the same package as the element I'm creating. I thought I
might be able to walk up the tree (IResource.getParent()) until I found an
element where instanceof IFolder was true, and then create a package
fragment there. But it turns out that instanceof IFolder is true for all of
the package pieces, also. So I had code in com.mycompany.example, and
wanted to create a new package com.mycompany.codegen, but what I ended up
with instead was com.mycompany.example.com.mycompany.codegen.
So is there a way to walk up the tree (or some other process) that gets me
to the 'parent of com' (without explicitly testing for that, since I don't
want to break if the code is in a different top-level domain, or even
(horrors) the default package.
Mike
"Sebastian Schmidt" <schmidts@iese.fraunhofer.de> wrote in message
news:evks1s$vtu$1@build.eclipse.org...
> Hi Mike
>
> you have to get or create the IFolder with the IProject, that is the
> source root of this project.
>
> IFolder folder = project.getFolder("src");
> if(!folder.exists)
> folder.create(..);
>
> and when you have the resource, i.e. the IFolder object, you just need the
> IJavaProject to call
>
> IPackageFragmentRoot fragRoot =
> javaProject.getPackageFragmentRoot(folder);
> IPackageFragment fragment = fragRoot.createPackageFragment(PACKAGE_NAME,
> FORCE, new NullProgressMonitor);
>
> et voil
|
|
|
Re: Create package fragment? (was: How to insert new file into project) [message #242806 is a reply to message #242776] |
Thu, 12 April 2007 18:55  |
Eclipse User |
|
|
|
Originally posted by: myawn.ebay.com
I think this is resolved now -- I'm using
project.getAllPackageFragmentRoots, and then creating a package fragment
under the first root for which isArchive() and isExternal() both return
false.
If the user has more than one qualifying root, I'm not sure there would be a
reliable way to pick the 'right' one.
Mike
"Mike Yawn" <myawn@ebay.com> wrote in message
news:evli8n$id6$1@build.eclipse.org...
> Thanks for your help on this ... still trying to work out a last detail or
> two.
>
> I don't want to be dependent on their being a folder called 'src' -- in my
> example project, for example, there isn't one, and packages are rooted
> directly under the project, rather than under a src folder.
>
> I'm not sure if there is a reliable way to get the 'root' folder -- either
> the project folder, or a 'src' or 'source' or whatever the user may have
> called it.
>
> I have an IResource (IFile) of another Java file that is in the project,
> but which isn't in the same package as the element I'm creating. I
> thought I might be able to walk up the tree (IResource.getParent()) until
> I found an element where instanceof IFolder was true, and then create a
> package fragment there. But it turns out that instanceof IFolder is true
> for all of the package pieces, also. So I had code in
> com.mycompany.example, and wanted to create a new package
> com.mycompany.codegen, but what I ended up with instead was
> com.mycompany.example.com.mycompany.codegen.
>
> So is there a way to walk up the tree (or some other process) that gets me
> to the 'parent of com' (without explicitly testing for that, since I
> don't want to break if the code is in a different top-level domain, or
> even (horrors) the default package.
>
> Mike
>
>
> "Sebastian Schmidt" <schmidts@iese.fraunhofer.de> wrote in message
> news:evks1s$vtu$1@build.eclipse.org...
>> Hi Mike
>>
>> you have to get or create the IFolder with the IProject, that is the
>> source root of this project.
>>
>> IFolder folder = project.getFolder("src");
>> if(!folder.exists)
>> folder.create(..);
>>
>> and when you have the resource, i.e. the IFolder object, you just need
>> the IJavaProject to call
>>
>> IPackageFragmentRoot fragRoot =
>> javaProject.getPackageFragmentRoot(folder);
>> IPackageFragment fragment = fragRoot.createPackageFragment(PACKAGE_NAME,
>> FORCE, new NullProgressMonitor);
>>
>> et voil
|
|
|
Goto Forum:
Current Time: Wed May 07 07:17:25 EDT 2025
Powered by FUDForum. Page generated in 0.29838 seconds
|