package com.vci.client.omd.attribpool.ui; import java.awt.BorderLayout; import java.awt.Color; 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.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import java.util.HashMap; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import com.vci.client.common.datatype.VTDouble; import com.vci.client.common.datatype.VTInteger; import com.vci.client.common.datatype.VTLong; import com.vci.client.common.datatype.VTString; import com.vci.client.omd.attribpool.toOutside.InterAP; import com.vci.client.omd.attribpool.toOutside.InterAPManager; import com.vci.client.omd.attribpool.toOutside.Tool; import com.vci.client.omd.attribpool.toOutside.UsedNames; import com.vci.corba.common.VCIError; import com.vci.corba.omd.atm.AttribItem; import com.vci.mw.InvocationUtility; import com.vci.omd.dataType.VTDataType; /** * 属性项界面 * @author Administrator * */ public class AttribPanel extends JPanel{ /** * */ private static final long serialVersionUID = -3191437674655429531L; private final String SYSUSEDNAMES = "sysUsedNames"; private final String DATABASEUSEDNAMES = "dataBaseUsedNames"; /** * 0:显示; 1: 创建; 2: 修改 */ private int flag; private JTextField tfName, tfLabel; private JTextArea taDescrip; private JComboBox combVTType; //vtTypePanel: 数据类型改变时,动态加载不同的Panel private JPanel centerPanel, vtTypePanel, btnPanel; private JButton btnOK, btnCancel; //各个数据类型独自的panel private VTDataTypePanel dataTypePanel; private AttribItem attribItem; private HashMap detailInfoMap = null; /** * oracle 查询的最大标识符是30, 为解决多表查询时, Hibernate * 覆盖同名列的值, 故留一位后缀(2)用作别名 */ private final int NAME_MAX_LENGTH = 29; private static AttribPanel attribPanel = null; public static AttribPanel getInstance(){ if(attribPanel == null){ attribPanel = new AttribPanel(); } return attribPanel; } public AttribPanel(){ initUI(); addListener(); } private void initUI(){ this.setBackground(Color.WHITE); this.setLayout(new BorderLayout(0, 0)); this.setBorder(BorderFactory.createTitledBorder("属性项")); //northPanel JPanel northPanel = new JPanel(); this.add(northPanel, BorderLayout.NORTH); centerPanel = new JPanel(); centerPanel.setBackground(Color.white); centerPanel.setLayout(new BorderLayout()); this.add(centerPanel, BorderLayout.CENTER); vtTypePanel = new JPanel(); vtTypePanel.setLayout(new BorderLayout()); centerPanel.add(vtTypePanel); btnPanel = new JPanel(); btnOK = new JButton("确定"); btnCancel = new JButton("取消"); btnPanel.add(btnOK); btnPanel.add(btnCancel); centerPanel.add(btnPanel, BorderLayout.SOUTH); northPanel.setLayout(new GridBagLayout()); GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.EAST; g.insets = new Insets(10, 5, 0, 5); g.gridx = 0; g.gridy = 0; northPanel.add(new JLabel("名称"), g); tfName = new JTextField(35); GridBagConstraints g1 = new GridBagConstraints(); g1.insets = new Insets(10, 5, 0, 5); g1.gridx = 1; g1.gridy = 0; northPanel.add(tfName, g1); g.gridx = 2; northPanel.add(new JLabel("标签"), g); tfLabel = new JTextField(35); g1.gridx = 3; g1.weighty = 1.0; g1.fill = GridBagConstraints.HORIZONTAL; g1.gridwidth = GridBagConstraints.REMAINDER; northPanel.add(tfLabel, g1); g.gridx = 0; g.gridy = 1; northPanel.add(new JLabel("描述"), g); taDescrip = new JTextArea(3, 20); taDescrip.setLineWrap(true); g1.gridx = 1; g1.gridy = 1; northPanel.add(new JScrollPane(taDescrip), g1); g.gridx = 0; g.gridy = 2; northPanel.add(new JLabel("属性类型"), g); combVTType = new JComboBox(); initCombVTType(); g1.gridx = 1; g1.gridy = 2; northPanel.add(combVTType, g1); } /** * @param abItem : 用来显示和修改属性项 * @param flag : 当前状态(创建,修改,删除) */ public void updateDataAndUI(AttribItem abItem, int flag){ this.flag = flag; if(abItem != null){ tfName.setText(abItem.name); tfLabel.setText(abItem.label); taDescrip.setText(abItem.description); combVTType.setSelectedItem(abItem.vtDataType); attribItem = abItem; }else{ tfName.setText(""); tfLabel.setText(""); taDescrip.setText(""); initCombVTType(); } if(flag == 0){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(false); tfLabel.setEditable(false); taDescrip.setEditable(false); combVTType.setEnabled(false); } dataTypePanel.updataUIStatus(abItem, flag); btnOK.setVisible(false); btnCancel.setVisible(false); } if(flag == 1){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(true); tfLabel.setEditable(true); taDescrip.setEditable(true); combVTType.setEnabled(true); } dataTypePanel.updataUIStatus(abItem, flag); btnOK.setVisible(true); btnCancel.setVisible(true); } if(flag == 2){ //名称,标签,描述,属性类型 组件群 { tfName.setEditable(false); tfLabel.setEditable(true); taDescrip.setEditable(true); combVTType.setEnabled(true); } dataTypePanel.updataUIStatus(abItem, flag); btnOK.setVisible(true); btnCancel.setVisible(true); } } public void addCancelActionListener(ActionListener listener) { btnCancel.addActionListener(listener); } /** * 为CombVTType添加数据类型 */ private void initCombVTType(){ combVTType.removeAllItems(); for(int i = 0; i < VTDataType.DateType.length; i++){ combVTType.addItem(VTDataType.DateType[i]); } } /** * 监听CombVTType */ private void typeItemChanged(ItemEvent e){ String typeItem = (String) combVTType.getSelectedItem(); if(typeItem.equals(VTDataType.VTSTRING)){ dataTypePanel = new VTStringPanel(); }else if(typeItem.equals(VTDataType.VTINTEGER)){ dataTypePanel = new VTIntegerPanel(); }else if(typeItem.equals(VTDataType.VTLONG)){ dataTypePanel = new VTLongPanel(); }else if(typeItem.equals(VTDataType.VTDOUBLE)){ dataTypePanel = new VTDoublePanel(); }else if(typeItem.equals(VTDataType.VTBOOLEAN)){ dataTypePanel = new VTBooleanPanel(); // }else if(typeItem.equals(VTDataType.VTIMAGE)){ // dataTypePanel = new VTImagePanel(); }else if(typeItem.equals(VTDataType.VTDATE)){ dataTypePanel = new VTDatePanel(); }else if(typeItem.equals(VTDataType.VTTIME)){ dataTypePanel = new VTTimePanel(); }else if(typeItem.equals(VTDataType.VTDATETIME)){ dataTypePanel = new VTDateTimePanel(); }else if(typeItem.equals(VTDataType.VTNOTE)){ dataTypePanel = new VTNotePanel(); }else if(typeItem.equals(VTDataType.VTFILEPATH)){ dataTypePanel = new VTFilePathPanel(); }else if(typeItem.equals(VTDataType.VTCLOB)){ dataTypePanel = new VTClobPanel(); } if(dataTypePanel == null){ dataTypePanel = new VTStringPanel(); } vtTypePanel.removeAll(); vtTypePanel.updateUI(); vtTypePanel.add(dataTypePanel, BorderLayout.CENTER); } private void addListener(){ btnOK.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e){ //创建属性项 if(flag == 1){ attribItem = new AttribItem(); if(!checkName()){ return; } detailInfoMap = dataTypePanel.getDetailInfo(); if(!checkDefValue()){ return; } attribItem.name = tfName.getText().toLowerCase().replaceAll(" ", ""); attribItem.label = tfLabel.getText(); attribItem.description = taDescrip.getText(); attribItem.vtDataType = (String)combVTType.getSelectedItem(); attribItem.defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE) == null ? "" : detailInfoMap.get(VTDataTypePanel.DEFVALUE); attribItem.rage = detailInfoMap.get(VTDataTypePanel.RAGE) == null ? "" : detailInfoMap.get(VTDataTypePanel.RAGE); attribItem.other = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER); String userName = InvocationUtility.getInvocation().userName; attribItem.creator = userName; attribItem.modifier = userName; boolean addSuccess = false; try { addSuccess = APClient.getService().addAttribItem(attribItem); } catch (VCIError e1) { e1.printStackTrace(); } if(!addSuccess){ JOptionPane.showMessageDialog(null, "添加属性失败", "添加属性失败", JOptionPane.ERROR_MESSAGE); return; } JOptionPane.showMessageDialog(null, "添加属性成功", "添加属性成功", JOptionPane.INFORMATION_MESSAGE); AttribPoolPanel.getInstance().initTableData(); AttribItem selectedAb = AttribPoolPanel.getInstance().getSelectedAb(); updateDataAndUI(selectedAb, 0); AttribPoolPanel.getInstance().setFlag(0); }else if(flag == 2){ if(attribItem == null){ JOptionPane.showMessageDialog(null, "请选择要修改的属性项", "选择属性项", JOptionPane.WARNING_MESSAGE); } detailInfoMap = dataTypePanel.getDetailInfo(); if(!checkDefValue()){ return; } boolean compatible = isCompatible(attribItem); boolean hasInstance = hasInstance(attribItem.name); //产生数据, 并且不兼容 if(hasInstance && !compatible){ JOptionPane.showMessageDialog(null, "无效变更, 不兼容已产生的数据", "无效变更", JOptionPane.ERROR_MESSAGE); return; } attribItem.label = tfLabel.getText(); attribItem.description = taDescrip.getText(); attribItem.vtDataType = (String)combVTType.getSelectedItem(); attribItem.defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE) == null ? "" : detailInfoMap.get(VTDataTypePanel.DEFVALUE); attribItem.rage = detailInfoMap.get(VTDataTypePanel.RAGE) == null ? "" : detailInfoMap.get(VTDataTypePanel.RAGE); attribItem.other = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER); attribItem.modifier = InvocationUtility.getInvocation().userName; try { boolean mdSuccess = APClient.getService().modifyAbItem(attribItem); if(!mdSuccess){ JOptionPane.showMessageDialog(null, "修改属性失败", "修改属性失败", JOptionPane.ERROR_MESSAGE); return; } JOptionPane.showMessageDialog(null, "修改属性成功", "修改属性成功", JOptionPane.INFORMATION_MESSAGE); AttribPoolPanel.getInstance().initTableData(); //修改业务类型, 链接类型中该属性字段 ArrayList interAPList = InterAPManager.getInstance().getInterAPList(); for(int i = 0; i < interAPList.size(); i++){ InterAP interAP = interAPList.get(i); interAP.alterAp(attribItem.name); } } 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(attribItem, 0); } } }); combVTType.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ typeItemChanged(e); } } }); } 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; } int length = name.length(); if(length > NAME_MAX_LENGTH){ JOptionPane.showMessageDialog(this, "属性名长度不能超过:" + NAME_MAX_LENGTH, "属性名过长", JOptionPane.WARNING_MESSAGE); return false; } String abName = name.toLowerCase(); if(usedBySystem(abName)){ JOptionPane.showMessageDialog(this, "属性名已被系统属性使用", "属性名无效", JOptionPane.WARNING_MESSAGE); return false; } if(usedByDataBase(abName)){ JOptionPane.showMessageDialog(this, "属性名是数据库关键字", "属性名无效", JOptionPane.WARNING_MESSAGE); return false; } try { if(APClient.getService().checkRowIsExists(abName)){ JOptionPane.showMessageDialog(this, "属性名已经存在", "注意", JOptionPane.WARNING_MESSAGE); return false; } } catch (VCIError e) { e.printStackTrace(); } catch (HeadlessException e) { e.printStackTrace(); } return true; } /** * 检查默认值与属性类型是否匹配 * @return */ private boolean checkDefValue(){ String defValue = detailInfoMap.get(VTDataTypePanel.DEFVALUE); String vtType = (String)combVTType.getSelectedItem(); String rageTemp = detailInfoMap.get(VTDataTypePanel.RAGE); String rages = null; if(rageTemp != null){ rages = detailInfoMap.get(VTDataTypePanel.RAGE); } if(defValue != null && !defValue.equals("")){ if(vtType.equals(VTDataType.VTSTRING)){ try{ String.valueOf(defValue); }catch(Exception e){ JOptionPane.showMessageDialog(null, "请输入String类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE); return false; } if(rages == null || rages.equals("")){ return true; } VTString obj = new VTString(String.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE); return false; } }else if(vtType.equals(VTDataType.VTINTEGER)){ try{ Integer.valueOf(defValue); }catch(Exception e){ JOptionPane.showMessageDialog(null, "请输入Integer类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE); return false; } if(rages == null || rages.equals("")){ return true; } VTInteger obj = new VTInteger(Integer.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE); return false; } }else if(vtType.equals(VTDataType.VTLONG)){ try{ Long.valueOf(defValue); }catch(Exception e){ JOptionPane.showMessageDialog(null, "请输入Long类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE); return false; } if(rages == null || rages.equals("")){ return true; } VTLong obj = new VTLong(Long.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE); return false; } }else if(vtType.equals(VTDataType.VTDOUBLE)){ try{ Double.valueOf(defValue); }catch(Exception e){ JOptionPane.showMessageDialog(null, "请输入Double类型的默认值", "请输入正确的默认值", JOptionPane.ERROR_MESSAGE); return false; } if(rages == null || rages.equals("")){ return true; } VTDouble obj = new VTDouble(Double.valueOf(defValue)); boolean flag = obj.checkRageValueByRage(rages); if(!flag){ JOptionPane.showMessageDialog(null, "默认值与值域冲突", "默认值与值域冲突", JOptionPane.ERROR_MESSAGE); return false; } } } return true; } /** * 获取other中指定项目的值 * @return */ public String getOtherValueByType(String other, String type){ String[] otherArray = other.split(";"); for(int i = 0; i < otherArray.length; i++){ String otherValue = otherArray[i]; if(otherValue.contains(type)){ return otherValue.substring(otherValue.indexOf("=") + 2, otherValue.length()); } } return null; } /** * 修改属性时, 判断当前输入的属性是否能兼容之前的属性 * @param abItem * @return */ private boolean isCompatible(AttribItem abItem){ //TODO String dataType = abItem.vtDataType; String other = abItem.other; String newOther = detailInfoMap.get(VTDataTypePanel.OTHER) == null ? "" : detailInfoMap.get(VTDataTypePanel.OTHER); String newType = (String)combVTType.getSelectedItem(); if(newType.equals(VTDataType.VTSTRING)){ if(dataType.equals(VTDataType.VTINTEGER) || dataType.equals(VTDataType.VTLONG)){ return true; } if(dataType.equals(VTDataType.VTSTRING)){ int length = Integer.valueOf(getOtherValueByType(other, "length")); int newLen = Integer.valueOf(getOtherValueByType(newOther, "length")); if(length <= newLen){ return true; }else{ return false; } } } if(newType.equals(dataType)){ return true; } return false; } /** * 判断该属性是否已经在业务类型中, 或者链接类型中产生了数据 * @param abName * @return */ private boolean hasInstance(String abName){ ArrayList interAPList = InterAPManager.getInstance().getInterAPList(); for(int i = 0; i < interAPList.size(); i++){ InterAP interAP = interAPList.get(i); boolean flag = interAP.hasInstance(abName); if(flag){ return flag; } } return false; } /** * 检查该属性名是否被系统属性使用 * @param abName * @return */ private boolean usedBySystem(String abName){ boolean flag = false; String[] names = UsedNames.getProperty(SYSUSEDNAMES).toUpperCase().split(","); ArrayList nameList = Tool.convertArrayToList(names); if(nameList.contains(abName.toUpperCase())){ flag = true; } return flag; } /** * 检查该属性名是否属于数据库关键字 * @param abName * @return */ private boolean usedByDataBase(String abName){ boolean flag = false; String[] names = UsedNames.getProperty(DATABASEUSEDNAMES).toUpperCase().split(","); ArrayList nameList = Tool.convertArrayToList(names); if(nameList.contains(abName.toUpperCase())){ flag = true; } return flag; } }