Getting all packages from a IJavaProject [message #257037] |
Thu, 23 October 2008 16:55  |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
What is the most straightforward way to get a collection or array of all
the packages from an IJavaProject? I need to present an
ElementListSelectionDialog whose elements are the packages, kind of like
what the New Java Class wizard does. I've looked at the code in
NewTypeWizardPage.choosePackage() but it depends on some other state in
the wizard and it's a bit hard to decipher.
Any help appreciated,
Eric
|
|
|
|
|
Re: Getting all packages from a IJavaProject [message #257220 is a reply to message #257041] |
Wed, 05 November 2008 09:15  |
Eclipse User |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
On 10/23/2008 5:48 PM, Eric Rizzo wrote:
> I should clarify that I need only the packages that come from source
> locations within the project, and I need to include empty packages, too.
>
> On 10/23/2008 4:55 PM, Eric Rizzo wrote:
>> What is the most straightforward way to get a collection or array of all
>> the packages from an IJavaProject? I need to present an
>> ElementListSelectionDialog whose elements are the packages, kind of like
>> what the New Java Class wizard does. I've looked at the code in
>> NewTypeWizardPage.choosePackage() but it depends on some other state in
>> the wizard and it's a bit hard to decipher.
Well, I wrote some code to do this (see below) but I'm very surprised
there is not a more convenient API for this, which seems to me like a
pretty fundamental function of JDT. Really?
protected Object[] getPackages(IJavaProject project) {
Set<Object> packages = new HashSet<Object>();
Set<String> packageNames = new HashSet<String>();
if (project != null && project.exists()) {
for (IPackageFragmentRoot aSourceRoot : getSourceRoots(project)) {
try {
for (IJavaElement aPackage : aSourceRoot.getChildren()) {
String packageName = aPackage.getElementName();
if (checkDefaultPacakge(packageName) &&
checkDuplicatePackage(packageName, packageNames)) {
packageNames.add(packageName);
packages.add(aPackage);
}
}
} catch (JavaModelException ex) {
SkywayUIPlugin.getDefault().logError("Error getting packages from
source root " + aSourceRoot, ex); //$NON-NLS-1$
}
}
}
return packages.toArray();
}
protected boolean checkDefaultPacakge(String packageName) {
// Skip the default package unless showDefaultPacakge is true
return showDefaultPacakge || !"".equals(packageName); //$NON-NLS-1$
}
protected boolean checkDuplicatePackage(String packageName, Set<String>
packageNames) {
// Only allow duplicates if showDuplicateNames is true
return showDuplicateNames || !packageNames.contains(packageName);
}
protected IPackageFragmentRoot[] getSourceRoots(IJavaProject project) {
ArrayList<IPackageFragmentRoot> result = new
ArrayList<IPackageFragmentRoot>();
try {
IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
for (int i = 0; i < roots.length; i++) {
if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
result.add(roots[i]);
}
}
} catch (JavaModelException ex) {
}
return result.toArray(new IPackageFragmentRoot[result.size()]);
}
|
|
|
Powered by
FUDForum. Page generated in 0.17841 seconds