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.List; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; import org.jdesktop.swingx.JXTable; import org.jdesktop.swingx.decorator.HighlighterFactory; import com.vci.client.omd.linktype.LinkTypeStart; import com.vci.client.ui.table.CheckBoxEditor; import com.vci.client.ui.table.CheckBoxRenderer; import com.vci.corba.common.VCIError; public class DeleteDataDialog extends JDialog{ /** * */ private static final long serialVersionUID = 2315167245802578900L; private String[] tables = new String[]{"pl_qtemplate", "PL_QTEMPLATEDEF", "pl_typeright", "plcommandparameter", "plpagedefination", "pluilayout", "pltabbutton", "pltabpage", "plportalvi", "plreleasedobj" }; /** * workFlowTables 中的表有主外键关系,当表主键或唯一键被引用为外键时, * 就算引用方没有数据, truncate还不是不能删除表中数据, 而 delete可以. */ private String[] workFlowTables = new String[]{ //流程(自定义表) "PLPROCESSTEMPLATE", "PLPROCESSCATEGORY", "PLPROCESSTASK",//"PLRMTEMPLATEPROCESS", "PLPROCESSTASKPROPERTY", "PLFLOWAPPROVEOPINION", "PLFLOWOBJECT", "PLFLOWINSTANCE", "WORKFLOW_TASK_CC_TABLE", "PLTASKDESC", "PLTASKSASSIGNED", "JBPM_SUBPROCESSTEM", "JBPM4_TASKANDUSERCONFIG", "PLREVOKEPOLICY", "JBPM4_TASKANDUSERCONFIGEX", //流程(JBPM系统表) "JBPM4_DEPLOYPROP", "JBPM4_HIST_DETAIL", "JBPM4_ID_MEMBERSHIP", "JBPM4_JOB", "JBPM4_PARTICIPATION", "JBPM4_VARIABLE", "JBPM4_HIST_ACTINST", "JBPM4_HIST_VAR", "JBPM4_ID_GROUP", "JBPM4_ID_USER", "JBPM4_LOB", "JBPM4_DEPLOYMENT", "JBPM4_HIST_PROCINST", "JBPM4_HIST_TASK", "JBPM4_TASK", "JBPM4_SWIMLANE", "JBPM4_EXECUTION", "JBPM4_PROPERTY" }; private List btNames, ltNames; private JPanel centerPanel; private JPanel southPanel; private JButton btnDelete; private JButton btnCancel; private JXTable table; private TableModelWithCheckBox model; private final int TABLE_HEADER_HEIGHT = 25; private final int ROW_HEIGHT = 30; private JButton btnSelAll; private JButton btnSelOpp; private JPanel northPanel; /** * * @param btNames * @param ltNames * @param exceptBts : 保留的业务类型 */ public DeleteDataDialog(List btNames, List ltNames){ this.btNames = btNames; this.ltNames = ltNames; 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()); northPanel = new JPanel(); northPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); centerPanel = new JPanel(); centerPanel.setLayout(new BorderLayout()); southPanel = new JPanel(); southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); this.add(northPanel, BorderLayout.NORTH); this.add(centerPanel, BorderLayout.CENTER); this.add(southPanel, BorderLayout.SOUTH); btnSelAll = new JButton("全选"); btnSelOpp = new JButton("反选"); northPanel.add(btnSelAll); northPanel.add(btnSelOpp); table = new JXTable(); model = new TableModelWithCheckBox(); model.setColumnCount(4); model.setColumnIdentifiers(new String[]{"", "类型名 ", "类型", "清除状态"}); table.setModel(model); TableColumn column = table.getColumn(0); column.setCellEditor(new CheckBoxEditor(new JCheckBox())); column.setCellRenderer(new CheckBoxRenderer()); column.setPreferredWidth(10); 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); btnDelete = new JButton("清除"); btnCancel = new JButton("关闭"); southPanel.add(btnDelete); southPanel.add(btnCancel); } private void initData(){ // 重新设置table的Cell的可编辑性 model.setInitFlag(false); int btSize = btNames.size(); int ltSize = ltNames.size(); model.setRowCount(btSize + ltSize + tables.length + workFlowTables.length); for(int i = 0; i < btSize; i++){ table.setValueAt(new JCheckBox(), i, 0); table.setValueAt(btNames.get(i), i, 1); table.setValueAt("业务类型", i, 2); table.setValueAt("未清除", i, 3); } for(int i = 0; i < ltSize; i++){ table.setValueAt(new JCheckBox(), i + btSize, 0); table.setValueAt(ltNames.get(i), i + btSize, 1); table.setValueAt("链接类型", i + btSize, 2); table.setValueAt("未清除", i + btSize, 3); } for(int i = 0; i< tables.length; i++){ table.setValueAt(new JCheckBox(), i + btSize + ltSize, 0); table.setValueAt(tables[i], i + btSize + ltSize, 1); table.setValueAt("-", i + btSize + ltSize, 2); table.setValueAt("未清除", i + btSize + ltSize, 3); } for(int i = 0; i< workFlowTables.length; i++){ table.setValueAt(new JCheckBox(), i + btSize + ltSize + tables.length, 0); table.setValueAt(workFlowTables[i], i + btSize + ltSize + tables.length, 1); table.setValueAt("-", i + btSize + ltSize + tables.length, 2); table.setValueAt("未清除", i + btSize + ltSize + tables.length, 3); } // 重新设置table的Cell的可编辑性 model.setInitFlag(true); } private void addListener() { btnSelAll.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { selectAll(); } }); btnSelOpp.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { selectOpposite(); } }); btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!hasSelect()){ JOptionPane.showMessageDialog(getInstance(), "请选择要清楚的数据类型", "未选中数据", JOptionPane.INFORMATION_MESSAGE); return; } int option = JOptionPane.showConfirmDialog(getInstance(), "确认删除所选类型数据?该操作不可回滚", "删除确认框", JOptionPane.YES_NO_OPTION); if(option != JOptionPane.YES_OPTION){ return; } int btSize = btNames.size(); int ltSize = ltNames.size(); //truncate业务类型数据 for(int i = 0; i < btSize; i++){ if(!((JCheckBox)table.getValueAt(i, 0)).isSelected()){ continue; } String btName = (String) table.getValueAt(i, 1); //String btmTableName = OmdTools.getBTTableName(btName); try { if(BtmClient.getService().truncateTable(btName)){ updateTable(i, 3, "成功"); } } catch (VCIError e1) { e1.printStackTrace(); updateTable(i, 3, "失败"); } } //truncate链接类型数据 for(int i = 0; i < ltSize; i++){ if(!((JCheckBox)table.getValueAt(i + btSize, 0)).isSelected()){ continue; } String ltName = (String) table.getValueAt(i + btSize, 1); //String ltTableName = OmdTools.getLTTableName(ltName); try { if(LinkTypeStart.getService().truncateTable(ltName)){ updateTable(i + btSize, 3, "成功"); } } catch (VCIError e1) { e1.printStackTrace(); updateTable(i + btSize, 3, "失败"); } } //truncate QT, RIGHT, UI数据 // for(int i = 0; i < tables.length; i++){ // if(!((JCheckBox)table.getValueAt(i + btSize + ltSize, 0)).isSelected()){ // continue; // } // String tableName = (String) table.getValueAt(i + btSize + ltSize, 1); // try { // if(BtmClient.getService().truncateTable(tableName)){ // updateTable(i + btSize + ltSize, 3, "成功"); // } // } catch (VCIError e1) { // e1.printStackTrace(); // updateTable(i + btSize + ltSize, 3, "失败"); // } // } //workflow数据 // for(int i = 0; i < workFlowTables.length; i++){ // if(!((JCheckBox)table.getValueAt(i + btSize + ltSize + tables.length, 0)).isSelected()){ // continue; // } // String tableName = (String) table.getValueAt(i + btSize + ltSize + tables.length, 1); // try { // if(BtmClient.getService().deleteTable(tableName)){ // updateTable(i + btSize + ltSize + tables.length, 3, "成功"); // } // } catch (VCIError e1) { // e1.printStackTrace(); // updateTable(i + btSize + ltSize + tables.length, 3, "失败"); // } // } JOptionPane.showMessageDialog(getInstance(), "清楚数据完成", "清楚数据完成", JOptionPane.INFORMATION_MESSAGE); } }); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { dipose_(); } }); } /** * 判断table中是否有选择的记录 * @return */ protected boolean hasSelect() { for(int i = 0; i < table.getRowCount(); i++){ if(((JCheckBox)table.getValueAt(i, 0)).isSelected()){ return true; } } return false; } /** * 更新table单元格的值 * @param row * @param column * @param value */ private void updateTable(int row, int column, String value) { // 重新设置table的Cell的可编辑性 model.setInitFlag(false); table.setValueAt(value, row, column); model.setInitFlag(true); table.updateUI(); } /** * 全选 */ private void selectAll() { for(int i = 0; i < table.getRowCount(); i++){ ((JCheckBox)table.getValueAt(i, 0)).setSelected(true); } table.updateUI(); } /** * 反选 */ private void selectOpposite() { for(int i = 0; i < table.getRowCount(); i++){ JCheckBox chb = ((JCheckBox)table.getValueAt(i, 0)); if(chb.isSelected()){ chb.setSelected(false); }else{ chb.setSelected(true); } } table.updateUI(); } private void dipose_(){ this.dispose(); } private JDialog getInstance(){ return this; } }