/**********************************************************************
Copyright (c) 2000, 2002 IBM Corp. and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html

Contributors:
    IBM Corporation - Initial implementation
    www.phpeclipse.de
**********************************************************************/
package net.sourceforge.phpeclipse.actions;

import java.util.Iterator;

import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

public class PHPDocumentorAction implements IObjectActionDelegate {

  private IWorkbenchPart workbenchPart;
  /**
   * Constructor for Action1.
   */
  public PHPDocumentorAction() {
    super();
  }

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

  //  public static void open(final URL url, final Shell shell, final String dialogTitle) {
  //    IHelp help= WorkbenchHelp.getHelpSupport();
  //    if (help != null) {
  //      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
  //    } else {
  //      showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
  //    }
  //  }

  public void run(IAction action) {
    ISelectionProvider selectionProvider = null;
    selectionProvider = workbenchPart.getSite().getSelectionProvider();

    StructuredSelection selection = null;
    selection = (StructuredSelection) selectionProvider.getSelection();

    final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
    final String phpExecutable = store.getString(PHPeclipsePlugin.PHP_RUN_PREF);

    //Shell shell = null;
    Iterator iterator = null;
    iterator = selection.iterator();
    while (iterator.hasNext()) {
      //  obj => selected object in the view
      Object obj = iterator.next();

      // is it a resource
      if (obj instanceof IResource) {
        IResource resource = (IResource) obj;

        // check the resource
        switch (resource.getType()) {
          case IResource.PROJECT :
            IProject project = (IProject) resource;
            String projectName = project.getLocation().toString();
            String targetDirectory = projectName + "/phpdoc";
            // example: 
            // C:\>php.exe "C:\Path\To\phpdoc" -t targetdir -o HTML:default:default -d parsedir
            String argument =
              "\"C:\\php\\phpDocumentor\\phpdoc\" -t " + targetDirectory + " -o HTML:default:default -d " + projectName;
            ExternalToolsUtil.execute("phpdocumentor", phpExecutable, argument, true);
            break;
            //          case IResource.FILE :
            //            // single file:
            //            IFile file = (IFile) resource;
            //            PHPParserSuperclass.phpExternalParse(file);
        }
      }
    }
  }

  /**
   * @see IActionDelegate#selectionChanged(IAction, ISelection)
   */
  public void selectionChanged(IAction action, ISelection selection) {
  }

}