/*
 * Created on 2004-09-07
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.actions;

import java.io.IOException;
import java.util.Iterator;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianCommandHelper;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.properties.ProjectProperties;

/**
 * Opens the SDK help which was autodetected by the SymbianSDKEntry
 * 
 * @author mikolaj
 */
public class SDKHelpAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	private IProject currentProject=null;
	

    /**
     * The constructor 
     */
    public SDKHelpAction() {
        super();
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
	    if (currentProject==null)
	    {
	        MessageDialog.openError(window.getShell(), SymbdevPlugin.MESSAGE_TITLE,
	                "Internal error: couldn't retrieve current project.");
	        return;
	    }
	    
	    String sdkHelp;
	    try
	    {
	        SymbianSDKEntry sdk=ProjectProperties.getSymbianSDK(currentProject);
	        sdkHelp=sdk.getSDKHelp();
	        if (sdkHelp==null)
	        {
		        MessageDialog.openInformation(window.getShell(), SymbdevPlugin.MESSAGE_TITLE,
                	"Couldn't locate help in this SDK.");
		        return;	            
	        }
	        
	    }
	    catch (CoreException e)
	    {
	        ErrorDialog.openError(window.getShell(), SymbdevPlugin.MESSAGE_TITLE,
	                "Couln't setup shell environment", e.getStatus());
	        return;
	    }
        
		String[] cmd=new String[4];
		cmd[0]=SymbianCommandHelper.getOSShell();
		cmd[1]="/c";
		cmd[2]="start";
		cmd[3]=sdkHelp;
	    
		try
		{
			Runtime.getRuntime().exec(cmd);
		}
		catch (IOException e)
		{
		    MessageDialog.openError(
					window.getShell(),
					SymbdevPlugin.MESSAGE_TITLE,
					"Couldn't open help: "+e.getLocalizedMessage());
			e.printStackTrace();
		}	    
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
     */
    public void dispose() {
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
     */
    public void init(IWorkbenchWindow window) {
		this.window = window;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection)selection;
			Iterator it=sel.iterator();
			while (it.hasNext())
			{
			    Object obj = it.next();
				if (obj instanceof IResource) {
			    	currentProject=((IResource)obj).getProject();
			    	return;
				}
				if (obj instanceof ICElement) {
			    	currentProject=((ICElement)obj).getCProject().getProject();
			    	return;
				}
			}
		}
		currentProject=null;
    }

}
