package com.vci.client.portal.Formdesign; import java.awt.BorderLayout; import java.awt.Component; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.border.EmptyBorder; import javax.swing.text.JTextComponent; import com.vci.client.LogonApplication; import com.vci.client.bof.ClientBusinessObjectOperation; import com.vci.client.portal.utility.PRM; import com.vci.client.portal.utility.PRMItem; import com.vci.client.portal.utility.UITools; import com.vci.client.ui.swing.components.VCIJComboBox; import com.vci.client.ui.swing.components.VCIJLabel; import com.vci.client.ui.swing.components.VCIJPanel; import com.vci.client.ui.swing.components.VCIJScrollPane; import com.vci.client.ui.swing.components.VCIJTextField; import com.vci.client.uif.engine.client.UIHelper; import com.vci.client.uif.engine.client.controls.ControlFactory; import com.vci.client.uif.engine.client.controls.ICustomControl; import com.vci.client.uif.engine.client.controls.RefObjChooseControl; import com.vci.client.uif.engine.client.controls.VCIJComboBoxModelValueObject; import com.vci.client.uif.engine.client.objopt.ObjectAddEditDialog.OperateType; import com.vci.common.portal.enums.ControlType; import com.vci.common.portal.enums.PortalVITypeFlag; import com.vci.corba.common.VCIError; import com.vci.corba.omd.data.AttributeValue; import com.vci.corba.portal.data.PortalVI; public class PreviewDialog extends JDialog { private final JPanel contentPanel = new JPanel(); /** * 将界面元素属性集合设置成全局变量 在保存时用于判断属性是否为空 */ private Map itemMap = new HashMap(); /** * 全部的Label */ private HashMap ctrlLabelMap = new HashMap(); /** * 全部的Component 控件 */ private HashMap ctrlComptMap = new HashMap(); /** * 控件之间有显示控制 KEY:被控属性字段 VALUE:主控属性字段 受控件控件的显示与否,受主控属性的部分取值的影响 */ private HashMap> hiddenControlsMap = new HashMap>(); private HashMap SELECT_ATTRIBUTE = new HashMap(); private HashMap SELECT_ATTRIBUTE_VALUE = new HashMap(); /** * 控件的数据之间有级联数据控制的控件 KEY:主控属性字段 VALUE:受控(数据级联影响)属性字段 * 受控控件的数据受主控属性控件的数据影响,一般是主控数据作为受控数据的查询条件 */ private HashMap> cascadeControlsMap = new HashMap>(); /** * 被控属性与主控属性之间在关联关联 key:被控属性 value:主控属性 */ private HashMap fieldToMasterFieldMap = new HashMap(); private OperateType operateType = OperateType.ADD; private HashMap ctrlComboBoxMap = new HashMap(); /** * 当前窗口的表单类型 0:业务类型,1:链接类型 对于不同类型的表单,窗口中属性的命名规则不同 */ private PortalVITypeFlag typeFlag = PortalVITypeFlag.BtmType; /** * 判断是正向还是反响向 */ private boolean direction = true; private PortalVI pvi; private VCIJPanel previewPanel; public static void main(String[] args) { // try { // PreviewDialog dialog = new PreviewDialog(pvi); // dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); // dialog.setVisible(true); // } catch (Exception e) { // e.printStackTrace(); // } } /** * Create the dialog. */ public PreviewDialog(PortalVI pvi, PortalVITypeFlag typeFlag) { super(LogonApplication.frame,true); this.pvi = pvi; this.typeFlag = typeFlag; previewPanel = createControlsPanel(pvi); init(); this.setLocationRelativeTo(null); } private void init() { this.setTitle(pvi.viName); setBounds(100, 100, 854, 636); getContentPane().setLayout(new BorderLayout()); contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); getContentPane().add(contentPanel, BorderLayout.CENTER); { JPanel buttonPane = new JPanel(); buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT)); getContentPane().add(buttonPane, BorderLayout.SOUTH); { JButton cancelButton = new JButton("关闭"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent paramActionEvent) { dispose(); } }); cancelButton.setActionCommand("Cancel"); buttonPane.add(cancelButton); } } contentPanel.setLayout(new BorderLayout(0, 0)); contentPanel.add(previewPanel); } private void addItemToMap(List items) { for (PRMItem item : items) { this.itemMap.put(item.getItemField(), item); } } private VCIJPanel createControlsPanel(PortalVI sheet) { VCIJPanel pal = new VCIJPanel(new GridBagLayout()); PRM sheetPrm = UITools.getPRM(sheet.prm); List itemList = sheetPrm.getPrmItemList(); // 缓存PRMItem addItemToMap(itemList); int showColumns = 2; if (!"".equals(sheetPrm.getShowCols())) { showColumns = Integer.valueOf(sheetPrm.getShowCols()); } int gridx = 0; int gridy = 0; int gridwidth = 1; int gridheight = 1; double weightx = 1.0; double weighty = 0.0; int padxy = 3; int latestGridx = 0; boolean latestIsTextArea = false; VCIJLabel lbl = null; Component compt = null; Component comptToAdd = null; for (int i = 0; i < itemList.size(); i++) { PRMItem item = itemList.get(i); String field = item.getItemField(); String itemCols = item.getItemCols(); if (itemCols == null || itemCols.trim().equals("") || itemCols.equals("0")) { continue; } lbl = new VCIJLabel(item.getItemName() + ":"); compt = ControlFactory.createControl(item); comptToAdd = compt; if (comptToAdd instanceof ICustomControl) { ((ICustomControl) comptToAdd).setParentComponent(this); } if (compt instanceof JTextArea) { comptToAdd = new VCIJScrollPane(compt); } // 缓存控件 ctrlLabelMap.put(field, lbl); ctrlComptMap.put(field, compt); // TextArea 占整行显示 if (item.getItemType().toLowerCase() .equals(ControlType.TextArea.name().toLowerCase())) { gridx = 0; gridy += 1; gridwidth = showColumns * 2 - 1; gridheight = 30; weightx = 1.0; weighty = 5.0; pal.add(lbl, getGBC(gridx, gridy, 1, gridheight, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, padxy)); gridx += 1; pal.add(comptToAdd, getGBC(gridx, gridy, gridwidth, gridheight, weightx, weighty, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, padxy)); latestIsTextArea = true; // 重置一行位置信息 gridx = 0; gridy += gridheight; gridwidth = 1; gridheight = 1; weightx = 1.0; weighty = 0.0; latestGridx = 0; } else { if (latestIsTextArea) { // 如果最近的一个控件是TextArea,则位置信息已经是设置好了的,此时直接使用 pal.add(lbl, getGBC(gridx, gridy, 1, gridheight, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, padxy)); gridx += 1; pal.add(comptToAdd, getGBC(gridx, gridy, gridwidth, gridheight, weightx, weighty, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, padxy)); latestIsTextArea = false; } else { if (latestGridx == showColumns) { // 本行已经到达最大,此时也需要换行,设置下一 // 重置一行位置信息 gridx = 0; gridy += 1; gridwidth = 1; gridheight = 1; weightx = 1.0; weighty = 0.0; latestGridx = 0; } else { // 重置一行位置信息 if (gridx != 0) { gridx += 1; } } // 普通单行内追加 pal.add(lbl, getGBC(gridx, gridy, 1, gridheight, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, padxy)); gridx += 1; pal.add(comptToAdd, getGBC(gridx, gridy, gridwidth, gridheight, weightx, weighty, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, padxy)); } latestGridx++; latestIsTextArea = false; String itemAndFilter = item.getItemAddFilter(); if (itemAndFilter != null && !"".equals(itemAndFilter)) { SELECT_ATTRIBUTE.put(field, itemAndFilter); } // 处理属性与属性之间的关联控制数据 cascadeControlsMapConfig(item); // 处理属性与属性之间的 显示 控制数据 parseFieldRefHideAndShowData(item); if (compt instanceof VCIJComboBox) { VCIJComboBox cbx = (VCIJComboBox) compt; // 主控属性,需要加入数据选择后的事件处理 if (cascadeControlsMap.containsKey(field)) { cbx.addActionListener(new CascadeComboBoxActionListener( item)); } if (hiddenControlsMap.containsKey(field)) { cbx.addActionListener(new HiddenOrShowComboBoxActionListener( item)); } ctrlComboBoxMap.put(field, cbx); } else if (compt instanceof RefObjChooseControl) { // 参照类型的控件,需要注册数据选择后的处理逻辑 RefObjChooseControl rocc = (RefObjChooseControl) compt; if (!"".equals(item.getItemListTable()) && !"".equals(item.getItemListVal()) && !"1".equals(item.getItemIsEditable())) { registeRefObjChooseListener(rocc); } } } // 当为浏览时设置空间不可编辑 if (operateType.equals(OperateType.BROWSER)) { // add by xchao 2014.04.21 begin // 浏览模式下的txt控件为只读 if (JTextComponent.class.isAssignableFrom(compt.getClass())) { compt.setEnabled(true); ((JTextComponent) compt).setEditable(false); } else { compt.setEnabled(false); } // add by xchao 2014.04.21 end } else if (operateType.equals(OperateType.SELECT)) { // 如果为选择窗口时,需要判断form的类型来禁用相关的控件 // 如果是业务类型,禁用所有控件,如果是link类型,禁用t_oid控件 if (typeFlag == PortalVITypeFlag.BtmType) { compt.setEnabled(false); } else { String head = ""; if (direction) { head = "t_oid"; } else { head = "f_oid"; } if (field.toLowerCase().startsWith(head)) { compt.setEnabled(false); } } } VCIJLabel lblFill = new VCIJLabel(""); pal.add(lblFill, getGBC(0, gridy + 10, showColumns * 2, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH, padxy)); } // 根据显示控制,隐藏控件显示 for (Iterator it = hiddenControlsMap.keySet().iterator(); it .hasNext();) { String masterField = it.next(); List list = hiddenControlsMap.get(masterField); for (String filedToControl : list) { if (ctrlLabelMap.containsKey(filedToControl)) ctrlLabelMap.get(filedToControl).setEnabled(false); if (ctrlComptMap.containsKey(filedToControl)) ctrlComptMap.get(filedToControl).setEnabled(false); } } loadComboboxDatas(); return pal; } private void loadComboboxDatas() { for (Iterator it = ctrlComboBoxMap.keySet().iterator(); it .hasNext();) { String field = it.next(); if (fieldToMasterFieldMap.containsKey(field)) continue; loadComboBoxData(field); } } private void loadComboBoxData(String field) { VCIJComboBox cbx = ctrlComboBoxMap.get(field); String sql = SELECT_ATTRIBUTE.get(field); if (sql == null) return; ComboBoxModel model = new DefaultComboBoxModel(); if (sql.indexOf(":") < 0) { // 纯SQL,直接执行查询 model = convertSQLDataToDataModel(getSQLData(sql, new AttributeValue[0])); } else { for (Iterator it = SELECT_ATTRIBUTE_VALUE.keySet() .iterator(); it.hasNext();) { String akey = it.next(); String[] ckeys = akey.split("-"); String ckey = ckeys[ckeys.length - 1]; if (ckey.indexOf("\\.") >= 0) { ckey = ckey.split("\\.")[0]; } if (sql.indexOf(":" + ckey) >= 0) { AttributeValue attrVal = new AttributeValue(); attrVal.attrName = ckey; attrVal.attrVal = SELECT_ATTRIBUTE_VALUE.get(ckey); model = convertSQLDataToDataModel(getSQLData(sql, new AttributeValue[] { attrVal })); break; } } } cbx.setModel(model); if (model.getSize() > 0) { VCIJComboBoxModelValueObject obj = (VCIJComboBoxModelValueObject) model .getElementAt(0); String key = field; if (field.indexOf(".") >= 0) { String[] keys = field.split("\\."); key = keys[keys.length - 1]; } SELECT_ATTRIBUTE_VALUE.put(key, obj.getValue()); cbx.setSelectedItem(obj); } if (operateType == OperateType.BROWSER) { cbx.setEnabled(false); } } private String[] getSQLData(String sql, AttributeValue[] attrVals) { String[] clsfVals = new String[0]; ClientBusinessObjectOperation bofactory = new ClientBusinessObjectOperation(); try { clsfVals = bofactory.getClassficationValue(sql, attrVals); } catch (VCIError e) { e.printStackTrace(); } return clsfVals; } private ComboBoxModel convertSQLDataToDataModel(String[] results) { DefaultComboBoxModel model = new DefaultComboBoxModel(); for (String res : results) { VCIJComboBoxModelValueObject obj = new VCIJComboBoxModelValueObject( res, res); model.addElement(obj); } return model; } 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); } /** * 处理属性之间的关联查询控制 * * @param item */ private void cascadeControlsMapConfig(PRMItem item) { String field = item.getItemField(); String itemQueryRefFields = item.getItemQueryRefFields(); if (itemQueryRefFields != null && !"".equals(itemQueryRefFields)) { // 一个属性能够关联 多个属性,被关联的属性名称之间用逗号,分隔 String[] itemQueryRefFieldsArray = itemQueryRefFields.split(","); List list = new ArrayList(); if (cascadeControlsMap.containsKey(field)) { list = cascadeControlsMap.get(field); } for (String refField : itemQueryRefFieldsArray) { // 建立 主控属性与被属性之间关联关系,以控制被控属性的取值 list.add(refField); // 建立被控属性 与 主控属性 之间的映射关联 fieldToMasterFieldMap.put(refField, field); } cascadeControlsMap.remove(field); cascadeControlsMap.put(field, list); } } private void parseFieldRefHideAndShowData(PRMItem item) { String field = item.getItemField(); String itemCtrlDisplayCol = item.getItemCtrlDisplyCol(); if (itemCtrlDisplayCol != null && !"".equals(itemCtrlDisplayCol)) { String[] itemCtrlDisplayCols = itemCtrlDisplayCol.split(","); List list = new ArrayList(); if (hiddenControlsMap.containsKey(field)) { list = hiddenControlsMap.get(field); } for (String refField : itemCtrlDisplayCols) { list.add(refField); } hiddenControlsMap.remove(field); hiddenControlsMap.put(field, list); } } private class CascadeComboBoxActionListener implements ActionListener { private PRMItem fromItem = null; public CascadeComboBoxActionListener(PRMItem fromItem) { this.fromItem = fromItem; } @Override public void actionPerformed(ActionEvent e) { VCIJComboBox cbx = (VCIJComboBox) e.getSource(); VCIJComboBoxModelValueObject valObj = (VCIJComboBoxModelValueObject) cbx .getSelectedItem(); if (fromItem.getItemQueryRefFields() != null && !"".equals(fromItem.getItemQueryRefFields())) { refFieldHandler(fromItem, valObj); } } } private void refFieldHandler(PRMItem item, VCIJComboBoxModelValueObject valObj) { String fromField = item.getItemField(); if (fromField.indexOf(".") >= 0) { String[] keys = fromField.split("\\."); String key = keys[keys.length - 1]; SELECT_ATTRIBUTE_VALUE.put(key, valObj.getValue()); } else { SELECT_ATTRIBUTE_VALUE.put(fromField, valObj.getValue()); } String toField = item.getItemQueryRefFields(); String[] toFields = toField.split(","); for (String field : toFields) { loadComboBoxData(field); } } private class HiddenOrShowComboBoxActionListener implements ActionListener { private PRMItem fromItem = null; public HiddenOrShowComboBoxActionListener(PRMItem fromItem) { this.fromItem = fromItem; } @Override public void actionPerformed(ActionEvent e) { VCIJComboBox cbx = (VCIJComboBox) e.getSource(); VCIJComboBoxModelValueObject valObj = (VCIJComboBoxModelValueObject) cbx .getSelectedItem(); String[] displayConditions = fromItem.getItemCtrlDisplyCondition() .split(";"); List controlToFiels = hiddenControlsMap.get(fromItem .getItemField()); for (int i = 0; i < controlToFiels.size(); i++) { // 被控制属性字段 String toField = controlToFiels.get(i); String condition = displayConditions[i]; // 允许显示出该属性的取值 String[] diplayShows = condition.split(","); boolean enable = false; for (String display : diplayShows) { enable |= valObj.getName().equals(display); } Component compt = ctrlLabelMap.get(toField); if (compt != null) compt.setEnabled(enable && operateType != OperateType.BROWSER); compt = ctrlComptMap.get(toField); if (compt != null) compt.setEnabled(enable && operateType != OperateType.BROWSER); } } } /** * 为参照属性注册数据选择&更换后的事件处理程序 * * @param rocc */ private void registeRefObjChooseListener(RefObjChooseControl rocc) { rocc.addPropertyChangeListener(RefObjChooseControl.REF_OBJECT_CHANGED, new PropertyChangeListener() { @SuppressWarnings("unchecked") @Override public void propertyChange(PropertyChangeEvent evt) { Map newValMap = (Map) evt .getNewValue(); RefObjChooseControl source = (RefObjChooseControl) evt .getSource(); refreshOtherRefObjChooseControlData(source, newValMap); } }); } /** * 刷新参照对象引用数据 * * @param rocc * @param newVal */ private void refreshOtherRefObjChooseControlData(RefObjChooseControl rocc, Map newVal) { PRMItem item = rocc.getPRMItem(); String itemField = item.getItemField(); String oid = newVal.get(UIHelper.OID); if (oid == null || "".equals(oid)) return; // 查询关联列,数据处理 if (item.getItemQueryRefFields() != null && !"".equals(item.getItemQueryRefFields())) { // 被关联的属性列表 String[] toFields = item.getItemQueryRefFields().split(","); for (String toField : toFields) { PRMItem toItem = itemMap.get(toField); if (toItem == null) continue; String toFilter = toItem.getItemAddFilter(); if (toFilter == null || "".equals(toFilter)) continue; if (!ctrlComboBoxMap.containsKey(toField)) continue; SELECT_ATTRIBUTE.put(toField, toFilter); SELECT_ATTRIBUTE_VALUE.put(itemField, oid); loadComboBoxData(toField); } } // 相同参照对象的数据处理 for (Iterator it = ctrlComptMap.keySet().iterator(); it .hasNext();) { String key = it.next(); Component compt = ctrlComptMap.get(key); // 规约:参照对象的其它参照只能是文本框。 if (compt instanceof VCIJTextField) { VCIJTextField txt = (VCIJTextField) compt; PRMItem itemOther = (PRMItem) txt.getObj(); String fieldOther = itemOther.getItemField(); if (fieldOther.indexOf(itemField) >= 0) { String[] fields = fieldOther.split("\\."); fieldOther = fields[fields.length - 1]; String valueOther = newVal.get(fieldOther); txt.setText(valueOther); } } } } }