
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     SpringSource    - (Andrew Eisenberg) Adapted for AJDT
 *******************************************************************************/
package org.eclipse.ajdt.internal.ui.wizards;

import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.PlatformUI;

/**
 * This abstract action delegate offers base functionality used by
 * other JAR Package based action delegates.
 * 
 * Based on JarPackageActionDelegate  
 * AspectJ changes marked
 */
public abstract class AJJarPackageActionDelegate implements IObjectActionDelegate {

    private IStructuredSelection fSelection;
    private Shell fShell;

    /**
     * Returns the active shell.
     * @return the active shell.
     */
    protected Shell getShell() {
        if (fShell != null)
            return fShell;
        return JavaPlugin.getActiveWorkbenchShell();
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        IWorkbenchPartSite site= targetPart.getSite();
        fShell= site != null ? site.getShell() : null;
    }

    /*
     * @see IActionDelegate
     */
    public void selectionChanged(IAction action, ISelection selection) {
        if (selection instanceof IStructuredSelection)
            fSelection= (IStructuredSelection)selection;
        else
            fSelection= StructuredSelection.EMPTY;
    }

    /**
     * Returns the description file for the first description file in
     * the selection. Use this method if this action is only active if
     * one single file is selected.
     * @param selection the current selection
     * @return description file
     */
    protected IFile getDescriptionFile(IStructuredSelection selection) {
        return (IFile)selection.getFirstElement();
    }

    /**
     * Returns a description file for each description file in
     * the selection. Use this method if this action allows multiple
     * selection.
     * @param selection the current selection
     * @return description files
     */
    protected IFile[] getDescriptionFiles(IStructuredSelection selection) {
        IFile[] files= new IFile[selection.size()];
        Iterator iter= selection.iterator();
        int i= 0;
        while (iter.hasNext())
            files[i++]= (IFile)iter.next();
        return files;
    }

    protected IWorkbench getWorkbench() {
        return PlatformUI.getWorkbench();
    }

    protected IStructuredSelection getSelection() {
        return fSelection;
    }
}
