package emn.fr.ascola.extractor;

import java.util.Vector;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

/**
 * Test to analyse a source code.
 * @author jroyer
 *
 */
public class Test implements IWorkbenchWindowActionDelegate {
	/**
	 * Associated shell.
	 */
	public static  Shell shell;
	/**
	 * We cache the parent window object to create the message dialog.
	 */
	protected IWorkbenchWindow window;


	public void run(IAction action) {
		//IProgressMonitor monitor = TODO;
		// IPackageFragment faut une instance  
		//ICompilationUnit compil = .createCompilationUnit("tmp", "???", true, null);
		//IPackageFragmentRoot.

	}
	
	/**
	* TODO 
	 */
	public Vector<ICompilationUnit> getPackageFragment() {
		// TODO define it
		JavaProject javaProject = null;
		IPackageFragmentRoot[] ipfr;
		Vector<ICompilationUnit> icu = new Vector<ICompilationUnit>();
		try {
			// acces the package root for Java resources
			ipfr = javaProject.getAllPackageFragmentRoots();
			for (int i = 0; i < ipfr.length; i++) {
				// select internal resources
				if (!ipfr[i].isExternal()) {
					// get the inner packages
					IJavaElement [] ije =  ipfr[i].getChildren();
					for (int j = 0; j < ije.length; j++) {
						// cast needed here 
						IPackageFragment pf = (IPackageFragment) ije[j];
						// if there are Java resources inside
						if (pf.containsJavaResources()) {
							// search for compilation unit
							ICompilationUnit [] tmp = pf.getCompilationUnits();
							for (int k = 0; k < tmp.length; k++) {
								ICompilationUnit cu = tmp[k];
								icu.add(cu);
							}
						}
					}
				}
			}
		} catch (JavaModelException e) {
			e.printStackTrace();
		}
		return icu;
	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void init(IWorkbenchWindow window) {
		this.window = window;
		this.shell = window.getShell();

	}

	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}
	
	public IWorkbenchWindow getWindow() {
		return window;
	}

}
