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.table.*;import javax.swing.event.*;
public class Asset_employe_set extends JPanel { private JButton btn_add = new JButton("增加"); private JButton btn_del = new JButton("删除"); private JButton btn_modi = new JButton(" 修改"); private JButton btn_return = new JButton(); private JScrollPane jScrollPane1 = new JScrollPane(); private JTable table; private Mytable model; private DBConnection dcon = null; private HashMap empMap = new HashMap(); private HashMap empMaptemp = new HashMap(); private String tempID = ""; private TableModel tmb; private String user; public Asset_employe_set(String _user) { try { this.user = _user; jbInit(); } catch (Exception ex) { ex.printStackTrace(); } }
/** * jbInit */ private void jbInit() { this.setSize(new Dimension(500, 400)); this.setLayout(null); btn_add.setBounds(new Rectangle(57, 23, 75, 35)); btn_del.setBounds(new Rectangle(260, 23, 75, 35)); btn_modi.setBounds(new Rectangle(158, 23, 75, 35)); btn_return.setBounds(new Rectangle(364, 23, 75, 35)); btn_return.setText("返回"); model = new Mytable(); model.setTitle(getTitle()); model.setContent(getContents()); table = new JTable(model); table.setFont(new Font("宋体", 1, 17)); jScrollPane1.setBounds(new Rectangle(47, 81, 395, 228)); table.setRowSelectionAllowed(false); this.add(jScrollPane1); this.add(btn_add); this.add(btn_return); this.add(btn_modi); this.add(btn_del); tmb = table.getModel(); initempMaptemp(); //talbe changed tmb.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_duty = table.getValueAt(table.getSelectedRow(), 2). toString().trim(); String table_mem = table.getValueAt(table.getSelectedRow(), 3). toString().trim(); Employee temp = getEmployeeMessage(table_id); String temp_name = temp.getName(); String temp_dutye = temp.getDuty(); String temp_mem = temp.getMem(); if (table_name.length() == 0) { table_name = ""; } if (table_duty.length() == 0) { table_duty = ""; } if (temp_mem.length() == 0) { temp_mem = ""; } Employee eee = new Employee(table_name, table_duty, table_mem); if (!temp_name.equals(table_name) || !temp_dutye.equals(table_duty) || !temp_mem.equals(table_mem)) { if (empMap.containsKey(table_id)) { Set set = empMap.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(eee); break; } } } else { empMap.put(table_id, eee); } modiEmployyeTempMap(table_id, eee); } } }); //update temp //行变动id的变动 table.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { if (table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) { tempID = table.getValueAt(table.getSelectedRow(), 0). toString().trim(); } else { tempID = ""; } }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { } }); jScrollPane1.getViewport().add(table); btn_return.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { closeEmployee(); } });
btn_add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addEmployee(); initempMaptemp(); } }); btn_del.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { deleteEmp(); } }); btn_modi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { modiEmployee(); } }); }
//modi private void modiEmployee() { if (empMap.size() != 0) { dcon = new DBConnection(); Set set = empMap.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); String id = me.getKey().toString().trim(); Employee epe = (Employee) me.getValue(); String sql = "update Employee set employee_name='"; sql += epe.getName().trim() + "',employee_post='"; sql += epe.getDuty() + "',remark='" + epe.getMem() + "' where employee_id='"; sql += id + "'"; dcon.update(1, sql); } JOptionPane.showMessageDialog(this, "你修改了" + empMap.size() + "条记录"); empMap.clear(); empMaptemp.clear(); initempMaptemp(); model.setContent(getContents()); table.updateUI(); this.updateUI(); } }
//updata temp private void modiEmployyeTempMap(String id, Employee emp) { Set set = empMaptemp.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); if (me.getKey().toString().trim().equals(id)) { me.setValue(emp); break; } } }
//return temp private Employee getEmployeeMessage(String id) { return (Employee) empMaptemp.get(id); }
//initempMaptemp(); private void initempMaptemp() { dcon = new DBConnection(); String sql = "select employee_id ,employee_name, employee_post ,remark from Employee order by employee_id asc"; Vector v = dcon.select(sql); int count = v.size(); for (int i = 0; i < count; i++) { String id = ((Vector) v.get(i)).get(0).toString(); String s1 = ((Vector) v.get(i)).get(1).toString(); String s2 = ((Vector) v.get(i)).get(2).toString(); String s3 = ((Vector) v.get(i)).get(3).toString(); empMaptemp.put(id, new Employee(s1, s2, s3)); } }
//del private void deleteEmp() { if (tempID.trim().length() != 0) { dcon = new DBConnection(); String sql = "Select * from Action where asset_use='" + tempID + "'"; if (dcon.isNull(sql)) { JOptionPane.showMessageDialog(this, "该人的帐上没有为空"); } else { sql = "delete from Employee where employee_id='" + tempID.trim() + "'"; JOptionPane.showMessageDialog(this, dcon.update(2, sql)); model.setContent(getContents()); table.updateUI(); this.updateUI(); } } else { JOptionPane.showMessageDialog(this, "没有选择要操作的数据"); } }
private void addEmployee() { employeeAdd ela = new employeeAdd(); Dimension frmsize = getSize(); Point loc = getLocation(); ela.setLocation((frmsize.width - ela.WIDTH) / 2 + loc.x, (frmsize.height - ela.HEIGHT) / 2 + loc.y); ela.setSize(250, 250); ela.setModal(true); ela.setVisible(true); model.setContent(getContents()); table.updateUI(); this.updateUI();
}
//获取表格的列表 private String[] getTitle() { dcon = new DBConnection(); String sql = "select employee_id as 编号,employee_name as 姓名, employee_post as 职务,remark as 备注 from Employee order by employee_id asc"; return dcon.getColumnname(sql); }
//获取表格的内容 private Vector getContents() { dcon = new DBConnection(); String sql = "select employee_id as 编号,employee_name as 姓名, employee_post as 职务,remark as 备注 from Employee order by employee_id asc"; return dcon.select(sql); }
//close private void closeEmployee() { this.removeAll(); Asset_OP asp = new Asset_OP(user); this.add(asp); this.updateUI();
}}