3 import flash.display.Stage;
4 import flash.display.Sprite;
5 import flash.display.LoaderInfo;
6 import flash.display.StageScaleMode;
8 import flash.display.StageAlign;
9 import flash.display.StageScaleMode;
10 import flash.external.ExternalInterface;
11 import flash.system.Security;
13 import flash.system.System;
14 import flash.net.FileReference;
15 import flash.net.FileFilter;
18 import org.alivepdf.pdf.PDF;
19 import org.alivepdf.data.Grid;
20 import org.alivepdf.data.GridColumn;
21 import org.alivepdf.layout.Orientation;
22 import org.alivepdf.layout.Size;
23 import org.alivepdf.layout.Unit;
24 import org.alivepdf.display.Display;
25 import org.alivepdf.saving.Method;
26 import org.alivepdf.fonts.FontFamily;
27 import org.alivepdf.fonts.Style;
28 import org.alivepdf.fonts.CoreFont;
29 import org.alivepdf.colors.RGBColor;
33 private var domId:String =
'';
34 private var button:Sprite;
35 private var clipText:String =
'blank';
36 private var fileName:String =
'';
37 private var action:String =
'copy';
38 private var incBom:Boolean =
true;
39 private var charSet:String =
'utf8';
44 stage.scaleMode = StageScaleMode.EXACT_FIT;
45 flash.system.Security.allowDomain(
"*");
48 var flashvars:Object = LoaderInfo( this.root.loaderInfo ).parameters;
49 domId = flashvars.id.split(
"\\").join(
"\\\\");
52 if ( domId !== parseInt( domId, 10 ).toString() ) {
53 throw new Error(
'Invalid DOM id' );
57 button =
new Sprite();
58 button.buttonMode =
true;
59 button.useHandCursor =
true;
60 button.graphics.beginFill(0x00FF00);
61 button.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
65 button.addEventListener(MouseEvent.CLICK,
function(event:Event):
void {
68 button.addEventListener(MouseEvent.MOUSE_OVER,
function(event:Event):
void {
69 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseOver', null );
71 button.addEventListener(MouseEvent.MOUSE_OUT,
function(event:Event):
void {
72 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseOut', null );
74 button.addEventListener(MouseEvent.MOUSE_DOWN,
function(event:Event):
void {
75 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseDown', null );
77 button.addEventListener(MouseEvent.MOUSE_UP,
function(event:Event):
void {
78 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'mouseUp', null );
83 stage.addEventListener(Event.ACTIVATE, addCallbacks);
86 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'load', null );
89 public function addCallbacks (evt:Event = null):void {
90 ExternalInterface.addCallback(
"setHandCursor", setHandCursor);
91 ExternalInterface.addCallback(
"clearText", clearText);
92 ExternalInterface.addCallback(
"setText", setText);
93 ExternalInterface.addCallback(
"appendText", appendText);
94 ExternalInterface.addCallback(
"setFileName", setFileName);
95 ExternalInterface.addCallback(
"setAction", setAction);
96 ExternalInterface.addCallback(
"setCharSet", setCharSet);
97 ExternalInterface.addCallback(
"setBomInc", setBomInc);
101 public function setCharSet(newCharSet:String):void {
102 if ( newCharSet ==
'UTF16LE' ) {
103 charSet = newCharSet;
109 public function setBomInc(newBomInc:Boolean):void {
113 public function clearText():void {
117 public function appendText(newText:String):void {
121 public function setText(newText:String):void {
125 public function setFileName(newFileName:String):void {
126 fileName = newFileName;
129 public function setAction(newAction:String):void {
133 public function setHandCursor(enabled:Boolean):void {
136 button.useHandCursor = enabled;
140 private function clickHandler(event:Event):void {
141 var fileRef:FileReference =
new FileReference();
142 fileRef.addEventListener(Event.COMPLETE, saveComplete);
144 if ( action ==
"save" ) {
146 if ( charSet ==
'UTF16LE' ) {
147 fileRef.save( strToUTF16LE(clipText), fileName );
149 fileRef.save( strToUTF8(clipText), fileName );
151 }
else if ( action ==
"pdf" ) {
153 var pdf:PDF = configPdf();
154 fileRef.save( pdf.save( Method.LOCAL ), fileName );
157 System.setClipboard( clipText );
158 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'complete', clipText );
163 private function saveComplete(event:Event):void {
164 ExternalInterface.call(
'ZeroClipboard_TableTools.dispatch', domId,
'complete', clipText );
168 private function getProp( prop:String, opts:Array ):String
171 for ( i=0, iLen=opts.length ; i<iLen ; i++ )
173 if ( opts[i].indexOf( prop+
":" ) != -1 )
175 return opts[i].replace( prop+
":",
"" );
182 private function configPdf():PDF
187 splitText:Array = clipText.split(
"--/TableToolsOpts--\n"),
188 opts:Array = splitText[0].split(
"\n"),
189 dataIn:Array = splitText[1].split(
"\n"),
190 aColRatio:Array = getProp(
'colWidth', opts ).split(
'\t'),
191 title:String = getProp(
'title', opts ),
192 message:String = getProp(
'message', opts ),
193 orientation:String = getProp(
'orientation', opts ),
194 size:String = getProp(
'size', opts ),
202 pdf =
new PDF( Orientation[orientation.toUpperCase()], Unit.MM, Size[size.toUpperCase()] );
203 pdf.setDisplayMode( Display.FULL_WIDTH );
205 iPageWidth = pdf.getCurrentPage().w-20;
206 pdf.textStyle(
new RGBColor(0), 1 );
209 pdf.setFont(
new CoreFont(FontFamily.HELVETICA), 14 );
212 pdf.writeText(11, title+
"\n");
215 pdf.setFont(
new CoreFont(FontFamily.HELVETICA), 11 );
218 pdf.writeText(11, message+
"\n");
222 for ( i=0, iLen=dataIn.length ; i<iLen ; i++ )
224 if ( dataIn[i] !=
"" )
226 dataOut.push( dataIn[i].split(
"\t") );
229 headers = dataOut.shift();
231 for ( i=0, iLen=headers.length ; i<iLen ; i++ )
233 columns.push(
new GridColumn(
" \n"+headers[i]+
"\n ", i.toString(), aColRatio[i]*iPageWidth,
'C' ) );
236 var grid:Grid =
new Grid(
240 new RGBColor (0xE0E0E0),
241 new RGBColor (0xFFFFFF),
243 new RGBColor ( 0x0 ),
249 pdf.addGrid( grid, 0, y );
260 private function strToUTF8( str:String ):ByteArray {
261 var utf8:ByteArray =
new ByteArray();
265 utf8.writeByte( 0xEF );
266 utf8.writeByte( 0xBB );
267 utf8.writeByte( 0xBF );
269 utf8.writeUTFBytes( str );
285 private function strToUTF16LE( str:String ):ByteArray {
286 var utf16:ByteArray =
new ByteArray();
288 var i:uint=0, iLen:uint = str.length;
292 utf16.writeByte( 0xFF );
293 utf16.writeByte( 0xFE );
297 iChar = str.charCodeAt(i);
299 if ( iChar < 0xFF ) {
301 utf16.writeByte( iChar );
302 utf16.writeByte( 0 );
305 utf16.writeByte( iChar & 0x00FF );
306 utf16.writeByte( iChar >> 8 );