package asset;
import java.awt.*;
import javax.swing.*;import java.awt.*;import tools.*;import java.util.*;import java.awt.event.*;
/** * <p>Title: </p> * * <p>Description: </p> * * <p>Copyright: Copyright (c) 2007</p> * * <p>Company: </p> * * @author not attributable * @version 1.0 */public class AssetAdd extends JDialog { JComboBox cmbfirst = new JComboBox(); JComboBox cmbsecond = new JComboBox(); JLabel jLabel1 = new JLabel(); JTextField txt_name = new JTextField(); JLabel jLabel2 = new JLabel(); JTextField txt_price = new JTextField(); JLabel jLabel3 = new JLabel(); JTextField txt_id = new JTextField(); JLabel jLabel4 = new JLabel(); JTextField txt_time = new JTextField(); JLabel jLabel5 = new JLabel(); JTextField txt_type = new JTextField(); JLabel jLabel6 = new JLabel(); JTextArea txt_mem = new JTextArea(4, 20); JLabel jLabel7 = new JLabel(); JComboBox cmbStatus = new JComboBox(); JButton btn_ok = new JButton(); JButton btn_cancle = new JButton(); private DBConnection dcon = null; private HashMap firstMap = new HashMap(); private HashMap secondMap = new HashMap();
public AssetAdd(Frame owner, String title, boolean modal) { super(owner, title, modal); try { setDefaultCloseOperation(DISPOSE_ON_CLOSE); jbInit(); pack(); } catch (Exception exception) { exception.printStackTrace(); } }
public AssetAdd() { this(new Frame(), "AssetAdd", false); }
private void getcmbsecondlist() { String[] s1; if(cmbfirst.getSelectedItem() != null){ s1 = getSecondList(getFirstID(cmbfirst.getSelectedItem(). toString().trim())); }else { s1 = new String[0]; } if (s1 != null) { cmbsecond.removeAllItems(); cmbsecond.removeItem(""); for (int i = 0; i < s1.length; i++) { cmbsecond.addItem(s1[i]); } } }
private void setAssetid() { if (cmbsecond.getSelectedItem() != null) { String s = cmbsecond.getSelectedItem().toString().trim(); txt_id.setText(getAssetID(s));
} else { txt_id.setText(""); } }
private void jbInit() throws Exception { String[] s = getFirstList(); if (s != null || s.length!=0) { cmbfirst.removeAllItems(); for (int i = 0; i < s.length; i++) { cmbfirst.addItem(s[i]); } } getcmbsecondlist(); cmbfirst.setEditable(false); this.getContentPane().setLayout(null); cmbfirst.setBounds(new Rectangle(43, 13, 84, 32)); jLabel2.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel2.setText("状态"); jLabel2.setBounds(new Rectangle(173, 128, 79, 27)); txt_price.setText(""); txt_price.setBounds(new Rectangle(210, 96, 81, 27)); jLabel3.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel3.setText("编号"); jLabel3.setBounds(new Rectangle(15, 59, 79, 27)); setAssetid(); txt_id.setBounds(new Rectangle(51, 60, 81, 27)); txt_id.setEditable(false); jLabel4.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel4.setText("时间"); jLabel4.setBounds(new Rectangle(14, 135, 79, 27)); Calendar today = Calendar.getInstance(); int month = today.get(Calendar.MONTH) + 1; int year = today.get(Calendar.YEAR); int day = today.get(Calendar.DATE); String monthStr; String dayStr; if (month < 10) { monthStr = "0" + month; } else { monthStr = String.valueOf(month); } if (day < 10) { dayStr = "0" + day; } else { dayStr = String.valueOf(day); } txt_time.setText(year + monthStr + dayStr); txt_time.setEditable(false); txt_time.setBounds(new Rectangle(51, 134, 81, 27)); jLabel5.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel5.setText("类型"); jLabel5.setBounds(new Rectangle(13, 94, 79, 27)); txt_type.setText(""); txt_type.setBounds(new Rectangle(51, 98, 81, 27)); jLabel6.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel6.setText("备注"); jLabel6.setBounds(new Rectangle(22, 176, 79, 27)); txt_mem.setText(""); txt_mem.setBounds(new Rectangle(61, 170, 252, 73)); txt_name.setBounds(new Rectangle(211, 60, 81, 27)); jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel1.setText("名称"); jLabel1.setBounds(new Rectangle(174, 59, 79, 27)); String[] stutas = {"正常", "报废", "维修"}; cmbStatus.removeAllItems(); cmbStatus.addItem(stutas[0]); cmbStatus.addItem(stutas[1]); cmbStatus.addItem(stutas[2]); cmbStatus.setBounds(new Rectangle(211, 134, 84, 28)); btn_ok.setBounds(new Rectangle(56, 254, 95, 33)); btn_ok.setText("确定"); btn_cancle.setBounds(new Rectangle(176, 254, 85, 32)); btn_cancle.setText("取消"); this.getContentPane().add(cmbfirst); jLabel7.setFont(new java.awt.Font("宋体", Font.BOLD, 13)); jLabel7.setText("单价"); jLabel7.setBounds(new Rectangle(174, 91, 79, 27)); cmbsecond.setEditable(false); cmbStatus.setEditable(false); this.getContentPane().add(cmbsecond); this.getContentPane().add(txt_type); this.getContentPane().add(txt_name); this.getContentPane().add(jLabel1); this.getContentPane().add(jLabel3); this.getContentPane().add(txt_id); this.getContentPane().add(jLabel5); this.getContentPane().add(jLabel4); this.getContentPane().add(jLabel6); this.getContentPane().add(txt_mem); this.getContentPane().add(jLabel7); this.getContentPane().add(jLabel2); this.getContentPane().add(txt_time); this.getContentPane().add(txt_price); this.getContentPane().add(cmbStatus); this.getContentPane().add(btn_cancle); this.getContentPane().add(btn_ok); this.setResizable(false); cmbsecond.setBounds(new Rectangle(164, 13, 106, 32)); btn_cancle.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { closeDialog(); } }); cmbfirst.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { getcmbsecondlist(); setAssetid(); } }); cmbsecond.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { setAssetid(); } }); btn_ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { addAsset();
} }); }
private void addAsset() { if (txt_id.getText().trim().length() == 0) { JOptionPane.showMessageDialog(this, "编号不能为空"); } else if (txt_name.getText().trim().length() == 0) { JOptionPane.showMessageDialog(this, "名称不能为空"); } else if (txt_type.getText().trim().length() == 0) { JOptionPane.showMessageDialog(this, "型号不能为空"); } else if (txt_price.getText().trim().length() == 0) { JOptionPane.showMessageDialog(this, "不能为空"); } else if (!priceIsRight(txt_price.getText().trim())) {} else { dcon = new DBConnection(); String mem; if (txt_mem.getText().trim().length() == 0) { mem = ""; } else { mem = txt_mem.getText().trim(); } String sql = "insert into Asset (asset_id,asset_name,asset_type,asset_price,asset_buytime,asset_status,asset_user,remark,childkind_id) values ('"; sql += txt_id.getText().trim() + "','"; sql += txt_name.getText().trim() + "','"; sql += txt_type.getText().trim() + "','"; sql += txt_price.getText().trim() + "','"; sql += txt_time.getText().trim() + "','"; sql += cmbStatus.getSelectedItem().toString().trim() + "','"; sql += "" + "','"; sql += mem + "','"; sql += getSecondID(cmbsecond.getSelectedItem().toString().trim()). trim() + "')"; JOptionPane.showMessageDialog(this, dcon.update(3, sql)); closeDialog(); } }
//price is right private boolean priceIsRight(String price) { for (int i = 0; i < price.trim().length(); i++) { if (!Character.isDigit(price.charAt(i))) { JOptionPane.showMessageDialog(this, "金额不能怎么能有字符呢"); return false; } } if (Integer.parseInt(price) < 2000) { JOptionPane.showMessageDialog(this, "金额不能小于2000"); return false; } return true; }
//close private void closeDialog() { this.dispose(); }
//get first list private String[] getFirstList() { dcon = new DBConnection(); String sql = "select fatherkind_id,fatherkind_name from (select childkind.fatherkind_id,fatherkind.fatherkind_name from childkind ,fatherkind where(childkind.fatherkind_id = fatherkind.fatherKind_id) ) as a group by fatherkind_id ,fatherkind_name"; Vector v = dcon.select(sql); String FirstStr[] = new String[v.size()]; for (int i = 0; i < v.size(); i++) { String value = ((Vector) v.get(i)).get(0).toString().trim(); FirstStr[i] = ((Vector) v.get(i)).get(1).toString().trim(); firstMap.put(FirstStr[i], value); } return FirstStr; }
//first name _> firstid private String getFirstID(String values) { return ((String) (firstMap.get(values))).trim(); }
//get Second list private String[] getSecondList(String fatherid) { dcon = new DBConnection(); String sql = "Select childkind_id,childkind_name from ChildKind where fatherkind_id='" + fatherid + "'"; Vector v = dcon.select(sql); String Second[] = new String[v.size() + 1]; for (int i = 0; i < v.size(); i++) { String value = ((Vector) v.get(i)).get(0).toString().trim(); Second[i] = ((Vector) v.get(i)).get(1).toString().trim(); secondMap.put(Second[i], value); } return Second; }
//second name _> firstid private String getSecondID(String id) { return ((String) (secondMap.get(id))).trim(); }
//get id private String getAssetID(String name) { String topid = getSecondID(name); dcon = new DBConnection(); Vector v = dcon.select( "select asset_id from Asset where childkind_id = '" + topid + "' order by childkind_id asc"); if (v.size() == 0) { return topid + "001"; } int autoID = 1; for (int i = 0; i < v.size(); i++) { String s = ((Vector) v.get(i)).get(0).toString().trim(); String Temp; if (autoID < 10) { Temp = (topid).trim() + "00" + autoID; } else if (autoID < 100) { Temp = (topid).trim() + "0" + autoID; } else { Temp = (topid).trim() + autoID; } if (!Temp.equals(s)) { return Temp; } autoID++; } if (v.size() < 10) { return topid + "00" + (v.size() + 1); } else if (v.size() < 100) { return topid + "0" + (v.size() + 1); } else { return topid + (v.size() + 1); } }}