Skip to content

Instantly share code, notes, and snippets.

@johngag
Created June 15, 2011 22:01
Show Gist options
  • Save johngag/1028243 to your computer and use it in GitHub Desktop.
Save johngag/1028243 to your computer and use it in GitHub Desktop.
Flex 3 Custom TreeItemRenderer
package itemrenderer
{
import flash.events.MouseEvent;
import mx.controls.Button;
import mx.controls.Image;
import mx.controls.treeClasses.TreeItemRenderer;
import mx.controls.treeClasses.TreeListData;
public class CustomTreeItemRenderer extends TreeItemRenderer
{
public var btn:Button;
public var img1:Image;
public var img2:Image;
[Embed(source="/images/icons/home/Printer-icon.png")]
public var img1Class:Class;
[Embed(source="/images/icons/home/email-icon4.png")]
public var img2Class:Class;
public function CustomTreeItemRenderer()
{
super();
}
override public function set data(value:Object):void{
if(value != null){
super.data = value;
}
}
override protected function createChildren():void{
super.createChildren();
img1 = new Image();
img1.width=25;
img1.height=25;
img1.source=imgPrintClass;
img1.enabled="true";
img1.useHandCursor="true";
img1.buttonMode="true";
imgPrint.addEventListener(MouseEvent.CLICK, img1_clickHandler);
img2 = new Image();
img2.width=25;
img2.height=25;
img2.source=imgEmailClass;
img2.enabled="true";
img2.useHandCursor="true";
img2.buttonMode="true";
img2.addEventListener(MouseEvent.CLICK, img2_clickHandler);
btn = new Button();
btn.useHandCursor="true";
btn.buttonMode="true";
btn.setStyle("cornerRadius","0");
btn.setStyle("fontSize","12");
btn.setStyle("fontWeight", "bold");
btn.width=140;
btn.height=25;
btn.setStyle("textAlign","center");
btn.addEventListener(MouseEvent.CLICK, btn_clickHandler);
addChild(img1);
addChild(img2);
addChild(btn);
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(super.data){
var tld:TreeListData = TreeListData(super.listData);
if(tld.hasChildren){
hideButtons();
}else{
showButtons();
}
if(img1.visible && img2.visible && btn.visible){
this.img1.x = super.label.x + 140;
this.img2.x = this.img1.x + 35;
this.btn.x = super.label.x -17;
btn.label=data.name;
}
}
}
private function hideButtons():void{
this.img1.visible=false;
this.img2.visible=false;
this.btn.visible=false;
super.label.visible=true;
}
private function showButtons():void{
this.img1.visible=true;
this.img2.visible=true;
this.btn.visible=true;
super.label.visible=false;
}
protected function img1_clickHandler(event:MouseEvent):void
{
}
protected function img2_clickHandler(event:MouseEvent):void
{
}
protected function btne_clickHandler(event:MouseEvent):void
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment