package yaopin;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import model.ProviderDAO;
import model.ProviderDTO;
public class Provider implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel jSubPanel = null;
private JLabel jLabelID, jLabelName, jLabelBank,jLabelBankID,jLabelAddress,jLabelLinkman, jLabelLinkPhone,jLabelFax,jLabelURL,jLabelEmail,jLabelPostCode;
private JTextField jTextID,jTextName, jTextPostCode,jTextBankID,jTextLinkman, jTextLinkPhone,jTextURL,jTextAddress,jTextEmail,jTextFax,jTextBank;
private JButton jBtnAdd, jBtnUpdate, jBtnDelete, jBtnSearch,jBtnCancel;
private DefaultTableModel tableModel;
private ProviderDTO providerDTO = new ProviderDTO();
private ProviderDAO providerDAO;
public DefaultTableModel getTableModel()
{
if (tableModel == null)
{
tableModel = new DefaultTableModel(); // 与控件名要一致。
String[] columnNames = {"供应商编号", "供应商名称", "联系人", "联系电话", "联系地址", "网址", "传真", "电子邮件","邮政编码","开户银行","银行帐号"};
for(String column : columnNames)
{
tableModel.addColumn(column);
}
providerDAO = new ProviderDAO();
for(ProviderDTO user : providerDAO.fillProvider())
{
tableModel.addRow(user.toArray());
}
}
return tableModel;
}
public JPanel getJSubPanel()
{
if (jSubPanel == null)
{
jLabelID = new JLabel("供应商编号");
jLabelID.setBounds(new Rectangle(90, 40, 68, 22));
jTextID = new JTextField();
jTextID.setName("供应商编号"); // 与列名要一致。
jTextID.setBounds(new Rectangle(160, 40, 200, 25));
jLabelName = new JLabel("供应商名称");
jLabelName.setBounds(new Rectangle(440, 40, 68, 22));
jTextName = new JTextField();
jTextName.setName("供应商名称");
jTextName.setBounds(new Rectangle(510, 40, 200, 25));
jLabelLinkman = new JLabel("联 系 人");
jLabelLinkman.setBounds(new Rectangle(90, 72, 68, 22));
jTextLinkman = new JTextField();
jTextLinkman.setName("联系人");
jTextLinkman.setBounds(new Rectangle(160, 72, 200, 25));
jLabelLinkPhone = new JLabel("联系电话");
jLabelLinkPhone.setBounds(new Rectangle(440, 72, 68, 22));
jTextLinkPhone = new JTextField();
jTextLinkPhone.setName("联系电话");
jTextLinkPhone.setBounds(new Rectangle(510, 72, 200, 25));
jLabelAddress = new JLabel("联系地址");
jLabelAddress.setBounds(new Rectangle(90, 104, 68, 22));
jTextAddress = new JTextField();
jTextAddress.setName("联系地址");
jTextAddress.setBounds(new Rectangle(160, 104, 200, 25));
jLabelURL = new JLabel("网 址");
jLabelURL.setBounds(new Rectangle(440, 104, 68, 22));
jTextURL = new JTextField();
jTextURL.setName("网址");
jTextURL.setBounds(new Rectangle(510, 104, 200, 25));
jLabelEmail = new JLabel("电子邮件");
jLabelEmail.setBounds(new Rectangle(440, 136, 68, 22));
jTextEmail = new JTextField();
jTextEmail.setName("电子邮件");
jTextEmail.setBounds(new Rectangle(510, 136, 200, 25));
jLabelFax = new JLabel("传 真");
jLabelFax.setBounds(new Rectangle(90, 136, 68, 22));
jTextFax = new JTextField();
jTextFax.setName("传真");
jTextFax.setBounds(new Rectangle(160, 136, 200, 25));
jLabelPostCode = new JLabel("邮政编码");
jLabelPostCode.setBounds(new Rectangle(90, 168, 68, 22));
jTextPostCode = new JTextField();
jTextPostCode.setName("邮政编码");
jTextPostCode.setBounds(new Rectangle(160, 168, 200, 25));
jLabelBank = new JLabel("开户银行");
jLabelBank.setBounds(new Rectangle(440, 168, 68, 22));
jTextBank = new JTextField();
jTextBank.setName("开户银行");
jTextBank.setBounds(new Rectangle(510, 168, 200, 25));
jLabelBankID = new JLabel("银行帐号");
jLabelBankID.setBounds(new Rectangle(90, 200, 68, 22));
jTextBankID = new JTextField();
jTextBankID.setName("银行帐号");
jTextBankID.setBounds(new Rectangle(160, 200, 200, 25));
jBtnAdd = new JButton("添加");
jBtnAdd.setBounds(new Rectangle(100, 260, 75, 23));
jBtnAdd.addActionListener(this);
jBtnUpdate = new JButton("修改");
jBtnUpdate.setBounds(new Rectangle(210, 260, 75, 23));
jBtnUpdate.addActionListener(this);
jBtnDelete = new JButton("删除");
jBtnDelete.setBounds(new Rectangle(320, 260, 75, 23));
jBtnDelete.addActionListener(this);
jBtnSearch = new JButton("查询");
jBtnSearch.setBounds(new Rectangle(500, 260, 75, 23));
jBtnSearch.addActionListener(this);
jBtnCancel = new JButton("取消");
jBtnCancel.setBounds(new Rectangle(610, 260, 75, 23));
jBtnCancel.addActionListener(this);
jSubPanel = new JPanel(null);
jSubPanel.add(jLabelID, null);
jSubPanel.add(jTextID, null);
jSubPanel.add(jLabelName, null);
jSubPanel.add(jTextName, null);
jSubPanel.add(jTextLinkman, null);
jSubPanel.add(jTextLinkPhone, null);
jSubPanel.add(jLabelLinkman, null);
jSubPanel.add(jLabelLinkPhone, null);
jSubPanel.add(jTextAddress, null);
jSubPanel.add(jLabelFax, null);
jSubPanel.add(jTextEmail, null);
jSubPanel.add(jLabelEmail, null);
jSubPanel.add(jTextFax, null);
jSubPanel.add(jTextURL, null);
jSubPanel.add(jLabelURL, null);
jSubPanel.add(jLabelBank, null);
jSubPanel.add(jLabelBankID, null);
jSubPanel.add(jTextBank, null);
jSubPanel.add(jLabelPostCode, null);
jSubPanel.add(jLabelAddress, null);
jSubPanel.add(jTextBankID, null);
jSubPanel.add(jTextPostCode, null);
jSubPanel.add(jBtnAdd, null);
jSubPanel.add(jBtnUpdate, null);
jSubPanel.add(jBtnDelete, null);
jSubPanel.add(jBtnSearch, null);
jSubPanel.add(jBtnCancel, null);
}
return jSubPanel;
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(jBtnSearch))
{
while(tableModel.getRowCount() > 0)
{
tableModel.removeRow(0); // 先清空所有数据
}
providerDTO = new ProviderDTO();
providerDTO.setProviderID(jTextID.getText());
providerDTO.setProviderName(jTextName.getText());
providerDTO.setLinkman(jTextLinkman.getText());
providerDAO.setProviderDTO(providerDTO);
for(ProviderDTO user : providerDAO.searchProvider())
{
tableModel.addRow(user.toArray());
}
}
else if(e.getSource().equals(jBtnAdd))
{
if (jSubPanel.getName() != null)
{
JOptionPane.showMessageDialog(jSubPanel,"供应商编号“" +jTextID.getText()+ "”已存在!");
return;
}
this.userInfo();
if(providerDAO.addProvider())
{
tableModel.addRow(providerDTO.toArray());
this.clearText();
}
else
JOptionPane.showMessageDialog(jSubPanel,"供应商编号“" +jTextID.getText()+ "”添加失败!");
}
else if(e.getSource().equals(jBtnUpdate))
{
if (jSubPanel.getName() == null)
{
JOptionPane.showMessageDialog(jSubPanel,"请选择要修改的行");
return;
}
this.userInfo();
if(providerDAO.updateProvider())
{
int rowIndex = Integer.parseInt(jSubPanel.getName());
tableModel.removeRow(rowIndex);
tableModel.insertRow(rowIndex, providerDTO.toArray());
this.clearText();
}
else
JOptionPane.showMessageDialog(jSubPanel,"供应商编号“" +jTextID.getText()+ "”修改失败!");
}
else if(e.getSource().equals(jBtnDelete))
{
if (jSubPanel.getName() == null)
{
JOptionPane.showMessageDialog(jSubPanel,"请选择要删除的行");
return;
}
if(JOptionPane.showConfirmDialog(jSubPanel, "确实要删除员工编号“" + jTextID.getText() + "”吗?", "系统提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0)
{
providerDTO.setProviderID(jTextID.getText());
providerDAO.setProviderDTO(providerDTO);
if(providerDAO.deleteProvider())
{
tableModel.removeRow(Integer.parseInt(jSubPanel.getName()));
this.clearText();
}
else
JOptionPane.showMessageDialog(jSubPanel,"供应商编号“" +jTextID.getText()+ "”删除失败!");
}
}
else
this.clearText();
}
private void userInfo()
{
providerDTO.setProviderID(jTextID.getText());
providerDTO.setProviderName(jTextName.getText());
providerDTO.setLinkman(jTextLinkman.getText());
providerDTO.setLinkPhone(jTextLinkPhone.getText());
providerDTO.setFax(jTextFax.getText());
providerDTO.setEmail(jTextEmail.getText());
providerDTO.setUrl(jTextURL.getText());
providerDTO.setAddress(jTextAddress.getText());
providerDTO.setPostCode(jTextPostCode.getText());
providerDTO.setBank(jTextBank.getText());
providerDTO.setBankAccount(jTextBankID.getText());
providerDAO.setProviderDTO(providerDTO);
}
private void clearText()
{
for(Component cpt : jSubPanel.getComponents())
{
if(cpt instanceof JTextField) //JAVA(instanceof) == .NET (is)
((JTextField)cpt).setText(null); // 清空文本框
}
jSubPanel.setName(null);
jTextID.setEditable(true);
jTextID.requestFocusInWindow(); // 设置焦点
}
}