/*******************************************************************************
 * Copyright (c) 2015, 2016 CNRS, IRIT, IRT AESE (IRT Saint Exupéry).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the LGPL 3.0
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0-standalone.html
 *
 * Contributors:
 *     Faiez Zalila - initial API and implementation and/or initial documentation
 *     Jorge Enrique Gutierrez Zuluaga
 *******************************************************************************/
package com.irtsaintexupery.fiacre.simulator.popup.actions;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.irtsaintexupery.fiacre.simulator.core.FiacreSimulator;
import com.irtsaintexupery.fiacre.simulator.perspective.ViewsController;


public class FiacreSimulationAction implements IObjectActionDelegate {

	private Shell shell;
	// added selection attribute
	private ISelection selection;
	
	/**
	 * Constructor for Action1.
	 */
	public FiacreSimulationAction() {
		super();
		//toto
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {				
		// charger le fichier .fcr
		
		//Jorge Gutierrez 12/08/2016
		try {
			ViewsController.INSTANCE.launchFeedBack();
		} catch (InvocationTargetException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		IFile file = getFile();
		
		FiacreSimulator simfcr = new FiacreSimulator(file);		
		 
	}

	// added getFile() method
	private IFile getFile() {
		return (IFile) ((IStructuredSelection) selection).getFirstElement();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		// Don't forget this line.
		// It is the most important line.
		this.selection = selection;
	}


}
