package op;
/**
*
Title:
Description:
Copyright: Copyright (c) 2007
Company:
public class Draw extends JPanel {
private JScrollPane jScrollPane1 = new JScrollPane();
private DBConnection dcon = null;
private JTable table;
private Mytable model;
private String tempID = "";
private String status = "";
private HashMap chMap = new HashMap();
String user;
JButton btnDraw = new JButton();
JComboBox choice = new JComboBox();
public Draw(String _user) {
try {
this.user = _user;
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//init choice
private void setChoice() {
if (getChoiceList() != null || getChoiceList().length != 0) {
String[] s = getChoiceList();
choice.removeAllItems();
for (int i = 0; i < s.length; i++) {
choice.addItem(s[i]);
}
}
}
private void jbInit() throws Exception {
setChoice();
model = new Mytable(4);
model.setTitle(getTitle());
if (choice.getSelectedItem() != null) {
model.setContent(getContents(choice.getSelectedItem().toString().
trim()));
} else {
model.setContent(getContents(""));
}
table = new JTable(model);
table.setRowSelectionAllowed(false);
this.setLayout(null);
jScrollPane1.setBounds(new Rectangle(11, 54, 475, 231));
btnDraw.setBounds(new Rectangle(296, 14, 84, 32));
btnDraw.setText("借出");
choice.setBounds(new Rectangle(39, 11, 122, 32));
this.add(jScrollPane1);
this.add(btnDraw);
this.add(choice);
jScrollPane1.getViewport().add(table);
this.setSize(500, 400);
//行选择编号改变
table.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (table.getSelectedRow() != -1 &&
table.getSelectedColumn() != -1) {
tempID = table.getValueAt(table.getSelectedRow(), 0).
toString().trim();
status = table.getValueAt(table.getSelectedRow(), 5).
toString().trim();
} else {
tempID = "";
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
btnDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (tempID.trim().length() != 0) {
if (status.endsWith("正常")) {
EmployeeDialog ed = new EmployeeDialog(tempID.trim(),
user);
Dimension frmsize = getSize();
Point loc = getLocation();
ed.setLocation((frmsize.width - ed.WIDTH) / 2 + loc.x,
(frmsize.height - ed.HEIGHT) / 2 + loc.y);
ed.setSize(250, 300);
ed.setModal(true);
ed.setVisible(true);
tempID = "";
setChoice();
model.setContent(getContents(choice.getSelectedItem().
toString().trim()));
table.updateUI();
} else {
JOptionPane.showMessageDialog(table,
"该设备要" + status + "不能借出");
}
} else {
JOptionPane.showMessageDialog(table, "请选择要操作的记录");
}
}
});
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(choice.getSelectedItem() !=null)
{
model.setContent(getContents(choice.getSelectedItem().
toString().trim()));
table.updateUI();
}
}
});
}
//获取choice列表
private String[] getChoiceList() {
dcon = new DBConnection();
String sql = "select b.childkind_id,a.childKind_name from childkind as a,(select childkind_id from Asset group by childkind_id) as b where ( b.childkind_id = a.childkind_id)";
Vector v = dcon.select(sql);
int count = v.size();
String s[] = new String[count];
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();
s[i] = name;
chMap.put(name, id);
}
return s;
}
//getid
private String getID(String name) {
if (chMap.get(name) != null) {
return chMap.get(name).toString().trim();
} else {
return "";
}
}
//获取表格的列表
private String[] getTitle() {
dcon = new DBConnection();
String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset order by asset_id asc";
return dcon.getColumnname(sql);
}
//获取表格的内容
private Vector getContents(String con1) {
String con = getID(con1);
dcon = new DBConnection();
// String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset where(asset_user is null or len(asset_user)=0) order by asset_id asc";
String sql = "select asset_id as 编号, asset_name as 名称,asset_type as 类型,asset_price as 单价,asset_buytime as 购买时间,asset_status as 状态,remark as 备注 from asset where((asset_user is null or len(asset_user)=0) and (childkind_id='" +
con + "')) order by asset_id asc";
return dcon.select(sql);
}
}