[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[List Home]
|
[stp-commits] r3313 - org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils
|
- From: genie@xxxxxxxxxxx
- Date: Tue, 15 Sep 2009 04:40:18 -0400 (EDT)
- Delivered-to: stp-commits@eclipse.org
Author: sdrapeau
Date: 2009-09-15 04:40:17 -0400 (Tue, 15 Sep 2009)
New Revision: 3313
Modified:
org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java
Log:
Added IFile getCompositeFile(String compositeName) method
Modified: org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java
===================================================================
--- org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java 2009-09-14 14:29:44 UTC (rev 3312)
+++ org.eclipse.stp.sca/trunk/org.eclipse.stp.sca.common/src/org/eclipse/stp/sca/common/utils/ResourceUtils.java 2009-09-15 08:40:17 UTC (rev 3313)
@@ -14,6 +14,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IContainer;
@@ -36,208 +37,280 @@
/**
* Utility methods to manage and look for resources in the workspace.
+ *
* @author Vincent Zurczak - EBM WebSourcing
+ * @contributor Stephane Drapeau - Obeo: Added the getCompositeFile method.
*/
public class ResourceUtils {
-
- /**
- * Gets all the files whose extension is <b>extension</b> and present in <b>container</b>.
- * @param container the container to explore
- * @param extension the file extension. Use "*" for any extension.
- * @return all the IFile contained into this container (with no limit in the level).
- */
- public static List<IFile> getFiles( IContainer container, String extension ) {
- List<IFile> result = new ArrayList<IFile> ();
+
+ /**
+ * Gets all the files whose extension is <b>extension</b> and present in
+ * <b>container</b>.
+ *
+ * @param container
+ * the container to explore
+ * @param extension
+ * the file extension. Use "*" for any extension.
+ * @return all the IFile contained into this container (with no limit in the
+ * level).
+ */
+ public static List<IFile> getFiles(IContainer container, String extension) {
+ List<IFile> result = new ArrayList<IFile>();
try {
IResource[] resources = container.members();
- for( IResource resource : resources ) {
- switch( resource.getType()) {
+ for (IResource resource : resources) {
+ switch (resource.getType()) {
case IResource.FILE:
- String fileExtension = resource.getFileExtension().toLowerCase();
- if( fileExtension.equals( extension )
- || fileExtension.equals( "*" )) //$NON-NLS-1$
- result.add( (IFile) resource );
+ String fileExtension = resource.getFileExtension()
+ .toLowerCase();
+ if (fileExtension.equals(extension)
+ || fileExtension.equals("*")) //$NON-NLS-1$
+ result.add((IFile) resource);
break;
case IResource.FOLDER:
IFolder subFolder = (IFolder) resource;
- result.addAll( getFiles( subFolder, extension ));
+ result.addAll(getFiles(subFolder, extension));
break;
- default: break;
+ default:
+ break;
}
}
} catch (CoreException e) {
e.printStackTrace();
}
-
+
return result;
}
-
-
- /**
+
+ /**
* Gets all the folders present in <b>container</b>.
- * @param container the container to explore
- * @return all the IFolder contained into this container (with no limit in the level).
- */
- public static List<IFolder> getFolders( IContainer container ) {
-
- List<IFolder> result = new ArrayList<IFolder> ();
+ *
+ * @param container
+ * the container to explore
+ * @return all the IFolder contained into this container (with no limit in
+ * the level).
+ */
+ public static List<IFolder> getFolders(IContainer container) {
+
+ List<IFolder> result = new ArrayList<IFolder>();
try {
IResource[] resources = container.members();
- for( IResource resource : resources ) {
- switch( resource.getType()) {
+ for (IResource resource : resources) {
+ switch (resource.getType()) {
case IResource.FOLDER:
IFolder subFolder = (IFolder) resource;
- result.add( subFolder );
- result.addAll( getFolders( subFolder ));
- default: break;
+ result.add(subFolder);
+ result.addAll(getFolders(subFolder));
+ default:
+ break;
}
}
- } catch( CoreException e ) {
+ } catch (CoreException e) {
e.printStackTrace();
}
-
+
return result;
}
-
-
+
/**
* Gets the file edited in the active editor.
- * @return the file edited in the active editor or null if it could not be retrieved.
+ *
+ * @return the file edited in the active editor or null if it could not be
+ * retrieved.
*/
public static IFile getIFileFromEditor() {
- try {
- IEditorPart editorPart =
- PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+ try {
+ IEditorPart editorPart = PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow().getActivePage()
+ .getActiveEditor();
return ((FileEditorInput) editorPart.getEditorInput()).getFile();
- }
- catch( Exception e ) {
- IStatus status = new Status(
- IStatus.ERROR,
+ } catch (Exception e) {
+ IStatus status = new Status(
+ IStatus.ERROR,
ScaCommonPlugin.PLUGIN_ID,
"Error while trying to get the file from the active editor.",
- e );
- ScaCommonPlugin.getDefault().getLog().log( status );
+ e);
+ ScaCommonPlugin.getDefault().getLog().log(status);
}
-
+
return null;
}
-
-
+
/**
- * Gets sub-containers of container having at least one resource of the given extensions.
+ * Gets sub-containers of container having at least one resource of the
+ * given extensions.
* <p>
- * The algorithm checks in the container children if it contains any valid element.
- * It searches the entire sub-folders until it finds something or until it checked
- * all the files in this container.
+ * The algorithm checks in the container children if it contains any valid
+ * element. It searches the entire sub-folders until it finds something or
+ * until it checked all the files in this container.
* </p>
*
- * @param container the container to search into
- * @param extensions the file extensions to search for
- * @param resourcesToSkip resources to skip. Can't be null.
+ * @param container
+ * the container to search into
+ * @param extensions
+ * the file extensions to search for
+ * @param resourcesToSkip
+ * resources to skip. Can't be null.
* @return the children that contains at least one composite file
*/
- public static IResource[] getDirectValidChildren(
- IContainer container, List<String> extensions, List<IResource> resourcesToSkip ) {
-
- if( container instanceof IProject
- && ! ((IProject) container).isOpen())
- return new IResource[ 0 ];
-
+ public static IResource[] getDirectValidChildren(IContainer container,
+ List<String> extensions, List<IResource> resourcesToSkip) {
+
+ if (container instanceof IProject && !((IProject) container).isOpen())
+ return new IResource[0];
+
IResource[] res;
try {
res = container.members();
- } catch( CoreException e ) {
+ } catch (CoreException e) {
e.printStackTrace();
- res = new IResource[ 0 ];
+ res = new IResource[0];
}
-
- ArrayList<IResource> resources = new ArrayList<IResource> ();
- for( IResource r : res ) {
- if( resourcesToSkip.contains( r ))
+
+ ArrayList<IResource> resources = new ArrayList<IResource>();
+ for (IResource r : res) {
+ if (resourcesToSkip.contains(r))
continue;
-
- if( r instanceof IContainer ) {
- IResource[] subRes = getDirectValidChildren((IContainer) r, extensions, resourcesToSkip );
- if( subRes.length > 0 )
- resources.add( r );
- }
- else if( r instanceof IFile ) {
+
+ if (r instanceof IContainer) {
+ IResource[] subRes = getDirectValidChildren((IContainer) r,
+ extensions, resourcesToSkip);
+ if (subRes.length > 0)
+ resources.add(r);
+ } else if (r instanceof IFile) {
String extension = ((IFile) r).getFileExtension();
- if( extensions.contains( extension ))
- resources.add( r );
+ if (extensions.contains(extension))
+ resources.add(r);
}
}
-
- res = new IResource[ resources.size()];
- return resources.toArray( res );
+
+ res = new IResource[resources.size()];
+ return resources.toArray(res);
}
-
-
+
/**
* @see #getDirectValidChildren(IContainer, List)
*/
- public static IResource[] getDirectValidChildren(
- IContainer container, String[] extensions, List<IResource> resourcesToSkip ) {
-
- return getDirectValidChildren( container, Arrays.asList( extensions ), resourcesToSkip );
+ public static IResource[] getDirectValidChildren(IContainer container,
+ String[] extensions, List<IResource> resourcesToSkip) {
+
+ return getDirectValidChildren(container, Arrays.asList(extensions),
+ resourcesToSkip);
}
-
-
+
/**
* Gets the plug-in binary path.
* <p>
- * The plug-in can be either in a jar file or as a plug-in project.
- * In case of a plug-in project, this method assumes the class files are in a "bin" folder.
+ * The plug-in can be either in a jar file or as a plug-in project. In case
+ * of a plug-in project, this method assumes the class files are in a "bin"
+ * folder.
* </p>
*
- * @param pluginId the plug-in ID
- * @param binaryFolderName the name of the binary folder in the plug-in.
- * <p>
- * If null, the name will be set by default to "bin".
- * </p>
+ * @param pluginId
+ * the plug-in ID
+ * @param binaryFolderName
+ * the name of the binary folder in the plug-in.
+ * <p>
+ * If null, the name will be set by default to "bin".
+ * </p>
*
* @return the file containing the binary resources of a plug-in.
*/
- public static File getPluginBinaryPath( final String pluginId, String binaryFolderName ) {
+ public static File getPluginBinaryPath(final String pluginId,
+ String binaryFolderName) {
- if( binaryFolderName == null
- || binaryFolderName.trim().length() == 0 )
+ if (binaryFolderName == null || binaryFolderName.trim().length() == 0)
binaryFolderName = "bin"; //$NON-NLS-1$
-
+
File bundleFile;
try {
- bundleFile = FileLocator.getBundleFile( Platform.getBundle( pluginId ));
- if( bundleFile.isFile())
+ bundleFile = FileLocator
+ .getBundleFile(Platform.getBundle(pluginId));
+ if (bundleFile.isFile())
return bundleFile;
- else if( bundleFile.isDirectory()) {
- File binaryFolder = new File( bundleFile, binaryFolderName );
- if( binaryFolder.exists() && binaryFolder.isDirectory())
+ else if (bundleFile.isDirectory()) {
+ File binaryFolder = new File(bundleFile, binaryFolderName);
+ if (binaryFolder.exists() && binaryFolder.isDirectory())
return binaryFolder;
}
-
- } catch( Exception e ) {
+
+ } catch (Exception e) {
e.printStackTrace();
}
-
- return null;
+
+ return null;
}
-
-
+
/**
* Returns the IFile corresponding to a File object.
* <p>
- * This is a convenience method for:
+ * This is a convenience method for:
* </p>
* <code>
* ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( new Path( file.getAbsolutePath()));
* </code>
*
- * @param file the file
- * @return the associated IFile, or null if the file is not in the current workspace
+ * @param file
+ * the file
+ * @return the associated IFile, or null if the file is not in the current
+ * workspace
* @see IWorkspaceRoot#getFileForLocation(org.eclipse.core.runtime.IPath)
*/
- public static IFile getIFile( File file ) {
- Path path = new Path( file.getAbsolutePath());
- return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation( path );
+ public static IFile getIFile(File file) {
+ Path path = new Path(file.getAbsolutePath());
+ return ResourcesPlugin.getWorkspace().getRoot()
+ .getFileForLocation(path);
}
+
+ /**
+ * Returns the first IFile corresponding to <code>compositeName</code>
+ *
+ * @param compositeName
+ * @return
+ */
+ public static IFile getCompositeFile(String compositeName) {
+ if (compositeName == null) {
+ return null;
+ }
+
+ IFile result = null;
+
+ IFile currentCompositeFile = ResourceUtils.getIFileFromEditor();
+ if (currentCompositeFile == null)
+ return null;
+
+ // First search in the current project
+ IProject project = currentCompositeFile.getProject();
+ Iterator<IFile> composites = ResourceUtils.getFiles(project,
+ "composite").iterator();
+ while (result == null && composites.hasNext()) {
+ IFile compositeFile = composites.next();
+ if (compositeName.equals(compositeFile.getName())) {
+ result = compositeFile;
+ }
+ }
+
+ if (result == null) {
+ // Not found in the current project. Search in the referenced
+ // projects.
+ try {
+ IProject[] projects = project.getReferencedProjects();
+ int i = 0;
+ while (result == null && i < projects.length) {
+ Iterator<IFile> compositesIt = ResourceUtils.getFiles(
+ projects[i], "composite").iterator();
+ while (result == null && compositesIt.hasNext()) {
+ IFile compositeFile = compositesIt.next();
+ if (compositeName.equals(compositeFile.getName())) {
+ result = compositeFile;
+ }
+ }
+ }
+ } catch (CoreException e) {
+ // Nothing
+ }
+ }
+ return result;
+ }
+
}