/**
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * Copyright © 2009 Mindoo GmbH
 *
 * @author Tammo Riedinger
 *
 */
package com.mindoo.ec09.example3.actions;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;

import com.mindoo.ec09.example3.SampleView;

/**
 * Global action to run a Swing-action with a specified name (parameter following the class of the action in plugin.xml,
 * e.g. "com.mindoo.ec09.example3.actions.SwingActionDelegate:Open document").<br>
 * The action will be run for the currently active view.
 * 
 * @author Tammo Riedinger
 */
public class SwingActionDelegate extends ActionDelegate implements IWorkbenchWindowActionDelegate, IExecutableExtension {
	private String m_actionName;
	
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
		if (data!=null)
			m_actionName=data.toString();
	}

	public void init(IWorkbenchWindow window) {
	}
	
	@Override
	public void run(IAction action) {
		// invoke the action with the configured name on the active sample-view, if any
		IWorkbenchWindow activeWindow=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (activeWindow==null)
			return;
		
		IWorkbenchPage activePage=activeWindow.getActivePage();
		if (activePage==null)
			return;
		
		IWorkbenchPart activePart=activePage.getActivePart();
		if (!(activePart instanceof SampleView))
			return;
		
		((SampleView)activePart).runSwingAction(m_actionName);
	}
}