| |
本文首先介绍模型转换的基本概念,然后介绍rsa模型转换框架,之后本文以两个具体的例子介绍如何在rsa开发平台中以模型转换框架为基础创建和扩展模型转换。
现在基于eclipse的应用越来越多,很多桌面应用都是用eclipse开发的。eclipse提供了一套swt/jface的控件库,使得人们开发界面应用极大的方便。但是,swt/jface的控件库毕竟有限,在应用开发是我们不可避免地要自己开发一些自定义的控件。本文通过开发一个颜色列表控件的实例介绍了eclipse自定义控件开发中所要用到的技术。
目标读者必须熟悉java开发,并且有一定的eclipse开发经验。
在eclipse网站上有一篇相关的文章"creating your own widgets using swt",该文介绍了开发自己控件的很多基本概念、方法,并且通过实例进行了介绍,非常好。但是其所用的实例比较简单,还有很多控件开发中所要涉及到的内容,例如键盘、鼠标事件的处理,滚动条、焦点的处理等等没有提及。本文通过开发一个自定义的颜色列表控件的实例,全面地介绍了自定义控件所涉及的技术。同时,读者也可以对该实例进行扩展,实现自己的列表控件。
swt中提供的标准列表控件非常简单,只能提供字符串的选择。我们经常需要提供一些图形列表供用户选择,这就需要自己开发自定义的列表控件。颜色选择列表是我们常用的一种图形列表,我们就以此为例进行介绍。以下是我们将要开发的颜色列表。
我们在开发自定义控件时主要考虑以下问题:
1、 自定义控件的绘制:通常我们需要自己对控件的形状或图案进行绘制;
2、 控件对键盘事件的响应:当焦点进入控件,用户进行键盘操作,通过键盘对控件进行控制时,我们需要让控件对用户的操作进行响应。例如在列表中,用户会通过上下箭头改变列表的选择项;
3、 控件对鼠标事件的响应:当用户用鼠标选中控件,进行操作时,控件必须作出相应的反应;
4、 控件对焦点事件的响应:当界面焦点进入或移出控件,通常我们需要将控件绘制成得到或失去焦点的形状。例如,当焦点进入列表时,一般被选中的列表项会有虚框表示选中。
5、 响应tab键:对于一个可操纵的控件,用户可以用tab键将焦点移入或移出。
6、 响应滚动条事件:当控件有滚动条时,我们需要响应用户对滚动条的操作,完成对控件的绘制工作。
7、 提供事件监听机制:程序员使用你的控件时通常需要监听控件中发生的一些事件,这样当事件发生时,他们能够进行相应处理。
8、 提供辅助功能(accessibility):辅助功能是方便残障人士使用时必须的,标准控件都会提供相应的支持,我们自定义的控件也不例外。
9、 提供功能接口方便程序员访问:通常为方便程序员使用时获取控件中的信息或进行设置,我们需要提供一些接口。
首先我们要开发的列表控件是一个基本控件,所以我们选择canvas作为我们开发的基类。
public class colorlist extends canvas { vector colors = new vector(); // 用于保存我们颜色控件中的颜色值 vector colornames = new vector(); // 用于保存颜色控件中的颜色名字 int rowsel = -1; // 用于保存当前选中的行号 int oldrowsel = -1; // 用于保存上一次选中的行号 int maxx, maxy; // 用于保存列表的宽度和高度 int lineheight; // 用于设置行高 int cx = 0; // 滚动条滚动后,控件的图形相对于控件可见区域左上角的x坐标 int cy = 0; // 滚动条滚动后,控件的图形相对于控件可见区域左上角的y坐标 }
控件开发最重要的就是控件的绘制了。控件的绘制可以通过添加paintlistener,在它的paintcontrol方法中进行。
addpaintlistener(new paintlistener() { public void paintcontrol(paintevent e) { gc gc = e.gc; point size = getsize(); int beginx = e.x; int beginy = (e.y / lineheight) * lineheight; int beginline = (e.y - cy) / lineheight; int endline = beginline + e.height / lineheight + 1; if (endline > getitemcount()) endline = getitemcount(); for (int i = beginline; i < endline; i++) { boolean selected = false; if (i == rowsel) selected = true; onpaint(gc, i, cx, beginy + (i - beginline) * lineheight, selected); } } });
这里要注意的是从paintevent中获取的x,y,height,width是需要重绘的区域,x,y是以控件的左上角为原点的坐标。在我们的程序中,为了性能起见,我们先根据需要重绘的区域计算出需要重绘的行数,只重绘相应的行,而不是将整个控件重绘。我们程序中用到的onpaint用于绘制一行。
接下来,我们要让我们的控件响应键盘上下键对列表项进行选择。我们已对向上键的处理为例,首先当用户按了向上键时,我们需要改变选择,并且重绘旧的和新的选择项。如果选择项已经到了列表的顶部,我们还需要同时滚动滚动条。
addlistener(swt.keydown, new listener() { public void handleevent(event event) { switch (event.keycode) { case swt.arrow_up: // 处理向上键 if (rowsel != 0) { oldrowsel = rowsel; rowsel--; if (oldrowsel != rowsel) { //发送消息让控件重绘 ((canvas) event.widget).redraw(cx, (rowsel + cy / lineheight) * lineheight, maxx, lineheight*2, false); } if (rowsel < -cy / lineheight) { //如果需要,滚动滚动条 scrollbar bar = ((canvas) event.widget) .getverticalbar(); bar.setselection(bar.getselection() - lineheight); scrollvertical(bar); } selectionchanged(); // 发送selectionchanged事件 } break; case swt.arrow_down: // down arror key … break; } } });
接下来,我们要让我们的控件响应鼠标对列表项进行选择。首先我们要计算出鼠标选中的行号,注意mouseevent中的y值只是相对于控件左上角的坐标,我们需要加上滚动出了控件的部分。
addmouselistener(new mouselistener() { public void mousedoubleclick(mouseevent e) { } public void mousedown(mouseevent e) { int row = (e.y - cy) / lineheight; //计算选中的行 if (row >= 0) { oldrowsel = rowsel; rowsel = row; } if (oldrowsel != rowsel) { // 重画旧的和新的选择项 ((canvas) e.getsource()).redraw(cx, (e.y / lineheight) * lineheight, maxx, lineheight, false); ((canvas) e.getsource()).redraw(cx, (oldrowsel + cy / lineheight) * lineheight, maxx, lineheight, false); } selectionchanged(); } public void mouseup(mouseevent e) { } });
当我们的控件获得焦点时,选中的列表项需要有虚框表示控件得到焦点。当获得或失去焦点是,我们这里只需要简单的通知选中的项重画。
addfocuslistener(new focuslistener() { public void focusgained(focusevent e) { ((canvas) e.getsource()).redraw(cx, rowsel * lineheight, maxx, lineheight, true); } public void focuslost(focusevent e) { ((canvas) e.getsource()).redraw(cx, rowsel * lineheight, maxx, lineheight, true); } });
我们在绘制每一个列表项时可以加入判断当前控件是否得到焦点,如果控件得到了焦点,我们就在选中的项目上画一个虚框。下面是我们绘制一个列表项的代码,注意在代码的最后绘制焦点的虚框。
void onpaint(gc gc, int row, int beginx, int beginy, boolean isselected) { color initcolor = gc.getbackground(); color initforecolor = gc.getforeground(); if (isselected) { gc.setbackground(display.getcurrent().getsystemcolor( swt.color_list_selection)); gc.fillrectangle(beginx, beginy, maxx, lineheight); gc.setforeground(display.getcurrent().getsystemcolor( swt.color_list_selection_text)); } else { gc.setbackground(initcolor); } gc.drawstring((string) colornames.get(row), beginx + 24, beginy); color color = display.getcurrent().getsystemcolor( ((integer) colors.get(row)).intvalue()); gc.setbackground(color); gc.fillrectangle(beginx + 2, beginy + 2, 20, lineheight - 4); gc.setbackground(initcolor); gc.setforeground(initforecolor); if (isfocuscontrol() && isselected) gc.drawfocus(cx, beginy, maxx, lineheight); }
作为一个可操作的控件,tab键的支持也是很重要的。由于我们的控件是从canvas继承过来的,不支持tab键。下面的代码使我们的控件有tab键的支持:
addtraverselistener(new traverselistener() { public void keytraversed(traverseevent e) { if (e.detail == swt.traverse_tab_next || e.detail == swt.traverse_tab_previous) { e.doit = true; } }; });
很多时候,我们需要有滚动条的支持。对于滚动条,我们只要在上面加上selectionlistener,处理它的widgetselected事件就可以。
bar = getverticalbar(); if (bar != null) { bar.addselectionlistener(new selectionadapter() { public void widgetselected(selectionevent event) { scrollvertical((scrollbar) event.widget); } }); }
下面是函数scrollvertical的代码。一旦用户对滚动条操作,我们就可以计算出要滚动的区域,然后调用scroll函数。对函数scroll函数的调用会导致相应区域的重绘。
void scrollvertical(scrollbar scrollbar) { rectangle bounds = getclientarea(); int y = -scrollbar.getselection(); if (y + maxy < bounds.height) { y = bounds.height - maxy; } if( y%lineheight !=0 ) y = y - y % lineheight - lineheight; scroll(cx, y, cx, cy, maxx, maxy, false); cy = y; }
现在我们的程序已经基本成形了,我们来进一步完善它。由于我们开发的控件是提供给程序员的,我们需要提供接口,让外部知道控件中发生的事件。其中最重要的是列表项的选中事件。我们需要提供接口让程序员能够添加事件监控器(listener)来监控发生的事件,并且一旦发生事件,我们需要通知监控器。
首先,我们添加一个成员来保存添加的事件监控器:
vector selectionlisteners = new vector();
我们再增加一个函数addselectionlistener,让程序员可以添加监控器
public void addselectionlistener(selectionlistener listener) { selectionlisteners.addelement(listener); }
在我们前面的代码中,我们注意到每次选择项改变,我们都会调用selectionchanged函数。下面是selectionchanged函数代码。这里,我们会生成一个selectionevent事件,并且逐个调用事件监控器的widgetselected方法。这样别人就可以监听到我们的事件了。
public void selectionchanged() { event event = new event(); event.widget = this; selectionevent e = new selectionevent(event); for (int i = 0; i < selectionlisteners.size(); i++) { selectionlistener listener = (selectionlistener) selectionlisteners.elementat(i); listener.widgetselected(e); } }
现在辅助功能(accessibility)也日益成为软件重要的部分,它是的残疾人也能够方便的使用我们的软件。美国已经立法,不符合accessibility规范的软件不能够在政府部门销售。我们开发的控件也需要支持accessibility.下面的代码使我们的控件有accessibility支持。其中最重要的是getrole和getvalue函数。我们的控件是从canvas继承,我们在getrole函数中返回acc.role_list,这样我们的控件才能让屏幕阅读软件将我们的控件作为列表控件对待。
accessible accessible = getaccessible(); accessible.addaccessiblecontrollistener(new accessiblecontroladapter() { public void getrole(accessiblecontrolevent e) { int role = 0; int childid = e.childid; if (childid == acc.childid_self) { role = acc.role_list; } else if (childid >= 0 && childid < colors.size()) { role = acc.role_listitem; } e.detail = role; } public void getvalue(accessiblecontrolevent e){ int childid = e.childid; if (childid == acc.childid_self) { e.result = gettext(); } else if (childid >= 0 && childid < colors.size()) { e.result = (string)colornames.get(childid); } } public void getchildatpoint(accessiblecontrolevent e) { point testpoint = tocontrol(new point(e.x, e.y)); int childid = acc.childid_none; childid = (testpoint.y - cy)/lineheight; if (childid == acc.childid_none) { rectangle location = getbounds(); location.height = location.height - getclientarea().height; if (location.contains(testpoint)) { childid = acc.childid_self; } } e.childid = childid; } public void getlocation(accessiblecontrolevent e) { rectangle location = null; int childid = e.childid; if (childid == acc.childid_self) { location = getbounds(); } if (childid >= 0 && childid < colors.size()) { location = new rectangle(cx,childid*lineheight+cy,maxx,lineheight); } if (location != null) { point pt = todisplay(new point(location.x, location.y)); e.x = pt.x; e.y = pt.y; e.width = location.width; e.height = location.height; } } public void getchildcount(accessiblecontrolevent e) { e.detail = colors.size(); } public void getstate(accessiblecontrolevent e) { int state = 0; int childid = e.childid; if (childid == acc.childid_self) { state = acc.state_normal; } else if (childid >= 0 && childid < colors.size()) { state = acc.state_selectable; if (isfocuscontrol()) { state |= acc.state_focusable; } if (rowsel == childid) { state |= acc.state_selected; if (isfocuscontrol()) { state |= acc.state_focused; } } } e.detail = state; } }); 最后,我们需要提供一些方法方便程序员使用我们的控件。
public void setselection(int index) { if (index >= getitemcount() || index < 0) return; oldrowsel = rowsel; rowsel = index; selectionchanged(); } public int getselectionindex() { return rowsel; } public int getitemheight() { return lineheight; } public void setitemheight(int height) { lineheight = height; } public int getitemcount() { return colors.size(); } public void add(int colorindex, string colorname) { colornames.add(colorname); colors.add(new integer(colorindex)); }
我们开发的控件的使用也是非常简单的。
customlist customlist = new customlist( parent, swt.v_scroll | swt.h_scroll ); customlist.add(swt.color_black,"black"); customlist.add(swt.color_blue,"blue"); customlist.setselection(1); customlist.setsize(400,400); customlist.setbackground(display.getdefault().getsystemcolor(swt.color_list_background));
以上我们介绍了如何开发一个简单的自定义控件所需要涉及的技术。这里我们只以一个简单的颜色控件为例,但是一旦我们掌握了方法,我们很容易就可以开发出各种不同的漂亮控件。
|
|