替换TWaver中Tree展开合并图标

news/2024/8/26 10:17:37 标签: tree, class, function, css, frameworks, image
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">         TWaver最大的优点之一是“ 灵活的定制功能”。光说不练不行,来个例子演练一下:定制Tree节点的标签。
  • 默认Tree和Network上的标签显示的是网元的name属性,设置Styles.TREE_LABEL属性后,可以让Tree显示Styles.TREE_LABEL的值,以达到Tree和Network显示不同标签的目的
  • 如果觉得这样还不够,可以设置Tree#labelFunction,比如下面的代码可以让Node显示name,Link显示Styles.TREE_LABEL:

class="language-javascript">class="tags" href="/tags/TREE.html" title=tree>tree.labelFunction = function(e:IElement):String{
	if(e is Link){
		return e.getStyle(Styles.TREE_LABEL);
	}else{
		return e.name;
	}
}

       既然TWaver这么灵活,那Tree的展开合并图标能定制么?

class="alignnone size-thumbnail wp-image-4352" title="disclosure2" src="http://twaver.servasoft.com/wp-content/uploads/2012/06/disclosure2-644x121.png" alt="" width="644" height="121" style="border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:649px" />

       先来看看用最原始的方式:

  1. 先定义2个class
    class="language-javascript">[Embed(source="assets/plus.png")]
    [Bindable]
    public var plus:Class;
    
    [Embed(source="assets/minus.png")]
    [Bindable]
    public var minus:Class;

  2. 再设置Tree的disclosureOpenIcon和disclosureClosedIcon属性为上面定义的class

  1. class="language-javascript"><tw:Tree id="class="tags" href="/tags/TREE.html" title=tree>tree2" width="100%" height="100%" disclosureOpenIcon="{minus}" disclosureClosedIcon="{plus}"/>
  1. 效果如下:
  1. class="alignnone size-thumbnail wp-image-4348" title="disclosure1" src="http://twaver.servasoft.com/wp-content/uploads/2012/06/disclosure1-644x194.png" alt="" width="644" height="194" style="border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:649px" />

        有同学就要问了,这样还是不够灵活,资源文件直接嵌入了SWF中,如果想换个图标,还得重新编译打包上传一把。这好说,可以用css定制组件样式(其实css也要编译成swf,这点adobe完全没有考虑到用户的实际需求,换个颜色还得编译swf,以后有时间给大家说说如何不编译swf也能用css定制样式),但是偏偏disclosureOpenIcon和disclosureClosedIcon无法用css定制样式。从mx.controls.Tree的源代码中可以看到

class="language-javascript">[Style(name="disclosureOpenIcon", type="Class", format="EmbeddedFile", inherit="no")]
[Style(name="disclosureClosedIcon", type="Class", format="EmbeddedFile", inherit="no")]

       这说明,disclosureOpenIcon和disclosureClosedIcon的style类型为classcss可以加载class?肯定不行,不信邪的同学可以试试:

class="language-javascript"><mx:Style>
	Tree {
		disclosureOpenIcon: "assets/minus.png";
		disclosureClosedIcon: "assets/plus.png";
	}
</mx:Style>

      运行程序会得到下面的错误:

TypeError: Error #1034: Type Coercion failed: cannot convert “assets/plus.png” to Class.
at mx.controls::Tree/initListData()[C:\autobuild\3.5.0\frameworks\projects\framework\src\mx\controls\Tree.as:2663]

      那我们就定义两个class,但是这两个class是什么class?我们来分析一下:
  1. 既然是图片,我们就继承Image组件吧。
  2. 既然是class,说明mx内部会不停地new这个class,所以图片资源要缓存起来。
  3. 加载图片是异步的,所以要等图片加载完成后,再注册这个class

      因此就有了这2个类的定义:

class="language-javascript">package {
	import flash.display.Bitmap;
	import flash.display.Loader;

	import mx.controls.Image;

	public class disclosureOpenIcon extends Image {
		public static const loader:Loader = new Loader();

		public function disclosureOpenIcon() {
			this.source = new Bitmap(Bitmap(loader.content).bitmapData);
			this.width = loader.content.width;
			this.height = loader.content.height;
		}
	}
}

package {
	import flash.display.Bitmap;
	import flash.display.Loader;

	import mx.controls.Image;

	public class disclosureClosedIcon extends Image {
		public static const loader:Loader = new Loader();

		public function disclosureClosedIcon() {
			this.source = new Bitmap(Bitmap(loader.content).bitmapData);
			this.width = loader.content.width;
			this.height = loader.content.height;
		}
	}
}

       下面是定制Tree展开合并图标的代码:

class="language-javascript">private static function registTreeDisclosureIcon(class="tags" href="/tags/TREE.html" title=tree>tree:UIComponent):void {
	registClassStyle(class="tags" href="/tags/TREE.html" title=tree>tree, disclosureOpenIcon, "assets/minus.png");
	registClassStyle(class="tags" href="/tags/TREE.html" title=tree>tree, disclosureClosedIcon, "assets/plus.png");
}

private static function registClassStyle(component:UIComponent, clazz:Class, value:String):void {
	if(clazz["loader"].content == null){
		clazz["loader"].contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
			component.setStyle(getQualifiedClassName(clazz), clazz);
		});
		clazz["loader"].load(new URLRequest(value), new LoaderContext(true));
	}
}

        如果想全局注册,可以考虑下面的代码:

class="language-javascript">private static function registTreeDisclosureIcon():void {
	var css:CSSStyleDeclaration = StyleManager.getStyleDeclaration("Tree");
	registClassStyle(css, disclosureOpenIcon, "assets/minus.png");
	registClassStyle(css, disclosureClosedIcon, "assets/plus.png");
}

private static function registClassStyle(css:CSSStyleDeclaration, clazz:Class, value:String):void {
	if(clazz["loader"].content == null){
		clazz["loader"].contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void {
			css.setStyle(getQualifiedClassName(clazz), clazz);
		});
		clazz["loader"].load(new URLRequest(value), new LoaderContext(true));
	}
}

         最后看看摆脱mx.controls.Tree的FastTree怎么定制展开合并图标,比如下面的代码就让展开合并图标颠倒了:

class="language-javascript">class="tags" href="/tags/TREE.html" title=tree>tree4.expandIcon = "collapsed_icon";
class="tags" href="/tags/TREE.html" title=tree>tree4.collapseIcon = "expanded_icon";

本文完整代码见附件:TreeDisclosureIcon

class="tags" href="/tags/TREE.html" title=tree>treeSkill">

http://www.niftyadmin.cn/n/1765853.html

相关文章

一个华为人写的忠告

一、“从小事做起&#xff0c;学会吃亏&#xff0c;与他人合作。   二、“心有多大&#xff0c;舞台就有多大。   三、“好好学习&#xff0c;天天向上"。   四、勇于实践&#xff0c;勇于犯错&#xff0c;善于反思。   五、要有方法、有套路&#xff0c;对问题系…

修改MySQL默认字符集

今天发现有库级字符集和表级字符集&#xff0c;实验了下发现&#xff0c;库级字符集是该库内表的默认字符集&#xff0c;当创建表时&#xff0c;如果未指定字符集&#xff0c;默认使用该表所属库的字符集。表也可使用不同于所属库的字符集。 MySQL对于字符集的指定可以细化到一…

oracle 10g WMSYS.WM_CONCAT 函數的用法

select t.rank, t.Name from t_menu_item t;10 CLARK10 KING10 MILLER20 ADAMS20 FORD20 JONES20 SCOTT20 SMITH30 ALLEN30 BLAKE30 JAMES30 MARTIN30 TURNER30 WARD-------------------------------- 我们通过 10g 所提供的 WMSYS.WM_CONCAT 函数即可以完成 行转列的效果selec…

减少Menu分割线的间距

之前看到社区中有介绍怎样灵活使用右键菜单的文章。现在积累下来右键菜单的方式还是比较多的&#xff0c;让我们来初步了解一下&#xff1a; 1. 使用flash自带的右键菜单&#xff0c;通过ContextMenu来设置&#xff0c;这是twaver最早右键菜单的解决方法&#xff0c;但这种…

TWaver图形界面之道(六)TWaver设计模式

继续上一章的TWaver图形界面之道&#xff08;五&#xff09;Hello TWaver TWaver设计模式 让我们详细探讨TWaver的设计模式&#xff0c;本章包括三个方面&#xff1a;MVC的设计模式&#xff0c;数据元素与数据容器&#xff0c;以及事件驱动机制。 什么是设计模式&#xff1f;…

Java多线程:什么时候使用哪种实现方式

广州疯狂软件学院拥有三大课程体系包括&#xff1a;java课程&#xff0c;android课程&#xff0c;ios课程&#xff0c;更多java知识&#xff0c;android知识&#xff0c;ios知识&#xff0c;疯狂软件官网持续更新中。 众所周知&#xff0c;JAVA里实现多线程有两种方式。 一是继…

用TWaver HTML5定制五彩斑斓的链路

最近有客户提到自定义链路的需求&#xff0c;个人感觉非常有代表意义&#xff0c;现在共享出来给大家参考一下。先来看看需求&#xff1a; 链路要分成两半&#xff0c;用两种颜色填充。填充百分比在不同值域时&#xff0c;用不同颜色。显示刻度有个开关&#xff0c;可以控制链…

作业 5 指针应用1

1、自学教材 第8章 指针&#xff0c;回答以下问题&#xff1a; 变量、内存单元和地址之间是什么关系&#xff1f;如何定义指针变量&#xff0c;怎样才能使用指针变量&#xff1f;什么是指针变量的初始化&#xff1f;指针变量的基本运算有哪些&#xff1f;如何使用指针操作所指向…