zoukankan      html  css  js  c++  java
  • 做组织机构树状图 spark

    很简单 基本上就是复制粘贴 再做两个图片 OK了

    摘自: http://hi.baidu.com/awol2005ex/item/8c673626e8ff493194f62bd0

    然后在spark 加一个插件用于展示组织机构, 参考了好多人的代码 

    插件主类增加一个 TAB用于展示机构树

    package  com.salesoa.orgtree; import java.net.URL;
    //展示OA的组织结构 public class OrgTreePlugin implements Plugin{
        private static ImageIcon  organ_icon=null;          public static ImageIcon    getOrganIcon(){//机构图标         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(){//机构图标         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();//机构树
                              // Add own Tab.         tabbedPane.addTab("组织架构",OrgTreePlugin.getOrganIcon(),orgTreePanel);     }
        @Override     public void shutdown() {         // TODO Auto-generated method stub
        }
        // 是否可关闭     @Override     public boolean canShutDown() {         // TODO Auto-generated method stub         return false;     }
        // 卸载     @Override     public void uninstall() {         // TODO Auto-generated method stub
        }
    }
    机构树类 ,根据openfire插件http过来数据展示 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;
    //组织机构树 public class OrgTree extends JPanel {     /**      *       */     private static final long serialVersionUID = 5238403584089521528L;
        private final Tree orgTree;// 机构树控件     private final OrgTreeNode rootNode = new OrgTreeNode("组织");// 根节点     private final DefaultTreeModel treeModel;// 树模型
        // 初始化机构树     public OrgTree() {
            // 根节点         rootNode.setUnitid(null);         rootNode.setSuperunitid1(null);         rootNode.setVisited(false);         rootNode.setAllowsChildren(true);// 允许有子节点         rootNode.setIcon(OrgTreePlugin.getOrganIcon());// 图标
            // 机构树         orgTree = new Tree(rootNode);         orgTree.setShowsRootHandles(true); // 显示根结点左边的控制手柄         orgTree.collapseRow(0); // 初始时只显示根结点         orgTree.setCellRenderer(new OrgTreeCellRenderer());         // 覆盖树展开事件,进行异步加载         orgTree.addTreeExpansionListener(new TreeExpansionListener() {             @Override             public void treeExpanded(TreeExpansionEvent event) {                 // 首先获取展开的结点                 final OrgTreeNode expandNode = (OrgTreeNode) event.getPath()                         .getLastPathComponent();
                    // 判断该节点是否已经被访问过                 // 是——无需到数据库中读取、什么事也不做                 // 否——开始异步加载                 if (!expandNode.getVisited()) {                     expandNode.setVisited(true); // 先改变visited字段的状态                     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()); // 加载完毕后要删除“载入中...”结点                             treeModel.nodeStructureChanged(expandNode); // 通知视图结构改变
                                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));// 设置边界
            add(panel, BorderLayout.CENTER);
            rootNode.add(new OrgTreeNode("加载中..."));// 用于显示正在加载     }
        // 从数据表中读取expandNode的子结点.返回值为处理时长     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);// 机构                     } else if ("staff".equals(node.getType())) {                         node.setAllowsChildren(false);// 人员                         if (u.containsKey("loginid")) {                             node.setLoginid(u.getString("loginid"));// 登陆账号                         }                     }
                        node.setVisited(false);                     node.setAllowsChildren(true);// 允许有子节点                     if ("unit".equals(node.getType())) {// 机构                         node.setIcon(OrgTreePlugin.getOrganIcon());// 图标                     } else if ("staff".equals(node.getType())) {// 人员                         node.setIcon(OrgTreePlugin.getUserIcon());// 图标                     }                     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) {// 取得返回组织架构         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) {// 取得返回组织架构的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();     } }

     

    有时间继续研究与联系人 ,对话之类结合的问题

     

     

    -----------------------补充

    //机构节点

    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);

     

     

            //设置图标

            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;

        }

     

        //取得图标

        private Icon getCustomIcon() {

            if (value instanceof JiveTreeNode) {

                JiveTreeNode node = (JiveTreeNode)value;

                return node.getClosedIcon();

            }

            return null;

        }

     

        //关闭的图标

        public Icon getClosedIcon() {

            return getCustomIcon();

        }

     

        public Icon getDefaultClosedIcon() {

            return getCustomIcon();

        }

     

        //叶子的图标

        public Icon getDefaultLeafIcon() {

            return getCustomIcon();

        }

     

        public Icon getDefaultOpenIcon() {

            return getCustomIcon();

        }

     

        public Icon getLeafIcon() {

            return getCustomIcon();

        }

     

        //打开的图标

        public Icon getOpenIcon() {

            return getCustomIcon();

        }

     

    }

     

     

    //机构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 ;//机构ID

    private String unitname ; //机构名称

    private String superunitid1; //上一级机构名称

    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;

    }

     

     

    }

     

     

    I'm falling off the sky all alone.The courage inside is gonna break the fall. Nothing can dim my light within. I am That I am 程序 = 数据结构 + 算法
  • 相关阅读:
    算法笔记codeup-Contest100000568
    算法笔记codeup-Contest100000567
    算法笔记codeup-Contest100000566
    人工智能各种知识点(大杂烩)
    人工智能基础知识复习:机器学习
    人工智能基础知识复习:神经计算 演化计算 模糊计算
    人工智能基础知识复习:问题求解与搜索
    人工智能基础知识复习:推理技术
    前端基础知识学习:概念篇
    MYSQL
  • 原文地址:https://www.cnblogs.com/IamThat/p/2948540.html
Copyright © 2011-2022 走看看