PDA

View Full Version : Convert AS2 code to AS3


pointer
01-01-2010, 02:28 AM
This is an Adobe example code for a multiLine cell for flash DataGrid, but I need help translating to AS3. Thanks.


public class MultiLineCell extends mx.core.UIComponent
{
private var multiLineLabel; // The label to be used for text.
private var owner; // The row that contains this cell.
private var listOwner; // The List/grid/tree that contains this cell.

// Cell height offset from the row height total and preferred cell width.
private static var PREFERRED_HEIGHT_OFFSET = 4;
private static var PREFERRED_WIDTH = 100;
// Starting depth.
private var startDepth:Number = 1;

// Constructor. Should be empty.
public function MultiLineCell()
{
}


public function createChildren():Void
{

var c = multiLineLabel = this.createLabel("multiLineLabel", startDepth);
// Links the style of the label to the style of the grid
c.styleName = listOwner;
c.selectable = false;
c.tabEnabled = false;
c.background = false;
c.border = false;
c.multiline = true;
c.wordWrap = true;
}

public function size():Void
{

var c = multiLineLabel;
c.setSize(__width, __height);
}

// Provides the preferred height of the cell. Inherited method.
public function getPreferredHeight():Number
{

return owner.__height - PREFERRED_HEIGHT_OFFSET;
}

// Called by the owner to set the value in the cell. Inherited method.
public function setValue(suggestedValue:String, item:Object, selected:Boolean):Void
{
if (item==undefined){
multiLineLabel.text._visible = false;
}
multiLineLabel.text = suggestedValue;
}

}

}


Moderators, please don't move my post, it is AS3. Please. Thanks.

pointer
01-01-2010, 04:38 PM
This is what I have so far, I ma almost there, but I am getting this error:
Parameter child must be non-null.


public class MultiLineCell extends TextField implements ICellRenderer
{
public var _listData:ListData;
public var _data:Object;
public var myText:TextField;
private var _selected:Boolean;


public function MultiLineCell()
{
super();
}


public function set listData(newListData:ListData):void {
_listData = newListData;
myText = new TextField ();
myText.selectable = false;
}

public function get listData():ListData {
return _listData;
}

public function set data(newData:Object):void {
_data = newData;

}

public function get data():Object {
return _data;

}



public function set selected(s:Boolean):void {
_selected = s;
}

public function get selected():Boolean {
return _selected;
}

public function setMouseState(p_state:String):void { }

public function setSize(width:Number, height:Number):void { }

}

pointer
01-01-2010, 05:42 PM
After some serious goole searches, I found a solution to the above error, but now I have amore unusual error.

I had to make a movieClip in the Library and set the export class as my MultiLineCell class. Now i have this error:


ReferenceError: Error #1065: Variable Button is not defined.
ReferenceError: Error #1065: Variable TextInput is not defined.
ReferenceError: Error #1065: Variable ComponentShim is not defined.


Is anyone familiar with any of this?

CyanBlue
01-01-2010, 06:17 PM
Sounds like you don't have TextInput & Button components in your library???

pointer
01-01-2010, 06:29 PM
I am happy someone replied. Thanks, CyanBlue.

I added those to the Library, and now I am getting this error:
ReferenceError: Error #1065: Variable ComponentShim is not defined.

This is so frustrating, I have been at it more than 12 hours. Having a DataGrid simply wordWrap should not take more hours to implement than it takes to build an entire Flash project.

CyanBlue
01-01-2010, 07:24 PM
Um... I thought that ComponentShim comes in as one of the default component asset element... Can you verify that???

I'd say post the sample file you have, FLA & AS, so that other people can have their hands wet... Preferrably in CS3 format cuz that seems to be more populat... That sometimes is whole lot easier and faster... ;)

As for the AS3 vs Components forum comment you have made above, which I missed in the first place... Yes, pretty much everything can go to AS3 forum, but then there is no point of creating sub forums like Components or Games forum... We did that because we want the information posted in the right place so that other people who search the forum can get help faster... People might think that AS3 forum gets more traffic than Components forum... Maybe it is so, but most of the people who help others in this forum tries their best to help others, and most of them go through the new posts lists rather than visiting the sub forum directly... So, let's keep the information where it best belongs... I'll move this thread to the Components forum later on... :cool:

pointer
01-01-2010, 07:42 PM
Cynablue, I didn't see ComponentShim anywhere, and have no idea what it is.
Here is my complete code:

The code on my main.fla file:

import MultiLineCell;
import fl.controls.CheckBox;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import flash.display.MovieClip;

myGrid_dg.setSize (500, 450);

var aLongString:String = "An example of a cell renderer class that displays a multiple line TextField"

var myDP:Array = new Array(
{firstName:"Winston", lastName:"Elstad", note:aLongString, item:100},
{firstName:"Ric", lastName:"Dietrich", note:aLongString, item:101},
{firstName:"Ewing", lastName:"Canepa", note:aLongString, item:102},
{firstName:"Kevin", lastName:"Wade", note:aLongString, item:103},
{firstName:"Kimberly", lastName:"Dietrich", note:aLongString, item:104},
{firstName:"AJ", lastName:"Bilow", note:aLongString, item:105},
{firstName:"Chuck", lastName:"Yushan", note:aLongString, item:106},
{firstName:"John", lastName:"Roo", note:aLongString, item:107}
);

myGrid_dg.dataProvider = new DataProvider(myDP);

myGrid_dg.setSize(430,190);
myGrid_dg.move(40,40);
myGrid_dg.rowHeight = 40;
myGrid_dg.getColumnAt(0).width = 70;
myGrid_dg.getColumnAt(1).width = 70;
myGrid_dg.getColumnAt(2).width = 130;
//myGrid_dg.resizableColumns = true;
myGrid_dg.setStyle("backgroundColor", 0xD5D5FF);

myGrid_dg.getColumnAt(2).cellRenderer = MultiLineCell;


Here is my MultiLineCell Class:
public class MultiLineCell extends TextField implements ICellRenderer
{
public var _listData:ListData;
public var _data:Object;
public var myText:TextField;
private var _selected:Boolean;

public function MultiLineCell()
{
super();
}


public function set listData(newListData:ListData):void
{
_listData = newListData;
myText = new TextField ();
myText.selectable = true;
myText.wordWrap = true;
}

public function get listData():ListData
{
return _listData;
}

public function set data(newData:Object):void
{
_data = newData;
}

public function get data():Object
{
return _data;
}


public function set selected(s:Boolean):void
{
_selected = s;
}

public function get selected():Boolean
{
return _selected;
}

public function setMouseState(p_state:String):void { }

public function setSize(width:Number, height:Number):void
{

}


}

pointer
01-02-2010, 11:42 AM
I found the part of the solution, but the rowHeight of the dataGrid is not auto adjusting to the content in each cell. I looked at the DataGrid documentation on Adobe's site and there is no variableRowHeight property. I ma still screwed.

Here is the code for anyone who has the same problem. I recommend using Flex for complex components like a DataGrid.


import fl.controls.listClasses.CellRenderer;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

public class MultiLineCell extends CellRenderer {
private var tf:TextFormat;

public function MultiLineCell() {
tf = new TextFormat();
tf.align = TextFormatAlign.RIGHT;
}

override protected function drawLayout():void {
textField.width = 120;//this.width;
textField.wordWrap = true;
textField.setTextFormat(tf);
super.drawLayout();
}
}