Guest User

TableExplorer for use with 4.14

a guest
Feb 7th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MXML 55.33 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.              xmlns:s="library://ns.adobe.com/flex/spark"
  4.              xmlns:mx="library://ns.adobe.com/flex/mx"
  5.              xmlns:local="*"
  6.              
  7.              minWidth="955" minHeight="600"
  8.              applicationComplete="importApplicationCompleteHandler(event)"
  9.              uncaughtError="uncaughtErrorHandler(event)"
  10.              error="errorHandler(event)">
  11.    
  12.     <fx:Style>
  13.         @namespace s "library://ns.adobe.com/flex/spark";
  14.         @namespace mx "library://ns.adobe.com/flex/mx";
  15.        
  16.         .header {
  17.             fontSize: 12;
  18.             fontWeight: normal;
  19.             backgroundColor: #E6E6E6;
  20.             color: #565656;
  21.             paddingTop: 6;
  22.             paddingBottom: 4;
  23.             paddingLeft: 4;
  24.             paddingRight: 4;
  25.         }
  26.         .header2 {
  27.             fontSize: 12;
  28.             fontWeight: normal;
  29.             backgroundColor: #FF0000;
  30.             color: #565656;
  31.             paddingTop: 6;
  32.             paddingBottom: 4;
  33.             paddingLeft: 4;
  34.             paddingRight: 4;
  35.         }
  36.        
  37.         global {
  38.             focusThickness: 0;
  39.         }
  40.        
  41.         .buttonGroup {
  42.             fontSize: 12;
  43.         }
  44.     </fx:Style>
  45.    
  46.     <fx:Declarations>
  47.         <fx:XML id="simpleTextMarkup">
  48.             <TextFlow fontSize="11" paddingLeft="8" paddingRight="8" paddingTop="8" whiteSpaceCollapse="collapse" version="3.0.0" xmlns="http://ns.adobe.com/textLayout/2008">
  49.                 <p>
  50.                     <span>A un Angleso it va semblar un simplificat Angles, quam un skeptic Cambridge amico dit me que Occidental es.Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. </span>
  51.                 </p>
  52.             </TextFlow>
  53.         </fx:XML>
  54.        
  55.         <fx:XML id="complexTextFlow">
  56.             <TextFlow fontSize="11" paddingLeft="8" paddingRight="8" paddingTop="8" whiteSpaceCollapse="collapse" version="3.0.0" xmlns="http://ns.adobe.com/textLayout/2008">
  57.                 <p>
  58.                     <img paddingRight="8" source="http://lh4.googleusercontent.com/-v0soe-ievYE/AAAAAAAAAAI/AAAAAAACTE4/HdLawjGMFsE/s120-c/photo.jpg" float="right"/>
  59.                     <span>A un Angleso it va semblar un simplificat Angles, quam un skeptic Cambridge amico dit me que Occidental es.Li Europan lingues es membres del sam familie. Lor separat existentie es un myth. Por scientie, musica, sport etc, litot Europa usa li sam vocabular. Li lingues differe solmen in li grammatica, li pronunciation e li plu commun vocabules. Omnicos directe al desirabilite de un nov lingua franca: On refusa continuar payar custosi traductores. At solmen va esser necessi far uniform grammatica, pronunciation e plu sommun paroles. </span>
  60.                 </p>
  61.             </TextFlow>
  62.         </fx:XML>
  63.        
  64.         <fx:XML id="simpleTableTextFlow">
  65.             <TextFlow fontSize="11" paddingLeft="8" paddingRight="8" paddingTop="8" whiteSpaceCollapse="collapse" version="3.0.0" xmlns="http://ns.adobe.com/textLayout/2008">
  66.                 <p>
  67.                     <span>A un Angleso it va semblar un simplificat Angles</span>
  68.                 </p>
  69.                 <table borderColor="0xFF00FF" borderThickness="1" borderStyle="solid" paddingLeft="8" paddingTop="2" paddingRight="8" paddingBottom="2">
  70.                     <tr italic="true">
  71.                         <td headers="t1"><p><span>T. Sexton</span></p></td>
  72.                         <td headers="t2"><p><span>10</span></p></td>
  73.                         <td headers="t3"><p><span>Espresso</span></p></td>
  74.                         <td headers="t4"><p><span>No</span></p></td>
  75.                     </tr>
  76.                     <tr>
  77.                         <td headers="t1"><p><span>J. Dinnen</span></p></td>
  78.                         <td headers="t2"><p><span>5</span></p></td>
  79.                         <td headers="t3"><p><span>Decaf</span></p></td>
  80.                         <td headers="t4"><p><span>Yes</span></p></td>
  81.                     </tr>
  82.                 </table>
  83.             </TextFlow>
  84.         </fx:XML>
  85.        
  86.         <fx:Component className="ImageButton" >
  87.             <s:Image useHandCursor="true" buttonMode="true"
  88.                      mouseDown="filters=buttonDownFilters"
  89.                      mouseUp="filters=bevelFilters"
  90.                      rollOut="filters=bevelFilters">
  91.                
  92.                 <s:filters>
  93.                     <fx:Array id="bevelFilters">
  94.                         <s:BevelFilter id="bevelFilter"
  95.                                        angle="45.0"
  96.                                        blurX="1" blurY="1"
  97.                                        distance="0.4"
  98.                                        highlightAlpha="1.0"
  99.                                        knockout="false"
  100.                                        highlightColor="#FFFFFF"
  101.                                        shadowColor="#000000"
  102.                                        quality="3"
  103.                                        shadowAlpha="1.0"
  104.                                        strength="1.5"
  105.                                        type="inner"/>
  106.                     </fx:Array>
  107.                 </s:filters>
  108.                
  109.                 <fx:Declarations>
  110.                     <fx:Array id="buttonDownFilters">
  111.                        
  112.                         <s:BevelFilter angle="-135"
  113.                                        distance="1"
  114.                                        strength=".8"
  115.                                        blurX="2" blurY="2"
  116.                                        highlightColor="0xffffff"
  117.                                        type="inner"/>
  118.                     </fx:Array>
  119.                 </fx:Declarations>
  120.             </s:Image>
  121.         </fx:Component>
  122.     </fx:Declarations>
  123.    
  124.     <fx:Script>
  125.         <![CDATA[
  126.             import flash.text.engine.TextLine;
  127.             import flash.utils.ByteArray;
  128.            
  129.             import mx.core.BitmapAsset;
  130.             import mx.core.mx_internal;
  131.            
  132.             import flashx.textLayout.TextLayoutVersion;
  133.             import flashx.textLayout.compose.TextFlowTableBlock;ByteArray
  134.            
  135.             import flashx.textLayout.formats.TextLayoutFormat;TextLayoutFormat
  136.            
  137.             import mx.core.IVisualElement;
  138.             import mx.events.FlexEvent;
  139.            
  140.             import spark.core.SpriteVisualElement;
  141.             import spark.events.TextOperationEvent;
  142.            
  143.             import flashx.textLayout.tlf_internal;
  144.             import flashx.textLayout.compose.ComposeState;
  145.             import flashx.textLayout.compose.IFlowComposer;
  146.             import flashx.textLayout.compose.ParcelList;
  147.             import flashx.textLayout.compose.StandardFlowComposer;
  148.             import flashx.textLayout.compose.TextFlowLine;
  149.             import flashx.textLayout.container.ColumnState;
  150.             import flashx.textLayout.container.ContainerController;
  151.             import flashx.textLayout.conversion.ITextExporter;
  152.             import flashx.textLayout.conversion.ITextImporter;
  153.             import flashx.textLayout.conversion.TextConverter;
  154.             import flashx.textLayout.edit.EditManager;
  155.             import flashx.textLayout.edit.ISelectionManager;
  156.             import flashx.textLayout.edit.SelectionFormat;
  157.             import flashx.textLayout.edit.SelectionManager;
  158.             import flashx.textLayout.elements.Configuration;Configuration
  159.             import flashx.textLayout.elements.FlowLeafElement;
  160.             import flashx.textLayout.elements.InlineGraphicElement;
  161.             import flashx.textLayout.elements.ParagraphElement;
  162.             import flashx.textLayout.elements.SpanElement;
  163.             import flashx.textLayout.elements.TableCellElement;
  164.             import flashx.textLayout.elements.TableColElement;
  165.             import flashx.textLayout.elements.TableElement;
  166.             import flashx.textLayout.elements.TableRowElement;
  167.             import flashx.textLayout.elements.TextFlow;
  168.             import flashx.textLayout.events.CompositionCompleteEvent;
  169.             import flashx.textLayout.events.FlowElementMouseEvent;
  170.             import flashx.textLayout.events.ModelChange;
  171.             import flashx.textLayout.events.SelectionEvent;
  172.             import flashx.textLayout.events.StatusChangeEvent;
  173.             import flashx.textLayout.formats.WhiteSpaceCollapse;WhiteSpaceCollapse
  174.             import flashx.undo.UndoManager;
  175.            
  176.             use namespace tlf_internal;
  177.            
  178.             public var textFlow:TextFlow;
  179.             public var container:SpriteVisualElement;
  180.             public var controller:ContainerController;
  181.             public var complexTableData:String;
  182.            
  183.             public var tableNotCreatedError:String = "Create the table before calling this method";
  184.             public var notImplementedMessage:String = "Not yet implemented";
  185.            
  186.             // embed table test
  187.             [Embed(source="data/tableExample.xml",mimeType="application/octet-stream")]
  188.             private var TableExample:Class;
  189.  
  190.            
  191.             protected function importApplicationCompleteHandler(event:FlexEvent):void {
  192.                 var tableByte:ByteArray = new TableExample();
  193.                 complexTableData = tableByte.readMultiByte(tableByte.length, "utf-8");
  194.                
  195.                 //textFlow = importer.importToFlow(tableData);
  196.                 importToFlow(simpleTextMarkup);
  197.                
  198.                 clearTextFlow();
  199.                
  200.                 versionInfo.text = "Vellum: " + flashx.textLayout.TextLayoutVersion.BUILD_NUMBER + (Configuration.tlf_internal::debugCodeEnabled ? " Debug" : " Release")
  201.                     + ", Flex: " + mx_internal::VERSION
  202.                     + ", Player: " + Capabilities.version;
  203.             }
  204.            
  205.             /******************************************************
  206.              * Table
  207.              *****************************************************/
  208.            
  209.             /**
  210.              * Adds a table to the text flow
  211.              **/
  212.             public function addTable(rows:int = 1, columns:int = 1, selectable:Boolean = false, editable:Boolean = false):TableElement {
  213.                 var table:TableElement = new TableElement();
  214.                 var cell:TableCellElement;
  215.                 var createTableMethod:int = 1;
  216.                 var i:int;
  217.                
  218.                
  219.                 if (createTableMethod==0) {
  220.                     cell = new TableCellElement();
  221.                     cell.textFlow = getTextFlowContent(null, selectable, editable);
  222.                     cell.colIndex = 0;
  223.                     cell.rowIndex = 0;
  224.                    
  225.                     table.numColumns = columns;
  226.                     table.addRow();
  227.                     table.addChild(cell);
  228.                     textFlow.addChild(table);
  229.                 }
  230.                 // add table rows
  231.                 else if (createTableMethod==1) {
  232.                     table.numColumns = columns;
  233.                    
  234.                     for (i=0;i<rows;i++) {
  235.                         addRow(table, false);
  236.                     }
  237.                    
  238.                     var p:ParagraphElement = new ParagraphElement();
  239.                     p.addChild(table);
  240.                     //table.cellSpacing = 8;
  241.                     textFlow.addChild(p);
  242.                 }
  243.                 // insert table rows
  244.                 else if (createTableMethod==2) {
  245.                     table.numColumns = columns;
  246.                    
  247.                     for (i=0;i<rows;i++) {
  248.                         table.insertRow();
  249.                     }
  250.                    
  251.                     textFlow.addChild(table);
  252.                    
  253.                 }
  254.                
  255.                 updateAllControllers();
  256.                
  257.                 return table;
  258.             }
  259.            
  260.             /**
  261.              * Inserts a row into the table. Creates the cells automatically.
  262.              **/
  263.             public function insertRow(table:TableElement, updateControllers:Boolean = true):TableRowElement {
  264.                 var rowIndex:int;
  265.                
  266.                 if (!table) {
  267.                     updateErrorsText(tableNotCreatedError);
  268.                     return null;
  269.                 }
  270.                
  271.                 rowIndex = table.numRows - 1;
  272.                
  273.                 table.insertRow();
  274.                
  275.                 updateAllControllers();
  276.                
  277.                 return table.getRowAt(rowIndex);
  278.             }
  279.            
  280.             /**
  281.              * Adds a row to the table. Cells are created and added manually.
  282.              **/
  283.             public function addRow(table:TableElement, updateControllers:Boolean = true):TableRowElement {
  284.                
  285.                 if (!table) {
  286.                     updateErrorsText(tableNotCreatedError);
  287.                     return null;
  288.                 }
  289.                
  290.                 table.addRow();
  291.                
  292.                 var rowIndex:int = table.numRows - 1;
  293.                 var columnCount:int = table.numColumns;
  294.                 var cell:TableCellElement;
  295.                 var cellTextFlow:TextFlow;
  296.                 var selectable:Boolean = textFlow.interactionManager is SelectionManager;
  297.                 var editable:Boolean = textFlow.interactionManager is EditManager;
  298.                 var borderColor:String = "#AA0000";
  299.                 var backgroundColor:String = "#FF0000";
  300.                 var borderWidth:int = 1;
  301.                 var backgroundAlpha:Number = .25;
  302.                
  303.                 for(var columnIndex:int;columnIndex<columnCount;columnIndex++){
  304.                     cell                    = new TableCellElement();
  305.                    
  306.                     cell.colIndex           = columnIndex;
  307.                     cell.rowIndex           = rowIndex;
  308.                     cell.textFlow           = getTextFlowContent(String(table.numChildren+1), selectable, editable);
  309.                    
  310.                     cell.borderTopColor     = borderColor;
  311.                     cell.borderLeftColor    = borderColor;
  312.                     cell.borderBottomColor  = borderColor;
  313.                     cell.borderRightColor   = borderColor;
  314.                    
  315.                     cell.borderTopWidth     = borderWidth;
  316.                     cell.borderLeftWidth    = borderWidth;
  317.                     cell.borderBottomWidth  = borderWidth;
  318.                     cell.borderRightWidth   = borderWidth;
  319.                    
  320.                     cell.backgroundColor    = backgroundColor;
  321.                     cell.backgroundAlpha    = backgroundAlpha;
  322.                    
  323.                     //cell.paddingLeft      = 20;
  324.                    
  325.                     table.addChild(cell);
  326.                 }
  327.                
  328.                 if (updateControllers) {
  329.                     updateAllControllers();
  330.                 }
  331.                
  332.                 return table.getRowAt(rowIndex);
  333.             }
  334.            
  335.             /**
  336.              * Adds a column to the table.
  337.              * TODO Cells must be added manually.
  338.              **/
  339.             public function addColumn(table:TableElement):TableColElement {
  340.                 var columnIndex:int;
  341.                
  342.                 if (!table) {
  343.                     updateErrorsText(tableNotCreatedError);
  344.                     return null;
  345.                 }
  346.                
  347.                 columnIndex = table.numColumns;
  348.                
  349.                 // add column does not add cells
  350.                 table.addColumn();
  351.                
  352.                 updateAllControllers();
  353.                
  354.                 return table.getColumnAt(columnIndex);
  355.             }
  356.            
  357.             /**
  358.              * Inserts a column to the table. Cells are adding automatically.
  359.              **/
  360.             public function insertColumn(table:TableElement):TableColElement {
  361.                 var columnIndex:int;
  362.                
  363.                 if (!table) {
  364.                     updateErrorsText(tableNotCreatedError);
  365.                     return null;
  366.                 }
  367.                
  368.                 columnIndex = table.numColumns;
  369.                
  370.                 // add column does not add cells
  371.                 //table.addColumn();
  372.                 table.insertColumn();
  373.                
  374.                 updateAllControllers();
  375.                
  376.                 return table.getColumnAt(columnIndex);
  377.             }
  378.            
  379.             /**
  380.              * Remove the selected table or last table in the text flow
  381.              **/
  382.             public function removeTable(table:TableElement):void {
  383.                
  384.                 if (!table) {
  385.                     updateErrorsText(tableNotCreatedError);
  386.                     return;
  387.                 }
  388.                
  389.                 //table.removeAllRowsWithContent();
  390.                 //updateAllControllers();
  391.                
  392.                 textFlow.removeChild(table);
  393.                
  394.                 updateAllControllers();
  395.                
  396.             }
  397.            
  398.             /**
  399.              * Removes the selected row or the last row in the table
  400.              **/
  401.             public function removeRow(table:TableElement):Array {
  402.                 var rowElements:Array;
  403.                
  404.                 if (!table) {
  405.                     updateErrorsText(tableNotCreatedError);
  406.                     return null;
  407.                 }
  408.                
  409.                 // removeRowAt does not remove cells - using remove row with content
  410.                 //rowElement = table.removeRowAt(table.numRows - 1);
  411.                 rowElements = table.removeRowWithContentAt(table.numRows - 1);
  412.                
  413.                 updateAllControllers();
  414.                
  415.                 return rowElements;
  416.             }
  417.            
  418.             /**
  419.              * Removes all the rows in the table
  420.              **/
  421.             public function removeAllRows(table:TableElement):Array {
  422.                 var rowElements:Array;
  423.                
  424.                 if (!table) {
  425.                     updateErrorsText(tableNotCreatedError);
  426.                     return null;
  427.                 }
  428.                
  429.                 table.removeAllRowsWithContent();
  430.                
  431.                 updateAllControllers();
  432.                
  433.                 return rowElements;
  434.             }
  435.            
  436.             /**
  437.              * Removes the selected column or last column in the table
  438.              **/
  439.             public function removeColumn(table:TableElement):Array {
  440.                 var columnElements:Array;
  441.                
  442.                 if (!table) {
  443.                     updateErrorsText(tableNotCreatedError);
  444.                     return null;
  445.                 }
  446.                
  447.                 // removeColumnAt does not remove cells - using remove column with content
  448.                 //columnElement = table.removeColumnAt(table.numColumns - 1) as TableColElement;
  449.                 columnElements = table.removeColumnWithContentAt(table.numColumns - 1);
  450.                
  451.                 updateAllControllers();
  452.                
  453.                 return columnElements;
  454.             }
  455.            
  456.             /**
  457.              * Selects the text flow content of a table cell
  458.              **/
  459.             public function selectCellContent(cell:TableCellElement = null):TableElement {
  460.                 var table:TableElement = getTable();
  461.                
  462.                 if (table) {
  463.                     if (!cell) {
  464.                         cell = getRandomCell(table);
  465.                     }
  466.                    
  467.                     var selectionManager:SelectionManager = cell ? cell.textFlow.interactionManager as SelectionManager : null;
  468.                    
  469.                     if (selectionManager) {
  470.                         // should not need to do this in final version
  471.                         selectionManager.currentTable = table;
  472.                         //selectionManager.selectRange(-1,-1);
  473.                         /*selectionManager.selectAll();
  474.                         selectionManager.setFocus();*/
  475.                         selectionManager.selectCellTextFlow(cell);
  476.                     }
  477.                 }
  478.                 else {
  479.                     updateErrorsText(tableNotCreatedError);
  480.                 }
  481.                
  482.                 return null;
  483.             }
  484.            
  485.             /**
  486.              * Selects the cell the cursor is in or the first cell in the table
  487.              **/
  488.             public function selectCell(cell:TableCellElement = null):TableElement {
  489.                 var table:TableElement = getTable();
  490.                
  491.                 if (table) {
  492.                    
  493.                     if (!cell) {
  494.                         cell = getRandomCell(table);
  495.                     }
  496.                    
  497.                     //var selectionManager:SelectionManager = textFlow ? textFlow.interactionManager as SelectionManager : null;
  498.                     var selectionManager:SelectionManager = cell ? cell.textFlow.interactionManager as SelectionManager : null;
  499.                    
  500.                     if (selectionManager) {
  501.                         // i don't think we should need to do this in final version
  502.                         //selectionManager.currentTable = table;
  503.                         //var selFormat:SelectionFormat = new SelectionFormat();
  504.                         //selectionManager.unfocusedSelectionFormat = selFormat;
  505.                         //selectionManager.focusedSelectionFormat = selFormat;
  506.                         //cell.textFlow.interactionManager.focusedSelectionFormat = selFormat;
  507.                         //cell.textFlow.interactionManager.unfocusedSelectionFormat = selFormat;
  508.                         /*selectionManager.unfocusedSelectionFormat = selFormat;
  509.                         selectionManager.inactiveSelectionFormat = selFormat;*/
  510.                        
  511.                         // In ContainerController in addSelectionChild
  512.                         // the selection format alpha is 0 so cell selection is not showing up
  513.                         // manually setting the alpha to 1 in addSelectionChild for testing
  514.                         selectionManager.currentTable = table;
  515.                         selectionManager.selectCell(cell);
  516.                     }
  517.                 }
  518.                 else {
  519.                     updateErrorsText(tableNotCreatedError);
  520.                 }
  521.                
  522.                 return null;
  523.             }
  524.            
  525.             /**
  526.              * Clears the selection of cells
  527.              **/
  528.             public function clearSelection(cells:Array = null):TableElement {
  529.                 var table:TableElement = getTable();
  530.                
  531.                 if (table) {
  532.                     var selectionManager:SelectionManager = textFlow.interactionManager as SelectionManager;
  533.                    
  534.                     if (selectionManager) {
  535.                         selectionManager.currentTable = table;
  536.                         selectionManager.clearSelection();
  537.                     }
  538.                 }
  539.                
  540.                 updateErrorsText(null);
  541.                
  542.                 return null;
  543.             }
  544.            
  545.             /**
  546.              * Selects the specified cells
  547.              **/
  548.             public function selectCells(cells:Array = null):TableElement {
  549.                 var table:TableElement = getTable();
  550.                 var selectedCells:Vector.<TableCellElement> = new Vector.<TableCellElement>();
  551.                
  552.                 if (table) {
  553.                    
  554.                     if (cells==null || cells.length==0) {
  555.                         selectedCells = getRandomCells(table);
  556.                     }
  557.                     else {
  558.                         selectedCells.concat(cells);
  559.                     }
  560.                    
  561.                    
  562.                     var selectionManager:SelectionManager = textFlow.interactionManager as SelectionManager;
  563.                    
  564.                     if (selectionManager) {
  565.                         selectionManager.currentTable = table;
  566.                         selectionManager.selectCells(selectedCells);
  567.                     }
  568.                 }
  569.                
  570.                 updateErrorsText(null);
  571.                
  572.                 return null;
  573.             }
  574.            
  575.             /**
  576.              * Selects a column in the table
  577.              **/
  578.             public function selectColumn(column:TableColElement = null):TableElement {
  579.                 var table:TableElement = getTable();
  580.                
  581.                 if (table) {
  582.                     if (!column) {
  583.                         column = getRandomColumn(table);
  584.                     }
  585.                    
  586.                     var selectionManager:SelectionManager = textFlow.interactionManager as SelectionManager;
  587.                    
  588.                     if (selectionManager) {
  589.                         selectionManager.currentTable = table;
  590.                         selectionManager.selectColumn(column);
  591.                     }
  592.                 }
  593.                
  594.                 updateErrorsText(null);
  595.                
  596.                 return null;
  597.             }
  598.            
  599.             /**
  600.              * Selects columns in the table
  601.              **/
  602.             public function selectColumns(columns:Array = null):TableElement {
  603.                 var table:TableElement = getTable();
  604.                
  605.                 if (table) {
  606.                     if (!columns) {
  607.                         //columns = getRandomColumns(table);
  608.                         columns = [];
  609.                         columns.push(table.getColumnAt(0));
  610.                         columns.push(table.getColumnAt(1));
  611.                     }
  612.                    
  613.                     var selectionManager:SelectionManager = textFlow.interactionManager as SelectionManager;
  614.                    
  615.                     if (selectionManager) {
  616.                         selectionManager.currentTable = table;
  617.                         selectionManager.selectColumns(columns);
  618.                     }
  619.                 }
  620.                
  621.                 updateErrorsText(null);
  622.                
  623.                 return null;
  624.             }
  625.            
  626.             /**
  627.              * Selects the table
  628.              **/
  629.             public function selectTable(rowIndex:int = 0):TableElement {
  630.                 var table:TableElement = getTable();
  631.                
  632.                 if (table) {
  633.                     var selectionManager:SelectionManager = textFlow ? textFlow.interactionManager as SelectionManager : null;
  634.                    
  635.                     if (selectionManager) {
  636.                         selectionManager.currentTable = table;
  637.                         selectionManager.selectTable(table);
  638.                     }
  639.                 }
  640.                 else {
  641.                     updateErrorsText(tableNotCreatedError);
  642.                 }
  643.                
  644.                 return null;
  645.             }
  646.            
  647.             /**
  648.              * Selects a row in the table
  649.              **/
  650.             public function selectRow(row:TableRowElement = null):TableElement {
  651.                 var selectionManager:SelectionManager;
  652.                 var table:TableElement = getTable();
  653.                 var row:TableRowElement;
  654.                
  655.                 if (table) {
  656.                    
  657.                     if (!row) {
  658.                         row = getRandomRow(table);
  659.                     }
  660.                    
  661.                     selectionManager = textFlow.interactionManager is ISelectionManager ? textFlow.interactionManager as SelectionManager : null;
  662.                    
  663.                     if (selectionManager) {
  664.                         selectionManager.currentTable = table;
  665.                         selectionManager.selectRow(row);
  666.                     }
  667.                 }
  668.                 else {
  669.                     updateErrorsText(tableNotCreatedError);
  670.                 }
  671.                
  672.                
  673.                 return null;
  674.             }
  675.            
  676.             /**
  677.              * Selects rows in the table
  678.              **/
  679.             public function selectRows(rows:Array = null):TableElement {
  680.                 var selectionManager:SelectionManager;
  681.                 var table:TableElement = getTable();
  682.                 var row:TableRowElement;
  683.                
  684.                 if (table) {
  685.                    
  686.                     if (!row) {
  687.                         rows = [];
  688.                         rows.push(table.getRowAt(0));
  689.                         rows.push(table.getRowAt(1));
  690.                     }
  691.                    
  692.                     selectionManager = textFlow.interactionManager is ISelectionManager ? textFlow.interactionManager as SelectionManager : null;
  693.                    
  694.                     if (selectionManager) {
  695.                         selectionManager.currentTable = table;
  696.                         selectionManager.selectRows(rows);
  697.                     }
  698.                 }
  699.                 else {
  700.                     updateErrorsText(tableNotCreatedError);
  701.                 }
  702.                
  703.                
  704.                 return null;
  705.             }
  706.            
  707.             /**
  708.              * Joins contiguous separate cells into one cell
  709.              **/
  710.             public function joinCells():TableElement {
  711.                 var table:TableElement = getTable();
  712.                
  713.                 if (table) {
  714.                    
  715.                 }
  716.                
  717.                 updateErrorsText(notImplementedMessage);
  718.                
  719.                 return null;
  720.             }
  721.            
  722.            
  723.             /******************************************************
  724.              * Bounds
  725.              *****************************************************/
  726.            
  727.             /**
  728.              * Table block areas
  729.              **/
  730.             public function showTextBlocksBounds():void
  731.             {
  732.                 var table:TableElement = getTable();
  733.                
  734.                 if (!table) {
  735.                     textFlowMarkupErrors.text = notImplementedMessage;
  736.                     return;
  737.                 }
  738.             }
  739.            
  740.             /**
  741.              * So the text line bounds.
  742.              * There are a few types of fills and they all seem to differ.
  743.              * There is:
  744.              * - background color fill style created by the BackgroundManager. see addParagraph()
  745.              * - selection fill. select some text
  746.              * - this fill method based on what I found online
  747.              * - Label background fill.
  748.              **/
  749.             public function showTextLineBounds():void
  750.             {
  751.                 var i:int;
  752.                 var j:int;
  753.                 var b1:Rectangle;
  754.                 var b2:Rectangle;
  755.                 var fc:IFlowComposer = textFlow.flowComposer;
  756.                 var controller:ContainerController = textFlow.flowComposer.getControllerAt(0);
  757.                 var color:uint = 0x00dd00;
  758.                 var method:int = 0;
  759.                 var clearedOnce:Boolean;
  760.                
  761.                 for (i = 0; i < fc.numLines; i++) {
  762.                     var _tfl:TextFlowLine = fc.getLineAt(i);
  763.                     var _tl:TextLine = _tfl.getTextLine();
  764.                     b1 = _tfl.getBounds();
  765.                     b2 = _tl.getBounds(_tfl.controller.container);
  766.                    
  767.                     if (method==1) {
  768.                         var g:Sprite = new Sprite;
  769.                         g.graphics.lineStyle(1, color, 0.6);
  770.                         g.graphics.drawRect(0, Math.max(-b1.height,-_tfl.height), b1.width, b1.height);
  771.                         _tl.removeChildren();
  772.                         _tl.addChild(g);
  773.                     }
  774.                     else {
  775.                         var graphics:Graphics = _tfl.controller.container.graphics;
  776.                         if (!clearedOnce && graphics.readGraphicsData().length!=0) {
  777.                             graphics.clear();
  778.                             clearedOnce = true;
  779.                         }
  780.                         graphics.beginFill(color);
  781.                         graphics.drawRect(b1.x, b1.y, b1.width, b1.height);
  782.                         graphics.endFill();
  783.                     }
  784.                 }
  785.             }
  786.            
  787.             /**
  788.              * Shows the bounds of the atoms. This is from the TLF forums.
  789.              **/
  790.             public function showAtomBounds():void
  791.             {
  792.                 var i:int;
  793.                 var j:int;
  794.                 var b1:Rectangle;
  795.                 var fc:IFlowComposer = textFlow.flowComposer;
  796.                 var color:uint = 0x00dd00;
  797.                
  798.                 for (i = 0; i < fc.numLines; i++)
  799.                 {
  800.                     var _tfl:TextFlowLine = fc.getLineAt(i);
  801.                     var _tl:TextLine = _tfl.getTextLine();
  802.                    
  803.                     for (j = 0; j < _tl.atomCount; j++)
  804.                     {
  805.                         var textBlockBeginIndex:int = _tl.getAtomTextBlockBeginIndex(j);
  806.                         var textFlowPosition:int = _tfl.paragraph.getAbsoluteStart()+_tl.getAtomTextBlockBeginIndex(j);
  807.                         b1 = _tl.getAtomBounds(j);
  808.                         var g:Sprite = new Sprite;
  809.                         g.graphics.lineStyle(1, color,0.6)
  810.                         g.graphics.drawRect(b1.x, b1.y , b1.width, b1.height);
  811.                         _tl.addChild(g);
  812.                        
  813.                         var leafElement:FlowLeafElement = textFlow.findLeaf(textFlowPosition);
  814.                         var leafRelativePosition:int = textFlowPosition-leafElement.getAbsoluteStart();
  815.                         var leafCharacter:String = leafElement.text.substr(leafRelativePosition,1);
  816.                         //trace(i,j,textBlockBeginIndex,textFlowPosition,b1);
  817.                     }
  818.                 }
  819.             }
  820.            
  821.             /**
  822.              * Show paragraph bounds. Example is from one of the AS3 examples in TLF-SDK directory
  823.              **/
  824.             public function showParagraphBounds():Rectangle {
  825.                 var bounds:Rectangle = new Rectangle();
  826.                 var paragraph:ParagraphElement = getParagraph();
  827.                 var container:ContainerController = paragraph.getEnclosingController(0);
  828.                 var sprite:Sprite = container.container;
  829.                 var pos:int = paragraph.getAbsoluteStart();
  830.                 var endPos:int = pos + paragraph.textLength;
  831.                 var color:int = 0x00FF00;
  832.                
  833.                 while (pos < endPos) {
  834.                     var line:TextFlowLine = paragraph.getTextFlow().flowComposer.findLineAtPosition(pos);
  835.                     //bounds = bounds.union(line.getTextLine().getBounds(this));
  836.                     bounds = bounds.union(line.getTextLine().getBounds(sprite));
  837.                     pos += line.textLength;
  838.                 }
  839.                
  840.                 sprite.graphics.clear();
  841.                 sprite.graphics.beginFill(color);
  842.                 sprite.graphics.drawRect(bounds.left, bounds.top, bounds.width, bounds.height);
  843.                 sprite.graphics.endFill();
  844.                 sprite.graphics.lineStyle(1, 0x0);
  845.                 sprite.graphics.moveTo(bounds.left - 1, bounds.top - 1);
  846.                 sprite.graphics.lineTo(bounds.right + 1, bounds.top - 1);
  847.                 sprite.graphics.lineTo(bounds.right + 1, bounds.bottom);
  848.                 sprite.graphics.lineTo(bounds.left - 1, bounds.bottom);
  849.                 sprite.graphics.lineTo(bounds.left - 1, bounds.top - 1);
  850.                
  851.                 return bounds;
  852.             }
  853.            
  854.             /**
  855.              * Show the bounds of the table. This is not working code.
  856.              **/
  857.             public function showTableBounds():Rectangle {
  858.                 var table:TableElement = getTable();
  859.                
  860.                 if (!table) return null;
  861.                
  862.                 // this does not work at this time
  863.                 var bounds:Rectangle = new Rectangle();
  864.                 var lineBounds:Rectangle = new Rectangle();
  865.                 var lineBounds2:Rectangle = new Rectangle();
  866.                 var container:ContainerController = table ? table.getEnclosingController(0) :null;
  867.                 var sprite:Sprite = container.container;
  868.                 var pos:int = table.getAbsoluteStart();
  869.                 var endPos:int = pos + table.textLength;
  870.                 var color:int = 0x00FF00;
  871.                
  872.                 while (pos < endPos) {
  873.                     //var line:TextFlowTableBlock = table.getTextFlow().flowComposer.findLineAtPosition(pos) as TextFlowTableBlock;
  874.                     var line:TextFlowLine = table.getTextFlow().flowComposer.findLineAtPosition(pos);
  875.                     //bounds = bounds.union(line.getTextLine().getBounds(this));
  876.                     lineBounds = new Rectangle(line.x, line.y, line.targetWidth, line.height);
  877.                     lineBounds2 = line.getTextLine().getBounds(sprite);
  878.                     bounds = bounds.union(lineBounds2);
  879.                     pos += line.textLength;
  880.                 }
  881.                
  882.                 var block:TextFlowTableBlock = table.getFirstBlock();
  883.                 bounds.y = block.y;
  884.                 bounds.width = block.container.width;
  885.                 bounds.height = block.height;
  886.                
  887.                 sprite.graphics.clear();
  888.                 sprite.graphics.beginFill(color);
  889.                 sprite.graphics.drawRect(bounds.left, bounds.top, bounds.width, bounds.height);
  890.                 sprite.graphics.endFill();
  891.                 sprite.graphics.lineStyle(1, 0x0);
  892.                 sprite.graphics.moveTo(bounds.left - 1, bounds.top - 1);
  893.                 sprite.graphics.lineTo(bounds.right + 1, bounds.top - 1);
  894.                 sprite.graphics.lineTo(bounds.right + 1, bounds.bottom);
  895.                 sprite.graphics.lineTo(bounds.left - 1, bounds.bottom);
  896.                 sprite.graphics.lineTo(bounds.left - 1, bounds.top - 1);
  897.                
  898.                 return bounds;
  899.             }
  900.            
  901.             /**
  902.              * Attempt to show all parcels or regions available to a text flow or table.
  903.              * This is not working code.
  904.              **/
  905.             public function showParcelBounds():void {
  906.                 var flowComposer:StandardFlowComposer = textFlow.flowComposer as StandardFlowComposer;
  907.                 var container:ContainerController = flowComposer.getControllerAt(flowComposer.numControllers-1);
  908.                 var columnState:ColumnState = container.columnState;
  909.                 var composeState:ComposeState = flowComposer.getComposeState();
  910.                 var parcelList:ParcelList = composeState.parcelList;
  911.                 var backgroundManager:Object = textFlow.getBackgroundManager();
  912.                 var blocks:Array = textFlow.getBackgroundManager()?textFlow.backgroundManager.getShapeRectArray():[];
  913.                 var selectionSprite:Sprite = container.getSelectionSprite(true) as Sprite;
  914.                 var backgroundSprite:Shape = container.getBackgroundShape() as Shape;
  915.                
  916.                 return;
  917.                 var bounds:Rectangle = container.getContentBounds();
  918.                 var g:Graphics = backgroundSprite.graphics;
  919.                 g.clear();
  920.                 g.beginFill(0x00ff00);
  921.                 g.drawRect(bounds.left, bounds.top, bounds.width, bounds.height);
  922.                 g.endFill();
  923.                 g.lineStyle(1, 0x0);
  924.                 g.moveTo(bounds.left - 1, bounds.top - 1);
  925.                 g.lineTo(bounds.right + 1, bounds.top - 1);
  926.                 g.lineTo(bounds.right + 1, bounds.bottom);
  927.                 g.lineTo(bounds.left - 1, bounds.bottom);
  928.                 g.lineTo(bounds.left - 1, bounds.top - 1);
  929.                
  930.             }
  931.            
  932.             /******************************************************
  933.              * Selection
  934.              *****************************************************/
  935.            
  936.             public function highlightOneCharacter():void {
  937.                 var selObj:Shape = new Shape();
  938.                
  939.                 var flowComposer:IFlowComposer = textFlow.flowComposer;
  940.                 var line:TextFlowLine = flowComposer.getLineAt(1);
  941.                 var selFormat:SelectionFormat = textFlow.interactionManager ? textFlow.interactionManager.currentSelectionFormat : null;
  942.                 //if (!selFormat) {
  943.                 selFormat = new SelectionFormat(0xFF0000, 1, "difference", 0xFF0000, 1, "difference");
  944.                 //}
  945.                 var selectionAbsoluteStart:int = 0;
  946.                 var selectionAbsoluteEnd:int = 1;
  947.                 var controller:ContainerController = flowComposer.getControllerAt(0);
  948.                 var container:Sprite = controller.container;
  949.                
  950.                 controller.addSelectionShapes(selFormat, 0, 10);
  951.                 //line.hiliteBlockSelection(selObj, selFormat, textFlow.flowComposer.getControllerAt(0).container as DisplayObject, selectionAbsoluteStart, selectionAbsoluteEnd, null, null);
  952.                
  953.             }
  954.            
  955.             public function selectRange():void {
  956.                 var flowComposer:IFlowComposer = textFlow.flowComposer;
  957.                 textFlow.interactionManager = new SelectionManager;
  958.                 var interactionManager:ISelectionManager = textFlow.interactionManager;
  959.                 interactionManager.selectRange(10, 22); // this is not working?
  960.             }
  961.            
  962.             /**
  963.              * Get next cell
  964.              **/
  965.             public function getNextCell():void {
  966.                 var table:TableElement = getTable();
  967.                
  968.                 if (table) {
  969.                     var cell:TableCellElement = table.getCellAt(0, 2);
  970.                     var nextCell:TableCellElement = table.getNextCell(cell);
  971.                     var prevCell:TableCellElement = table.getPreviousCell(cell);
  972.                 }
  973.             }
  974.            
  975.             /**
  976.              * Get previous cell
  977.              **/
  978.             public function getPreviousCell():void {
  979.                 var table:TableElement = getTable();
  980.                 var cell:TableCellElement;
  981.                 var prevCell:TableCellElement;
  982.                
  983.                 if (table) {
  984.                     cell = table.getCellAt(1, 0);
  985.                     prevCell = table.getPreviousCell(cell);
  986.                    
  987.                     if (prevCell.rowIndex==0 && prevCell.colIndex==2) {
  988.                         //trace("OK");
  989.                     }
  990.                    
  991.                     cell = table.getCellAt(0, 2);
  992.                     prevCell = table.getPreviousCell(cell);
  993.                    
  994.                     if (prevCell.rowIndex==0 && prevCell.colIndex==1) {
  995.                         //trace("OK");
  996.                     }
  997.                    
  998.                     cell = table.getCellAt(0, 0);
  999.                     prevCell = table.getPreviousCell(cell);
  1000.                    
  1001.                     if (prevCell==null) {
  1002.                         //trace("OK");
  1003.                     }
  1004.                    
  1005.                 }
  1006.             }
  1007.            
  1008.             /**
  1009.              * Show cursor used to select table
  1010.              **/
  1011.             public function showSelectTableCursor():void {
  1012.                 Mouse.cursor = SelectionManager.SelectTable;
  1013.             }
  1014.            
  1015.             /**
  1016.              * Show cursor used to select row
  1017.              **/
  1018.             public function showSelectRowCursor():void {
  1019.                 Mouse.cursor = SelectionManager.SelectTableRow;
  1020.             }
  1021.            
  1022.             /**
  1023.              * Show cursor used to select column
  1024.              **/
  1025.             public function showSelectColumnCursor():void {
  1026.                 Mouse.cursor = SelectionManager.SelectTableColumn;
  1027.             }
  1028.            
  1029.             /******************************************************
  1030.              * Misc
  1031.              *****************************************************/
  1032.            
  1033.             /**
  1034.              * Gets a random cell from the table
  1035.              */
  1036.             public function getRandomCell(table:TableElement):TableCellElement {
  1037.                 var cell:TableCellElement;
  1038.                
  1039.                 var rowIndex:int = Math.floor(Math.random() * table.numRows);
  1040.                 var columnIndex:int = Math.floor(Math.random() * table.numColumns);
  1041.                
  1042.                 cell = table.getCellAt(rowIndex, columnIndex);
  1043.                
  1044.                 if (!random) {
  1045.                     cell = table.getCellAt(0, 0);
  1046.                 }
  1047.                
  1048.                 return cell;
  1049.             }
  1050.            
  1051.             /**
  1052.              * Gets a random cells from the table
  1053.              */
  1054.             public function getRandomCells(table:TableElement):Vector.<TableCellElement> {
  1055.                 var cell:TableCellElement;
  1056.                 var m:int = Math.floor(Math.random()*2);
  1057.                 var cells:Array = table.getCellsArray();
  1058.                 var selectedCells:Vector.<TableCellElement> = new Vector.<TableCellElement>();
  1059.                
  1060.                 for (var i:int = 0; i < cells.length; i++) {
  1061.                     var v1:Number = Math.random()*2;
  1062.                     var v:int = Math.floor(v1);
  1063.                     if (v==0) {
  1064.                         selectedCells.push(cells[i]);
  1065.                     }
  1066.                 }
  1067.                
  1068.                 if (!random) {
  1069.                     selectedCells = new Vector.<TableCellElement>();
  1070.                     selectedCells.push(table.getCellAt(0, 0));
  1071.                     selectedCells.push(table.getCellAt(1, 1));
  1072.                     selectedCells.push(table.getCellAt(2, 2));
  1073.                 }
  1074.                
  1075.                 return selectedCells;
  1076.             }
  1077.            
  1078.             /**
  1079.              * Gets a random row from the table
  1080.              */
  1081.             public function getRandomRow(table:TableElement):TableRowElement {
  1082.                 var row:TableRowElement;
  1083.                
  1084.                 var rowIndex:int = Math.floor(Math.random() * table.numRows);
  1085.                
  1086.                 row = table.getRowAt(rowIndex);
  1087.                
  1088.                 if (!random) {
  1089.                     row = table.getRowAt(0);
  1090.                 }
  1091.                
  1092.                 return row;
  1093.             }
  1094.            
  1095.             /**
  1096.              * Gets a random column from the table
  1097.              */
  1098.             public function getRandomColumn(table:TableElement):TableColElement {
  1099.                 var column:TableColElement;
  1100.                
  1101.                 var columnIndex:int = Math.floor(Math.random() * table.numColumns);
  1102.                
  1103.                 column = table.getColumnAt(columnIndex);
  1104.                
  1105.                 if (!random) {
  1106.                     column = table.getColumnAt(0);
  1107.                 }
  1108.                
  1109.                 return column;
  1110.             }
  1111.            
  1112.             /**
  1113.              * Updates all controllers. Call after the text flow is modified in any way.
  1114.              **/
  1115.             public function updateAllControllers():void {
  1116.                 textFlow.flowComposer.updateAllControllers();
  1117.                
  1118.                 exportTextFlow(textFlow);
  1119.                
  1120.                 updateTableInformation();
  1121.             }
  1122.            
  1123.             /**
  1124.              * Handles graphic status change events
  1125.              **/
  1126.             public function graphicStatusChanged(event:StatusChangeEvent):void {
  1127.                 var img:InlineGraphicElement = event.element as InlineGraphicElement;
  1128.                 updateAllControllers();
  1129.                
  1130.                 if (event.status == "ready" || event.status == "sizePending") {
  1131.                    
  1132.                     var mirror:IEventDispatcher = event.element.getEventMirror();
  1133.                     mirror.addEventListener(FlowElementMouseEvent.MOUSE_DOWN,traceEvent);
  1134.                     mirror.addEventListener(FlowElementMouseEvent.MOUSE_UP,traceEvent);
  1135.                     mirror.addEventListener(FlowElementMouseEvent.MOUSE_MOVE,traceEvent);
  1136.                     mirror.addEventListener(FlowElementMouseEvent.ROLL_OVER,traceEvent);
  1137.                     mirror.addEventListener(FlowElementMouseEvent.ROLL_OUT,traceEvent);
  1138.                     mirror.addEventListener(FlowElementMouseEvent.CLICK,traceEvent);
  1139.                    
  1140.                     updateAllControllers();
  1141.                 }
  1142.             }
  1143.            
  1144.             /**
  1145.              * Handler for model change events
  1146.              **/
  1147.             public function elementModified(event:Event):void
  1148.             {
  1149.                 //trace("event: " + event.type);
  1150.             }
  1151.            
  1152.             /**
  1153.              * Used to check mouse events
  1154.              **/
  1155.             public function traceEvent(event:FlowElementMouseEvent):void
  1156.             {
  1157.                 //trace(event.flowElement.defaultTypeName+ " " + getTimer() + " " + event.toString() + event.originalEvent.toString());
  1158.                
  1159.                 // attempt to add a cursor over an image / table
  1160.                 if (event.type==FlowElementMouseEvent.ROLL_OVER ||
  1161.                     event.type==FlowElementMouseEvent.MOUSE_MOVE) {
  1162.                     Mouse.cursor = MouseCursor.HAND;
  1163.                 }
  1164.                 else if (event.type==FlowElementMouseEvent.ROLL_OUT) {
  1165.                     Mouse.cursor = MouseCursor.AUTO;
  1166.                 }
  1167.             }
  1168.            
  1169.             /**
  1170.              * Adds a paragraph to the text flow
  1171.              **/
  1172.             public function addParagraph(text:String = null):void {
  1173.                 var paragraph:ParagraphElement = new ParagraphElement();
  1174.                 var span:SpanElement = new SpanElement();
  1175.                
  1176.                 if (text) {
  1177.                     span.text = text;
  1178.                 }
  1179.                 else {
  1180.                     span.text = "This is a paragraph \nSecond line.";
  1181.                 }
  1182.                
  1183.                 paragraph.backgroundAlpha = 0.2;
  1184.                 paragraph.backgroundColor = 0x0000FF;
  1185.                
  1186.                 paragraph.addChild(span);
  1187.                
  1188.                 textFlow.addChild(paragraph);
  1189.                
  1190.                 updateAllControllers();
  1191.             }
  1192.            
  1193.             /**
  1194.              * Creates a text flow from the text value passed in. Used for table cell text flows.
  1195.              **/
  1196.             public function getTextFlowContent(text:String = null, selectable:Boolean = false, editable:Boolean = false):TextFlow {
  1197.                 var config:Configuration= new Configuration();
  1198.                 config.manageTabKey = true;
  1199.                 var textFlow:TextFlow = new TextFlow(config);
  1200.                 var paragraph:ParagraphElement = new ParagraphElement();
  1201.                 var span:SpanElement = new SpanElement();
  1202.                
  1203.                 if (text) {
  1204.                     span.text = text;
  1205.                 }
  1206.                 else {
  1207.                     span.text = "This is cell";
  1208.                 }
  1209.                
  1210.                 paragraph.backgroundAlpha = 0.2;
  1211.                 paragraph.backgroundColor = 0xFF0000;
  1212.                 paragraph.addChild(span);
  1213.                
  1214.                 /*textFlow.paddingLeft = 10;
  1215.                 textFlow.paddingRight = 10;
  1216.                 textFlow.paddingTop = 10;
  1217.                 textFlow.paddingBottom = 10;*/
  1218.                 textFlow.addChild(paragraph);
  1219.                
  1220.                 if (editable) {
  1221.                     // setting the edit manager adds a default paragraph
  1222.                     //textFlow.interactionManager = textFlow.interactionManager;
  1223.                     textFlow.interactionManager = new EditManager(new UndoManager);
  1224.                 }
  1225.                 else if (selectable) {
  1226.                     textFlow.interactionManager = new SelectionManager();
  1227.                 }
  1228.                
  1229.                 return textFlow;
  1230.             }
  1231.            
  1232.             /**
  1233.              * Gets the paragraph the cursor is in or the first available paragraph
  1234.              **/
  1235.             public function getParagraph():ParagraphElement {
  1236.                 var paragraph:ParagraphElement;
  1237.                
  1238.                 if (textFlow.interactionManager) {
  1239.                     var cursor:int = Math.max(0, textFlow.interactionManager.anchorPosition);
  1240.                     paragraph = textFlow.findAbsoluteParagraph(cursor);
  1241.                 }
  1242.                
  1243.                 if (!paragraph) {
  1244.                     var items:Array = textFlow.getElementsByTypeName("p");
  1245.                     paragraph = items.length ? items[0] : null;
  1246.                 }
  1247.                
  1248.                 return paragraph;
  1249.             }
  1250.            
  1251.             /**
  1252.              * Gets the first table available or null if none are available
  1253.              **/
  1254.             public function getTable():TableElement {
  1255.                 var table:TableElement;
  1256.                
  1257.                 if (textFlow.interactionManager) {
  1258.                     var cursor:int = Math.max(0, textFlow.interactionManager.anchorPosition);
  1259.                     //table = textFlow.findAbsoluteParagraph(cursor);
  1260.                 }
  1261.                
  1262.                 if (!table) {
  1263.                     var items:Array = textFlow.getElementsByTypeName("table");
  1264.                     table = items.length ? items[0] : null;
  1265.                 }
  1266.                
  1267.                 return table;
  1268.             }
  1269.            
  1270.             /**
  1271.              * Removes the shapes added by show boundary methods
  1272.              **/
  1273.             public function removeShapes():void {
  1274.                 var paragraph:ParagraphElement = getParagraph();
  1275.                 if (!paragraph) return;
  1276.                 var bounds:Rectangle = new Rectangle();
  1277.                 var container:ContainerController = paragraph.getEnclosingController(0);
  1278.                 var sprite:Sprite = container.container;
  1279.                
  1280.                 var i:int;
  1281.                 var j:int;
  1282.                 var b1:Rectangle;
  1283.                 var fc:IFlowComposer = textFlow.flowComposer;
  1284.                 var color:uint = 0x00dd00;
  1285.                
  1286.                 for (i = 0; i < fc.numLines; i++)
  1287.                 {
  1288.                     var _tfl:TextFlowLine = fc.getLineAt(i);
  1289.                     var _tl:TextLine = _tfl.getTextLine();
  1290.                     _tl.removeChildren();
  1291.                 }
  1292.                
  1293.                 if (sprite && sprite.graphics.readGraphicsData().length!=0) {
  1294.                     sprite.graphics.clear();
  1295.                 }
  1296.             }
  1297.            
  1298.             /**
  1299.              * Removes all the children in the text flow
  1300.              **/
  1301.             public function clearTextFlow():void {
  1302.                 //textFlow.setTextLength(0);
  1303.                 //textFlow.deleteContainerText(0, textFlow.textLength);
  1304.                
  1305.                 if (textFlow.numChildren) {
  1306.                     textFlow.replaceChildren(0, textFlow.numChildren);
  1307.                 }
  1308.                
  1309.                 removeShapes();
  1310.                
  1311.                 updateAllControllers();
  1312.                
  1313.                 if (textFlow.interactionManager) {
  1314.                     textFlow.interactionManager.selectAll();
  1315.                     textFlow.interactionManager.setFocus();
  1316.                 }
  1317.             }
  1318.            
  1319.             /**
  1320.              * Flag indicating if currently importing text
  1321.              * If the text flow has inline graphics that have not loaded yet it can cause
  1322.              * another composition complete event causing a reexport.
  1323.              **/
  1324.             public var updatingTextFlow:Boolean;
  1325.  
  1326.             /**
  1327.              * Imports the text flow markup into new text flow content when changed.
  1328.              * If the text flow has inline graphics that have not loaded yet it can cause
  1329.              * another composition complete event causing a reexport.
  1330.              **/
  1331.             protected function textFlowMarkup_changeHandler(event:TextOperationEvent):void {
  1332.                 updatingTextFlow = true;
  1333.                 importToFlow(textFlowMarkup.text);
  1334.                 updatingTextFlow = false;
  1335.             }
  1336.            
  1337.             /**
  1338.              * Imports the text into a new text flow
  1339.              **/
  1340.             public function importToFlow(value:String):TextFlow {
  1341.                 var errors:Vector.<String>;
  1342.                 var importer:ITextImporter;
  1343.                 var newTextFlow:TextFlow;
  1344.                
  1345.                 importer = TextConverter.getImporter(TextConverter.TEXT_LAYOUT_FORMAT);
  1346.                 importer.throwOnError = false;
  1347.                
  1348.                 //importer.configuration = new Configuration();
  1349.                 newTextFlow = importer.importToFlow(value);
  1350.                 errors = importer.errors;
  1351.                
  1352.                 // if text flow is not null add it and ignore errors
  1353.                 //if (!errors && newTextFlow) {
  1354.                 if (newTextFlow) {
  1355.                     setupTextFlow(newTextFlow);
  1356.                 }
  1357.                
  1358.                 updateErrorsText(importer.errors);
  1359.                
  1360.                 return newTextFlow;
  1361.             }
  1362.            
  1363.             /**
  1364.              * Adds listeners to the text flow and adds it to the container
  1365.              **/
  1366.             public function setupTextFlow(newTextFlow:TextFlow):void {
  1367.                 if (!newTextFlow) return;
  1368.                
  1369.                 // remove listeners from previous text flow
  1370.                 // we may need to do other things to clear out the old textflow
  1371.                 if (textFlow) {
  1372.                     textFlow.removeEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, compositionComplete);
  1373.                     textFlow.removeEventListener(ModelChange.ELEMENT_MODIFIED, graphicStatusChanged);
  1374.                     textFlow.removeEventListener(StatusChangeEvent.INLINE_GRAPHIC_STATUS_CHANGE, graphicStatusChanged);
  1375.                     textFlow.removeEventListener(SelectionEvent.SELECTION_CHANGE, selectionChange);
  1376.                 }
  1377.                
  1378.                 textFlow = newTextFlow;
  1379.                
  1380.                 textFlow.addEventListener(CompositionCompleteEvent.COMPOSITION_COMPLETE, compositionComplete, false, 0, true);
  1381.                 textFlow.addEventListener(ModelChange.ELEMENT_MODIFIED, elementModified, false, 0, true);
  1382.                 textFlow.addEventListener(StatusChangeEvent.INLINE_GRAPHIC_STATUS_CHANGE, graphicStatusChanged, false, 0, true);
  1383.                 textFlow.addEventListener(SelectionEvent.SELECTION_CHANGE, selectionChange, false, 0, true);
  1384.                
  1385.                 if (!container) {
  1386.                     container = new SpriteVisualElement();
  1387.                 }
  1388.                
  1389.                 container.removeChildren();
  1390.                
  1391.                 if (!controller) {
  1392.                     controller = new ContainerController(container, group.width, group.height);
  1393.                 }
  1394.                
  1395.                 // Add controller to text flow; add container to stage and display text
  1396.                 textFlow.flowComposer.addController(controller);
  1397.                
  1398.                 //textFlow.interactionManager = new SelectionManager();
  1399.                 textFlow.interactionManager = new EditManager(new UndoManager);
  1400.                
  1401.                 group.addElement(container as IVisualElement);
  1402.                
  1403.                 updateAllControllers();
  1404.             }
  1405.            
  1406.             /**
  1407.              * When TextFlow has finished composition we export the markup
  1408.              **/
  1409.             protected function compositionComplete(event:CompositionCompleteEvent):void
  1410.             {
  1411.                 exportTextFlow(textFlow);
  1412.             }
  1413.            
  1414.             /**
  1415.              * Selection change
  1416.              **/
  1417.             protected function selectionChange(event:SelectionEvent):void
  1418.             {
  1419.                 // not used yet
  1420.             }
  1421.            
  1422.             /**
  1423.              * Exports the text flow markup and reports any errors.
  1424.              * Changes can be overwritten when using inline graphics
  1425.              **/
  1426.             public function exportTextFlow(textFlow:TextFlow):void {
  1427.                
  1428.                 // do not export if importing or user is typing into the text flow markup text area
  1429.                 if (updatingTextFlow ||
  1430.                     textFlowMarkup.focusManager.getFocus()==textFlowMarkup) {
  1431.                     return;
  1432.                 }
  1433.                
  1434.                
  1435.                 var exporter:ITextExporter = TextConverter.getExporter(TextConverter.TEXT_LAYOUT_FORMAT);
  1436.                 exporter.throwOnError = false;
  1437.                 exporter.config.whiteSpaceCollapse = WhiteSpaceCollapse.COLLAPSE;
  1438.                
  1439.                 var object:Object = exporter.export(textFlow, TextConverter.TEXT_LAYOUT_FORMAT);               
  1440.                 textFlowMarkup.text = object is XML ? XML(object).toXMLString() : null;
  1441.                
  1442.                 updateErrorsText(exporter.errors);
  1443.             }
  1444.            
  1445.             /**
  1446.              * Adds errors during export or import to the errors text box
  1447.              **/
  1448.             public function updateErrorsText(errors:*):void {
  1449.                
  1450.                 if (errors is Vector.<String>) {
  1451.                     textFlowMarkupErrors.text = errors ? errors.join("\n") : null;
  1452.                 }
  1453.                 else if (errors is String) {
  1454.                     textFlowMarkupErrors.text = String(errors);
  1455.                 }
  1456.                 else if (errors is Object) {
  1457.                     textFlowMarkupErrors.text = Object(errors).toString();
  1458.                 }
  1459.                 else {
  1460.                     textFlowMarkupErrors.text = "";
  1461.                 }
  1462.             }
  1463.            
  1464.             public function updateTableInformation():void {
  1465.                 var table:TableElement = getTable();
  1466.                
  1467.                 if (table) {
  1468.                     tableWidth.text = String(table.width);
  1469.                     tableHeight.text = String(Number(table.getFirstBlock().height).toFixed(1));
  1470.                     tableBlocks.text = table.tableBlocks ? String(table.tableBlocks.length) : "";
  1471.                     tableCells.text = String(table.numCells);
  1472.                     tableRowsStepper.value = table.numRows;
  1473.                     tableColumnsStepper.value = table.numColumns;
  1474.                     //tableCellSpacing.value = table.cellSpacing!=undefined ? table.cellSpacing : 0;
  1475.                 }
  1476.                 else {
  1477.                     tableWidth.text = "";
  1478.                     tableHeight.text = "";
  1479.                     tableBlocks.text = "";
  1480.                     tableCells.text = "";
  1481.                     tableRowsStepper.value = 0;
  1482.                     tableColumnsStepper.value = 0;
  1483.                     //tableCellSpacing.value = 0;
  1484.                 }
  1485.             }
  1486.            
  1487.             public var random:Boolean;
  1488.            
  1489.             /**
  1490.              * Switches between random and non random selection
  1491.              **/
  1492.             public function randomizeSelection():void {
  1493.                 random = !random;
  1494.                 randomCheckMark.visible = random;
  1495.                 randomCheckMark.includeInLayout = random;
  1496.                 randomCheckMarkSpacer.visible = random;
  1497.                 randomCheckMarkSpacer.includeInLayout = random;
  1498.             }
  1499.            
  1500.             protected function tableRowsStepper_changeHandler(event:Event):void
  1501.             {
  1502.                 var table:TableElement = getTable();
  1503.                
  1504.                 if (!table) {
  1505.                     updateErrorsText(tableNotCreatedError);
  1506.                     return;
  1507.                 }
  1508.                
  1509.                 // this adds the row but not the cells
  1510.                 //table.numRows = tableRowsStepper.value;
  1511.                 //updateAllControllers();
  1512.                
  1513.                 if (tableRowsStepper.value<table.numRows) {
  1514.                     removeRow(table);
  1515.                 }
  1516.                 else {
  1517.                     addRow(table);
  1518.                     //insertRow(table);
  1519.                 }
  1520.                
  1521.             }
  1522.            
  1523.             protected function tableColumnStepper_changeHandler(event:Event):void
  1524.             {
  1525.                 var table:TableElement = getTable();
  1526.                
  1527.                 if (!table) {
  1528.                     updateErrorsText(tableNotCreatedError);
  1529.                     return;
  1530.                 }
  1531.                
  1532.                     // this adds the row but not the cells
  1533.                 //table.numColumns = tableColumnsStepper.value;
  1534.                 //updateAllControllers();
  1535.                
  1536.                 if (tableColumnsStepper.value<table.numColumns) {
  1537.                     removeColumn(table);
  1538.                 }
  1539.                 else {
  1540.                     addColumn(table);
  1541.                     //insertColumn(table);
  1542.                 }
  1543.                
  1544.             }
  1545.            
  1546.             protected function tableWidth_changeHandler(event:TextOperationEvent):void
  1547.             {
  1548.                 var table:TableElement = getTable();
  1549.                
  1550.                 if (!table) {
  1551.                     updateErrorsText(tableNotCreatedError);
  1552.                     return;
  1553.                 }
  1554.                
  1555.                 table.width = Number(tableWidth.text);
  1556.                
  1557.                 updateAllControllers();
  1558.             }
  1559.            
  1560.             protected function cellSpacing_changeHandler(event:Event):void
  1561.             {
  1562.                 var table:TableElement = getTable();
  1563.                
  1564.                 if (!table) {
  1565.                     updateErrorsText(tableNotCreatedError);
  1566.                     return;
  1567.                 }
  1568.                
  1569.                 //table.cellSpacing = Number(tableCellSpacing.value);
  1570.                
  1571.                 updateAllControllers();
  1572.                
  1573.             }
  1574.            
  1575.             protected function uncaughtErrorHandler(event:UncaughtErrorEvent):void
  1576.             {
  1577.                 if (event.error is TypeError) {
  1578.                     var message:String = TypeError(event.error).message;
  1579.                     updateErrorsText(message);
  1580.                 }
  1581.                 else {
  1582.                     updateErrorsText(Object(event.error).toString());
  1583.                 }
  1584.             }
  1585.            
  1586.             protected function errorHandler(event:ErrorEvent):void
  1587.             {
  1588.                 updateErrorsText(event.text);
  1589.             }
  1590.            
  1591.         ]]>
  1592.     </fx:Script>
  1593.  
  1594.     <s:HGroup width="100%" minHeight="400" top="50" left="10" right="10" styleName="header">
  1595.         <s:VGroup width="65%" height="100%">
  1596.             <s:HGroup width="100%" gap="1" >
  1597.                 <s:Label text="TextFlow" width="100%" styleName="header"/>
  1598.                 <s:Label text="clear"
  1599.                          click="clearTextFlow()"
  1600.                          styleName="header"
  1601.                          rollOver="event.currentTarget.styleName='header2'"
  1602.                          rollOut="event.currentTarget.styleName='header'"
  1603.                          useHandCursor="true" buttonMode="true"
  1604.                          />
  1605.             </s:HGroup>
  1606.             <s:BorderContainer id="group" width="100%" height="100%" minHeight="250"/>
  1607.             <s:HGroup minHeight="22" verticalAlign="middle">
  1608.                 <local:ImageButton source="assets/images/addTableIcon.png"
  1609.                          toolTip="Add Table"
  1610.                          click="addTable(3, 3)" />
  1611.                 <local:ImageButton toolTip="Remove Table"
  1612.                          source="assets/images/removeTableIcon.png"
  1613.                          click="removeTable(getTable())" />
  1614.                 <s:Label text="|" />
  1615.                 <local:ImageButton source="assets/images/addRowIcon.png"
  1616.                                    toolTip="Add Row"
  1617.                                    click="addRow(getTable())" />
  1618.                 <local:ImageButton source="assets/images/removeRowIcon.png"
  1619.                                    toolTip="Remove Row"
  1620.                                    click="removeRow(getTable())" />
  1621.                 <s:Label text="|" />
  1622.                 <local:ImageButton source="assets/images/addColumnIcon.png"
  1623.                                    toolTip="Add Column"
  1624.                                    click="addColumn(getTable())" />
  1625.                 <local:ImageButton source="assets/images/removeColumnIcon.png"
  1626.                                    toolTip="Remove Column"
  1627.                                    click="removeColumn(getTable())" />
  1628.                 <s:Label text="|" />
  1629.                 <s:Image toolTip="Rows" source="assets/images/rowsIcon.png" />
  1630.                 <s:NumericStepper id="tableRowsStepper"
  1631.                                   width="50" maximum="250"
  1632.                                   change="tableRowsStepper_changeHandler(event)"/>
  1633.                 <s:Image toolTip="Columns" source="assets/images/columnsIcon.png" />
  1634.                 <s:NumericStepper id="tableColumnsStepper"
  1635.                                   width="50" maximum="50"
  1636.                                   change="tableColumnStepper_changeHandler(event)"/>
  1637.                 <s:Label text="|" />
  1638.                 <s:Label text="Width:" />
  1639.                 <s:TextInput id="tableWidth" width="40" change="tableWidth_changeHandler(event)"/>
  1640.                 <s:Label text="Height:" />
  1641.                 <s:Label id="tableHeight" />
  1642.                 <!--<s:Label text="Cell Spacing:" />
  1643.                 <s:NumericStepper id="tableCellSpacing"
  1644.                                   width="50" maximum="50"
  1645.                                   change="cellSpacing_changeHandler(event)"/>-->
  1646.                 <s:Label text="Total Cells:" />
  1647.                 <s:Label id="tableCells" />
  1648.                 <s:Label text="Blocks:" />
  1649.                 <s:Label id="tableBlocks" />
  1650.             </s:HGroup>
  1651.             <s:Label text="TextFlow Markup" width="100%" styleName="header"/>
  1652.             <s:TextArea id="textFlowMarkup" width="100%" height="100%"
  1653.                         focusThickness="0" change="textFlowMarkup_changeHandler(event)"/>
  1654.             <s:Label text="TextFlow Markup Errors" width="100%" styleName="header"
  1655.                         />
  1656.             <s:TextArea id="textFlowMarkupErrors" width="100%" height="30%" color="red"
  1657.                         >
  1658.             </s:TextArea>
  1659.         </s:VGroup>
  1660.         <s:VGroup width="40%" height="100%" >
  1661.             <s:Label text="Elements" width="100%" styleName="header"/>
  1662.             <s:HGroup styleName="buttonGroup">
  1663.                 <mx:LinkButton label="Add Editable Table" click="addTable(3, 3, false, true)"/>
  1664.                 <mx:LinkButton label="Add Selectable Table" click="addTable(3, 3, true)"/>
  1665.                 <mx:LinkButton label="Add Table" click="addTable(3, 3)"/>
  1666.                 <mx:LinkButton label="Remove Table" click="removeTable(getTable())"/>
  1667.                 <mx:LinkButton label="Remove All Rows" click="removeAllRows(getTable())"/>
  1668.             </s:HGroup>
  1669.             <s:HGroup>
  1670.                 <mx:LinkButton label="Add Paragraph" click="addParagraph()"/>
  1671.                 <mx:LinkButton label="Remove Paragraph" />
  1672.                 <mx:LinkButton label="Remove Shapes" click="removeShapes()"/>
  1673.                 <mx:LinkButton label="Remove All" click="clearTextFlow()"/>
  1674.             </s:HGroup>
  1675.             <s:HGroup width="100%" gap="1" >
  1676.                 <s:Label text="Selection" width="100%" styleName="header"/>
  1677.                
  1678.                 <s:Label id="randomCheckMark" text="✓"  
  1679.                          styleName="header"
  1680.                          visible="false"
  1681.                          includeInLayout="false"
  1682.                          />
  1683.                 <s:Spacer id="randomCheckMarkSpacer" width="-2" />
  1684.                 <s:Label text="Random" click="randomizeSelection()"
  1685.                          styleName="header"
  1686.                          useHandCursor="true"
  1687.                          buttonMode="true"
  1688.                          />
  1689.             </s:HGroup>
  1690.             <s:HGroup>
  1691.                 <mx:LinkButton label="Select Table" click="selectTable()"/>
  1692.                 <mx:LinkButton label="Select Row" click="selectRow()"/>
  1693.                 <mx:LinkButton label="Select Rows" click="selectRows()"/>
  1694.                 <mx:LinkButton label="Select Column" click="selectColumn()"/>
  1695.                 <mx:LinkButton label="Select Columns" click="selectColumns()"/>
  1696.             </s:HGroup>
  1697.             <s:HGroup>
  1698.                 <mx:LinkButton label="Select Cell" click="selectCell()"/>
  1699.                 <mx:LinkButton label="Select Cells" click="selectCells()"/>
  1700.                 <mx:LinkButton label="Select Cell Content" click="selectCellContent()"/>
  1701.                 <mx:LinkButton label="Clear Selection" click="clearSelection()"/>
  1702.             </s:HGroup>
  1703.             <s:HGroup>
  1704.                 <mx:LinkButton label="Select Text Range" click="selectRange()"/>
  1705.             </s:HGroup>
  1706.             <s:Label text="Misc" width="100%" styleName="header"/>
  1707.             <s:HGroup>
  1708.                 <mx:LinkButton label="Join Cells" click="joinCells()"/>
  1709.                 <mx:LinkButton label="Get next cell" click="getNextCell()"/>
  1710.                 <mx:LinkButton label="Get previous cell" click="getPreviousCell()"/>
  1711.                 <mx:LinkButton label="Update all controllers" click="updateAllControllers()"/>
  1712.             </s:HGroup>
  1713.             <s:HGroup>
  1714.                 <mx:LinkButton label="Show Row Cursor" click="showSelectRowCursor()"/>
  1715.                 <mx:LinkButton label="Show Column Cursor" click="showSelectColumnCursor()"/>
  1716.                 <mx:LinkButton label="Show Table Cursor" click="showSelectTableCursor()"/>
  1717.             </s:HGroup>
  1718.             <s:Label text="Boundaries" width="100%" styleName="header"/>
  1719.             <s:HGroup>
  1720.                 <mx:LinkButton label="Table" click="showTableBounds()"/>
  1721.                 <mx:LinkButton label="TableBlocks" click="showTextBlocksBounds()"/>
  1722.                 <mx:LinkButton label="TextLines" click="showTextLineBounds()"/>
  1723.                 <mx:LinkButton label="Paragraph" click="showParagraphBounds()"/>
  1724.                 <mx:LinkButton label="Parcels" click="showParcelBounds()"/>
  1725.                 <mx:LinkButton label="Atoms" click="showAtomBounds()"/>
  1726.             </s:HGroup>
  1727.             <s:Label text="Import Text Flows" width="100%" styleName="header"/>
  1728.             <s:HGroup>
  1729.                 <mx:LinkButton label="Simple Table TextFlow" click="importToFlow(simpleTableTextFlow)"/>
  1730.                 <mx:LinkButton label="Complex Table TextFlow" click="importToFlow(complexTableData)"/>
  1731.                 <mx:LinkButton label="Simple TextFlow" click="importToFlow(simpleTextMarkup)"/>
  1732.                 <mx:LinkButton label="Complex TextFlow" click="importToFlow(complexTextFlow)"/>
  1733.             </s:HGroup>
  1734.             <s:Label text="Version Information" width="100%" styleName="header"/>
  1735.             <s:HGroup minHeight="22" verticalAlign="middle">
  1736.                 <s:Label id="versionInfo" paddingLeft="10"/>
  1737.             </s:HGroup>
  1738.            
  1739.             <!--<s:TextArea width="100%" height="10%" color="blue"
  1740.                         >
  1741.                 <s:content>
  1742.                     <s:p>
  1743.                         <s:span>test</s:span>
  1744.                     </s:p>
  1745.                     <s:table>
  1746.                         <s:tr>
  1747.                             <s:td>
  1748.                                 Hello world
  1749.                             </s:td>
  1750.                         </s:tr>
  1751.                     </s:table>
  1752.                 </s:content>
  1753.             </s:TextArea>-->
  1754.         </s:VGroup>
  1755.     </s:HGroup>
  1756.    
  1757. </s:Application>
Advertisement
Add Comment
Please, Sign In to add comment