类库开刀(二)
SWT的TABLE对象的ToolTip一步实现自动折行
(不用每个TABLE实例都修改)
第一章 概要
最近的一个项目,涉及到离线办公需求,索性弄了一个CS客户端满足这个需求。不过用户在CS已经成型的时候竟然又追加了一个需求:每个表格的单元格的ToolTip可以自动折行,大家知道SWT的TABLE的ToolTip是不可以直接自动折行的。到网上搜索了一下有没有可以简单的、比用修改每个列表的一步解决方法,我比较懒总是喜欢一步到位。项目到了收尾阶段还要全盘修改一些代码,简直就是测试人员的噩梦,开发人员的悲剧。
网上的解决办法是,table实例加上监听鼠标悬停事件,我们这个CS一共有300多个表格,如果一个一个加起来。。。苦苦寻觅了几个小时仍旧是无果。
我觉得加监听不是正道,因为Table已经实现了ToolTip的表示,只是表示在一行而已,所以我认为一定会有简单的方法,让ToolTip表示的时候自动折行!索性给SWT的TABLE.CLASS开刀!
我的目标是,修改一个类库文件,就可以实现300多表格的ToolTip可自动折行。
第二章 修改步骤
第一节 锁定目标
经过验证,还是将监听加入到:org\eclipse\swt\widgets\Table.java中比较好。
这个源文件在\plugins\org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar中。
第二节 创建等位目录
我们需要在项目工程的src目录下创建与org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar这个包相同的路径,才可以实现Debug调试我们所修改的内容是否可以成功。目录结构下图所示,仅供参考。
第三节 创建Table.java文件
在widgets目录创建Table.java文件,文件代码直接j将org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar的Table.class反编译后的代码拷贝即可。
第四节 开始动刀
一.修改文件第一处
找到如下方法:
LRESULT wmNotifyToolTip (NMHDR hdr, int /*long*/ wParam, int /*long*/ lParam)
的如下内容:在6776行(Eclipse每个版本可能行数会不同):
- <span>int width = toolRect.right - toolRect.left, height = toolRect.bottom - toolRect.top;
- SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);
- </span>
int width = toolRect.right - toolRect.left, height = toolRect.bottom - toolRect.top;SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);
将上面内容替换:
- <span>// CONAC 曲文博 添加 开始
- int haba = 288;
- if (toolRect.right - toolRect.left >haba) {
- int length = toolRect.right - toolRect.left;
- int width = 298, height = (Math.round(length/haba) + 1) * 18;
- SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);
- } else {
- int width = toolRect.right - toolRect.left, height = toolRect.bottom - toolRect.top;
- SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);
- }
- // CONAC 曲文博 添加 结束</span>
// CONAC 曲文博 添加 开始int haba = 288;if (toolRect.right - toolRect.left >haba) {int length = toolRect.right - toolRect.left;int width = 298, height = (Math.round(length/haba) + 1) * 18;SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);} else {int width = toolRect.right - toolRect.left, height = toolRect.bottom - toolRect.top;SetWindowPos (hwndToolTip, 0, toolRect.left , toolRect.top, width, height, flags);}// CONAC 曲文博 添加 结束
二.修改文件第二处
找到如下方法:
LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, int /*long*/ lParam)
的如下内容:在6898行(Eclipse每个版本可能行数会不同):
int flags = OS.DT_NOPREFIX | OS.DT_SINGLELINE | OS.DT_VCENTER;
将上面内容替换:
int flags = OS.DT_NOPREFIX | OS.DT_WORDBREAK | OS.DT_VCENTER;
三.修改文件第三处
找到如下方法:
LRESULT wmNotifyToolTip (NMTTCUSTOMDRAW nmcd, int /*long*/ lParam)
在6909行添加如下内容:
- <span>// CONAC 曲文博 添加 开始
- int haba = 288;
- if (textRect.right - textRect.left >haba) {
- int length = textRect.right - textRect.left;
- textRect.right = haba + textRect.left;
- textRect.bottom = textRect.top + (Math.round(length/haba)+1)*18;
- }
- // CONAC 曲文博 添加 结束</span>
// CONAC 曲文博 添加 开始int haba = 288;if (textRect.right - textRect.left >haba) {int length = textRect.right - textRect.left;textRect.right = haba + textRect.left;textRect.bottom = textRect.top + (Math.round(length/haba)+1)*18;}// CONAC 曲文博 添加 结束
第三章 编译
到此Table.java文件修改结束,那么在工程内编译一下,会得到如下两个文件:
Table$1.class、Table.class
将这两个文件直接拷贝到org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar中对应的目录下覆盖即可,此时Eclipse应该关闭。
第四章 总结
重启Eclipse,启动工程后,我们发现所有的Table都实现了那个监听,达到了目的。只要鼠标点击单元格一下,内容就可以被复制到剪贴板中,也省去了用户的复制操作。
如有不足希望大家指正。
第五章 文件下载
一、Table.java
二、Table$1.class、Table.class
三、org.eclipse.swt.win32.win32.x86_3.4.1.v3452b.jar
这个文件的版本有点低,呵呵,我们做的CS系统主要用的是RCP所以就定在了比较低的版本上,如果无特殊需要可以不必下载 http://download.csdn.net/detail/jingyinghai/4673813