做组织机构树状图 spark
?
很簡單 基本上就是復(fù)制粘貼 再做兩個圖片 OK了
摘自: http://hi.baidu.com/awol2005ex/item/8c673626e8ff493194f62bd0
?
然后在spark?加一個插件用于展示組織機構(gòu),?參考了好多人的代碼?
插件主類增加一個?TAB用于展示機構(gòu)樹
package??com.salesoa.orgtree; import?java.net.URL;
//展示OA的組織結(jié)構(gòu) public?class?OrgTreePlugin?implements?Plugin{
????private?static?ImageIcon??organ_icon=null; ???? ????public?static?ImageIcon????getOrganIcon(){//機構(gòu)圖標(biāo) ????????if(organ_icon==null){ ????????????ClassLoader??cl=OrgTreePlugin.class.getClassLoader(); ????????????URL???imageURL=cl.getResource("images/organ.gif"); ????????????organ_icon=new???ImageIcon(imageURL); ????????} ????????return??organ_icon; ????} ???? ????private?static?ImageIcon??user_icon=null; ???? ????public??????????static????ImageIcon?????getUserIcon(){//機構(gòu)圖標(biāo) ????????if(user_icon==null){ ????????????ClassLoader????cl=OrgTreePlugin.class.getClassLoader(); ????????????URL??imageURL=cl.getResource("images/user.gif"); ????????????user_icon=new???ImageIcon(imageURL); ????????} ????????return?user_icon; ????} ???? ????@Override ????public?void?initialize()?{ ????????//?TODO?Auto-generated?method?stub ????????Workspace??workspace=SparkManager.getWorkspace();
????????SparkTabbedPane???tabbedPane=workspace.getWorkspacePane();
????????OrgTree???orgTreePanel=newOrgTree();//機構(gòu)樹
???????? ???????? ????????//?Add?own?Tab. ????????tabbedPane.addTab("組織架構(gòu)",OrgTreePlugin.getOrganIcon(),orgTreePanel); ????}
????@Override ????public?void?shutdown()?{ ????????//?TODO?Auto-generated?method?stub
????}
????//?是否可關(guān)閉 ????@Override ????public?boolean?canShutDown()?{ ????????//?TODO?Auto-generated?method?stub ????????return?false; ????}
????//?卸載 ????@Override ????public?void?uninstall()?{ ????????//?TODO?Auto-generated?method?stub
????}
}
機構(gòu)樹類?,根據(jù)openfire插件http過來數(shù)據(jù)展示 package?com.salesoa.orgtree;
import?java.awt.BorderLayout; import?java.awt.Color; import?java.awt.GridBagConstraints; import?java.awt.GridBagLayout; import?java.awt.Insets; import?java.io.IOException;
import?javax.swing.BorderFactory; import?javax.swing.JPanel; import?javax.swing.JScrollPane; import?javax.swing.SwingWorker; import?javax.swing.event.TreeExpansionEvent; import?javax.swing.event.TreeExpansionListener; import?javax.swing.tree.DefaultTreeModel;
import?net.sf.json.JSONArray; import?net.sf.json.JSONObject;
import?org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import?org.apache.commons.httpclient.HttpClient; import?org.apache.commons.httpclient.HttpException; import?org.apache.commons.httpclient.methods.GetMethod; import?org.apache.commons.httpclient.params.HttpMethodParams; import?org.jivesoftware.spark.SparkManager; import?org.jivesoftware.spark.component.Tree; import?org.jivesoftware.spark.util.log.Log;
//組織機構(gòu)樹 public?class?OrgTree?extends?JPanel?{ ????/** ?????*? ?????*/ ????private?static?final?long?serialVersionUID?=?5238403584089521528L;
????private?final?Tree?orgTree;//?機構(gòu)樹控件 ????private?final?OrgTreeNode?rootNode?=?new?OrgTreeNode("組織");//?根節(jié)點 ????private?final?DefaultTreeModel?treeModel;//?樹模型
????//?初始化機構(gòu)樹 ????public?OrgTree()?{
????????//?根節(jié)點 ????????rootNode.setUnitid(null); ????????rootNode.setSuperunitid1(null); ????????rootNode.setVisited(false); ????????rootNode.setAllowsChildren(true);//?允許有子節(jié)點 ????????rootNode.setIcon(OrgTreePlugin.getOrganIcon());//?圖標(biāo)
????????//?機構(gòu)樹 ????????orgTree?=?new?Tree(rootNode); ????????orgTree.setShowsRootHandles(true);?//?顯示根結(jié)點左邊的控制手柄 ????????orgTree.collapseRow(0);?//?初始時只顯示根結(jié)點 ????????orgTree.setCellRenderer(new?OrgTreeCellRenderer()); ????????//?覆蓋樹展開事件,進(jìn)行異步加載 ????????orgTree.addTreeExpansionListener(new?TreeExpansionListener()?{ ????????????@Override ????????????public?void?treeExpanded(TreeExpansionEvent?event)?{ ????????????????//?首先獲取展開的結(jié)點 ????????????????final?OrgTreeNode?expandNode?=?(OrgTreeNode)?event.getPath() ????????????????????????.getLastPathComponent();
????????????????//?判斷該節(jié)點是否已經(jīng)被訪問過 ????????????????//?是——無需到數(shù)據(jù)庫中讀取、什么事也不做 ????????????????//?否——開始異步加載 ????????????????if?(!expandNode.getVisited())?{ ????????????????????expandNode.setVisited(true);?//?先改變visited字段的狀態(tài) ????????????????????orgTree.setEnabled(false);?//?暫時禁用JTree
????????????????????//?使用swingworker框架 ????????????????????new?SwingWorker<Long,?Void>()?{ ????????????????????????@Override ????????????????????????protected?Long?doInBackground()?throws?Exception?{ ????????????????????????????return?asynchLoad(expandNode); ????????????????????????}
????????????????????????@Override ????????????????????????protected?void?done()?{ ????????????????????????????treeModel.removeNodeFromParent(expandNode ????????????????????????????????????.getFirstLeaf());?//?加載完畢后要刪除“載入中...”結(jié)點 ????????????????????????????treeModel.nodeStructureChanged(expandNode);?//?通知視圖結(jié)構(gòu)改變
????????????????????????????orgTree.setEnabled(true);//重新啟用JTree ????????????????????????} ????????????????????}.execute();
????????????????} ????????????}
????????????@Override ????????????public?void?treeCollapsed(TreeExpansionEvent?event)?{ ????????????} ????????});
????????treeModel?=?(DefaultTreeModel)?orgTree.getModel();//?樹模型
????????//排版 ????????setLayout(new?BorderLayout());
????????final?JPanel?panel?=?new?JPanel(); ????????panel.setLayout(new?GridBagLayout()); ????????panel.setBackground(Color.white);
????????final?JScrollPane?treeScroller?=?new?JScrollPane(orgTree);//?滾動條 ????????treeScroller.setBorder(BorderFactory.createEmptyBorder()); ????????panel.add(treeScroller,?new?GridBagConstraints(0,?1,?1,?1,?1.0,?1.0, ????????????????GridBagConstraints.WEST,?GridBagConstraints.BOTH,?new?Insets(5, ????????????????????????5,?5,?5),?0,?0));//?設(shè)置邊界
????????add(panel,?BorderLayout.CENTER);
????????rootNode.add(new?OrgTreeNode("加載中..."));//?用于顯示正在加載 ????}
????//?從數(shù)據(jù)表中讀取expandNode的子結(jié)點.返回值為處理時長 ????private?long?asynchLoad(OrgTreeNode?expandNode)?{ ????????long?handleTime?=?0L;?//?本次異步加載的處理時長 ????????long?start?=?System.currentTimeMillis();?//?開始處理的時刻 ????????try?{ ????????????Thread.sleep(1000);?//?sleep一段時間以便看清楚整個過程
????????????JSONArray?childJSON?=?this.getOrgTreeJSON(expandNode.getUnitid()); ????????????if?(childJSON?!=?null?&&?childJSON.size()?>?0)?{
????????????????for?(int?i?=?0,?s?=?childJSON.size();?i?<?s;?i++)?{ ????????????????????JSONObject?u?=?childJSON.getJSONObject(i); ????????????????????OrgTreeNode?node?=?new?OrgTreeNode(u.getString("unitname")); ????????????????????node.setUnitid(u.getString("unitid")); ????????????????????if?(u.containsKey("superunitid1"))?{ ????????????????????????node.setSuperunitid1(u.getString("superunitid1")); ????????????????????} ????????????????????node.setType(u.getString("type")); ????????????????????if?("unit".equals(node.getType()))?{ ????????????????????????node.setAllowsChildren(true);//?機構(gòu) ????????????????????}?else?if?("staff".equals(node.getType()))?{ ????????????????????????node.setAllowsChildren(false);//?人員 ????????????????????????if?(u.containsKey("loginid"))?{ ????????????????????????????node.setLoginid(u.getString("loginid"));//?登陸賬號 ????????????????????????} ????????????????????}
????????????????????node.setVisited(false); ????????????????????node.setAllowsChildren(true);//?允許有子節(jié)點 ????????????????????if?("unit".equals(node.getType()))?{//?機構(gòu) ????????????????????????node.setIcon(OrgTreePlugin.getOrganIcon());//?圖標(biāo) ????????????????????}?else?if?("staff".equals(node.getType()))?{//?人員 ????????????????????????node.setIcon(OrgTreePlugin.getUserIcon());//?圖標(biāo) ????????????????????} ????????????????????expandNode.add(node); ????????????????????if?("unit".equals(node.getType()))?{ ????????????????????????node.add(new?OrgTreeNode("加載中..."));//?用于顯示正在加載 ????????????????????} ????????????????} ????????????}
????????}?catch?(Exception?ex)?{ ????????????Log.error("",?ex); ????????}?finally?{ ????????????handleTime?=?System.currentTimeMillis()?-?start;?//?計算出處理時長 ????????} ????????return?handleTime;
????}
????private?JSONArray?getOrgTreeJSON(String?unitid)?{//?取得返回組織架構(gòu) ????????JSONArray?result?=?new?JSONArray(); ????????HttpClient?httpClient?=?new?HttpClient(); ????????GetMethod?getMethod?=?new?GetMethod(this.getOrgUrl(unitid)); ????????getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, ????????????????new?DefaultHttpMethodRetryHandler()); ????????try?{ ????????????httpClient.executeMethod(getMethod);
????????????byte[]?responseBody?=?getMethod.getResponseBody(); ????????????String?responseMsg?=?new?String(responseBody,?"GBK"); ????????????result?=?JSONArray.fromObject(responseMsg); ????????}?catch?(HttpException?e)?{ ????????????//?TODO?Auto-generated?catch?block ????????????Log.error("",?e); ????????}?catch?(IOException?e)?{ ????????????//?TODO?Auto-generated?catch?block ????????????Log.error("",?e); ????????}?finally?{
????????????getMethod.releaseConnection(); ????????}
????????return?result; ????}
????private?String?getOrgUrl(String?unitid)?{//?取得返回組織架構(gòu)的url ????????String?host?=?SparkManager.getConnection().getHost(); ????????StringBuffer?url?=?new?StringBuffer("http://"); ????????url.append(host); ????????url.append(":9090/plugins/orgtree/orgtreeservlet"); ????????if?(unitid?!=?null)?{ ????????????url.append("?unitid="); ????????????url.append(unitid); ????????} ????????return?url.toString(); ????} }
?
有時間繼續(xù)研究與聯(lián)系人?,對話之類結(jié)合的問題
?
?
-----------------------補充
//機構(gòu)節(jié)點
public?class?OrgTreeCellRenderer?extends?DefaultTreeCellRenderer?{
?
/**
*?
*/
private?static?final?long?serialVersionUID?=?1759820655239259659L;
private?Object?value;
?
????/**
?????*?Empty?Constructor.
?????*/
????public?OrgTreeCellRenderer()?{
????}
?
????public?Component?getTreeCellRendererComponent(JTree?tree,?Object?value,?boolean?selected,?boolean?expanded,?boolean?leaf,?int?row,?boolean?hasFocus)?{
????????this.value?=?value;
?
????????final?Component?c?=?super.getTreeCellRendererComponent(tree,?value,?selected,?expanded,?leaf,?row,?hasFocus);
?
?
????????//設(shè)置圖標(biāo)
????????setIcon(getCustomIcon());
?
????????//用中文字體解決亂碼問題
????????//?Root?Nodes?are?always?bold
????????JiveTreeNode?node?=?(JiveTreeNode)value;
????????if?(node.getAllowsChildren())?{
????????????setFont(new?Font("宋體",?Font.BOLD,?13));
????????}
????????else?{
????????????setFont(new?Font("宋體",?Font.PLAIN,?13));
????????}
?
?
????????return?c;
????}
?
????//取得圖標(biāo)
????private?Icon?getCustomIcon()?{
????????if?(value?instanceof?JiveTreeNode)?{
????????????JiveTreeNode?node?=?(JiveTreeNode)value;
????????????return?node.getClosedIcon();
????????}
????????return?null;
????}
?
????//關(guān)閉的圖標(biāo)
????public?Icon?getClosedIcon()?{
????????return?getCustomIcon();
????}
?
????public?Icon?getDefaultClosedIcon()?{
????????return?getCustomIcon();
????}
?
????//葉子的圖標(biāo)
????public?Icon?getDefaultLeafIcon()?{
????????return?getCustomIcon();
????}
?
????public?Icon?getDefaultOpenIcon()?{
????????return?getCustomIcon();
????}
?
????public?Icon?getLeafIcon()?{
????????return?getCustomIcon();
????}
?
????//打開的圖標(biāo)
????public?Icon?getOpenIcon()?{
????????return?getCustomIcon();
????}
?
}
?
?
//機構(gòu)Node
public?class?OrgTreeNode?extends?JiveTreeNode?implements?java.io.Serializable{
?
public?OrgTreeNode(String?unitname)?{
super(unitname);
}
?
/**
*?
*/
private?static?final?long?serialVersionUID?=?-5358854185627562145L;?
private?String?unitid?;//機構(gòu)ID
private?String?unitname?;?//機構(gòu)名稱
private?String?superunitid1;?//上一級機構(gòu)名稱
private?Boolean?visited;//是否已訪問
private?String?type;?//類型
private?String?loginid;//登陸賬號
public?String?getUnitid()?{
return?unitid;
}
?
public?void?setUnitid(String?unitid)?{
this.unitid?=?unitid;
}
?
public?String?getUnitname()?{
return?unitname;
}
?
public?void?setUnitname(String?unitname)?{
this.unitname?=?unitname;
}
?
public?String?getSuperunitid1()?{
return?superunitid1;
}
?
public?void?setSuperunitid1(String?superunitid1)?{
this.superunitid1?=?superunitid1;
}
?
public?Boolean?getVisited()?{
return?visited;
}
?
public?void?setVisited(Boolean?visited)?{
this.visited?=?visited;
}
?
public?String?getType()?{
return?type;
}
?
public?void?setType(String?type)?{
this.type?=?type;
}
?
public?String?getLoginid()?{
return?loginid;
}
?
public?void?setLoginid(String?loginid)?{
this.loginid?=?loginid;
}
?
?
}
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/IamThat/archive/2013/03/07/2948540.html
總結(jié)
以上是生活随笔為你收集整理的做组织机构树状图 spark的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux几个常用的环境变量配置文件
- 下一篇: Evernote 印象笔记赞一个(非广告