/*
 * ElementTransformation.java
 *
 * Created on 29 giugno 2003, 0.32
 */

package it.businesslogic.ireport.undo;
import it.businesslogic.ireport.*;
import it.businesslogic.ireport.IReportFont;
import it.businesslogic.ireport.crosstab.CrosstabCell;
import java.awt.*;


/**
 *
 * @author Giulio
 * Modified by Robert Lamping, June 3, 2005
 * Consideration:
 *   An ElementTransformation should be able by itself to undo or redo
 *   its transformation. This is behaviour that should be encapsulated.
 *
 *   How to inform the environment that the element has changed is another thing to
 *   research.
 *
 *   What to capture to be able to restore the old situation should be
 *   as much as possible be the responsibility of the ElementTransformation.
 *
 */
public class ElementTransformation implements it.businesslogic.ireport.UndoOperation, ITransformation
{
    public  ReportElement element;
    
    public Rectangle oldBounds;
    public Rectangle newBounds;
    
    public Band oldBand;
    public Band newBand;
    
    public CrosstabCell oldCell;
    public CrosstabCell newCell;
    
    private Object oldFontSize;
    private Object newFontSize;
    
    
    public ElementTransformation()
    {
        // oldBounds = newBounds = new Rectangle();
    }
    
    // As the element will not be removed.
    // the element should be able to undo itself
    
    
    
    public boolean equals(Object obj)
    {
        boolean isEqual = false;
        if (obj instanceof ReportElement)
        {
            if (this.element != null )
            {
                isEqual = this.element.equals( (ReportElement) obj );
            }
        }
        
        return isEqual;
    }
    
    public void undo()
    {
        //System.out.println("Old bound: " + this.oldBounds);
        
        element.setPosition( new Point( this.oldBounds.x, this.oldBounds.y) ) ;
        
        element.setWidth( this.oldBounds.width);
        element.setHeight(this.oldBounds.height);
        
        element.updateBounds();
        
        // band
        if ( this.oldBand != null)
        {
            element.setBand( this.oldBand );
        }
        
        if ( this.oldCell != null)
        {
            element.setCell( this.oldCell );
        }
        
        // Restore relative position...
        if (element.getCell() != null)
        {
            element.setRelativePosition( new Point(element.getPosition().x - element.getCell().getLeft()- 10, element.getPosition().y - element.getCell().getTop()- 10)); 
        }
        
        // font
        if ( element instanceof TextReportElement)
        {
            try
            {
                ((TextReportElement) element).getIReportFont().setPropertyValue( IReportFont.FONT_SIZE,  oldFontSize);
            }
            catch (Exception e )
            {
                // fontsize not properly set
            }
        }
        
    }
    
    public void redo()
    {
        // location and size
        
        element.setPosition( new Point( this.newBounds.x, this.newBounds.y) );
        
        element.setWidth(this.newBounds.width);
        element.setHeight(this.newBounds.height);
        
        element.updateBounds( );
        
        // band
        if ( this.newBand != null)
        {
            element.setBand( this.newBand );
        }
        
        if ( this.newCell != null)
        {
            element.setCell( this.newCell );
        }
        
        // Restore relative position...
        if (element.getCell() != null)
        {
            element.setRelativePosition( new Point(element.getPosition().x - element.getCell().getLeft()- 10, element.getPosition().y - element.getCell().getTop()- 10)); 
        }
        
        // font
        if ( element instanceof TextReportElement)
        {
            try
            {    
                 ((TextReportElement) element).getIReportFont().setPropertyValue( IReportFont.FONT_SIZE,  newFontSize);
            }
            catch (Exception e )
            {
                // fontsize not properly set
            }
        }
        
    }
    
    /**
     * Let this class decide on its own which information is captures
     * for a later redo.
     */
    public void captureCurrent( Object obj)
    {
        ReportElement element = (ReportElement) obj;
        this.element = element;
        this.oldBounds = new Rectangle( element.getBounds() );
        this.oldBand = element.getBand();
        this.oldCell = element.getCell();
        
        if ( element instanceof TextReportElement)
        {
            this.oldFontSize = ((TextReportElement) element).getIReportFont().getPropertyValue( IReportFont.FONT_SIZE);
        }
    }
    
    /**
     * Let this class decide on its own which information is captures
     * for a later unddo
     */
    public void captureModified(Object obj)    // implicit typecast from Object to ReportElement?? NO!!
    {
        ReportElement element = (ReportElement) obj;
        this.newBounds = new Rectangle( element.getBounds() );
        this.newBand = element.getBand();
        this.newCell = element.getCell();
        
        if ( element instanceof TextReportElement)
        {
            this.newFontSize = ((TextReportElement) element).getIReportFont().getPropertyValue( IReportFont.FONT_SIZE);
        }
        
    }
    
    
}