package eu.scasefp7.eclipse.storyboards.handlers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;

public abstract class ProjectAwareHandler extends AbstractHandler {

	/**
	 * Returns the project that the selected file(s) belong to.
	 * 
	 * @param selectionList the selected file(s).
	 * @return the project that the selected file(s) belong to.
	 */
	protected IProject getProjectOfSelectionList(List<Object> selectionList) {
		IProject project = null;
		for (Object object : selectionList) {
			IFile file = (IFile) Platform.getAdapterManager().getAdapter(object, IFile.class);
			IProject theproject = null;
			if (file != null) {
				theproject = file.getProject();
			} else {
				theproject = (IProject) Platform.getAdapterManager().getAdapter(object, IProject.class);
			}
			if (theproject != null) {
				if (project == null) {
					project = theproject;
				} else {
					if (!project.equals(theproject)) {
						return null;
					}
				}
			}
		}
		return project;
	}

	private void processContainer(IContainer container, ArrayList<IFile> files, String extension) {
		try {
			IResource[] members = container.members();
			for (IResource member : members) {
				if (member instanceof IContainer) {
					processContainer((IContainer) member, files, extension);
				} else if (member instanceof IFile) {
					if (extension.equals("") || ((IFile) member).getName().endsWith("." + extension))
						files.add((IFile) member);
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	protected ArrayList<IFile> getFilesOfProject(IProject project, String extension) {
		ArrayList<IFile> files = new ArrayList<IFile>();
		processContainer(project, files, extension);
		return files;
	}
}

@@ -412,16 +412,106 @@
 tform;%0A%0A
+/**%0A * Project aware handler for receiving the current project.%0A * %0A * @author themis%0A */%0A
 public a
@@ -1383,16 +1383,274 @@
 ct;%0A%09%7D%0A%0A
+%09/**%0A%09 * Finds the files of a container recursively.%0A%09 * %0A%09 * @param container a workspace container (e.g. project or folder).%0A%09 * @param files a list of files that is populated.%0A%09 * @param extension the extension of the files that are to be retrieved.%0A%09 */%0A
 %09private
@@ -2167,16 +2167,270 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns the files of the given project.%0A%09 * %0A%09 * @param project the project of which the files are returned.%0A%09 * @param extension the extension of the returned files.%0A%09 * @return a list of the files of the project with the given extension.%0A%09 */%0A
 %09protect

Add comments to project aware handler