Flex4 使用itemRenderer 为Tree加线具体实现
程序员文章站
2022-03-07 18:51:54
复制代码 代码如下: package modules { import flash.display.bitmapdata; import flash.display.gra...
复制代码 代码如下:
package modules
{
import flash.display.bitmapdata;
import flash.display.graphics;
import mx.collections.icollectionview;
import mx.collections.ilist;
import mx.controls.tree;
import mx.controls.treeclasses.itreedatadescriptor;
import mx.controls.treeclasses.treeitemrenderer;
/**
* alpha value for the tree lines.
* @default 1
*/
[style(name="linealpha", type="number", format="length", inherit="no")]
/**
* color of the tree lines.
* @default 0x808080
*/
[style(name="linecolor", type="uint", format="color", inherit="no")]
/**
* thickness value for the tree lines.
* @default 1
*/
[style(name="linethickness", type="number", format="length", inherit="no")]
/**
* the tree line style - none, dotted (default), or solid.
* @default "dotted"
*/
[style(name="linestyle", type="string", enumeration="solid", inherit="no")]
/**
* extends treeitemrenderer to draw the dotted lines of the tree.
* it supports 3 line styles - dotted (default), solid, or none.
* and the tree line color, alpha, and thickness values are configurable styles too.
*
* <pre>
* <ui:treeitemlinesrenderer
* <b>styles</b>
* linealpha="1"
* linecolor="#808080"
* linethickness="1"
* linestyle="dotted"
* >
* ...
* <i>child tags</i>
* ...
* </ui:treeitemlinesrenderer>
* </pre>
*
* @author chris callendar
* @date april 20th, 2009
*/
public class treeitemlinesrenderer extends treeitemrenderer
{
public static const dotted:string = "dotted"; // default
public static const solid:string = "solid";
public static const none:string = "none";
public function treeitemlinesrenderer() {
super();
}
override public function set data(value:object):void {
super.data = value;
}
override protected function updatedisplaylist(w:number, h:number):void {
super.updatedisplaylist(w, h);
if ((w > 0) && (h > 0)) {
// go up the hierarchy, drawing the vertical dotted lines for each node
var tree:tree = (owner as tree);
var desc:itreedatadescriptor = tree.datadescriptor;
var currentnode:object = data;
var parentnode:object = tree.getparentitem(currentnode);
// the level is zero at this node, then increases as we go up the tree
var levelsup:int = 0;
var linestyle:string = getstyle("solid");
var linecolor:uint = getcolorstyle("linecolor", 0x808080);
var linealpha:number = getnumberstyle("linealpha", 1);
var linethickness:number = getnumberstyle("linethickness", 1);
var indentation:number = tree.getstyle("indentation");
// move the icon and label over to make room for the lines (less for root nodes)
var shift:int = (parentnode == null ? 2 : 6) + linethickness;
if (icon) {
icon.move(icon.x + shift, icon.y);
}
if (label) {
label.move(label.x + shift, label.y);
}
var g:graphics = graphics;
g.clear();
if ((linestyle != none) && (linealpha > 0) && (linethickness > 0)) {
while (parentnode != null) {
var children:icollectionview = desc.getchildren(parentnode);
if (children is ilist) {
var itemindex:int = (children as ilist).getitemindex(currentnode);
// if this node is the last child of the parent
var islast:boolean = (itemindex == (children.length - 1));
drawlines(g, w, h, linestyle, linecolor, linealpha, linethickness, islast, levelsup, indentation);
// go up to the parent, increasing the level
levelsup++;
currentnode = parentnode;
parentnode = tree.getparentitem(parentnode);
} else {
break;
}
}
}
}
}
protected function drawlines(g:graphics, w:number, h:number, linestyle:string, linecolor:uint,
linealpha:number, linethickness:number, islastitem:boolean, levelsup:int, indentation:number):void {
var midy:number = math.round(h / 2);
var linex:number = 0;
if (disclosureicon) {
linex = disclosureicon.x + (disclosureicon.width / 2);
} else if (icon) {
linex = icon.x - 8;
} else if (label) {
linex = label.x - 8;
}
linex = math.floor(linex) - int(linethickness / 2);
// adjust the x position based on the indentation
if (levelsup > 0) {
if (!isnan(indentation) && (indentation > 0)) {
linex = linex - (levelsup * indentation);
} else {
// invalid indentation style value
return;
}
}
var liney:number = h;
// stop the dotted line halfway on the last item
if (islastitem) {
liney = midy;
// no lines need to be drawn for parents of the last item
if (levelsup > 0) {
return;
}
}
g.linestyle(0, 0, 0);
if (linestyle == solid) {
g.beginfill(linecolor, linealpha);
} else {
var verticaldottedline:bitmapdata = createdottedline(linecolor, linealpha, linethickness, true);
g.beginbitmapfill(verticaldottedline);
}
// draw the vertical line
g.drawrect(linex, 0, linethickness, liney);
// end the fill and start it again otherwise the lines overlap and it create white squares
g.endfill();
// draw the horizontal line - only needed on this node (not on any parents)
if (levelsup == 0) {
var startx:int = linex + 1 + int(linethickness / 2);
var endx:int = startx + 11; // 5 dots
if (islastitem) {
startx = linex;
}
var starty:number = midy - int(linethickness / 2);
if (linestyle == solid) {
g.beginfill(linecolor, linealpha);
} else {
var horizontaldottedline:bitmapdata = createdottedline(linecolor, linealpha, linethickness, false);
g.beginbitmapfill(horizontaldottedline);
}
g.drawrect(startx, starty, endx - startx, linethickness);
g.endfill();
}
}
/**
* creates a bitmapdata that is used to renderer a dotted line.
* if the vertical parameter is true, then it creates a rectangle bitmap that is
* twice as long as it is wide (linethickness). otherwise it creates a rectangle
* that is twice as wide as it is long.
* the first half of the rectangle is filled with the line color (and alpha value),
* then second half is transparent.
*/
private function createdottedline(linecolor:uint, linealpha:number, linethickness:number,
vertical:boolean = true):bitmapdata {
var w:number = (vertical ? linethickness : 2 * linethickness);
var h:number = (vertical ? 2 * linethickness : linethickness);
var color32:uint = combinecolorandalpha(linecolor, linealpha);
var dottedline:bitmapdata = new bitmapdata(w, h, true, 0x00ffffff);
// create a dotted bitmap
for (var i:int = 0; i < linethickness; i++) {
for (var j:int = 0; j < linethickness; j++) {
dottedline.setpixel32(i, j, color32);
}
}
return dottedline;
}
/**
* combines the color value and the alpha value into a 32 bit uint like #aarrggbb.
*/
private function combinecolorandalpha(color:uint, alpha:number):uint {
// make sure the alpha is a valid number [0-1]
if (isnan(alpha)) {
alpha = 1;
} else {
alpha = math.max(0, math.min(1, alpha));
}
// convert the [0-1] alpha value into [0-255]
var alphacolor:number = alpha * 255;
// bitshift it to come before the color
alphacolor = alphacolor << 24;
// combine the two values: #aarrggbb
var combined:uint = alphacolor | color;
return combined;
}
private function getcolorstyle(propname:string, defaultvalue:uint):uint {
var color:uint = defaultvalue;
if (propname != null) {
var n:number = getstyle(propname);
if (!isnan(n)) {
color = uint(n);
}
}
return color;
}
private function getnumberstyle(propname:string, defaultvalue:number):number {
var number:number = defaultvalue;
if (propname != null) {
var n:number = getstyle(propname);
if (!isnan(n)) {
number = n;
}
}
return number;
}
}
}
下一篇: flex导出excel具体实现