openfire + spark 展現組織機構(客戶端)

在spark 加一個插件用於展現組織機構, 參考了好多人的代碼 



插件主類增長一個 TAB用於展現機構樹

java

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過來數據展現node

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

}
相關文章
相關標籤/搜索