package com.vci.client.omd.enumManager.ui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.HeadlessException; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; import org.jdesktop.swingx.JXTable; import org.jdesktop.swingx.decorator.HighlighterFactory; import com.vci.mw.InvocationUtility; import com.vci.client.common.providers.ServiceProvider; import com.vci.client.omd.enumManager.toOutside.InterEM; import com.vci.client.omd.enumManager.toOutside.InterEMManager; import com.vci.client.omd.enumManager.wrapper.EnumChildWrapper; import com.vci.client.ui.table.CheckBoxEditor; import com.vci.client.ui.table.CheckBoxRenderer; import com.vci.corba.common.VCIError; import com.vci.corba.omd.etm.EnumChild; import com.vci.corba.omd.etm.EnumItem; public class EnumItemPanel extends JPanel{ /** * */ private static final long serialVersionUID = -6093961120146139979L; private static EnumItemPanel enumItemPanel = null; private JPanel northPanel, centerPanel, southPanel; private JTextField tfName, tfLabel, tfLength; private JComboBox cbType; private JPanel cen_centerPanel, cen_southPanel; private JXTable emChildTable; private EmItemTableModel tableModel; private final int TABLE_HEADER_HEIGHT = 25; private final int ROW_HEIGHT = 30; private JButton btnCreate, btnModify, btnDelete; private JButton btnOK, btnCancel; public ArrayList emChildList = new ArrayList(); public final String ENUMITEM = "enumItem"; private EnumItem emItem = null; //0:显示; 1: 创建; 2: 修改 //保存当前状态״̬ private int flag; private EnumItemPanel(){ initUI(); initTableData(); addListener(); } public static EnumItemPanel getInstance(){ if(enumItemPanel == null){ enumItemPanel = new EnumItemPanel(); } return enumItemPanel; } private void initUI(){ this.setBackground(Color.WHITE); this.setLayout(new BorderLayout(0, 0)); this.setBorder(BorderFactory.createTitledBorder("枚举信息")); //northPanel:名称, 标签, 返回类型 northPanel = new JPanel(); this.add(northPanel, BorderLayout.NORTH); northPanel.setLayout(new GridBagLayout()); int gridy = 0; northPanel.add(new JLabel("名称"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3)); tfName = new JTextField(30); northPanel.add(tfName, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3)); gridy++; northPanel.add(new JLabel("标签"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3)); tfLabel = new JTextField(30); northPanel.add(tfLabel, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3)); gridy++; northPanel.add(new JLabel("类型"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3)); cbType = new JComboBox(); cbType.addItem("Integer"); cbType.addItem("String"); northPanel.add(cbType, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3)); gridy++; northPanel.add(new JLabel("长度"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3)); tfLength = new JTextField(30); northPanel.add(tfLength, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3)); // // GridBagConstraints g = new GridBagConstraints(); // /*g.anchor = GridBagConstraints.EAST;*/ // g.anchor = GridBagConstraints.WEST; //add by caill 2016.1.14 将第一列控件左对齐 // g.insets = new Insets(10, 5, 0, 5); // g.gridx = 0; // g.gridy = 0; // northPanel.add(new JLabel("名称"), g); // // tfName = new JTextField(30); //add by caill 2016.1.14将文本框长度增加 // GridBagConstraints g1 = new GridBagConstraints(); // g1.anchor = GridBagConstraints.WEST; // g1.insets = new Insets(10, 5, 0, 5); // g1.gridx = 1; // g1.gridy = 0; // g1.weighty = 1.0; // //g1.fill = GridBagConstraints.HORIZONTAL; // // g1.gridwidth = GridBagConstraints.REMAINDER; // northPanel.add(tfName, g1); // g.gridy = 1; // northPanel.add(new JLabel("标签"), g); // tfLabel = new JTextField(30); // g1.gridy = 1; // northPanel.add(tfLabel, g1); // g.gridy = 2; // northPanel.add(new JLabel("类型"), g); // cbType = new JComboBox(); // cbType.addItem("Integer"); // cbType.addItem("String"); // g1.gridy = 2; // northPanel.add(cbType, g1); // g.gridy = 3; // northPanel.add(new JLabel("长度"), g); // tfLength = new JTextField(30); // g1.gridy = 3; // northPanel.add(tfLength, g1); //centerPanel:枚举项列表 ,增加枚举项,修改枚举项,删除枚举项按钮 centerPanel = new JPanel(); this.add(centerPanel, BorderLayout.CENTER); centerPanel.setLayout(new BorderLayout()); //cen_centerPanel:枚举项列表 cen_centerPanel = new JPanel(); centerPanel.setBorder(BorderFactory.createTitledBorder("枚举项")); //cen_southPanel:增加枚举项,修改枚举项,删除枚举项按钮 cen_southPanel = new JPanel(); centerPanel.add(cen_centerPanel, BorderLayout.CENTER); centerPanel.add(cen_southPanel, BorderLayout.SOUTH); tableModel = new EmItemTableModel(); emChildTable = new JXTable(tableModel); TableColumn column = emChildTable.getColumn(0); column.setCellEditor(new CheckBoxEditor(new JCheckBox())); column.setCellRenderer(new CheckBoxRenderer()); column.setMinWidth(30); column.setMaxWidth(30); column.setPreferredWidth(30); emChildTable.setRowHeight(ROW_HEIGHT); emChildTable.setHorizontalScrollEnabled(true); emChildTable.setHighlighters(HighlighterFactory.createAlternateStriping()); //排序后,引起其他页面的数据不显示 emChildTable.setSortable(false); //设置表头高度 JTableHeader tableHeader = emChildTable.getTableHeader(); Dimension size = tableHeader.getPreferredSize(); size.height = TABLE_HEADER_HEIGHT; tableHeader.setPreferredSize(size); cen_centerPanel.setBorder(new EmptyBorder(5, 5, 0, 5)); cen_centerPanel.setAutoscrolls(true); cen_centerPanel.setLayout(new BorderLayout(5, 5)); cen_centerPanel.add(new JScrollPane(emChildTable), BorderLayout.CENTER); btnCreate = new JButton("增加"); btnModify = new JButton("修改"); btnDelete = new JButton("删除"); cen_southPanel.add(btnCreate); cen_southPanel.add(btnModify); cen_southPanel.add(btnDelete); //southPanel: 确定,取消按钮 southPanel = new JPanel(); btnOK = new JButton("确定"); btnCancel = new JButton("取消"); southPanel.add(btnOK); southPanel.add(btnCancel); this.add(southPanel, BorderLayout.SOUTH); } private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, int padxy) { return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy); } private void addListener(){ btnCreate.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(!checkLength()){ return; } int length = Integer.valueOf(tfLength.getText()); EmItemDialog emItemDialog = new EmItemDialog(null, (String)cbType.getSelectedItem(), length); emItemDialog.setVisible(true); } }); btnModify.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int row = emChildTable.getSelectedRow(); if(row >= 0){ EnumChild emItem = ((EnumChildWrapper)emChildTable.getValueAt(row, 1)).emChild; //TODO 用事件机制,对该emItem进行使用情况检查 if(!checkLength()){ return; } int length = Integer.valueOf(tfLength.getText()); EmItemDialog emItemDialog = new EmItemDialog(emItem, (String)cbType.getSelectedItem(), length); emItemDialog.setVisible(true); }else{ JOptionPane.showMessageDialog(null, "请选中数据", "无数据", JOptionPane.WARNING_MESSAGE); } } }); btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(getInstance(), "是否确认删除选择的枚举项", "确认框", JOptionPane.YES_NO_OPTION)){ return; } deleteEmItems(); initTableData(); } }); btnOK.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //创建枚举 if(flag == 1){ if(!checkName()){ return; } emItem = new EnumItem(); emItem.name = tfName.getText(); emItem.label = tfLabel.getText(); emItem.type = (String)cbType.getSelectedItem(); if(tfLength.getText().equals("")){ emItem.length = 50; }else{ emItem.length = Integer.valueOf(tfLength.getText()); } if(!checkEMChildValue(emItem.type, emItem.length)){ return; } emItem.children = emChildList.toArray(new EnumChild[0]); String userName = InvocationUtility.getInvocation().userName; emItem.creator = userName; emItem.modifier = userName; try { boolean addSuccess = ServiceProvider.getOMDService().getEnumService().addEmItem(emItem); if(!addSuccess){ JOptionPane.showMessageDialog(null, "添加枚举失败", "添加枚举失败", JOptionPane.ERROR_MESSAGE); return; } JOptionPane.showMessageDialog(null, "添加枚举成功", "添加枚举成功", JOptionPane.INFORMATION_MESSAGE); EnumManagerPanel.getInstance().initTableData(); EnumItem selected = EnumManagerPanel.getInstance().getSelectedEnum(); updateDataAndUI(selected, 0); EnumManagerPanel.getInstance().setFlag(0); } catch (VCIError e1) { e1.printStackTrace(); } //修改枚举 }else if(flag == 2){ if(emItem == null){ JOptionPane.showMessageDialog(null, "请选择要修改的枚举", "选择枚举", JOptionPane.WARNING_MESSAGE); } emItem.label = tfLabel.getText(); emItem.type = (String)cbType.getSelectedItem(); if(tfLength.getText().equals("")){ emItem.length = 0; }else{ emItem.length = Integer.valueOf(tfLength.getText()); } if(!checkEMChildValue(emItem.type, emItem.length)){ return; } emItem.children = emChildList.toArray(new EnumChild[0]); emItem.modifier = InvocationUtility.getInvocation().userName; try { boolean mdSuccess = ServiceProvider.getOMDService().getEnumService().modifyEmItem(emItem); if(!mdSuccess){ JOptionPane.showMessageDialog(null, "修改枚举失败", "修改枚举失败", JOptionPane.ERROR_MESSAGE); return; } //更改使用枚举的属性值域 ArrayList interEMList = InterEMManager.getInstance().getInterEMList(); for(InterEM inter : interEMList){ inter.updateAtt(emItem); } JOptionPane.showMessageDialog(null, "修改枚举成功", "修改枚举成功", JOptionPane.INFORMATION_MESSAGE); EnumManagerPanel.getInstance().initTableData(); EnumItem selected = EnumManagerPanel.getInstance().getSelectedEnum(); updateDataAndUI(selected, 0); EnumManagerPanel.getInstance().setFlag(0); } catch (VCIError e1) { e1.printStackTrace(); } } } }); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(flag == 1){ updateDataAndUI(null, 0); }else if(flag == 2){ updateDataAndUI(emItem, 0); } } }); } public void addCancelActionListener(ActionListener listener) { btnCancel.addActionListener(listener); } public void initTableData(){ if(emChildList == null){ return; } tableModel.setRowCount(emChildList.size()); //重新设置table的Cell的可编辑性 tableModel.setInitFlag(false); EnumChild emChild; for(int i = 0; i < emChildList.size(); i++){ emChild = emChildList.get(i); emChildTable.setValueAt(new JCheckBox(), i, 0); emChildTable.setValueAt(new EnumChildWrapper(emChild), i, 1); emChildTable.setValueAt(emChild.value, i, 2); emChildTable.setValueAt(emChild.description, i, 3); } //重新设置table的Cell的可编辑性 tableModel.setInitFlag(true); emChildTable.updateUI(); } /** * @param emItem : 用来显示和修改属性项 * @param flag : 当前状态(创建,修改,删除) */ public void updateDataAndUI(EnumItem emItem, int flag){ this.flag = flag; if(emItem != null){ tfName.setText(emItem.name); tfLabel.setText(emItem.label); cbType.setSelectedItem(emItem.type); tfLength.setText(String.valueOf(emItem.length)); emChildList = new ArrayList(); for(int i = 0; i < emItem.children.length; i++){ emChildList.add(emItem.children[i]); } initTableData(); this.emItem = emItem; }else{ tfName.setText(""); tfLabel.setText(""); tfLength.setText("50"); emChildList = new ArrayList(); initTableData(); } if(flag == 0){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(false); tfLabel.setEditable(false); cbType.setEnabled(false); tfLength.setEditable(false); } btnCreate.setVisible(false); btnModify.setVisible(false); btnDelete.setVisible(false); btnOK.setVisible(false); btnCancel.setVisible(false); } if(flag == 1){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(true); tfLabel.setEditable(true); cbType.setEnabled(true); tfLength.setEditable(true); } btnCreate.setVisible(true); btnModify.setVisible(true); btnDelete.setVisible(true); btnOK.setVisible(true); btnCancel.setVisible(true); } if(flag == 2){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(false); tfLabel.setEditable(true); cbType.setEnabled(true); tfLength.setEditable(true); } btnCreate.setVisible(true); btnModify.setVisible(true); btnDelete.setVisible(true); btnOK.setVisible(true); btnCancel.setVisible(true); } } private boolean checkName(){ if(tfName.getText().equals("")){ JOptionPane.showMessageDialog(this, "请输入枚举名", "注意", JOptionPane.WARNING_MESSAGE); return false; } String name = tfName.getText(); if(!name.matches("[a-z A-Z]*")){ JOptionPane.showMessageDialog(this, "枚举名只能为英文字母", "注意", JOptionPane.WARNING_MESSAGE); return false; } try { if(ServiceProvider.getOMDService().getEnumService().checkRowIsExists(tfName.getText())){ JOptionPane.showMessageDialog(this, "枚举名已经存在", "注意", JOptionPane.WARNING_MESSAGE); return false; } } catch (VCIError e) { e.printStackTrace(); } catch (HeadlessException e) { e.printStackTrace(); } return true; } /** * 删除checkBox选中的属性项, * 没有选中项时,提示 */ private void deleteEmItems(){ boolean hasSeletedChildFlag = false; for(int i = 0; i < emChildTable.getRowCount(); i++){ if(((JCheckBox)emChildTable.getValueAt(i, 0)).isSelected()){ //TODO 用事件机制,对该emItem进行使用情况检查 emChildList.remove((((EnumChildWrapper)emChildTable.getValueAt(i, 1)).emChild)); hasSeletedChildFlag = true; } } if(!hasSeletedChildFlag){ JOptionPane.showMessageDialog(this, "请选中要删除的枚举项", "未选枚举项", JOptionPane.WARNING_MESSAGE); } } /** * 检查枚举项的值类型 */ private boolean checkEMChildValue(String type, long length){ String value; for(int i = 0; i < emChildList.size(); i++){ value = emChildList.get(i).value; if(value.equals("")){ JOptionPane.showMessageDialog(this, "枚举项值不能空", "枚举项值不能空", JOptionPane.WARNING_MESSAGE); return false; } if(value.length() > length){ JOptionPane.showMessageDialog(this, "超过最大长度", "超过最大长度", JOptionPane.ERROR_MESSAGE); return false; } if(type.equals("Integer")){ if(!value.matches("-?[1-9][0-9]*")){ JOptionPane.showMessageDialog(this, "枚举项值只能为Integer", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE); return false; } }else if(type.equals("String")){ // if(!value.matches("([a-z A-Z]*[0-9]*)*") && !value.matches("-?[1-9][0-9]*")){ // JOptionPane.showMessageDialog(this, "枚举项值只能为String", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE); // return false; // } } } return true; } /** * 检查长度 * @return */ private boolean checkLength(){ if(tfLength.getText().equals("")){ JOptionPane.showMessageDialog(this, "请输入最大长度", "请输入最大长度", JOptionPane.ERROR_MESSAGE); return false; } if(!tfLength.getText().matches("[1-9][0-9]*")){ JOptionPane.showMessageDialog(this, "长度只能为数字", "长度只能为数字", JOptionPane.WARNING_MESSAGE); return false; } return true; } }