/*************************************************************************************
 * Copyright (c) 2004 Actuate 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:
 * Actuate Corporation - Initial implementation.
 ************************************************************************************/

package org.eclipse.birt.report.designer.internal.ui.command;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.model.api.CommandStack;
import org.eclipse.birt.report.model.api.activity.ActivityStackListener;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStackListener;


/**
 * @author David Michonneau
 * 
 * This class is a command stack for the GEF framework. It internally access the
 * ActivityStack class of the DE. No commands are pushed to the command stack or
 * ActivityStack here since the Design handles take care of that when executed.
 * Commands are executed as transactions.
 */
public class WrapperCommandStack extends org.eclipse.gef.commands.CommandStack
{

	private CommandStack ar;

	public WrapperCommandStack( )
	{
		this( SessionHandleAdapter.getInstance( )
				.getCommandStack( ) );
	}

	public WrapperCommandStack(
			org.eclipse.birt.report.model.api.CommandStack ar )
	{
		this.ar = ar;
	}

	public boolean canUndo( )
	{
		return ar.canUndo( );
	}

	public boolean canRedo( )
	{
		return ar.canRedo( );
	}

	public void undo( )
	{
		if ( canUndo( ) )
			ar.undo( );
	}

	public void redo( )
	{
		if ( canRedo( ) )
			ar.redo( );
	}

	public void flush( )
	{
		ar.flush( );
	}

	public Command getRedoCommand( )
	{
		return new CommandWrap4DE( ar.getRedoRecord( ) );
	}

	public Command getUndoCommand( )
	{
		return new CommandWrap4DE( ar.getUndoRecord( ) );
	}

	public void execute( Command command )
	{
		ar.startTrans( command.getLabel() );
		command.execute( );
		ar.commit( );

	}

	public void setUndoLimit( int undoLimit )
	{
		ar.setStackLimit( undoLimit );
	}

	public void addCommandStackListener( ActivityStackListener listener )
	{
		ar.addListener( listener );
	}

	public void removeCommandStackListener( ActivityStackListener listener )
	{
		ar.removeListener( listener );
	}

	/*
	 * Do not use, use addCommandStackListener(ActivityStackListener) instead
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.CommandStack#addCommandStackListener(org.eclipse.gef.commands.CommandStackListener)
	 */
	public void addCommandStackListener( CommandStackListener listener )
	{
		// use addCommandStackListener(ActivityStackListener) instead
		//this method will called by GEF.
		//can't assert false.
		//see bugzilla 147687
//		assert false;
	}

	/*
	 * Do not use, use removeCommandStackListener(ActivityStackListener) instead
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.CommandStack#removeCommandStackListener(org.eclipse.gef.commands.CommandStackListener)
	 */
	public void removeCommandStackListener( CommandStackListener listener )
	{
		// use removeCommandStackListener(ActivityStackListener) instead
//		assert false;
	}

	public void setActivityStack( CommandStack ar )
	{
		this.ar = ar;
	}

	public void dispose( )
	{
		// TODO Auto-generated method stub
		super.dispose( );
		ar.flush();
		ar.clearListeners();
		ar = null;
	}
}
@@ -899,17 +899,16 @@
 tener;%0A%0A
-%0A
 /**%0A * @
@@ -1413,13 +1413,8 @@
 e( )
-%0A%09%09%09%09
 .get
@@ -2054,24 +2054,71 @@
 ommand )%0A%09%7B%0A
+%09%09if ( command == null )%0A%09%09%7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09%0A
 %09%09ar.startTr
@@ -2139,16 +2139,17 @@
 etLabel(
+ 
 ) );%0A%09%09c
@@ -2859,16 +2859,17 @@
 ead%0A%09%09//
+ 
 this met
@@ -2896,16 +2896,17 @@
 EF.%0A%09%09//
+ 
 can't as
@@ -2921,16 +2921,17 @@
 se.%0A%09%09//
+ 
 see bugz
@@ -2942,20 +2942,21 @@
  147687%0A
-//
 %09%09
+// 
 assert f
@@ -3343,12 +3343,13 @@
 ead%0A
-//
 %09%09
+// 
 asse
@@ -3529,16 +3529,17 @@
 r.flush(
+ 
 );%0A%09%09ar.
@@ -3553,16 +3553,17 @@
 steners(
+ 
 );%0A%09%09ar 

handle null command case in execute() method.