package pcdiysystem.Panel;
import java.awt.*;
import pcdiysystem.Dao.MotherboardsDAO;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import java.util.Vector;
import pcdiysystem.Dao.ProcessorsDAO;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.DefaultCellEditor;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.event.MouseEvent;
import pcdiysystem.Dao.DefaultProjectDAO;
import pcdiysystem.Panel.TableModelOfEditPanel;
import pcdiysystem.Panel.ComponentListPanel_cboManufacturer_itemAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import pcdiysystem.Dao.MemoryDAO;
/**
* Title: 自助装机系统
*
* Description:
*
* Copyright: Copyright (c) 2006
*
* Company:
*
* @author kamiiiyu
* @version 1.0
*/
public class EditPanel
extends ComponentListPanel {
JButton btnAdd = new JButton("添加记录");
JButton btnNextRecord = new JButton("再添加一条记录");
JButton btnReload = new JButton("刷新");
JButton btnWrite = new JButton("把数据写入数据库");
DefaultTableModel editTableModel;
JTable tabAddRecord;
EditPanel_cboManufacturer_mouseAdapter cboMouseEvent;
public EditPanel() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
btnSelect.setText("修改当前选中记录");
btnCancel.setText("删除当前选中记录");
creatNewComponentTable("Intel");
pnlBottom.add(btnReload);
pnlBottom.add(btnAdd);
pnlBottom.add(btnNextRecord);
pnlBottom.add(btnWrite);
btnNextRecord.setEnabled(false);
btnWrite.setEnabled(false);
btnAdd.addActionListener(new EditPanel_btnAdd_actionAdapter(this));
btnReload.addActionListener(new EditPanel_btnReload_actionAdapter(this));
btnNextRecord.addActionListener(new EditPanel_btnNextRecord_actionAdapter(this));
btnWrite.addActionListener(new EditPanel_btnWrite_actionAdapter(this));
}
void creatNewComponentTable(String manufacturer) {
int index = cboComponents.getSelectedIndex();
Vector colName = new Vector();
Vector cellData = new Vector();
switch (index) {
case 0:
ProcessorsDAO pd = new ProcessorsDAO();
cellData = pd.getBaseInfomation(manufacturer);
colName.add("型号");
colName.add("主频");
colName.add("前端总线");
colName.add("接口类型");
colName.add("库存");
colName.add("价格");
break;
case 1: {
break;
}
case 2: {
MotherboardsDAO md = new MotherboardsDAO();
cellData = md.getBaseInfomation(manufacturer, MotherboardsDAO.EDITPANEL);
colName.add("数据库记录编号");
colName.add("型号");
colName.add("CPU插槽类型");
colName.add("北桥芯片");
colName.add("支持CPU类型");
colName.add("支持显卡标准");
colName.add("硬件描述");
colName.add("库存");
colName.add("价格");
}
case 3: {
MemoryDAO md = new MemoryDAO();
cellData = md.getBaseInfomation(manufacturer);
colName.add("型号");
colName.add("类型");
colName.add("容量");
colName.add("总线频率");
colName.add("硬件描述");
colName.add("库存");
colName.add("价格");
break;
}
}
editTableModel = new TableModelOfEditPanel();
editTableModel.setDataVector(cellData, colName);
tabComData = new JTable(editTableModel);
tabComData.addMouseMotionListener(new
ComponentListPanel_tabComData_mouseMotionAdapter(this));
tabComData.addMouseListener(new ComponentListPanel_tabComData_mouseAdapter(this));
for (int i = 0; i < colName.size(); i++) {
tabComData.getColumnModel().getColumn(i).setPreferredWidth(150);
}
tabComData.getColumnModel().getColumn(4).setPreferredWidth(200);
tabComData.setAutoResizeMode(tabComData.AUTO_RESIZE_OFF);
tabComData.setDragEnabled(false);
tabComData.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (cellData.size() != 0) {
tabComData.setRowSelectionInterval(0, 0);
}
tabComData.setDragEnabled(false);
tabComData.getTableHeader().setReorderingAllowed(false);
scpListOfComponent.getViewport().add(tabComData);
}
public void creatInsertTable() {
JTextField txtTemp = new JTextField("tt");
txtTemp.setToolTipText("标识列,不允许修改!");
txtTemp.setEditable(false);
tabAddRecord = new JTable(1, 9);
tabAddRecord.getColumnModel().getColumn(0).setCellEditor(new
DefaultCellEditor(txtTemp));
for (int i = 0; i < 9; i++) {
tabAddRecord.getColumnModel().getColumn(i).setPreferredWidth(150);
}
tabAddRecord.getColumnModel().getColumn(4).setPreferredWidth(200);
tabAddRecord.setValueAt("¥", 0, 8);
tabAddRecord.setTableHeader(tabComData.getTableHeader());
tabAddRecord.setAutoResizeMode(tabAddRecord.AUTO_RESIZE_OFF);
tabAddRecord.setDragEnabled(false);
tabAddRecord.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tabAddRecord.setDragEnabled(false);
tabAddRecord.getTableHeader().setReorderingAllowed(false);
scpListOfComponent.getViewport().add(tabAddRecord);
}
public void btnSelect_actionPerformed(ActionEvent e) {
int selectedRow = tabComData.getSelectedRow();
Object recordId = tabComData.getValueAt(selectedRow, 0);
Vector data = new Vector();
for (int i = 1; i < tabComData.getColumnCount(); i++) {
data.add(tabComData.getValueAt(selectedRow, i));
}
Object[] buttonText = {
"是的,不改了", "让我再想想"};
int index = JOptionPane.showOptionDialog(this,
"您确定修改数据库编号为" + recordId + "的记录吗?",
"",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
buttonText, buttonText[0]);
if (index == 0) {
MotherboardsDAO md = new MotherboardsDAO();
boolean isSuceed = md.updateRecord(Integer.parseInt(String.valueOf(
recordId)), data);
if (isSuceed) {
JOptionPane.showMessageDialog(this, "已经成功修改数据");
}
else {
JOptionPane.showMessageDialog(this, "修改数据失败,请检查输入的数据是否合法");
}
}
}
public void btnCancel_actionPerformed(ActionEvent e) {
int selectedRow = tabComData.getSelectedRow();
Object recordId = tabComData.getValueAt(selectedRow, 0);
Object[] buttonText = {
"是的,我要删除", "让我再想想"};
int index = JOptionPane.showOptionDialog(this,
"您确定删除数据据库编号为" + recordId + "的记录吗?",
"",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
buttonText, buttonText[0]);
if (index == 0) {
MotherboardsDAO md = new MotherboardsDAO();
boolean isSuceed = md.deleteRecord(Integer.parseInt(String.valueOf(
recordId)));
if (isSuceed) {
JOptionPane.showMessageDialog(this, "已经成功删除数据");
}
else {
JOptionPane.showMessageDialog(this, "删除数据失败");
}
System.out.println(isSuceed);
}
}
public void btnAdd_actionPerformed(ActionEvent e) {
if (btnSelect.isEnabled()) {
cboMouseEvent = new EditPanel_cboManufacturer_mouseAdapter(this);
JOptionPane.showMessageDialog(this, "进入添加记录模式");
this.creatInsertTable();
btnNextRecord.setEnabled(true);
btnWrite.setEnabled(true);
btnAdd.setText("退出添加记录模式");
cboComponents.setEnabled(false);
for (int i = 0; i < cboManufacturer.getItemListeners().length; i++) {
cboManufacturer.removeItemListener(cboManufacturer.
getItemListeners()[i]);
}
cboManufacturer.addItemListener(new EditPanel_cboManufacturer_itemAdapter(this));
cboManufacturer.addMouseListener(cboMouseEvent);
btnSelect.setEnabled(false);
btnCancel.setEnabled(false);
}
else {
JOptionPane.showMessageDialog(this, "退出添加记录模式");
btnAdd.setText("添加记录");
btnNextRecord.setEnabled(false);
btnWrite.setEnabled(false);
cboComponents.setEnabled(true);
btnSelect.setEnabled(true);
btnCancel.setEnabled(true);
for (int i = 0; i < cboManufacturer.getItemListeners().length; i++) {
cboManufacturer.removeItemListener(cboManufacturer.
getItemListeners()[i]);
}
cboManufacturer.removeMouseListener(cboMouseEvent);
if (cboManufacturer.getItemCount() != 0) {
this.creatNewComponentTable(String.valueOf(cboManufacturer.
getSelectedItem()));
}
cboManufacturer.addItemListener(new
ComponentListPanel_cboManufacturer_itemAdapter(this));
}
}
public void btnReload_actionPerformed(ActionEvent e) {
if (cboManufacturer.getItemCount() != 0) {
this.creatNewComponentTable(String.valueOf(cboManufacturer.
getSelectedItem()));
}
}
public void btnNextRecord_actionPerformed(ActionEvent e) {
DefaultTableModel temp = ( (DefaultTableModel) tabAddRecord.getModel());
temp.addRow(new Vector());
tabAddRecord.setValueAt("¥", temp.getRowCount() - 1, 8);
}
public void btnWrite_actionPerformed(ActionEvent e) {
Object[] buttonText = {
"是的,我确定", "不,让我再想想"};
int index = JOptionPane.showOptionDialog(this, "您确定把表格中的数据添加到数据库吗?", "",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null,
buttonText, buttonText[0]);
if (index == 0) {
int i;
int lastColumn = tabAddRecord.getModel().getColumnCount();
int rowCount = tabAddRecord.getModel().getRowCount();
for (i = 0; i < rowCount; i++) {
MotherboardsDAO md = new MotherboardsDAO(this);
if (!md.isPriceFormatCorrect(String.valueOf(tabAddRecord.getModel().
getValueAt(i, lastColumn - 1)))) {
break;
}
}
if (i == rowCount) {
int id = cboManufacturer.getSelectedIndex();
MotherboardsDAO md = new MotherboardsDAO();
Vector insertData = md.getDataFromTableMOdel(tabAddRecord.getModel());
md.insertFile(String.valueOf(id), insertData);
JOptionPane.showMessageDialog(this, "已经成功写入数据库");
System.out.println( ( (Vector) insertData.get(0)).get(0));
}
}
}
public void tabComData_mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1 && e.getButton() == e.BUTTON1) {
int row = tabComData.getSelectedRow();
String text = String.valueOf(tabComData.getValueAt(row, 6));
String[] words = text.split("/");
txtDetail.setText("");
for (int i = 0; i < words.length; i++) {
txtDetail.append(words[i] + "\n");
}
}
}
public void tabComData_mouseMoved(MouseEvent e) {
int rowIndex = this.tabComData.getSelectedRow();
int lastColumnIndex = this.tabComData.getColumnCount() - 1;
tabComData.setToolTipText("数据库记录编号:" + tabComData.getValueAt(rowIndex, 0));
}
public void EditPanel_cboManufacturer_itemStateChanged(ItemEvent e) {
creatInsertTable();
}
public void cboManufacturer_mouseEntered(MouseEvent e) {
JOptionPane.showMessageDialog(this, "做出选择后未保存数据将会丢失!", "警告",
JOptionPane.INFORMATION_MESSAGE);
}
}
class TableModelOfEditPanel
extends DefaultTableModel {
public TableModelOfEditPanel() {
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return false;
}
return true;
}
}
class EditPanel_btnAdd_actionAdapter
implements ActionListener {
private EditPanel adaptee;
EditPanel_btnAdd_actionAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnAdd_actionPerformed(e);
}
}
class EditPanel_btnReload_actionAdapter
implements ActionListener {
private EditPanel adaptee;
EditPanel_btnReload_actionAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnReload_actionPerformed(e);
}
}
class EditPanel_btnNextRecord_actionAdapter
implements ActionListener {
private EditPanel adaptee;
EditPanel_btnNextRecord_actionAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnNextRecord_actionPerformed(e);
}
}
class EditPanel_btnWrite_actionAdapter
implements ActionListener {
private EditPanel adaptee;
EditPanel_btnWrite_actionAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnWrite_actionPerformed(e);
}
}
class EditPanel_cboManufacturer_itemAdapter
implements ItemListener {
private EditPanel adaptee;
EditPanel_cboManufacturer_itemAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void itemStateChanged(ItemEvent e) {
adaptee.EditPanel_cboManufacturer_itemStateChanged(e);
}
}
class EditPanel_cboManufacturer_mouseAdapter
extends MouseAdapter {
private EditPanel adaptee;
EditPanel_cboManufacturer_mouseAdapter(EditPanel adaptee) {
this.adaptee = adaptee;
}
public void mouseEntered(MouseEvent e) {
adaptee.cboManufacturer_mouseEntered(e);
}
}