summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-07-26 15:45:10 (EDT)
committerpelder2006-07-26 15:45:10 (EDT)
commit6699bf3fb5a6fd6c0ac45a513ddab2af4bec4a97 (patch)
tree8715a849fda4f9f058a33f60b6c0bef62ae09495
parentc387a2f90123fd11e0e6786ce1b23243140040d5 (diff)
downloadorg.eclipse.jet-6699bf3fb5a6fd6c0ac45a513ddab2af4bec4a97.zip
org.eclipse.jet-6699bf3fb5a6fd6c0ac45a513ddab2af4bec4a97.tar.gz
org.eclipse.jet-6699bf3fb5a6fd6c0ac45a513ddab2af4bec4a97.tar.bz2
[149189] Generalize Java source folder and package handling to include both IFolder's and IProjects.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java8
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java119
2 files changed, 81 insertions, 46 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
index 6b08fad..2300b29 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
@@ -16,6 +16,7 @@
*/
package org.eclipse.jet.internal.taglib.java;
+import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
@@ -58,9 +59,12 @@ public class JavaPackageAction extends AbstractWorkspaceAction implements IWorks
*/
public void performAction(IProgressMonitor monitor) throws JET2TagException
{
- IFolder folder = JavaActionsUtil.getFolderForPackage(srcFolderPath, packageName);
+ IContainer container = JavaActionsUtil.getContainerForPackage(srcFolderPath, packageName);
- ActionsUtil.ensureFolderExists(folder, monitor);
+ if(container instanceof IFolder)
+ {
+ ActionsUtil.ensureFolderExists((IFolder)container, monitor);
+ }
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
index 04ce139..86bb471 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
@@ -19,8 +19,10 @@ package org.eclipse.jet.taglib.java;
import java.text.MessageFormat;
+import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
@@ -62,10 +64,23 @@ public class JavaActionsUtil
* @param packageName the package name
* @return the folder corresponding to the package
* @throws JET2TagException if the source folder is not valid.
+ * @deprecated Use {@link #getContainerForPackage(IPath,String)} instead
*/
public static IFolder getFolderForPackage(IPath sourceFolder, String packageName) throws JET2TagException
{
- IFolder result = null;
+ return (IFolder)getContainerForPackage(sourceFolder, packageName);
+ }
+
+ /**
+ * Return the {@link IFolder} corresponding to the given Java package name.
+ * @param sourceFolder the source folder containing the package
+ * @param packageName the package name
+ * @return the folder corresponding to the package
+ * @throws JET2TagException if the source folder is not valid.
+ */
+ public static IContainer getContainerForPackage(IPath sourceFolder, String packageName) throws JET2TagException
+ {
+ IContainer result = null;
if (sourceFolder == null || packageName == null)
{
throw new NullPointerException();
@@ -76,79 +91,82 @@ public class JavaActionsUtil
}
// if the JDT is not sync'd with the workspace changes, this may return null.
- IJavaElement jElementForFolder = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getFolder(sourceFolder));
+ IJavaElement jElementForFolder = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().findMember(sourceFolder));
if (jElementForFolder == null)
{
// checkpoint the workspace (inform the builders) of our changes to date...
ActionsUtil.checkpointWorkspace(true, new NullProgressMonitor());
// and try again...
- jElementForFolder = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getFolder(sourceFolder));
+ jElementForFolder = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().findMember(sourceFolder));
}
if (jElementForFolder != null)
{
// find the package fragment root corresponding to sourceFolder
- if (!(jElementForFolder instanceof IPackageFragmentRoot))
- {
- throw new JET2TagException(MessageFormat.format(JET2Messages.JavaActionsUtil_InvalidSourceFolder, new Object []{ sourceFolder.toString() }));
- }
- IPackageFragmentRoot root = (IPackageFragmentRoot)jElementForFolder;
- final IPackageFragment packageFragment = root.getPackageFragment(packageName);
- result = (IFolder)packageFragment.getResource();
- }
- else
- {
- // sourceFolder wasn't a package root, maybe its just a project name, and we need to use the first package
- // root in that project that contains the packageName
- if (sourceFolder.segmentCount() >= 1)
+ if (jElementForFolder instanceof IPackageFragmentRoot)
{
- String msg;
- msg = JET2Messages.JavaActionsUtil_InvalidSourceFolder;
- throw new JET2TagException(MessageFormat.format(msg, new Object []{ sourceFolder.toString() }));
+ IPackageFragmentRoot root = (IPackageFragmentRoot)jElementForFolder;
+ final IPackageFragment packageFragment = root.getPackageFragment(packageName);
+ result = (IContainer)packageFragment.getResource();
}
- IJavaProject jProject = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(sourceFolder.segment(0)));
- try
+ else if (jElementForFolder instanceof IJavaProject)
{
- result = findOrCreateJavaPackage(jProject, packageName);
+ try
+ {
+ result = (IContainer)findOrCreateJavaPackage((IJavaProject)jElementForFolder, packageName);
+ }
+ catch (JavaModelException e)
+ {
+ final String msg = JET2Messages.JavaActionsUtil_CannotFindSourceFolder;
+ // it's serious this time, throw an exception;
+ throw new JET2TagException(MessageFormat.format(msg, new Object []{ sourceFolder.segment(0), packageName, }));
+ }
}
- catch (JavaModelException e1)
+ else
{
- final String msg = JET2Messages.JavaActionsUtil_CannotFindSourceFolder;
- // it's serious this time, throw an exception;
- throw new JET2TagException(MessageFormat.format(msg, new Object []{ sourceFolder.segment(0), packageName, }));
+ throw new JET2TagException(MessageFormat.format(JET2Messages.JavaActionsUtil_InvalidSourceFolder, new Object []{ sourceFolder.toString() }));
}
}
+ else
+ {
+ throw new JET2TagException(MessageFormat.format(JET2Messages.JavaActionsUtil_InvalidSourceFolder, new Object []{ sourceFolder.toString() }));
+ }
return result;
}
/**
- * @param jProject
- * @param packageName
- * @return
- * @throws JavaModelException
+ * Find the a container corresponding the the given package in the specified project.
+ * Traverse all project source package roots, looking for an existing instance of a package fragment corresponding to packageName.
+ * If not found, return the first non-existant fragment found.
+ * If the project has no source package roots, then null is returned.
+ * @param jProject the Java project to search
+ * @param packageName the Java package for which a container is sought.
+ * @return the container corresponding to the package, or null.
+ * @throws JavaModelException if the package roots or root kinds cannot be determined.
*/
- private static IFolder findOrCreateJavaPackage(IJavaProject jProject, String packageName) throws JavaModelException
+ private static IContainer findOrCreateJavaPackage(IJavaProject jProject, String packageName) throws JavaModelException
{
- final IPackageFragment[] fragments = jProject.getPackageFragments();
- for (int i = 0; i < fragments.length; i++)
- {
- if (packageName.equals(fragments[i].getElementName()))
- {
- return (IFolder)fragments[i].getResource();
- }
- }
- // didn't find an existing fragment, we'll create a fragment in the first package root...
+ IPackageFragment firstNonExistantFragment = null;
+ // Traverse package roots, looking for an existing instance of the package fragment corresponding to packageName.
+ // Otherwise, return the first non-existant fragment
final IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots();
for (int i = 0; i < roots.length; i++)
{
if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE)
{
IPackageFragment fragment = roots[i].getPackageFragment(packageName);
- return (IFolder)fragment.getResource();
+ if(fragment.exists())
+ {
+ return (IContainer)fragment.getResource();
+ }
+ else if(firstNonExistantFragment == null)
+ {
+ firstNonExistantFragment = fragment;
+ }
}
}
- return null;
+ return firstNonExistantFragment != null ? (IContainer)firstNonExistantFragment.getResource() : null;
}
/**
@@ -198,8 +216,21 @@ public class JavaActionsUtil
public static IFile getResourceForJavaResource(IPath srcFolderPath, String pkgName, String fileName) throws JET2TagException
{
- IFolder folder = getFolderForPackage(srcFolderPath, pkgName);
- return folder.getFile(fileName);
+ IContainer container = getContainerForPackage(srcFolderPath, pkgName);
+ if(container instanceof IFolder)
+ {
+ return ((IFolder)container).getFile(fileName);
+ }
+ else if(container instanceof IProject)
+ {
+ return ((IProject)container).getFile(fileName);
+ }
+ else
+ {
+ final String msg = JET2Messages.JavaActionsUtil_CannotFindSourceFolder;
+ // it's serious this time, throw an exception;
+ throw new JET2TagException(MessageFormat.format(msg, new Object []{ srcFolderPath.toString(), pkgName, }));
+ }
}
}