package com.vci.client.omd.btm.ui; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import org.jdesktop.swingx.JXTable; import org.jdesktop.swingx.decorator.HighlighterFactory; import com.vci.corba.common.VCIError; public class ConsistencyCheckDialog extends JDialog{ /** * */ private static final long serialVersionUID = 6997597482667501498L; Map map; private JPanel centerPanel; private JPanel southPanel; private JButton btnRepair; private JButton btnCancel; private JXTable table; private DefaultTableModel model; private final int TABLE_HEADER_HEIGHT = 25; private final int ROW_HEIGHT = 30; public ConsistencyCheckDialog(Map map){ this.map = map; initUI(); addListener(); initData(); } private void initUI(){ this.setTitle("修复对话框"); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setSize(screenSize.width/2, screenSize.height/2); this.setModal(true); this.setLocationRelativeTo(null); this.setResizable(false); this.setLayout(new BorderLayout()); centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout()); southPanel = new JPanel(); southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); this.add(centerPanel, BorderLayout.CENTER); this.add(southPanel, BorderLayout.SOUTH); table = new JXTable(); model = new DefaultTableModel(); model.setColumnCount(3); model.setColumnIdentifiers(new String[]{"类型名 ", "操作", "状态"}); table.setModel(model); table.setHorizontalScrollEnabled(true); table.setAutoscrolls(true); table.setSortable(false); table.setHighlighters(HighlighterFactory.createAlternateStriping()); table.setRowHeight(ROW_HEIGHT); //设置表头高度 JTableHeader tableHeader = table.getTableHeader(); Dimension size = tableHeader.getPreferredSize(); size.height = TABLE_HEADER_HEIGHT; tableHeader.setPreferredSize(size); centerPanel.add(new JScrollPane(table), BorderLayout.CENTER); btnRepair = new JButton("修复"); btnCancel = new JButton("关闭"); southPanel.add(btnRepair); southPanel.add(btnCancel); } private void initData(){ table.setEditable(true); model.setRowCount(map.size()); int i = 0; for(Iterator ite = map.keySet().iterator(); ite.hasNext();){ String typeName = ite.next(); String dml = map.get(typeName); table.setValueAt(typeName, i, 0); String dml_ = dml.replace("_CREATE", "创建表"); dml_ = dml_.replace("_ADD", "增加列"); dml_ = dml_.replace("_DROP", "移除列"); table.setValueAt(dml_, i, 1); table.setValueAt("未修复", i, 2); i++; } table.setEditable(false); } private void addListener() { btnRepair.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(map.size() < 1){ JOptionPane.showMessageDialog(getDialog(), "已经全部成功修复", "已经全部成功修复", JOptionPane.INFORMATION_MESSAGE); return; } if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog( getDialog(), "修复将改变数据库的结构", "修复确认框", JOptionPane.YES_NO_OPTION )){ return; } List list = getRepairDML(); if(list.size() < 1){ return; } try { String[] result = BtmClient.getService().executeRepair(list.toArray(new String[0])); updateStatus(result); } catch (VCIError e1) { e1.printStackTrace(); } } }); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dipose_(); } }); } private void dipose_(){ this.dispose(); } /** * 获取需要修复的伪sql * @return */ private List getRepairDML() { List list = new ArrayList(); for(Iterator ite = map.keySet().iterator(); ite.hasNext();){ String type = ite.next(); String dml = map.get(type); list.add(type + "/DML" + dml); } return list; } /** * 根据修复的结果, 更新table中的状态 * 并且将已成功的type, 从 map中移除 * 部分成功的, 移除map中value的成功部分 * @param result */ private void updateStatus(String[] result) { table.setEditable(true); List list = Arrays.asList(result); for(int i = 0; i < table.getRowCount(); i++){ String typeName = (String) table.getValueAt(i, 0); if(list.contains(typeName)){ table.setValueAt("已修复", i, 2); map.remove(typeName); }else if(list.contains(typeName + "_ADD")){ table.setValueAt("增加已修复", i, 2); String sql = map.get(typeName); sql = sql.substring(sql.indexOf(";") + 1, sql.length()); map.put(typeName, sql); }else if(list.contains(typeName + "_DROP")){ table.setValueAt("移除已修复", i, 2); String sql = map.get(typeName); sql = sql.substring(0, sql.indexOf(";")); map.put(typeName, sql); }else{ if(!((String) table.getValueAt(i, 2)).equals("已修复")){ table.setValueAt("修复失败", i, 2); } } } table.updateUI(); table.setEditable(false); } private JDialog getDialog(){ return this; } }