SavePDFView class

/**************************************************************************
flashscriptMVC, by Joachim Schnier, flashs46@flashscript.biz

Copyright 2010 Joachim Schnier (Flashscript.biz)

class SavePDFView (Viewer for the Flex MVC)
*****************************************************************************/
package com.flashscriptMVC.views
{
	import com.flashscriptMVC.AbstractClass;
	import com.flashscriptMVC.IControlHandler;
	import com.flashscriptMVC.IModel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.controls.TextInput;
	
	import org.alivepdf.display.Display;
	import org.alivepdf.layout.Orientation;
	import org.alivepdf.pdf.PDF;
	import org.alivepdf.saving.Method;
	
	public class SavePDFView extends AbstractClass
	{
		public var md:IModel; 
		public var c:IControlHandler;
		public var controller:IControlHandler;
		private var myPDF:PDF;
		private var dataFile:String;

/*
* We add getter - setter functions to comunicate with the Flex file.
*/

		private var _email:String;
		public function setemail(em:String):void
		{
			_email = em;
		}
		public function getemail():String
		{
			return _email;
		}
		private var _message:String;
		public function setmessage(m:String):void
		{
			_message = m;
		}
		public function getmessage():String
		{
			return _message;
		}
		private var _setPDF:Image;
		public function setMyPDF(p:Image):void
		{
			_setPDF = p;
		}
		public function getMyPDF():Image
		{
			return _setPDF;
		}
		private var _setTextField:TextInput;
		public function setTextField(tf:TextInput):void
		{
			_setTextField = tf;
		}
		public function getTextField():TextInput
		{
			return _setTextField;
		}
		public function SavePDFView (aModel:IModel,aController:IControlHandler=null):void
		{

/*
* We initiate the model and controller and add all objects, which we use on the stage.
*/

			super (aModel,aController);
			controller = aController;
			md = model as IModel;
			c = controller as IControlHandler;
		}

/*
* The update function is from the Model class and automatically initiated.
* Here we can retrieve the xml data. Usually we can add other responses. However,
* for the PDF data saving we use the Mediator class for all other responses
* from the Model class. The Mediator class can then further communicate with the
* this class using the Event design pattern, since we can only create the
* SavePDFView object once.
*/

		override public function update (event:Event=null, err:String = null):void
		{
			if (event.type == "viewWindow")
			{
				getTextField().htmlText = "<b>ready!</b>";
				getMyPDF().source = md.getXMLData().image;
				dataFile = md.getXMLData().phpfile;
				myObject.addEventListener (SENDMESSAGE, processEvent);
			}
		}
		public function sendPDF ():void
		{
			myPDF = new PDF(Orientation.PORTRAIT);
			myPDF.setDisplayMode ( Display.FULL_PAGE );
			myPDF.addPage ();
			myPDF.addImage (getMyPDF().content);

/*
* Here we have modified the save method from the PDF class and added an additional parameter
* to send email to a recipient.
*/

			myPDF.save ( Method.BASE_64, dataFile, "attachment", "SavedPDF.pdf", getemail() );
			getTextField().htmlText = "<b>PDF sent!</b>" ;
		}

/*
* This message is from the Mediator object, when there is a response from the Model.
*/

		private function processEvent (event:Event):void
		{
			if (get_Message() == "dataSent")
			{
				getTextField().htmlText = "<b>PDF was sent.</b>";
			}
			else
			{
				getTextField().htmlText = "<b>"+get_Message()+"</b>";
			}
		}
	}
}