Home » Language IDEs » Java Development Tools (JDT) » How to find out if an IFile is in a source folder
How to find out if an IFile is in a source folder [message #246963] |
Tue, 21 August 2007 22:58  |
Eclipse User |
|
|
|
Originally posted by: rob.brainkandy-dot-org.org
Hello,
I wrote this method tonight. It finds all the source pacakge fragment
roots of an IJavaProject, and tries to find the one that is the parent
of a supplied IFile.
private boolean isFileInSourceFolder(
IFile file, IJavaProject jp)
throws JavaModelException {
for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
// Only accept source roots.
if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
continue;
}
IPath rootFullPath = root.getCorrespondingResource().getFullPath();
IPath fileFullPath = file.getFullPath();
if (rootFullPath.isPrefixOf(fileFullPath)) {
return true;
}
}
return false;
}
I imagine there must be an easier way to do this. For example, create an
ICompilationUnit from the IFile, and find its IPackageFragmentRoot, but
I don't know.
Do you have any recommendations?
Thanks, Robert
|
|
|
Re: How to find out if an IFile is in a source folder [message #246968 is a reply to message #246963] |
Tue, 21 August 2007 23:07   |
Eclipse User |
|
|
|
Originally posted by: rob.brainkandy-dot-org.org
Robert Konigsberg wrote:
> Hello,
>
> I wrote this method tonight. It finds all the source pacakge fragment
> roots of an IJavaProject, and tries to find the one that is the parent
> of a supplied IFile.
>
> private boolean isFileInSourceFolder(
> IFile file, IJavaProject jp)
> throws JavaModelException {
> for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
> // Only accept source roots.
> if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
> continue;
> }
> IPath rootFullPath = root.getCorrespondingResource().getFullPath();
> IPath fileFullPath = file.getFullPath();
> if (rootFullPath.isPrefixOf(fileFullPath)) {
> return true;
> }
> }
> return false;
> }
>
> I imagine there must be an easier way to do this. For example, create an
> ICompilationUnit from the IFile, and find its IPackageFragmentRoot, but
> I don't know.
>
> Do you have any recommendations?
>
> Thanks, Robert
Ah, I found something slightly better. It doesn't require the
IJavaProject, and it doesn't throw any checked exceptions:
private IPackageFragmentRoot getPackageFragmentRoot(IFile file) {
IJavaElement elem = JavaCore.create(file.getParent());
while (elem != null && !(elem instanceof IPackageFragmentRoot)) {
elem = elem.getParent();
}
return (IPackageFragmentRoot) elem;
}
private boolean isFileInSourceFolder(IFile file) {
return getPackageFragmentRoot(file) != null;
}
Thoughts on this one? I still believe there must be an easy way to get
from any element to its IPFR.
Thanks, Robert
|
|
|
Re: How to find out if an IFile is in a source folder [message #246973 is a reply to message #246968] |
Tue, 21 August 2007 23:36   |
Eclipse User |
|
|
|
Originally posted by: rob.brainkandy-dot-org.org
Robert Konigsberg wrote:
> Robert Konigsberg wrote:
>
>> Hello,
>>
>> I wrote this method tonight. It finds all the source pacakge fragment
>> roots of an IJavaProject, and tries to find the one that is the parent
>> of a supplied IFile.
>>
>> private boolean isFileInSourceFolder(
>> IFile file, IJavaProject jp)
>> throws JavaModelException {
>> for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
>> // Only accept source roots.
>> if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
>> continue;
>> }
>> IPath rootFullPath = root.getCorrespondingResource().getFullPath();
>> IPath fileFullPath = file.getFullPath();
>> if (rootFullPath.isPrefixOf(fileFullPath)) {
>> return true;
>> }
>> }
>> return false;
>> }
>>
>> I imagine there must be an easier way to do this. For example, create
>> an ICompilationUnit from the IFile, and find its IPackageFragmentRoot,
>> but I don't know.
>>
>> Do you have any recommendations?
>>
>> Thanks, Robert
>
>
> Ah, I found something slightly better. It doesn't require the
> IJavaProject, and it doesn't throw any checked exceptions:
>
> private IPackageFragmentRoot getPackageFragmentRoot(IFile file) {
> IJavaElement elem = JavaCore.create(file.getParent());
> while (elem != null && !(elem instanceof IPackageFragmentRoot)) {
> elem = elem.getParent();
> }
> return (IPackageFragmentRoot) elem;
> }
>
> private boolean isFileInSourceFolder(IFile file) {
> return getPackageFragmentRoot(file) != null;
> }
>
> Thoughts on this one? I still believe there must be an easy way to get
> from any element to its IPFR.
>
> Thanks, Robert
Remy to the rescue:
IJavaElement elem = JavaCore.create(file.getParent());
return (elem == null)
? null
: (IPackageFragmentRoot)
elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
|
|
|
Re: How to find out if an IFile is in a source folder [message #246987 is a reply to message #246973] |
Wed, 22 August 2007 05:12   |
Eclipse User |
|
|
|
Robert Konigsberg wrote:
> Robert Konigsberg wrote:
>
>> Robert Konigsberg wrote:
>>
>>> Hello,
>>>
>>> I wrote this method tonight. It finds all the source pacakge
>>> fragment roots of an IJavaProject, and tries to find the one that is
>>> the parent of a supplied IFile.
>>>
>>> private boolean isFileInSourceFolder(
>>> IFile file, IJavaProject jp)
>>> throws JavaModelException {
>>> for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
>>> // Only accept source roots.
>>> if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
>>> continue;
>>> }
>>> IPath rootFullPath = root.getCorrespondingResource().getFullPath();
>>> IPath fileFullPath = file.getFullPath();
>>> if (rootFullPath.isPrefixOf(fileFullPath)) {
>>> return true;
>>> }
>>> }
>>> return false;
>>> }
>>>
>>> I imagine there must be an easier way to do this. For example,
>>> create an ICompilationUnit from the IFile, and find its
>>> IPackageFragmentRoot, but I don't know.
>>>
>>> Do you have any recommendations?
>>>
>>> Thanks, Robert
>>
>>
>>
>> Ah, I found something slightly better. It doesn't require the
>> IJavaProject, and it doesn't throw any checked exceptions:
>>
>> private IPackageFragmentRoot getPackageFragmentRoot(IFile file) {
>> IJavaElement elem = JavaCore.create(file.getParent());
>> while (elem != null && !(elem instanceof IPackageFragmentRoot)) {
>> elem = elem.getParent();
>> }
>> return (IPackageFragmentRoot) elem;
>> }
>>
>> private boolean isFileInSourceFolder(IFile file) {
>> return getPackageFragmentRoot(file) != null;
>> }
>>
>> Thoughts on this one? I still believe there must be an easy way to
>> get from any element to its IPFR.
>
You might also want to look at IJavaProject.isOnClasspath(IResource
resource);
Dani
>>
>> Thanks, Robert
>
>
> Remy to the rescue:
>
> IJavaElement elem = JavaCore.create(file.getParent());
> return (elem == null)
> ? null
> : (IPackageFragmentRoot)
> elem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
|
|
|
Re: How to find out if an IFile is in a source folder [message #250977 is a reply to message #246963] |
Sun, 27 January 2008 13:24   |
Eclipse User |
|
|
|
(1) You can find out the parent of every IResource in the workspace.
(2) Haven't not explored yet, but may be the JDT has an utility method
to determine if an IFolder is a source folder, once you get the parent
of the IFile?
Robert Konigsberg wrote:
> Hello,
>
> I wrote this method tonight. It finds all the source pacakge fragment
> roots of an IJavaProject, and tries to find the one that is the parent
> of a supplied IFile.
>
> private boolean isFileInSourceFolder(
> IFile file, IJavaProject jp)
> throws JavaModelException {
> for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
> // Only accept source roots.
> if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
> continue;
> }
> IPath rootFullPath = root.getCorrespondingResource().getFullPath();
> IPath fileFullPath = file.getFullPath();
> if (rootFullPath.isPrefixOf(fileFullPath)) {
> return true;
> }
> }
> return false;
> }
>
> I imagine there must be an easier way to do this. For example, create an
> ICompilationUnit from the IFile, and find its IPackageFragmentRoot, but
> I don't know.
>
> Do you have any recommendations?
>
> Thanks, Robert
|
|
|
Re: How to find out if an IFile is in a source folder [message #250981 is a reply to message #246963] |
Sun, 27 January 2008 13:25  |
Eclipse User |
|
|
|
(1) You can find out the parent of every IResource in the workspace.
(2) Haven't not explored yet, but may be the JDT has an utility method
to determine if an IFolder is a source folder, once you get the parent
of the IFile?
Robert Konigsberg wrote:
> Hello,
>
> I wrote this method tonight. It finds all the source pacakge fragment
> roots of an IJavaProject, and tries to find the one that is the parent
> of a supplied IFile.
>
> private boolean isFileInSourceFolder(
> IFile file, IJavaProject jp)
> throws JavaModelException {
> for (IPackageFragmentRoot root : jp.getPackageFragmentRoots()) {
> // Only accept source roots.
> if (root.getKind() != IPackageFragmentRoot.K_SOURCE) {
> continue;
> }
> IPath rootFullPath = root.getCorrespondingResource().getFullPath();
> IPath fileFullPath = file.getFullPath();
> if (rootFullPath.isPrefixOf(fileFullPath)) {
> return true;
> }
> }
> return false;
> }
>
> I imagine there must be an easier way to do this. For example, create an
> ICompilationUnit from the IFile, and find its IPackageFragmentRoot, but
> I don't know.
>
> Do you have any recommendations?
>
> Thanks, Robert
|
|
|
Goto Forum:
Current Time: Thu Apr 17 12:12:12 EDT 2025
Powered by FUDForum. Page generated in 0.03049 seconds
|