package com.vci.client.uif.engine.client.tableArea; import java.awt.BorderLayout; import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; import javax.swing.JTextField; import com.vci.client.portal.utility.DataModelFactory; import com.vci.client.portal.utility.PLDefination; 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.VCISwingUtil; import com.vci.client.ui.swing.components.VCIJButton; import com.vci.client.ui.swing.components.VCIJComboBox; import com.vci.client.ui.swing.components.VCIJDialog; 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.actions.client.UIFCache; import com.vci.client.uif.actions.client.UIFUtils; 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.VCIJComboBoxModelValueObject; import com.vci.corba.common.VCIError; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.portal.data.PortalVI; import com.vci.mw.ClientContextVariable; import com.vci.omd.dataType.VTDataType; public class TablePanalAdvanceSearchDialog extends VCIJDialog implements ActionListener{ /** * */ private static final long serialVersionUID = 4547945867911768830L; private String btmType = ""; private String context = ""; private PLDefination defination = null; private DataModelFactory factory = null; private TablePanel tablePanel = null; private VCIJButton btnSearch = VCISwingUtil.createVCIJButton("search", "查询", "查询", "search.png", this); private VCIJButton btnCancel = VCISwingUtil.createVCIJButton("cancel", "取消", "取消", "cancel.png", this); private HashMap ctlLblMap = new HashMap(); private HashMap ctlComptMap = new HashMap(); private final String AND_NAME = "并且"; private final String AND_VALUE = "\\AND"; private final String OR_NAME = "或者"; private final String OR_VALUE = "\\OR"; private final String EQUAL_NAME = "等于"; private final String EQUAL_NOT_NAME = "不等于"; private final String EQUAL_NOT_VALUE = "!="; private final String CONTAIN_NAME = "包含"; private final String CONTAIN_VALUE = "*"; private final String FUZZY = "模糊"; private final String GREAT_NAME = "大于"; private final String GREAT_VALUE = ">"; private final String GREAT_EQUAL_NAME = "大于等于"; private final String GREAT_EQUAL_VALUE = ">="; private final String LESS_NAME = "小于"; private final String LESS_VALUE = "<"; private final String LESS_EQUAL_NAME = "小于等于"; private final String LESS_EQUAL_VALUE = "<="; public TablePanalAdvanceSearchDialog( String type, String context, PLDefination defination, DataModelFactory factory, TablePanel tablePanel) { super(ClientContextVariable.getFrame(), true); this.btmType = type; this.context = context; this.defination = defination; this.factory = factory; this.tablePanel = tablePanel; } public void init(){ setIconImage(VCISwingUtil.createImageIcon("search.png").getImage()); setTitle("高级查询"); setLayout(new BorderLayout()); add(new VCIJScrollPane(getCenterAreaPanel()), BorderLayout.CENTER); add(getSouthButtonPanel(), BorderLayout.SOUTH); } private VCIJPanel getCenterAreaPanel(){ VCIJPanel pal = new VCIJPanel(new GridBagLayout()); PortalVI tableDef = null; try { if(!"".equals(getDefination().getLinkType())){ tableDef = factory.getTableDefinationByTypeAndTableName(getDefination().getLinkType(), getDefination().getTemplateId()); } else { tableDef = factory.getTableDefinationByTypeAndTableName(getDefination().getShowType(), getDefination().getTemplateId()); } PRM tablePrm = UITools.getPRM(tableDef.prm); if(tablePrm == null) return pal; String[] queryCols = tablePrm.getPrmItemList().get(0).getItemKeyFields().split(","); if(queryCols == null) return pal; if (queryCols.length == 0) { return pal; } // 第个属性出现多少次 Map colAndUseCountMap = tablePrm.getPrmItemList().get(0).getColAndUseCountMap(); if(colAndUseCountMap == null || colAndUseCountMap.size() == 0){ return pal; } // 属性对应的名称 PortalVI formView = factory.getFormViewById(tablePrm.getPrmItemList().get(0).getItemInObj()); PRM formPrm = UITools.getPRM(formView.prm); //Map colAndNameMap = formPrm.getColAndNameMap(); //得到每一个控件配置信息 Map prmItemMap = UIFUtils.getColAndPRMItemMap(formPrm); pal = loadAttrToPanel(colAndUseCountMap, prmItemMap); } catch (VCIError e) { e.printStackTrace(); } return pal; } private VCIJPanel loadAttrToPanel(Map mapNums, Map prmItemMap){ VCIJPanel pal = new VCIJPanel(new GridBagLayout()); Iterator keyNums = mapNums.keySet().iterator(); int gridx = -1; int gridy = -1; while(keyNums.hasNext()){ gridx = 0; gridy++; String field = keyNums.next(); Integer count = mapNums.get(field); PRMItem item = prmItemMap.get(field); if(item == null) { continue; } //去掉item必填和只读的属性 item.setItemIsEditable("0"); item.setItemIsRequired("0"); //获得属性的类型 String type = field; if(type.indexOf(".") != -1) { type = type.substring(type.lastIndexOf(".") + 1); } //判断是否是系统默认的属性 if(UIFUtils.getDefaultAttributeDataType(type) != null) { type = UIFUtils.getDefaultAttributeDataType(type); } else { AttribItem ai = UIFCache.getInstance().getAllAttributeMap().get(type); if(ai != null) { type = ai.vtDataType; } } VCIJLabel lbl = new VCIJLabel(item.getItemName()); VCIJComboBox cbx = new VCIJComboBox(getNewComboBoxModel(type)); Component txt = ControlFactory.createControl(item); pal.add(lbl, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE)); gridx++; pal.add(cbx, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE)); gridx++; GridBagConstraints gbc = getGBC(gridx, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH); if(count == 1){ Insets insets = gbc.insets; gbc.insets = new Insets(insets.top, insets.left, 10, insets.right); } pal.add(txt, gbc); LinkedList list = new LinkedList(); InnerStruct is = new InnerStruct(field, null, cbx, txt); list.add(is); singleFilterMap.put(field, list); if(count <= 1) { continue; } gridy = add(count, gridy, pal, field, item); } gridy++; VCIJLabel lblFill = new VCIJLabel(""); pal.add(lblFill, getGBC(0, gridy, 3, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH)); return pal; } private int add(int count, int gridyCurrent, VCIJPanel pal, String field, PRMItem item){ int gridx = 0; int gridy = gridyCurrent; LinkedList dupFilterList = new LinkedList(); for(int i = 2; i <= count; i++){ //获得属性的类型 String type = field; if(type.indexOf(".") != -1) { type = type.substring(type.lastIndexOf(".") + 1); } //判断是否是系统默认的属性 if(UIFUtils.getDefaultAttributeDataType(type) != null) { type = UIFUtils.getDefaultAttributeDataType(type); } else { AttribItem ai = UIFCache.getInstance().getAllAttributeMap().get(type); if(ai != null) { type = ai.vtDataType; } } VCIJComboBox cbxAndOr = new VCIJComboBox(getNewAndOrComboBoxModel()); VCIJComboBox cbxFilter = new VCIJComboBox(getNewComboBoxModel(type)); Component txtFilter = ControlFactory.createControl(item); gridx = 0; gridy++; pal.add(cbxAndOr, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE)); gridx++; pal.add(cbxFilter, getGBC(gridx, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE)); gridx++; GridBagConstraints gbc = getGBC(gridx, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.BOTH); if(i == count){ Insets insets = gbc.insets; gbc.insets = new Insets(insets.top, insets.left, 10, insets.right); } pal.add(txtFilter, gbc); InnerStruct is = new InnerStruct(field, cbxAndOr, cbxFilter, txtFilter); dupFilterList.add(is); } // 存储每个属性重复出现的查询条件数据结构 dupFilterMap.put(field, dupFilterList); return gridy; } /** * */ private HashMap> singleFilterMap = new HashMap>(); /** * 同一个属性,重复定义出的那些组查询条件 */ private HashMap> dupFilterMap = new HashMap>(); class InnerStruct{ private String field = ""; private VCIJComboBox cbxAndOr = null; private VCIJComboBox cbxFilter = null; private Component vlaueCompt = null; public InnerStruct( String field, VCIJComboBox cbxAndOr, VCIJComboBox cbxFilter, Component txtValue) { super(); this.cbxAndOr = cbxAndOr; this.cbxFilter = cbxFilter; this.vlaueCompt = txtValue; } public String getField() { return field; } public void setField(String field) { this.field = field; } public VCIJComboBox getCbxAndOr() { return cbxAndOr; } public void setCbxAndOr(VCIJComboBox cbxAndOr) { this.cbxAndOr = cbxAndOr; } public VCIJComboBox getCbxFilter() { return cbxFilter; } public void setCbxFilter(VCIJComboBox cbxFilter) { this.cbxFilter = cbxFilter; } public Component getVlaueCompt() { return vlaueCompt; } public void setVlaueCompt(Component txtValue) { this.vlaueCompt = txtValue; } } private DefaultComboBoxModel getNewAndOrComboBoxModel(){ DefaultComboBoxModel model = new DefaultComboBoxModel(); model.addElement(AND_NAME); model.addElement(OR_NAME); return model; } /** * 获得属性类型支持的查询条件 * TODO 未完成 * @param type 属性的类型 * @return */ private DefaultComboBoxModel getNewComboBoxModel(String type){ DefaultComboBoxModel model = new DefaultComboBoxModel(); if(type.equals(VTDataType.VTSTRING)) { model.addElement(EQUAL_NAME); //等于 model.addElement(EQUAL_NOT_NAME); //不等于 model.addElement(CONTAIN_NAME); //包含 model.addElement(FUZZY); //模糊 } else if (type.equals(VTDataType.VTINTEGER) || type.equals(VTDataType.VTLONG) || type.equals(VTDataType.VTDOUBLE)) { model.addElement(EQUAL_NAME); //等于 model.addElement(EQUAL_NOT_NAME); //不等于 model.addElement(GREAT_NAME); //大于 model.addElement(GREAT_EQUAL_NAME); //大于等于 model.addElement(LESS_NAME); //小于 model.addElement(LESS_EQUAL_NAME); //小于等于 } else if (type.equals(VTDataType.VTBOOLEAN)) { model.addElement(EQUAL_NAME); //等于 model.addElement(EQUAL_NOT_NAME); //不等于 } else if (type.equals(VTDataType.VTDATETIME) || type.equals(VTDataType.VTDATE) || type.equals(VTDataType.VTTIME)) { model.addElement(EQUAL_NAME); //等于 model.addElement(EQUAL_NOT_NAME); //不等于 model.addElement(GREAT_NAME); //大于 model.addElement(GREAT_EQUAL_NAME); //大于等于 model.addElement(LESS_NAME); //小于 model.addElement(LESS_EQUAL_NAME); //小于等于 } else { model.addElement(EQUAL_NAME); } // model.addElement(EQUAL_NAME); // model.addElement(EQUAL_NOT_NAME); // model.addElement(CONTAIN_NAME); // model.addElement(GREAT_NAME); // model.addElement(GREAT_EQUAL_NAME); // model.addElement(LESS_NAME); // model.addElement(LESS_EQUAL_NAME); return model; } private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill) { int padxy = 2; return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy); } private VCIJPanel getSouthButtonPanel(){ VCIJPanel pal = new VCIJPanel(); pal.add(btnSearch); pal.add(btnCancel); return pal; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String actionCommand = e.getActionCommand(); if(actionCommand.equals(btnSearch.getActionCommand())) search(); else if(actionCommand.equals(btnCancel.getActionCommand())) cancel(); } private void search(){ HashMap conditionMap = getSearchConditionMap(); this.tablePanel.getDataTablePanel().setPageIndex(1); this.tablePanel.getDataProvider().setLoadByCondition(true); this.tablePanel.getDataProvider().setNormalSearchFilterMap(conditionMap); this.tablePanel.getDataTablePanel().refreshTableData(); cancel(); } private HashMap getSearchConditionMap(){ HashMap conditionMap = new HashMap(); Iterator keys = singleFilterMap.keySet().iterator(); while(keys.hasNext()){ String field = keys.next(); InnerStruct singleStruct = singleFilterMap.get(field).get(0); VCIJComboBox cbxFilterSignle = singleStruct.getCbxFilter(); Component txtValueSingle = singleStruct.getVlaueCompt(); String filter = cbxFilterSignle.getSelectedItem().toString(); String value = getComponentValue(txtValueSingle); String valueToAdd = ""; if(EQUAL_NAME.equals(filter)){ valueToAdd = value; } else if(EQUAL_NOT_NAME.equals(filter)) { valueToAdd = EQUAL_NOT_VALUE + value; } else if(CONTAIN_NAME.equals(filter)){ valueToAdd = CONTAIN_VALUE + value + CONTAIN_VALUE; } else if(FUZZY.equals(filter)){ if(value.contains("%")){ value = value.replace("%", "*"); } valueToAdd = value; }else if(GREAT_NAME.equals(filter)) { valueToAdd = GREAT_VALUE + value; } else if(GREAT_EQUAL_NAME.equals(filter)) { valueToAdd = GREAT_EQUAL_VALUE + value; } else if(LESS_NAME.equals(filter)) { valueToAdd = LESS_VALUE + value; } else if(LESS_EQUAL_NAME.equals(filter)) { valueToAdd = LESS_EQUAL_VALUE + value; } String dupFilterValue = getDupFilterConditonString(field); String mergeFilterValue = valueToAdd + dupFilterValue; if(!"".equals(mergeFilterValue)){ conditionMap.put(field, mergeFilterValue); } } return conditionMap; } private String getDupFilterConditonString(String field){ String res = ""; LinkedList dupFliterList = dupFilterMap.get(field); if(dupFliterList == null) return res; else if(dupFliterList.size() == 0) return res; for(InnerStruct is : dupFliterList){ VCIJComboBox cbxAndOr = is.getCbxAndOr(); VCIJComboBox cbxFilter = is.getCbxFilter(); Component cbxValue = is.getVlaueCompt(); String value = getComponentValue(cbxValue); if(value == null || "".equals(value)) continue; String filter = cbxFilter.getSelectedItem().toString(); String valueToAdd = ""; if(EQUAL_NAME.equals(filter)){ valueToAdd = value; } else if(EQUAL_NOT_NAME.equals(filter)) { valueToAdd = EQUAL_NOT_VALUE + value; } else if(CONTAIN_NAME.equals(filter)){ valueToAdd = CONTAIN_VALUE + value + CONTAIN_VALUE; } else if(GREAT_NAME.equals(filter)) { valueToAdd = GREAT_VALUE + value; } else if(GREAT_EQUAL_NAME.equals(filter)) { valueToAdd = GREAT_EQUAL_VALUE + value; } else if(LESS_NAME.equals(filter)) { valueToAdd = LESS_VALUE + value; } else if(LESS_EQUAL_NAME.equals(filter)) { valueToAdd = LESS_EQUAL_VALUE + value; } String andOr = cbxAndOr.getSelectedItem().toString(); if(AND_NAME.equals(andOr)){ valueToAdd = AND_VALUE + valueToAdd; } else if(OR_NAME.equals(andOr)){ valueToAdd = OR_VALUE + valueToAdd; } res += valueToAdd; } return res; } private void cancel(){ setDialogResult(DialogResult.CANCEL); this.setVisible(false); this.dispose(); } /** * 获得控件的值 * @param compt * @return */ private String getComponentValue(Component compt){ String value = ""; if(JTextField.class.isAssignableFrom(compt.getClass())){ JTextField txtCompt = (JTextField)compt; String text = txtCompt.getText(); value = text; } else if(compt instanceof VCIJComboBox){ VCIJComboBox cbx = (VCIJComboBox)compt; ComboBoxModel model = cbx.getModel(); Object selectObj = model.getSelectedItem(); if(selectObj instanceof VCIJComboBoxModelValueObject){ VCIJComboBoxModelValueObject vo = (VCIJComboBoxModelValueObject) selectObj; value = vo.getValue(); } } else if(compt instanceof ICustomControl){ ICustomControl ctrl = (ICustomControl)compt; value = ctrl.getValue(); } return value; } public String getBtmType() { return btmType; } public void setBtmType(String type) { this.btmType = type; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public PLDefination getDefination() { return defination; } public void setDefination(PLDefination defination) { this.defination = defination; } public DataModelFactory getFactory() { return factory; } public void setFactory(DataModelFactory factory) { this.factory = factory; } }