package asset;
/** * <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.table.*;import tools.*;import javax.swing.event.TableModelListener;import javax.swing.event.TableModelEvent;import quary.AssetFullDialog;import op.AssetReturn;
public class InAndOut extends JPanel { private JButton btn_add = new JButton(); private JButton btn_delete = new JButton(); private JScrollPane jScrollPane1 = new JScrollPane(); private DBConnection dcon = null; private JTable table; private Mytable model; private HashMap kindMap = new HashMap(); private HashMap assetMap = new HashMap(); private HashMap assetMapTemp = new HashMap(); private String user; private String tempID = ""; private String tempUser = ""; private TableModel tmb; JTabbedPane jTabbedPane1 = new JTabbedPane(); JButton btnmodify = new JButton(); JButton btnCard = new JButton(); public InAndOut(String _user) { try { this.user = _user; jbInit(); } catch (Exception ex) { ex.printStackTrace(); } }
private void jbInit() throws Exception { model = new Mytable(3); model.setTitle(getTitle()); model.setContent(getContents()); table = new JTable(model); table.setRowSelectionAllowed(false); this.setLayout(null); btn_add.setBounds(new Rectangle(47, 21, 81, 32)); btn_add.setText("增加"); btn_delete.setBounds(new Rectangle(273, 21, 81, 32)); btn_delete.setToolTipText(""); btn_delete.setText("出库"); jScrollPane1.setBounds(new Rectangle(15, 71, 477, 223)); jTabbedPane1.setBounds(new Rectangle( -44, 86, 5, 5)); btnmodify.setBounds(new Rectangle(155, 21, 84, 32)); btnmodify.setText("修改"); btnCard.setBounds(new Rectangle(389, 21, 76, 32)); btnCard.setText("卡片"); this.add(jScrollPane1); this.add(jTabbedPane1); this.add(btn_add); this.add(btnmodify); this.add(btn_delete); this.add(btnCard); this.setSize(500, 400); jScrollPane1.getViewport().add(table); initassetMapTemp(); tmb = table.getModel();
if (getKindList() != null) { JComboBox cmb = new JComboBox(getKindList()); TableColumnModel tcm = table.getColumnModel(); tcm.getColumn(8).setCellEditor(new DefaultCellEditor(cmb)); }
//tmb 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_type = table.getValueAt(table.getSelectedRow(), 2). toString().trim(); ; String table_price = table.getValueAt(table.getSelectedRow(), 3). toString().trim(); String table_buytime = table.getValueAt(table.getSelectedRow(), 4).toString().trim(); String table_status = table.getValueAt(table.getSelectedRow(), 5).toString().trim(); String table_mem; if (table.getValueAt(table.getSelectedRow(), 7) != null) { table_mem = table.getValueAt(table.getSelectedRow(), 7). toString().trim(); } else { table_mem = ""; } String table_kinds = table.getValueAt(table.getSelectedRow(), 8). toString().trim(); Asset asset = new Asset(table_name, table_type, table_price, table_buytime, table_status, table_mem, table_kinds); Asset assetTemp = getAssetTemp(table_id); String temp_name = assetTemp.getName().trim(); String temp_type = assetTemp.getType().trim(); String temp_price = assetTemp.getPrice().trim(); String temp_buytime = assetTemp.getBuytime().trim(); String temp_status = assetTemp.getStatus().trim(); String temp_mem = assetTemp.getMem().trim(); String temp_kinds = assetTemp.getKinds().trim(); if (table_name.length() == 0) { table_name = temp_name; table.setValueAt(temp_name, table.getSelectedRow(), 1); } if (table_type.length() == 0) { table_type = temp_type; table.setValueAt(temp_type, table.getSelectedRow(), 2); } if (table_price.length() == 0) { table_price = temp_price; table.setValueAt(temp_price, table.getSelectedRow(), 3); } if (table_buytime.length() == 0) { table_buytime = temp_buytime; table.setValueAt(temp_buytime, table.getSelectedRow(), 4); } if (table_status.length() == 0) { table_status = temp_status; table.setValueAt(temp_status, table.getSelectedRow(), 5); } if (table_kinds.length() == 0) { table_kinds = temp_kinds; table.setValueAt(temp_kinds, table.getSelectedRow(), 8);
} else if (!isPriceRight(table_price)) { table.setValueAt(temp_price, table.getSelectedRow(), 3); } else if (!dateIsright(table_id, table_buytime)) { table.setValueAt(temp_buytime, table.getSelectedRow(), 4); } else if (! temp_name. equals(table_name) || !temp_type.equals(table_type) || !temp_price.equals(table_price) || !temp_buytime.equals(table_buytime) || !temp_status.equals(table_status) || !temp_mem.equals(table_mem) || !temp_kinds.equals(table_kinds)) { if (assetMap.containsKey(table_id)) { Set set = assetMap.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(asset); break; } } } else { assetMap.put(table_id, asset); } } else { modifyMapTemp(table_id, asset); } } }); //行选择编号改变 table.addMouseListener(new MouseListener() { public void mouseClicked(MouseEvent e) { if (table.getSelectedRow() != -1 && table.getSelectedColumn() != -1) { tempID = table.getValueAt(table.getSelectedRow(), 0). toString(); if( table.getValueAt(table.getSelectedRow(), 6)!=null && table.getValueAt(table.getSelectedRow(), 6).toString().trim().length()!=0 ) { tempUser = table.getValueAt(table.getSelectedRow(), 6). toString(); }else { tempUser=""; } } else { tempID = ""; } }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { } }); btn_delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { outAsset(); } }); btnmodify.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { modiAsset(); } }); btn_add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addAsset(); } }); btnCard.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { MxCard(); } }); } private void MxCard() { dcon = new DBConnection(); String sql = "select * from Action where asset_id = '" + tempID + "'"; if (!dcon.isNull(sql)) { AssetFullDialog afd = new AssetFullDialog(tempID); Dimension frmsize = getSize(); Point loc = getLocation(); afd.setLocation((frmsize.width - afd.WIDTH) / 2 + loc.x, (frmsize.height - afd.HEIGHT) / 2 + loc.y); afd.setSize(350, 330); afd.setModal(true); afd.setVisible(true); } else { sql = "select employee_id from Employee where employee_name='" + tempUser + "'"; Vector v = dcon.select(sql); String userid = ((Vector) v.get(0)).get(0).toString().trim(); AssetReturn ar = new AssetReturn(tempUser, tempID,userid); Dimension frmsize = getSize(); Point loc = getLocation(); ar.setLocation((frmsize.width - ar.WIDTH) / 2 + loc.x, (frmsize.height - ar.HEIGHT) / 2 + loc.y); ar.setSize(350, 450); ar.hiden(); ar.setModal(true); ar.setVisible(true);
}
}
private void addAsset() { AssetAdd aa = new AssetAdd(); Dimension frmsize = getSize(); Point loc = getLocation(); aa.setLocation((frmsize.width - aa.WIDTH) / 2 + loc.x, (frmsize.height - aa.HEIGHT) / 2 + loc.y); aa.setSize(350, 350); aa.setModal(true); aa.setVisible(true); model.setContent(getContents()); table.updateUI(); this.updateUI(); assetMapTemp.clear(); assetMap.clear(); initassetMapTemp();
}
private void modiAsset() { if (assetMap.size() != 0) { Set set = assetMap.entrySet(); Iterator it = set.iterator(); dcon = new DBConnection(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); String id = me.getKey().toString().trim(); Asset asset = (Asset) me.getValue(); String name = asset.getName(); String type = asset.getType(); String price = asset.getPrice(); String buytime = asset.getBuytime(); String status = asset.getStatus(); String remark = asset.getMem(); String childkindid = getKindID(asset.getKinds()); String sql = "update asset set asset_name='"; sql += name + "',asset_type='"; sql += type + "',asset_price='"; sql += price + "',asset_buytime='"; sql += buytime + "',asset_status='"; sql += status + "',remark='"; sql += remark + "',childkind_id='" + childkindid + "' where asset_id='"; sql += id + "'"; dcon.update(1, sql); } JOptionPane.showMessageDialog(this, "你共修改了" + assetMap.size() + "条记录"); assetMapTemp.clear(); assetMap.clear(); initassetMapTemp(); } else { JOptionPane.showMessageDialog(this, "你没有修改记录"); } }
// modifyMapTemp private void modifyMapTemp(String id, Asset a) { Set set = assetMapTemp.entrySet(); Iterator it = set.iterator(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); if (me.getKey().toString().trim().equals(id)) { me.setValue(a); break; } } }
//getAssetTemp() private Asset getAssetTemp(String id) { return (Asset) assetMapTemp.get(id); }
//time isDateRight private boolean dateIsright(String id, String dateStr) { for (int i = 0; i < dateStr.length(); i++) { if (!Character.isDigit(dateStr.charAt(i))) { return false; } } if (dateStr.length() != 8) { return false; } int[] s = {Integer.parseInt(dateStr.substring(0, 4)), Integer.parseInt(dateStr.substring(4, 6)), Integer.parseInt(dateStr.substring(6, 8))}; if (s[1] < 1 || s[1] > 12) { return false; } int dayCounts = count(s[1], s[0]); if (s[2] < 1 || s[2] > dayCounts) { return false; } Calendar c = Calendar.getInstance(); int month = c.get(Calendar.MONTH) + 1; int year = c.get(Calendar.YEAR); int day = c.get(Calendar.DATE); String sm; String sd; if (month < 10) { sm = "0" + String.valueOf(month); } else { sm = String.valueOf(month); } if (day < 10) { sd = "0" + String.valueOf(day); } else { sd = String.valueOf(day); } String StrTime = year + sm + sd; long today = Long.parseLong(StrTime); dcon = new DBConnection(); String sql = "select action_time from Action where asset_id='" + id + "'"; if (dcon.isNull(sql)) { Vector v = dcon.select(sql); long baseTime = Long.parseLong(((Vector) v.get(0)).get(0).toString(). trim()); long tableTime = Long.parseLong(dateStr); if (tableTime > baseTime) { JOptionPane.showMessageDialog(this, "发生时间早于购买时间"); return false; }
if (tableTime > today) { JOptionPane.showMessageDialog(this, "发生时间晚于当前时间"); return false; } } else { if (Integer.parseInt(dateStr) > today) { return false; } }
return true; }
//is loop public boolean loop(int year) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { return false; } }
//dayCounts public int count(int month, int year) { if (month == 2) { if (loop(year)) { return 29; } else { return 28; } } else if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) { return 31; } else { return 30; } }
//priceIs valueble private boolean isPriceRight(String price) { for (int i = 0; i < price.trim().length(); i++) { if (!Character.isDigit(price.charAt(i))) { JOptionPane.showMessageDialog(table, "金额怎么能是字母呢"); return false; } }
if (Integer.parseInt(price) < 2000) { JOptionPane.showMessageDialog(this, "金额不能小与两千"); return false; } else { return true; } }
/** *out */ private void outAsset() { if (tempID.trim().length() != 0) { dcon = new DBConnection(); String sql = "select * from Action where asset_id='" + tempID + "'"; if (dcon.isNull(sql)) { JOptionPane.showMessageDialog(this, "此货借出,不能从帐上删除"); } else { sql = "delete from asset where asset_id = '" + tempID + "'"; dcon.update(2, sql); JOptionPane.showMessageDialog(this, "编号是" + tempID + "被删除"); tempID = ""; model.setContent(getContents()); table.updateUI(); this.updateUI(); } } else { JOptionPane.showMessageDialog(this, "请选择要操作的编号"); } }
/** * initassetMapTemp */ private void initassetMapTemp() { dcon = new DBConnection(); String sql = "select a.asset_id as 编号, a.asset_name as 名称,a.asset_type as 类型,a.asset_price as 单价,a.asset_buytime as 购买时间,a.asset_status as 状态,a.remark as 备注,c.childkind_name as 科目 from asset as a left join employee as b on a.asset_user = b.employee_id ,ChildKind as c where (a.childkind_id = c.childkind_id) order by a.asset_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().trim(); String name = ((Vector) v.get(i)).get(1).toString().trim(); String type = ((Vector) v.get(i)).get(2).toString().trim(); String price = ((Vector) v.get(i)).get(3).toString().trim(); String buytime = ((Vector) v.get(i)).get(4).toString().trim(); String status = ((Vector) v.get(i)).get(5).toString().trim(); String remark; if (((Vector) v.get(i)).get(6) == null) { remark = ""; } else { remark = ((Vector) v.get(i)).get(6).toString().trim(); }
String childkindid = ((Vector) v.get(i)).get(7).toString().trim();
Asset a = new Asset(name, type, price, buytime, status, remark, childkindid); assetMapTemp.put(id, a); } }
//获取表格的列表 private String[] getTitle() { dcon = new DBConnection(); String sql = "select a.asset_id as 编号, a.asset_name as 名称,a.asset_type as 类型,a.asset_price as 单价,a.asset_buytime as 购买时间,a.asset_status as 状态,b.employee_name as 用户,a.remark as 备注,c.childkind_name as 科目 from asset as a left join employee as b on a.asset_user = b.employee_id ,ChildKind as c where (a.childkind_id = c.childkind_id) order by a.asset_id asc "; return dcon.getColumnname(sql); }
//获取表格的内容 private Vector getContents() { dcon = new DBConnection(); String sql = "select a.asset_id as 编号, a.asset_name as 名称,a.asset_type as 类型,a.asset_price as 单价,a.asset_buytime as 购买时间,a.asset_status as 状态,b.employee_name as 用户,a.remark as 备注,c.childkind_name as 科目 from asset as a left join employee as b on a.asset_user = b.employee_id ,ChildKind as c where (a.childkind_id = c.childkind_id) order by a.asset_id asc "; return dcon.select(sql); } //get kindList private String[] getKindList() { dcon = new DBConnection(); String sql = "select childkind_id,childkind_name from ChildKind"; 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 getKindID(String name) { return kindMap.get(name).toString().trim(); }
}