4、复杂控件
(1) expandablecomposite
l web页面中一个通用的主题是具有收缩一部分页面内容的能力
l eclipse form也提供了这样一个控件:expandablecomposite
l 下面的代码片断是使用expandablecomposite的一个例子:
expandablecomposite ec = toolkit.createexpandablecomposite(body,
expandablecomposite.tree_node
| expandablecomposite.client_indent);
ec.settext("expandable composite title");
string ctext = "we will now create a somewhat long text so that "
+ "we can use it as content for the expandable composite. "
+ "expandable composite is used to hide or show the text using the "
+ "toggle control";
label client = toolkit.createlabel(ec, ctext, swt.wrap);
ec.setclient(client);
td = new tablewrapdata();
td.colspan = 2;
ec.setlayoutdata(td);
ec.addexpansionlistener(new expansionadapter() {
public void expansionstatechanged(expansionevent e) {
form.reflow(true);
}
});
l 这个控件有很多风格,tree_node使得该控件具有树型节点的展开、收缩功能;而twistie使得控件具有三角箭头风格
l expanded使得初始展开显示
l client_indent使得client内容缩进对齐
l expandablecomposite呈现为激活控件和标题,而可以展开、收缩的内容称为client
l client必须是可展开的composite(上例是label控件)
l 最后需要添加expansion监听器在状态变化时,reflow form(即根据控件的新的大小重新定位和更新滚动条)
l 下面是上例的运行结果:
(2)section
l eclipse form中最常用的定制控件就是section(在pde中到处可见)
l section扩展expandablecomposite,但具有下面的新特性:
n 在标题下面有一个分隔控件
n 在分隔控件下面可以有一个描述文本
l 下面的代码片断是使用section的一个例子,代码和expandablecomposite没有太大差别,这里是用了twistie风格:
section section = toolkit.createsection(body, section.description
| section.twistie | section.expanded);
td = new tablewrapdata(tablewrapdata.fill);
td.colspan = 2;
section.setlayoutdata(td);
section.addexpansionlistener(new expansionadapter() {
public void expansionstatechanged(expansionevent e) {
form.reflow(true);
}
});
section.settext("section title");
toolkit.createcompositeseparator(section);
section
.setdescription("this is the description that goes below the title");
composite sectionclient = toolkit.createcomposite(section);
sectionclient.setlayout(new gridlayout());
button = toolkit.createbutton(sectionclient, "radio 1", swt.radio);
button = toolkit.createbutton(sectionclient, "radio 2", swt.radio);
section.setclient(sectionclient);
l 下面是上例的运行结果:
