s1(java)项目实战(固定资产管理)(set_Asset_kinds_set )

来源:互联网 发布:淘宝logo设计理念 编辑:程序博客网 时间:2024/04/30 22:36

package set;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import tools.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class Asset_kinds_set extends JPanel {
    private JButton Asset_manager_set_modi = new JButton("修改");
    private JButton Asset_manager_set_add = new JButton("增加");
    private JButton Asset_manager_set_del = new JButton("删除");
    private TableModel tmd;
    private String user;
    private HashMap fathermap = new HashMap();
    private HashMap fathermaptemp = new HashMap();
    private HashMap childmap = new HashMap();
    private HashMap childmaptemp = new HashMap();
    private String kindlayout = "二级科目";
    private DBConnection dcon = null;
    private JButton btn_return = new JButton();
    private JScrollPane table_pan = new JScrollPane();
    private Mytable model;
    private JTable table;
    private String tempID = "";
    private JComboBox cbb_kinds_choice;
    public Asset_kinds_set(String _user) {
        try {
            user = _user;
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(500, 350));
        this.setLayout(null);
        String[] kindsType = {"二级科目", "一级科目"};
        cbb_kinds_choice = new JComboBox(kindsType);
        Asset_manager_set_modi.setBounds(new Rectangle(227, 37, 65, 32));
        Asset_manager_set_add.setBounds(new Rectangle(152, 38, 70, 31));
        Asset_manager_set_del.setBounds(new Rectangle(297, 36, 64, 32));
        btn_return.setBounds(new Rectangle(369, 37, 62, 31));
        btn_return.setToolTipText("");
        btn_return.setText("返回");
        table_pan.setBounds(new Rectangle(6, 84, 491, 265));
        cbb_kinds_choice.setBounds(new Rectangle(53, 38, 89, 31));
        this.add(table_pan);
        this.add(cbb_kinds_choice);
        this.add(Asset_manager_set_del);
        this.add(Asset_manager_set_modi);
        this.add(Asset_manager_set_add);
        this.add(btn_return);
        table.setFont(new Font("宋体", 1, 17));
        table.setRowSelectionAllowed(false);
        model = new Mytable(1);
        model.setTitle(getTitle(cbb_kinds_choice.getSelectedItem().toString().
                                trim()));
        model.setContent(getContents(cbb_kinds_choice.getSelectedItem().
                                     toString().trim()));
        table = new JTable(model);
        table.setRowSelectionAllowed(false);
        table_pan.getViewport().add(table);

        tmd = table.getModel();
        initfathermaptemp();
        initchildmaptemp();

        //表格行选择的变更
        table.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                if (table.getSelectedColumn() != -1 &&
                    table.getSelectedRow() != -1) {
                    tempID = table.getValueAt(table.getSelectedRow(), 0).
                             toString();
                } else {
                    tempID = "";
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
        cbb_kinds_choice.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                String typelist = cbb_kinds_choice.getSelectedItem().toString().
                                  trim();
                if (typelist.length() != 0) {
                    kindschanged(typelist);
                }
            }
        });
        btn_return.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeAsset_kinds_set();
            }
        });
        //表格的变动
        tmd.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                String table_id = table.getValueAt(table.getSelectedRow(), 0).
                                  toString().trim();
                String table_name = table.getValueAt(table.getSelectedRow(), 1).
                                    toString().trim();
                if (kindlayout.equals("二级科目")) { // update childmap
                    String temp_name = returnTemp(childmaptemp, table_id).trim();
                    if (!temp_name.equals(table_name)) {
                        if (childmap.containsKey(table_id)) {
                            Set set = childmap.entrySet();
                            Iterator it = set.iterator();
                            while (it.hasNext()) {
                                Map.Entry me = (Map.Entry) it.next();
                                if (me.getKey().toString().trim().equals(
                                        table_id)) {
                                    me.setValue(table_name);
                                    break;
                                }
                            }
                        } else {
                            childmap.put(table_id, table_name);
                        }
                        modichikdtempMap(table_id, table_name);
                    }
                } else { // update fathermap
                    String temp_name = returnTemp(fathermaptemp, table_id).trim();
                    if (!temp_name.equals(table_name)) {
                        if (fathermap.containsKey(table_id)) {
                            Set set = fathermap.entrySet();
                            Iterator it = set.iterator();
                            while (it.hasNext()) {
                                Map.Entry me = (Map.Entry) it.next();
                                if (me.getKey().toString().trim().equals(
                                        table_id)) {
                                    me.setValue(table_name);
                                }
                            }
                        } else {
                            fathermap.put(table_id, table_name);
                        }
                        modifathertempMap(table_id, table_name);
                    }
                }

            }
        });
        Asset_manager_set_modi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (fathermap.size() != 0) {
                    dcon = new DBConnection();
                    Set set = fathermap.entrySet();
                    Iterator it = set.iterator();
                    while (it.hasNext()) {
                        Map.Entry me = (Map.Entry) it.next();
                        String id = me.getKey().toString().trim();
                        String name = me.getValue().toString().trim();
                        String sql =
                                "update FatherKind set fatherkind_name = '" +
                                name + "' where fatherkind_id='" + id + "'";
                        JOptionPane.showMessageDialog(table_pan,
                                "一级科目:" + dcon.update(1, sql));
                        fathermap.clear();
                        fathermaptemp.clear();
                        initchildmaptemp();

                    }
                } else {
                    JOptionPane.showMessageDialog(table_pan, "一级科目没有修改");
                }
                if (childmap.size() != 0) {
                    dcon = new DBConnection();
                    Set set = childmap.entrySet();
                    Iterator it = set.iterator();
                    while (it.hasNext()) {
                        Map.Entry me = (Map.Entry) it.next();
                        String id = me.getKey().toString().trim();
                        String name = me.getValue().toString().trim();
                        String sql = "update ChildKind set childkind_name = '" +
                                     name + "' where childkind_id='" + id + "'";
                        JOptionPane.showMessageDialog(table_pan,
                                "二级科目:" + dcon.update(1, sql));
                    }
                    childmap.clear();
                    childmaptemp.clear();
                    initfathermaptemp();
                } else {
                    JOptionPane.showMessageDialog(table_pan, "二级科目没有修改");
                }
            }
        });
        Asset_manager_set_del.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (tempID.trim().length() == 0) {
                    JOptionPane.showMessageDialog(table_pan, "还没有选择该要操作的记录");
                } else {
                    if (kindlayout.trim().equals("二级科目")) {
                        dcon = new DBConnection();
                        String sql = "select * from Asset where childkind_id='" +
                                     tempID.trim() + "'";
                        if (dcon.isNull(sql)) {
                            JOptionPane.showMessageDialog(table_pan,
                                    "资产表有该类型的记录,不能删除");
                        } else {
                            sql = "delete from ChildKind where childkind_id='" +
                                  tempID.trim() + "'";
                            dcon.update(1, sql);
                            JOptionPane.showMessageDialog(table_pan,
                                    "ID是" + tempID + "被删除");
                        }
                    } else if (kindlayout.trim().equals("一级科目")) {
                        dcon = new DBConnection();
                        String sql =
                                "select * from ChildKind where childkind_id='" +
                                tempID.trim() + "'";
                        if (dcon.isNull(sql)) {
                            JOptionPane.showMessageDialog(table_pan,
                                    "下级科目有该类型的记录,不能删除");
                        } else {
                            sql =
                                    "delete from FatherKind where fatherkind_id='" +
                                    tempID.trim() + "'";
                            dcon.update(1, sql);
                            JOptionPane.showMessageDialog(table_pan,
                                    "ID是" + tempID + "被删除");
                        }

                    }
                }
                tempID = "";
            }
        });
    }


//科目选择后表格的变化
    private void kindschanged(String typelist) {
        table.remove(table);
        if (typelist.equals("一级科目")) {
            model = new Mytable(0);
        } else {
            model = new Mytable(1);
        }
        model.setTitle(getTitle(typelist));
        model.setContent(getContents(typelist));
        table = new JTable(model);
        table_pan.getViewport().add(table);
        table_pan.updateUI();
        kindlayout = typelist;
        tempID = "";
        table.setRowSelectionAllowed(false);
        table.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                if (table.getSelectedColumn() != -1 &&
                    table.getSelectedRow() != -1) {
                    tempID = table.getValueAt(table.getSelectedRow(), 0).
                             toString();
                }
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });

    }

//初始化一级科目的临时表
    private void initfathermaptemp() {
        dcon = new DBConnection();
        String sql =
                "select fatherkind_id as 科目编号 ,fatherkind_name as 科目名称 from FatherKind";
        Vector v = dcon.select(sql);
        for (int i = 0; i < v.size(); i++) {
            String id = ((Vector) v.get(i)).get(0).toString();
            String name = ((Vector) v.get(i)).get(1).toString();
            fathermaptemp.put(id, name);
        }
    }

//初始化二级科目的临时表
    private void initchildmaptemp() {
        dcon = new DBConnection();
        String sql = "select  childkind.childkind_id as 科目编号 ,childkind.childkind_name as 科目名称,fatherkind.fatherkind_name as 上级科目 from childkind,fatherkind where (childkind.fatherkind_id = fatherkind.fatherkind_id)";
        Vector v = dcon.select(sql);
        for (int i = 0; i < v.size(); i++) {
            String id = ((Vector) v.get(i)).get(0).toString();
            String name = ((Vector) v.get(i)).get(1).toString();
            childmaptemp.put(id, name);
        }
    }

//修改临时的一级科目的临时表
    private void modifathertempMap(String id, String m) {
        Set set = fathermaptemp.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            if (me.getKey().toString().equals(id)) {
                me.setValue(m);
                break;
            }
        }
    }

//修改临时的二级级科目的临时表
    private void modichikdtempMap(String id, String m) {
        Set set = childmaptemp.entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            if (me.getKey().toString().equals(id)) {
                me.setValue(m);
                break;
            }
        }
    }

//返回修改的
    private String returnTemp(HashMap hm, String id) {
        Set set = hm.entrySet();
        Iterator it = set.iterator();
        String s = "";
        while (it.hasNext()) {
            Map.Entry me = (Map.Entry) it.next();
            if (me.getKey().toString().equals(id)) {
                s = me.getValue().toString();
                break;
            }
        }
        return s;
    }

//获取表格的列表
    private String[] getTitle(String kindsType) {
        dcon = new DBConnection();
        String sql = "";
        if (kindsType.equals("一级科目")) {
            sql =
                    "select fatherkind_id as 科目编号 ,fatherkind_name as 科目名称 from FatherKind";
        } else {
            sql = "select  childkind.childkind_id as 科目编号 ,childkind.childkind_name as 科目名称,fatherkind.fatherkind_name as 上级科目 from childkind,fatherkind where (childkind.fatherkind_id = fatherkind.fatherkind_id)";
        }
        return dcon.getColumnname(sql);
    }

//获取表格的内容
    private Vector getContents(String kindsType) {
        dcon = new DBConnection();
        String sql;
        if (kindsType.equals("一级科目")) {
            sql =
                    "select fatherkind_id as 科目编号 ,fatherkind_name as 科目名称 from FatherKind";
        } else {
            sql = "select  childkind.childkind_id as 科目编号 ,childkind.childkind_name as 科目名称,fatherkind.fatherkind_name as 上级科目 from childkind,fatherkind where (childkind.fatherkind_id = fatherkind.fatherkind_id)";
        }
        return dcon.select(sql);
    }

//关闭Asset_manager_set
    private void closeAsset_kinds_set() {
        this.removeAll();
        Asset_OP asp = new Asset_OP(user);
        this.add(asp);
        this.updateUI();
    }
}
 

原创粉丝点击