您的位置:首页 > 其它

开发总结:基于SWT保存两树型表的关联关系

2006-08-17 21:51 239 查看
开发总结:基于SWT保存两树型表的关联关系

开发步骤总体分为三部分:
一 SWT界面设计及事件流程处理
1通过SWT Designer 插件,对界面进行可视化设计;

2 根据功能要求和业务要求对界面上可能产生的事件进行分析和统计,合理分配和协调各个事件之间的处理时间;例如原先把对树的选择值保存放在了左边树的单击事件,使得点击左边时处理过慢,后来将树的选择值保存放在了右边树的单击事件,处理速度明显提高。
左边树的单击事件代码:
//发生选项改变时,即刻触发此事件。
orgTree.addListener(SWT.Selection, new Listener() {
public void handleEvent(final Event event) {
int treeCount = subjectTree.getItemCount();
currOrgNode = event.item.getData(); // 获得左边选中的对象。
// 清空上次选择的复选框
// 第一次点击时,不用执行,因为树初始化时没有被选上。即oldlist为null
if (curlist != null) {
for (int i = 0; i < treeCount; i++) {
SWTNativeUtil.setState(subjectTree.getItem(i), curlist,
false);
}
}
// 取出已经选择的关系集合
curlist = (List) hashMap.get(currOrgNode);
//System.out.println("curlist==" + curlist);
// 恢复选择的复选框
if (curlist != null) {
for (int i = 0; i < treeCount; i++) {
SWTNativeUtil.setState(subjectTree.getItem(i), curlist,
true);
}
}
}
});
右边树的单击事件代码:
/*
* 此事件为先在树上选上项,然后再把所有选上的值保存在一个集合里。
*/
subjectTree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if (event.detail == SWT.CHECK) {
TreeItem item = (TreeItem) event.item;
SWTNativeUtil.doCheck(item, item.getChecked());
if (curlist == null)
curlist = new ArrayList();
if (curlist != null)
curlist.clear();
int treeCount = subjectTree.getItemCount();
// 递归取出子树item已经选择上的记录
for (int i = 0; i < treeCount; i++) {
SWTNativeUtil.getItemsNative(subjectTree.getItem(i),
curlist);
}
System.out.println("currOrgNode==" + currOrgNode);
hashMap.put(currOrgNode, curlist);
}
}
});
基于SWT控件Tree树型显示及算法,及针对树型VO的设计。
1 显示算法:
//构建层次树,相同的层保存在同一个List里。
public static List sortSubjList(List data) {
Map map = new HashMap();
SubjVO root = new SubjVO();
map.put("0", root);//映射根节点
Iterator iter1 = data.iterator();
while (iter1.hasNext()) {
SubjVO vo = (SubjVO) iter1.next();
map.put(vo.getFid(), vo);//映射ID 与 VO对象的关系
}
Iterator iter2 = data.iterator(); //因为上面有iter1.next();会移动位置,所以这里要重头开始
while ( iter2.hasNext()) {
SubjVO node = (SubjVO) iter2.next();
SubjVO parentNode = (SubjVO) map.get(node.getFparentid());
if (parentNode != null)
parentNode.getChildren().add(node);
}
return root.getChildren();
}

//递归创建TreeItem.
public static void createChildItem(TreeItem item,SubjVO node){
for (Iterator iter = node.getChildren().iterator(); iter.hasNext();) {
SubjVO childNode = (SubjVO) iter.next();
TreeItem childitem = new TreeItem(item,SWT.NONE);
childitem.setText(childNode.getFname());
childitem.setData(childNode.getFid());

createChildItem(childitem, childNode);

}
}

2 处理选择值的算法
//递归选择item下面的子item;
public static void doCheck(TreeItem item, boolean ischecked) {
item.setChecked(ischecked);
for (int i = 0; i < item.getItemCount(); i++) {
TreeItem item1 = item.getItem(i);
doCheck(item1, ischecked);
}
}

// 递归取出item 已选择上的记录
public static void getItemsNative(TreeItem item, List list) {
//如果选上,则放到集合里
if (item.getChecked()) {
list.add(item.getData());
}
int count = item.getItemCount();
for (int i = 0; i < count; i++) {
TreeItem item1 = item.getItem(i);
getItemsNative(item1, list);
}
}

// 此方法有两个作用:一种用于清空选择上的记录(state为false),一种用于选上已经临时保存在list的记录(state为tree)
public static void setState(TreeItem item, List list, boolean state) {
if(list!=null && item!=null){
if (list.contains(item.getData())) {
item.setChecked(state);
}
int count = item.getItemCount();
for (int i = 0; i < count; i++) {
TreeItem item1 = item.getItem(i);
setState(item1, list, state);
}
}
}

3 树型VO的设计:
根据算法及构成树的基本条件,至少包含以下属性:
private String name; // 支出名称
private String id; // id
private String parentid; // parentid
private List children = new ArrayList();//保存下一层子的节点

通过在HTTP请求对象流中传输数据,实现显示层与数据库之间的数据转换。
以下为读取或写入数据对象的通用工具类。
public class DataViewUtil {
public static String httpUrl;
/*
* 把对象转换成字节数组,保存在上下文的流中。
*/
public static byte[] writeObject(Object object) throws SQLException {
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] ab = baos.toByteArray();
return ab;
} catch (IOException ex) {
throw new SQLException("convert object to byte array error: "
+ ex.getMessage());
} finally {
try {
if (oos != null)
oos.close();
if (baos != null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 在上下文中的流中读取对象~~~
*/
public static Object readObject(InputStream inputStream) throws Exception {
Object o = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(inputStream);
o = ois.readObject();
} catch (Exception ex) {
ex.printStackTrace();
// throw new SQLException("convert stream to object error: "
// + ex.getMessage());
} finally {
if (ois != null)
ois.close();
}
return o;
}
//获得请求链接 ~~
protected URLConnection getCon(String method) throws Exception {
java.net.URL url = new java.net.URL(httpUrl + method);
java.net.URLConnection con = url.openConnection();
con.setUseCaches(true);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-type", "application/octest-stream");
return con;
}
/*
* 根据不同的method,从流中读取或写入数据对象。
*/
public Object parseObject(Object obj1, String method) {
Object obj2 = null;
URLConnection con = null;
DataOutputStream dataout = null;
byte[] buf = null;
try {
con = getCon(method);
} catch (Exception e) {
e.printStackTrace();
}
/* 写入数据 */
if (obj1 != null) {
try {
dataout = new DataOutputStream(con.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
buf = writeObject(obj1); //转换成字节数组
} catch (SQLException e) {
e.printStackTrace();
}
try {
dataout.write(buf); //写入流中
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dataout != null) {
dataout.flush();
dataout.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* 读取数据 */
DataInputStream in = null;
try {
in = new DataInputStream(con.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
try {
obj2 = readObject(in);//从流中读取对象~~~
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return obj2;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐