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

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

 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 javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import tools.*;

public class Asset_kinds_set_child 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 kindMap = new HashMap();
    private HashMap childmap = new HashMap();
    private HashMap childmaptemp = new HashMap();
    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 = "";
    JComboBox cmb;
    public Asset_kinds_set_child(String _user) {
        try {
            this.user = _user;
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(500, 350));
        this.setLayout(null);
        String[] kindsType = new String[2];
        Asset_manager_set_modi.setBounds(new Rectangle(171, 37, 65, 32));
        Asset_manager_set_add.setBounds(new Rectangle(57, 35, 70, 31));
        Asset_manager_set_del.setBounds(new Rectangle(264, 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));
        this.add(table_pan);
        this.add(btn_return);
        this.add(Asset_manager_set_add);
        this.add(Asset_manager_set_modi);
        this.add(Asset_manager_set_del);
        model = new Mytable(0);
        model.setTitle(getTitle());
        model.setContent(getContents());
        table = new JTable(model);
        table.setFont(new Font("宋体", 1, 17));
        table.setRowSelectionAllowed(false);
        table_pan.getViewport().add(table);
        table.setRowSelectionAllowed(false);
        if (getKindList() != null) {
            JComboBox cmb = new JComboBox(getKindList());
            TableColumnModel tcm = table.getColumnModel();
            tcm.getColumn(2).setCellEditor(new DefaultCellEditor(cmb));
        }
        initchildmaptemp();
        tempID = "";
        tmd = table.getModel();
        //表格行选择的变更
        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) {
            }
        });
        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();
                String table_kindname = table.getValueAt(table.getSelectedRow(),
                        2).
                                        toString().trim();
                String temp_name = ((Fatherkinds) childmaptemp.get(table_id)).
                                   getName().trim();
                String temp_kindname = ((Fatherkinds) childmaptemp.get(table_id)).
                                       getKindName().trim();
                Fatherkinds fks = new Fatherkinds(table_name, table_kindname);
                if (!temp_name.equals(table_name) ||
                    !(table_kindname.equals(temp_kindname))) {
                    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(fks);
                                break;
                            }
                        }
                    } else {
                        childmap.put(table_id, fks);
                    }
                    modichildtempMap(table_id, fks);
                }
            }
        });
        Asset_manager_set_modi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                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 = ((Fatherkinds) me.getValue()).getName().
                                      trim();
                        String kindid = getID(((Fatherkinds) me.getValue()).
                                              getKindName().trim());
                        String sql = "update ChildKind set childkind_name = '" +
                                     name + "',fatherkind_id='" + kindid +
                                     "' where childkind_id='" + id + "'";
                        dcon.update(1, sql);
                    }
                    JOptionPane.showMessageDialog(table_pan,
                                                  "修改了" + childmap.size() +
                                                  "条记录");
                    childmap.clear();
                    childmaptemp.clear();
                    initchildmaptemp();
                }
            }
        });
        Asset_manager_set_del.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (tempID.trim().length() == 0) {
                    JOptionPane.showMessageDialog(table_pan, "还没有选择要操作的记录");
                } else {
                    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 + "被删除");
                        model.setContent(getContents());
                        table.updateUI();
                        childmap.clear();
                        childmaptemp.clear();
                        initchildmaptemp();

                    }

                }
                tempID = "";
            }
        });
        Asset_manager_set_add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                add();
            }
        });

    }

//add
    private void add() {
        dcon = new DBConnection();
        String sql = "select * from FatherKind";
        if (dcon.isNull(sql)) {
            childkindsAdd cka = new childkindsAdd(user);
            Dimension frmsize = getSize();
            Point loc = getLocation();
            cka.setLocation((frmsize.width - cka.WIDTH) / 2 + loc.x,
                            (frmsize.height - cka.HEIGHT) / 2 + loc.y);
            cka.setSize(250, 250);
            cka.setModal(true);
            cka.setVisible(true);
            childmap.clear();
            childmaptemp.clear();
            initchildmaptemp();

            model.setContent(getContents());
            table.updateUI();
            this.updateUI();
        } else {
            JOptionPane.showMessageDialog(this, "上级科目为空");
        }
    }

//初始化一级科目的临时表
    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();
            String kindname = ((Vector) v.get(i)).get(2).toString();
            Fatherkinds fk = new Fatherkinds(name, kindname);
            childmaptemp.put(id, fk);
        }
    }

//修改临时的一级科目的临时表
    private void modichildtempMap(String id, Fatherkinds 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;
            }
        }
    }

//get kindList
    private String[] getKindList() {
        dcon = new DBConnection();
        String sql = "select fatherkind_id,fatherkind_name from FatherKind";
        Vector v = dcon.select(sql);
        String[] slist = new String[v.size()];
        for (int i = 0; i < v.size(); i++) {
            String id = ((Vector) v.get(i)).get(0).toString().trim();
            String name = ((Vector) v.get(i)).get(1).toString().trim();
            slist[i] = name;
            kindMap.put(name, id);
        }
        return slist;
    }

    //get id
    private String getID(String name) {
        return kindMap.get(name).toString().trim();
    }

//获取表格的列表
    private String[] getTitle() {
        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)";
        return dcon.getColumnname(sql);
    }

//获取表格的内容
    private Vector getContents() {
        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)";
        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();
    }
}

原创粉丝点击