package org.yakindu.sct.statechart.diagram.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.emf.workspace.util.WorkspaceSynchronizer;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.notation.BooleanValueStyle;
import org.eclipse.gmf.runtime.notation.NotationFactory;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionDelegate;
import org.yakindu.sct.statechart.diagram.editparts.StateEditPart;

/**
 * Action that toggles the alignment flag in the notation view element
 * 
 * @author andreas muelder
 * @author terfloth
 * 
 */
public class ToggleSubRegionLayoutCommand implements IActionDelegate {

	private View view;

	public void run(IAction arg0) {
		TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(view);
		Command toggleCommand = new Command(editingDomain, view);
		
		try {
			toggleCommand.execute(new NullProgressMonitor(), null);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

//		BooleanValueStyle style = (BooleanValueStyle) view
//				.getStyle(NotationPackage.Literals.BOOLEAN_VALUE_STYLE);
//		boolean isHorizontal = (style != null) ? style.isBooleanValue() : true;
//
//		SetValueCommand setCommand = new SetValueCommand(new SetRequest(style,
//				NotationPackage.Literals.BOOLEAN_VALUE_STYLE__BOOLEAN_VALUE,
//				!isHorizontal));
//		try {
//			setCommand.execute(new NullProgressMonitor(), null);
//		} catch (ExecutionException e) {
//			e.printStackTrace();
//		}

	}

	protected View unwrap(ISelection selection) {
		IStructuredSelection structuredSelection = (IStructuredSelection) selection;
		Object firstElement = structuredSelection.getFirstElement();
		if (firstElement == null)
			return null;
		return ((StateEditPart) firstElement).getNotationView();
	}

	public void selectionChanged(IAction arg0, ISelection selection) {
		view = unwrap(selection);
	}

	
	protected static class Command extends AbstractTransactionalCommand {
		
		protected static final String TOGGLE_REGION_ALIGNMENT = "Toggle Region Alignment";

		protected View view; 
		
		public Command(TransactionalEditingDomain editingDomain, View view) {
			super(editingDomain, TOGGLE_REGION_ALIGNMENT, null);
			this.view = view;
		}

	    @SuppressWarnings({ "rawtypes", "unchecked" })
		public List getAffectedFiles() {
	    	
	        if (view != null) {
	            List result = new ArrayList();
	            IFile file = WorkspaceSynchronizer.getFile(view.eResource());
	            
	            if (file != null) {
	                result.add(file);
	            }
	            return result;
	        }
	        
	        return super.getAffectedFiles();
	    }

		/** 
		 * Executes the command that switches the subregion layout orientation.
		 */
		@SuppressWarnings("unchecked")
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
	            IAdaptable info)
	        throws ExecutionException {
	        
			BooleanValueStyle style = (BooleanValueStyle) view
					.getStyle(NotationPackage.Literals.BOOLEAN_VALUE_STYLE);
			
			if (style == null) {
				style = NotationFactory.eINSTANCE.createBooleanValueStyle();
				style.setBooleanValue(true);
				view.getStyles().add(style);
			} else {
				style.setBooleanValue(! style.isBooleanValue());
			}
			
			return CommandResult.newOKCommandResult(view);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.gmf.runtime.common.core.command.ICommand#getLabel()
		 */
		public String getLabel() {
			return TOGGLE_REGION_ALIGNMENT;
		}
		
	}
}

@@ -153,24 +153,94 @@
 Exception;%0D%0A
+import org.eclipse.core.commands.operations.OperationHistoryFactory;%0D%0A
 import org.e
@@ -674,141 +674,8 @@
 t;%0D%0A
-import org.eclipse.gmf.runtime.diagram.ui.commands.CommandProxy;%0D%0Aimport org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;%0D%0A
 impo
@@ -1595,16 +1595,22 @@
 ew);%0D%0A%09%09
+Toggle
 Command 
@@ -1629,16 +1629,22 @@
 d = new 
+Toggle
 Command(
@@ -1686,537 +1686,77 @@
 %0A%09%09%09
-toggleCommand.execute(new NullProgressMonitor(), null);%0D%0A%09%09%7D catch (ExecutionException e) %7B%0D%0A%09%09%09e.printStackTrace();%0D%0A%09%09%7D%0D%0A%0D%0A//%09%09BooleanValueStyle style = (BooleanValueStyle) view%0D%0A//%09%09%09%09.getStyle(NotationPackage.Literals.BOOLEAN_VALUE_STYLE);%0D%0A//%09%09boolean isHorizontal = (style != null) ? style.isBooleanValue() : true;%0D%0A//%0D%0A//%09%09SetValueCommand setCommand = new SetValueCommand(new SetRequest(style,%0D%0A//%09%09%09%09NotationPackage.Literals.BOOLEAN_VALUE_STYLE__BOOLEAN_VALUE,%0D%0A//%09%09%09%09!isHorizontal));%0D%0A//%09%09try %7B%0D%0A//%09%09%09setCommand.execute(
+OperationHistoryFactory.getOperationHistory().execute(toggleCommand, 
 new 
@@ -1786,18 +1786,16 @@
 null);%0D%0A
-//
 %09%09%7D catc
@@ -1822,18 +1822,16 @@
 n e) %7B%0D%0A
-//
 %09%09%09e.pri
@@ -1851,10 +1851,8 @@
 );%0D%0A
-//
 %09%09%7D%0D
@@ -2271,16 +2271,82 @@
 %7D%0D%0A%0D%0A%09%0D%0A
+%09/**%0D%0A%09 * GMF command class...%0D%0A%09 * %0D%0A%09 * @author terfloth%0D%0A%09 */%0D%0A
 %09protect
@@ -2361,16 +2361,22 @@
 c class 
+Toggle
 Command 
@@ -2545,16 +2545,22 @@
 %09public 
+Toggle
 Command(

add toggle subregion alignment command to operation history