package set;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import tools.*;
public class childkindsAdd extends JDialog {
private JLabel jLabel3 = new JLabel();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JTextField txt_kindsID = new JTextField();
private JTextField txt_kindsname = new JTextField();
private JComboBox cmb_toplist;
private JButton btn_ok = new JButton();
private JButton btn_cancle = new JButton();
private DBConnection dcon = null;
private HashMap topmap = new HashMap();
private String user;
public childkindsAdd(Frame owner, String title, boolean modal,String _user) {
super(owner, title, modal);
try {
this.user = _user;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jbInit();
pack();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public childkindsAdd(String _user) {
this(new Frame(), "childkindsAdd", false,_user);
}
private void jbInit() throws Exception {
inittopmap();
cmb_toplist = new JComboBox(getNamelist());
this.getContentPane().setLayout(null);
jLabel3.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jLabel3.setToolTipText("");
jLabel3.setText("科 目 ID");
jLabel3.setBounds(new Rectangle(33, 65, 72, 39));
this.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jLabel2.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jLabel2.setToolTipText("");
jLabel2.setText("上级科目");
jLabel2.setBounds(new Rectangle(32, 27, 72, 39));
cmb_toplist.setBounds(new Rectangle(95, 36, 84, 24));
txt_kindsname.setText("");
txt_kindsname.setBounds(new Rectangle(93, 114, 80, 25));
txt_kindsID.setText("");
txt_kindsID.setBounds(new Rectangle(93, 73, 79, 24));
this.getContentPane().add(jLabel3);
txt_kindsID.setText(getchildKindsID(cmb_toplist.getSelectedItem().
toString().trim()));
txt_kindsID.setEditable(false);
btn_cancle.setBounds(new Rectangle(125, 171, 70, 35));
btn_cancle.setText("关闭");
btn_ok.setBounds(new Rectangle(27, 169, 72, 36));
btn_ok.setText("确定");
this.getContentPane().add(jLabel1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(txt_kindsname);
this.getContentPane().add(txt_kindsID);
this.getContentPane().add(cmb_toplist);
this.getContentPane().add(btn_ok);
this.getContentPane().add(btn_cancle);
jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
jLabel1.setToolTipText("");
jLabel1.setText("科目名称");
jLabel1.setBounds(new Rectangle(31, 107, 72, 39));
cmb_toplist.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
txt_kindsID.setText(getchildKindsID(cmb_toplist.getSelectedItem().
toString().trim()));
}
});
btn_ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addkinds();
closedialog();
}
});
btn_cancle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
closedialog();
}
});
}
private void closedialog() {
this.dispose();
}
private void addkinds() {
if (txt_kindsname.getText().trim().length() == 0) {
JOptionPane.showMessageDialog(this, "名称不能为空");
} else {
dcon = new DBConnection();
String sql = "select * from ChildKind where childkind_name = '" +
txt_kindsname.getText().trim() + "'";
if (dcon.isNull(sql)) {
JOptionPane.showMessageDialog(this, "该名称已经存在");
} else {
sql =
"insert into ChildKind (fatherkind_id,childkind_id,childkind_name) values ('";
sql += getTopid(cmb_toplist.getSelectedItem().toString().trim()) +
"','";
sql += txt_kindsID.getText().trim() + "','";
sql += txt_kindsname.getText().trim() + "')";
JOptionPane.showMessageDialog(this, dcon.update(3, sql));
txt_kindsname.setText("");
}
}
}
// 初始化HashMap
private void inittopmap() {
dcon = new DBConnection();
String sql = "select * 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();
topmap.put(name, id);
}
}
//id list
private String[] getNamelist() {
String[] str = new String[topmap.size()];
Set set = topmap.entrySet();
Iterator it = set.iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
str[i] = me.getKey().toString();
i++;
}
return str;
}
public String getTopid(String name) {
return topmap.get(name).toString();
}
private String getchildKindsID(String name) {
String topid = getTopid(name);
dcon = new DBConnection();
Vector v = dcon.select(
"select childkind_id from ChildKind where fatherkind_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++;
}
autoID = v.size() + 1;
if(autoID<10)
{
return topid + "00"+autoID;
}else if(autoID < 100)
{
return topid + "0"+autoID;
}else{
return topid + autoID;}
}
}