package com.vci.client.oq.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureEvent; import java.awt.dnd.DragGestureListener; import java.awt.dnd.DragSource; import java.awt.dnd.DragSourceDragEvent; import java.awt.dnd.DragSourceDropEvent; import java.awt.dnd.DragSourceEvent; import java.awt.dnd.DragSourceListener; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDragEvent; import java.awt.dnd.DropTargetDropEvent; import java.awt.dnd.DropTargetEvent; import java.awt.dnd.DropTargetListener; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.swing.BorderFactory; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.jdesktop.swingx.JXDatePicker; import com.vci.corba.omd.atm.AttribItem; import com.vci.corba.omd.btm.BtmItem; import com.vci.corba.omd.data.BusinessObject; import com.vci.corba.omd.ltm.LinkType; import com.vci.corba.omd.qtm.QTD; import com.vci.corba.omd.qtm.QTInfo; import com.vci.omd.objects.OtherInfo; import com.vci.client.common.oq.OQTool; import com.vci.client.common.providers.ServiceProvider; import com.vci.client.omd.provider.ApProvider; import com.vci.client.omd.provider.LinkTypeProvider; import com.vci.client.oq.QTClient; import com.vci.client.oq.QTDClient; import com.vci.client.oq.wrapper.QTDWrapper; import com.vci.common.qt.object.ChildrenInfo; import com.vci.common.qt.object.Condition; import com.vci.common.qt.object.ConditionItem; import com.vci.common.qt.object.Connector; import com.vci.common.qt.object.LeafInfo; import com.vci.common.qt.object.LeafValue; import com.vci.common.qt.object.Operator; import com.vci.common.qt.object.QTConstants; import com.vci.common.qt.object.QueryTemplate; import com.vci.corba.common.VCIError; public class ConditionPanel extends JPanel implements DropTargetListener, DragGestureListener, DragSourceListener{ /** * */ private static final long serialVersionUID = -3154410049944995334L; private JPanel westPanel, centerPanel; private JPanel wp_northPanel; private JPanel wp_centerPanel; private JComboBox combQTD; private JPanel cp_northPanel; private JPanel cp_centerPanel; private JPanel cp_southPanel; private DragSource dragSource; private JRadioButton rdGeneral; private JRadioButton rdSenior; private JButton btnClear; private JButton panelClear; private ButtonGroup btnGroup; private JButton btnQuery; private JButton btnCancel; private QTDTree qtdTree; private BtmItem selectedBtm = null; /** * Senior Level Tree */ private JTree seniorTree; private DefaultMutableTreeNode seniorRoot; private final String EQUAL = "="; private final String UNEQUAL = "!="; private final String CONTAINS = "包含"; private final String IN = "in"; private final String NOTIN = "not in"; private final String GT = ">"; private final String GTE = ">="; private final String LT = "<"; private final String LTE = "<="; private final String AND = "并且"; private final String OR = "或者"; private JButton btnAdd, btnDelete, btnModify; /** * 是否为权限提供 */ private boolean isForRight = false; private boolean isShowQueryAndCancelBTN = true; //Add BY ZhongGY 2015-01-20 private JRadioButton rdBtm; private JRadioButton rdLinkType; private ButtonGroup btmAndLinkGroup; public ConditionPanel(BtmItem btm, boolean isForRight,boolean isShowQueryAndCancelBTN){ this.isShowQueryAndCancelBTN = isShowQueryAndCancelBTN; this.selectedBtm = btm; this.isForRight = isForRight; initUI(); initCombQTD(); initQTDAbTree(); initCP_centerPanel(); addListener(); } public ConditionPanel(BtmItem btm, boolean isForRight){ this.selectedBtm = btm; this.isForRight = isForRight; initUI(); initCombQTD(); initQTDAbTree(); initCP_centerPanel(); addListener(); } public void updateUI(BtmItem btm){ this.selectedBtm = btm; initCombQTD(); } private void initUI(){ this.setLayout(new BorderLayout()); westPanel = new JPanel(); centerPanel = new JPanel(); //JPanel wpSP = new JPanel(); //wpSP.setViewportView(westPanel); //JScrollPane cpSP = new JScrollPane(); //cpSP.setViewportView(centerPanel); this.add(westPanel, BorderLayout.WEST); this.add(centerPanel, BorderLayout.CENTER); //候选查询条件 westPanel.setBorder(BorderFactory.createTitledBorder("候选条件")); wp_northPanel = new JPanel(); wp_centerPanel = new JPanel(); westPanel.setLayout(new BorderLayout()); westPanel.add(wp_northPanel, BorderLayout.NORTH); westPanel.add(wp_centerPanel, BorderLayout.CENTER); wp_northPanel.setLayout(new GridBagLayout()); GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.gridx = 0; g.gridy = 0; g.weighty = 1.0; g.fill = GridBagConstraints.BOTH; if(isForRight){ rdBtm = new JRadioButton("业务类型"); rdLinkType = new JRadioButton("链接类型"); btmAndLinkGroup = new ButtonGroup(); btmAndLinkGroup.add(rdBtm); btmAndLinkGroup.add(rdLinkType); rdBtm.setSelected(true); wp_northPanel.add(rdBtm, g); g.gridx = 1; g.weightx = 0; wp_northPanel.add(rdLinkType); g.gridx = 0; g.gridy = 1; g.weighty = 0.0; } combQTD = new JComboBox(); combQTD.setPreferredSize(new Dimension(200, 23)); wp_northPanel.add(new JLabel("查询模板定义"), g); g.gridx = 1; g.weightx = 1.0; wp_northPanel.add(combQTD, g); wp_centerPanel.setLayout(new GridBagLayout()); //查询条件 centerPanel.setBorder(BorderFactory.createTitledBorder("查询条件")); centerPanel.setLayout(new BorderLayout()); cp_northPanel = new JPanel(); cp_centerPanel = new JPanel(); cp_southPanel = new JPanel(); //拖拽到cpmcpcp_centerPanel dragSource = DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this); new DropTarget(cp_centerPanel, DnDConstants.ACTION_COPY_OR_MOVE, this); centerPanel.add(cp_northPanel, BorderLayout.NORTH); centerPanel.add(cp_centerPanel, BorderLayout.CENTER); centerPanel.add(cp_southPanel, BorderLayout.SOUTH); cp_northPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); rdGeneral = new JRadioButton("普通"); rdSenior = new JRadioButton("高级"); btnClear = new JButton("清空值"); panelClear = new JButton("删除全部条件"); cp_northPanel.add(rdGeneral); cp_northPanel.add(rdSenior); cp_northPanel.add(btnClear); cp_northPanel.add(panelClear); btnGroup = new ButtonGroup(); btnGroup.add(rdGeneral); btnGroup.add(rdSenior); rdGeneral.setSelected(true); cp_southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); btnAdd = new JButton("增加逻辑"); btnModify = new JButton("修改条件"); btnDelete = new JButton("删除"); btnAdd.setVisible(false); btnModify.setVisible(false); btnDelete.setVisible(false); cp_southPanel.add(btnAdd); cp_southPanel.add(btnModify); cp_southPanel.add(btnDelete); if (isShowQueryAndCancelBTN) { btnQuery = new JButton("查询"); btnCancel = new JButton("取消"); cp_southPanel.add(btnQuery); btnQuery.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { queryAction(); } }); cp_southPanel.add(btnCancel); } } private void addListener(){ if(isForRight){ rdBtm.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(isForRight){ initCombQTD(); } } }); rdLinkType.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(isForRight){ initCombQTD(); } } }); } combQTD.addItemListener(new ItemListener() { //在用户已选定或取消选定某项时调用 //removeAll时调用,第一次addItem时调用 @Override public void itemStateChanged(ItemEvent e) { initQTDAbTree(); initCP_centerPanel(); cp_centerPanel.removeAll(); } }); rdGeneral.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { btnAdd.setVisible(false); btnModify.setVisible(false); btnDelete.setVisible(false); // initCP_centerPanel(); } }); rdSenior.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { btnAdd.setVisible(true); btnModify.setVisible(true); btnDelete.setVisible(true); initCP_centerPanel(); } }); btnClear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { clearValue(); } }); panelClear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cp_centerPanel.removeAll(); cp_centerPanel.updateUI(); } }); /** * 增加逻辑条件 */ btnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addCondition(); } }); /** * 修改条件项或逻辑条件 */ btnModify.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { modifyCondition(); } }); /** * 删除逻辑条件或者条件项 */ btnDelete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { deleteCondition(); } }); } /** * 设置当前业务类型下的模板定义列表 */ public void initCombQTD(){ combQTD.removeAllItems(); if(selectedBtm == null){ return; } String btmName = selectedBtm.name; if(btmName == null || btmName.equals("")){ return; } QTD[] qtdArray = null; try { if(isForRight){ if(rdBtm.isSelected()){ qtdArray = ServiceProvider.getOMDService().getQTDService().getBizTypeQTDs(btmName); }else if(rdLinkType.isSelected()){ List qtdList = new ArrayList(); LinkType[] linkTypes = LinkTypeProvider.getInstance().getLinkTypeByBtmName(btmName, "from"); for(int i = 0; i < linkTypes.length; i++){ QTD[] qtds = ServiceProvider.getOMDService().getQTDService().getLinkTypeQTDs(linkTypes[i].name); for(int k = 0; k < qtds.length; k++){ qtdList.add(qtds[k]); } } qtdArray = qtdList.toArray(new QTD[0]); } }else{ qtdArray = ServiceProvider.getOMDService().getQTDService().getBizTypeQTDs(btmName); } } catch (VCIError e) { e.printStackTrace(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } if(qtdArray == null){ return; } for(int i = 0; i < qtdArray.length; i++){ combQTD.addItem(new QTDWrapper(qtdArray[i])); } } /** * 设置候选条件中查询模板的属性树 */ private void initQTDAbTree(){ QTDWrapper qtdWrapper = (QTDWrapper)combQTD.getSelectedItem(); wp_centerPanel.removeAll(); wp_centerPanel.updateUI(); if(qtdWrapper == null){ return; } QTD qtd = qtdWrapper.qtd; GridBagConstraints g1 = new GridBagConstraints(); g1.gridx = 0; g1.gridy = 0; g1.weightx = 1.0; g1.weighty = 1.0; g1.fill = GridBagConstraints.BOTH; qtdTree = new QTDTree(qtd, null); //qtdTree.setBorder(BorderFactory.createBevelBorder(1)); qtdTree.setAutoscrolls(true); qtdTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); JScrollPane jScrollPane = new JScrollPane(); jScrollPane.getViewport().add(qtdTree, null); wp_centerPanel.add(jScrollPane, g1); } /** * 查询条件中的查询条件 */ private void initCP_centerPanel(){ QTDWrapper qtdWrapper = (QTDWrapper)combQTD.getSelectedItem(); cp_centerPanel.removeAll(); cp_centerPanel.updateUI(); cp_centerPanel.setAutoscrolls(true); if(qtdWrapper == null){ return; } QTD qtd = qtdWrapper.qtd; cp_centerPanel.setLayout(new GridBagLayout()); String[] abNames = qtd.abNames; if(rdGeneral.isSelected()){ for(int i = 0; i < abNames.length; i++){ GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.gridx = 0; g.gridy = i; g.insets = new Insets(1, 1, 1, 1); final JButton btnRemove = new JButton("x"); cp_centerPanel.add(btnRemove, g); final JLabel labName = new JLabel(abNames[i]); g.gridx = 1; cp_centerPanel.add(labName, g); g.gridx = 2; final JComboBox combCons = new JComboBox(); combCons.setPreferredSize(new Dimension(80, 20)); String attName = abNames[i]; if(attName.contains(".")){ attName = attName.substring(attName.lastIndexOf(".") + 1, attName.length()); } String attType = OQTool.getAbItemDataType(attName); if(attType.equalsIgnoreCase("VTInteger") || attType.equalsIgnoreCase("VTDouble") || attType.equalsIgnoreCase("VTLong")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); }else if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.LTE); }else{ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); } cp_centerPanel.add(combCons, g); g.gridx = 3; //add by zhangweiwei 2014/12/09 start final JTextField tfValue = new JTextField(80); final JXDatePicker datePicker = new JXDatePicker(); datePicker.setPreferredSize(tfValue.getPreferredSize()); if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ cp_centerPanel.add(datePicker, g); }else{ cp_centerPanel.add(tfValue, g); } g.gridx =4; final JButton chooseButton = new JButton("选择查询模板"); cp_centerPanel.add(chooseButton, g); g.gridwidth = GridBagConstraints.REMAINDER; cp_centerPanel.updateUI(); chooseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // TODO 后续窗口调整 显示查询模板窗口 SelectorQTDialog eid = new SelectorQTDialog(cp_centerPanel); QTInfo item = eid.getItem(); String attr = eid.getAttr(); //选中的查询模板 tfValue.setText(item.qtName + ";" + attr); } }); btnRemove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cp_centerPanel.remove(btnRemove); cp_centerPanel.remove(labName); cp_centerPanel.remove(combCons); cp_centerPanel.remove(tfValue); cp_centerPanel.remove(datePicker); cp_centerPanel.remove(chooseButton); cp_centerPanel.updateUI(); } }); //add by zhangweiwei 2014/12/09 end } }else if (rdSenior.isSelected()){ initSeniorTree(); } } @Override public void dragEnter(DragSourceDragEvent dsde) { // TODO Auto-generated method stub } @Override public void dragOver(DragSourceDragEvent dsde) { // TODO Auto-generated method stub } @Override public void dropActionChanged(DragSourceDragEvent dsde) { // TODO Auto-generated method stub } @Override public void dragExit(DragSourceEvent dse) { // TODO Auto-generated method stub } @Override public void dragDropEnd(DragSourceDropEvent dsde) { // TODO Auto-generated method stub } @Override public void dragGestureRecognized(DragGestureEvent dge) { // TODO Auto-generated method stub } @Override public void dragEnter(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } @Override public void dragOver(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } @Override public void dropActionChanged(DropTargetDragEvent dtde) { // TODO Auto-generated method stub } @Override public void dragExit(DropTargetEvent dte) { // TODO Auto-generated method stub } @Override public void drop(DropTargetDropEvent dtde) { try { DataFlavor stringFlavor = DataFlavor.stringFlavor; Transferable trans = dtde.getTransferable(); if (dtde.isDataFlavorSupported(stringFlavor)) { String attPath = (String) trans.getTransferData(stringFlavor); dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); if(rdGeneral.isSelected()){ int count = cp_centerPanel.getComponentCount(); //添加选择查询模板按钮,组件个数改变,将每行4个组件改成5个 add by zhangweiwei int rowCount = count/5; GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.gridx = 0; g.gridy = rowCount + 1; g.insets = new Insets(1, 1, 1, 1); final JButton btnRemove = new JButton("x"); cp_centerPanel.add(btnRemove, g); final JLabel labName = new JLabel(attPath); g.gridx = 1; cp_centerPanel.add(labName, g); g.gridx = 2; final JComboBox combCons = new JComboBox(); combCons.setPreferredSize(new Dimension(80, 20)); String attName = attPath; if(attName.contains(".")){ attName = attName.substring(attName.lastIndexOf(".") + 1, attName.length()); } String attType = OQTool.getAbItemDataType(attName); if(attType.equalsIgnoreCase("VTInteger") || attType.equalsIgnoreCase("VTDouble") || attType.equalsIgnoreCase("VTLong")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); }else if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.LTE); }else{ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); } cp_centerPanel.add(combCons, g); g.gridx = 3; //add by zhangweiwei 2014/12/09 start final JTextField tfValue = new JTextField(80); // final JXDatePicker datePicker = new JXDatePicker(); // datePicker.setPreferredSize(tfValue.getPreferredSize()); final Chooser chooser = Chooser.getInstance(); chooser.setLayout(cp_centerPanel.getLayout()); if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ JTextField text = new JTextField(80); chooser.register(text); cp_centerPanel.add(chooser, g); }else{ cp_centerPanel.add(tfValue, g); } g.gridx =4; final JButton chooseButton = new JButton("选择查询模板"); cp_centerPanel.add(chooseButton, g); g.gridwidth = GridBagConstraints.REMAINDER; cp_centerPanel.updateUI(); chooseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // TODO 后续窗口调整 显示查询模板窗口 SelectorQTDialog eid = new SelectorQTDialog(cp_centerPanel); QTInfo item = eid.getItem(); String attr = eid.getAttr(); //选中的查询模板 if(item!=null && attr!=null){ tfValue.setText(item.qtName + ";" + attr); } } }); btnRemove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cp_centerPanel.remove(btnRemove); cp_centerPanel.remove(labName); cp_centerPanel.remove(combCons); cp_centerPanel.remove(tfValue); cp_centerPanel.remove(chooser); cp_centerPanel.remove(chooseButton); cp_centerPanel.updateUI(); } }); //add by zhangweiwei 2014/12/09 end }else if(rdSenior.isSelected()){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) seniorTree.getLastSelectedPathComponent(); if(selectedNode == null){ JOptionPane.showMessageDialog(cp_centerPanel, "请选中查询条件树", "请选中查询条件树", JOptionPane.WARNING_MESSAGE); return; } Object obj = selectedNode.getUserObject(); if(!(obj instanceof String)){ JOptionPane.showMessageDialog(cp_centerPanel, "请选中查询条件树", "请选中查询条件树", JOptionPane.WARNING_MESSAGE); return; }else if(!obj.equals(AND) && !obj.equals(OR)){ JOptionPane.showMessageDialog(cp_centerPanel, "请选中逻辑条件添加查询项", "请选中逻辑条件", JOptionPane.WARNING_MESSAGE); return; }else{ DefaultMutableTreeNode node = new DefaultMutableTreeNode(attPath); selectedNode.add(node); seniorTree.updateUI(); } } } else { dtde.rejectDrop(); } } catch (UnsupportedFlavorException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 去掉节点中已经包含的查询条件值 * 注意:逻辑上在操作符的 前后都应该带有空格,但是由于使用了trim,保证了空格的去除。 * 考虑到 in可以作为字母出现在属性名称中,在这里对in做了前空格的识别 * @param userObj * @return */ private String trimUserObj(String userObj){ if(userObj.contains(UNEQUAL)){ userObj = userObj.substring(0, userObj.indexOf(UNEQUAL)).trim(); }else if(userObj.contains(GTE)){ userObj = userObj.substring(0, userObj.indexOf(GTE)).trim(); }else if(userObj.contains(" " + NOTIN)){ //add by caill 此处必须先判断not in 再判断in userObj = userObj.substring(0, userObj.indexOf(" " + NOTIN)).trim(); }else if(userObj.contains(" " + IN)){ userObj = userObj.substring(0, userObj.indexOf(" " + IN)).trim(); }else if(userObj.contains(GT)){ userObj = userObj.substring(0, userObj.indexOf(GT)).trim(); }else if(userObj.contains(LTE)){ userObj = userObj.substring(0, userObj.indexOf(LTE)).trim(); }else if(userObj.contains(LT)){ userObj = userObj.substring(0, userObj.indexOf(LT)).trim(); }else if(userObj.contains(EQUAL)){ userObj = userObj.substring(0, userObj.indexOf(EQUAL)).trim(); }else if(userObj.contains(CONTAINS)){ userObj = userObj.substring(0, userObj.indexOf(CONTAINS)).trim(); } return userObj; } /** * 清空查询条件的值 */ private void clearValue(){ if(rdGeneral.isSelected()){ Component[] comps = cp_centerPanel.getComponents(); for(int i = 0; i < comps.length; i++){ // if(i % 4 == 3)(i%5*%+3){ if(i == i/5*5+3){ if(comps[i] instanceof JXDatePicker){ ((JXDatePicker) comps[i]).setDate(null); }else if(comps[i] instanceof Chooser){ Component[] texts = ((Chooser)comps[i]).getComponents(); ((JTextField)texts[0]).setText(""); }else{ ((JTextField) comps[i]).setText(""); } } } cp_centerPanel.updateUI(); }else if(rdSenior.isSelected()){ if(seniorTree == null){ return; } DefaultMutableTreeNode node = (DefaultMutableTreeNode)seniorTree.getModel().getRoot(); while(node != null){ if(node.getLevel() >= 2){ String userObj = trimUserObj((String)node.getUserObject()); node.setUserObject(userObj); } node = node.getNextNode(); } seniorTree.updateUI(); } } /** * 执行查询 */ private void queryAction(){ QueryTemplate qt = getQT(); String checkInfo = OQTool.checkQT(qt); if(!checkInfo.equals("OK")){ JOptionPane.showMessageDialog(this, checkInfo, checkInfo, JOptionPane.ERROR_MESSAGE); return; } qt.setId("qt1"); try { BusinessObject[] result = QTClient.getService().findBTMObjects(qt.getId(), OQTool.qtTOXMl(qt).asXML()); //TODO clauseList 查询显示列 QueryResultDialog dialog = new QueryResultDialog(result, qt.getClauseList()); dialog.setVisible(true); } catch (VCIError e) { e.printStackTrace(); } } /** * 组装查询模板 * @return */ public QueryTemplate getQT(){ QueryTemplate qt = new QueryTemplate(); qt.setType(QTConstants.TYPE_BTM); //TODO String qtId = String btmType = selectedBtm.name; qt.setBtmType(btmType); List clauseList = new ArrayList(); //TODO 查询列 显示列 clauseList.add("*"); qt.setClauseList(clauseList); Condition con = new Condition(); qt.setCondition(con); HashMap ciMap = getCIMap(); con.setCIMap(ciMap); con.setRootCIName(con.getRootCINameByCIMap(ciMap)); return qt; } /** * 获取高级树的UI结构 * @return */ public Document getSeniorTreeDoc(){ if(rdGeneral.isSelected()){ return null; } return OQTool.parseTreeToDoc(seniorTree); } //add by caill start 添加getList()方法用来获取到条件框中的值 public ArrayList getList(){ ArrayList list = new ArrayList(); if(rdGeneral.isSelected()){ Component[] comps = cp_centerPanel.getComponents(); int k = comps.length / 5; for(int i = 0; i < k; i++){ String text = null; if(comps[i * 5 + 3] instanceof JXDatePicker){ Date date = ((JXDatePicker) comps[i * 5 + 3]).getDate(); if(date != null){ text = OQTool.df.format(date); } else { text = "null"; } }else{ text = ((JTextField) comps[i * 5 + 3]).getText(); list.add(text); } } } return list; } //add by caill end /** * 获取查询条件 * @return */ private HashMap getCIMap(){ HashMap ciMap = new HashMap(); //普通查询条件 if(rdGeneral.isSelected()){ Component[] comps = cp_centerPanel.getComponents(); //add by zhangweiwei 2014/12/09 start 因增加选择查询模板按钮将每行组件数由4改成5 int k = comps.length / 5; //移除 ArrayList compList = new ArrayList(); for(int i = 0; i < k; i++){ String text = null; if(comps[i * 5 + 3] instanceof JXDatePicker){ Date date = ((JXDatePicker) comps[i * 5 + 3]).getDate(); if(date != null){ text = OQTool.df.format(date); } else { text = "null"; } }else if(comps[i * 5 + 3] instanceof Chooser){ Component[] texts = ((Chooser)comps[i * 5 + 3]).getComponents(); text = ((JTextField)texts[0]).getText(); }else{ text = ((JTextField) comps[i * 5 + 3]).getText(); } if(text != null && !text.equals("")){ compList.add(comps[i * 5 + 0]); compList.add(comps[i * 5 + 1]); compList.add(comps[i * 5 + 2]); compList.add(comps[i * 5 + 3]); compList.add(comps[i * 5 + 4]); } } Component[] newComps = compList.toArray(new Component[0]); int newK = newComps.length / 5; /** * 加入叶子节点 */ for(int i = 0; i < newK; i++){ ConditionItem ci = new ConditionItem(); String id = "ci" + (i + 1); ci.setId(id); ci.setLeafFlag(true); String clause = ((JLabel) newComps[i * 5 + 1]).getText(); String operator = (String) ((JComboBox)newComps[i * 5 + 2]).getSelectedItem(); String ordinaryValue = null; if(newComps[i * 5 + 3] instanceof JXDatePicker){ Date cdate = ((JXDatePicker) comps[i * 5 + 3]).getDate(); if (cdate == null) { ordinaryValue = "null"; } else { ordinaryValue = OQTool.df.format(cdate); } }else if(newComps[i * 5 + 3] instanceof Chooser){ Component[] texts = ((Chooser)comps[i * 5 + 3]).getComponents(); ordinaryValue = ((JTextField)texts[0]).getText(); }else{ ordinaryValue = ((JTextField) newComps[i * 5 + 3]).getText(); } //add by zhangweiwei 2014/12/09 end 因增加选择查询模板按钮将每行组件数由4改成5 LeafInfo leafInfo = new LeafInfo(); //属性为参照属性 if(clause.contains(".")){ int fpIndex = clause.indexOf("."); String refAbName = clause.substring(0, fpIndex); clause = clause.substring(fpIndex + 1); leafInfo.setClause(refAbName); leafInfo.setOperator(Operator.IN); QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue); LeafValue lValue = new LeafValue(); lValue.setQueryTemplate(qt); leafInfo.setValue(lValue); //属性为非参照属性 }else{ leafInfo.setClause(clause); leafInfo.setOperator(operator); LeafValue lValue = new LeafValue(); leafInfo.setValue(lValue); //add by zhangweiwei 2014/12/09 start //如果嵌套Link if(ordinaryValue.contains(";")){ if (operator.equals("not in")) { leafInfo.setOperator(Operator.NOTIN); }else { leafInfo.setOperator(Operator.IN); } String[] values = ordinaryValue.split(";"); String QTname = values[0]; String attr = values[1].substring(0, values[1].length()); try { QTInfo qt = ServiceProvider.getOMDService().getQTDService().getQT(QTname); String qtText = qt.qtText; QueryTemplate qt_ = OQTool.getQTByQTText(qt.qtName, qtText); List clauseList = new ArrayList(); clauseList.add(attr); qt_.setClauseList(clauseList); lValue.setQueryTemplate(qt_); } catch (VCIError e) { e.printStackTrace(); } catch (DocumentException e1) { e1.printStackTrace(); } }else{ lValue.setOrdinaryValue(ordinaryValue); } //add by zhangweiwei 2014/12/09 end } ci.setLeafInfo(leafInfo); ciMap.put(ci.getId(), ci); } Iterator iterator = ciMap.keySet().iterator(); ArrayList ciList = new ArrayList(); String lCIId = null; while(iterator.hasNext()){ lCIId = iterator.next(); break; } int count = ciMap.size(); while(iterator.hasNext()){ String rCIId = iterator.next(); ConditionItem pCI = new ConditionItem(); String pId = "ci" + ++count; pCI.setId(pId); pCI.setLeafFlag(false); ChildrenInfo pChildrenInfo = new ChildrenInfo(); pChildrenInfo.setLeftCIName(lCIId); pChildrenInfo.setConnector(Connector.AND); pChildrenInfo.setRightCIName(rCIId); pCI.setChildrenInfo(pChildrenInfo); ciList.add(pCI); lCIId = pId; } for(Iterator i = ciList.iterator(); i.hasNext();){ ConditionItem ci = i.next(); ciMap.put(ci.getId(), ci); } //高级查询条件 }else if(rdSenior.isSelected()){ ciMap = getCIMapForSeniorTree(); OQTool.parseTreeToDoc(seniorTree); } return ciMap; } /** * 获取高级查询的查询条件集合 * @return */ private HashMap getCIMapForSeniorTree() { HashMap ciMap = new HashMap(); DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) seniorTree.getModel().getRoot(); DefaultMutableTreeNode nextNode = rootNode.getNextNode(); if (nextNode == null) { // 无查询条件的查询 return null; } ArrayList ciList = new ArrayList(); String connector = (String) rootNode.getUserObject(); @SuppressWarnings("unchecked") Enumeration children = rootNode.children(); while(children.hasMoreElements()){ DefaultMutableTreeNode child = children.nextElement(); String obj = (String) child.getUserObject(); if(obj.equals(AND) || obj.equals(OR)){ List subCIList = getCIList(child, ciList.size() + 1); if(ciList.size() > 0){ ConditionItem leftCI = ciList.get(ciList.size() - 1); ConditionItem rightCI = subCIList.get(subCIList.size() - 1); ciList.addAll(subCIList); ConditionItem pCI = new ConditionItem(); pCI.setId("ci" + (ciList.size() + 1)); ciList.add(pCI); pCI.setLeafFlag(false); ChildrenInfo cInfo = new ChildrenInfo(); pCI.setChildrenInfo(cInfo); cInfo.setLeftCIName(leftCI.getId()); cInfo.setConnector(connector); cInfo.setRightCIName(rightCI.getId()); }else{ ciList.addAll(subCIList); } }else{ String text = (String) child.getUserObject(); if(text == null || text.equals("")){ continue; } if(!text.contains(Operator.EQUAL) && !text.contains(Operator.UNEQUAL) && !text.contains(Operator.CONTAINS) && !text.contains(Operator.IN) && !text.contains(Operator.GTE) && !text.contains(Operator.GT) && !text.contains(Operator.LTE) && !text.contains(Operator.LT)){ continue; } if(ciList.size() > 0){ ConditionItem leftCI = ciList.get(ciList.size() - 1); ConditionItem rightCI = getCIByNode(child, ciList.size() + 1); ciList.add(rightCI); ConditionItem pCI = new ConditionItem(); pCI.setId("ci" + (ciList.size() + 1)); ciList.add(pCI); pCI.setLeafFlag(false); ChildrenInfo cInfo = new ChildrenInfo(); pCI.setChildrenInfo(cInfo); cInfo.setLeftCIName(leftCI.getId()); cInfo.setConnector(connector); cInfo.setRightCIName(rightCI.getId()); }else{ ConditionItem ci = getCIByNode(child, ciList.size() + 1); ciList.add(ci); } } } for(int i = 0; i < ciList.size(); i++){ ConditionItem ci = ciList.get(i); ciMap.put(ci.getId(), ci); } return ciMap; } /** * 树化一个查询条件组节点 * @param node * @param beginId * @return */ private List getCIList(DefaultMutableTreeNode node, int beginId){ List ciList = new ArrayList(); String connector = (String)node.getUserObject(); @SuppressWarnings("unchecked") Enumeration children = node.children(); while(children.hasMoreElements()){ DefaultMutableTreeNode child = children.nextElement(); String obj = (String) child.getUserObject(); if(obj.equals(AND) || obj.equals(OR)){ List subCIList = getCIList(child, beginId); if(ciList.size() > 0){ ConditionItem leftCI = ciList.get(ciList.size() - 1); ConditionItem rightCI = subCIList.get(subCIList.size() - 1); ciList.addAll(subCIList); beginId = beginId + subCIList.size(); ConditionItem pCI = new ConditionItem(); pCI.setId("ci" + beginId); ciList.add(pCI); beginId = beginId + 1; pCI.setLeafFlag(false); ChildrenInfo cInfo = new ChildrenInfo(); pCI.setChildrenInfo(cInfo); cInfo.setLeftCIName(leftCI.getId()); cInfo.setConnector(connector); cInfo.setRightCIName(rightCI.getId()); }else{ ciList.addAll(subCIList); beginId = beginId + subCIList.size(); } }else{ String text = (String) child.getUserObject(); if(text == null || text.equals("")){ continue; } if(!text.contains(Operator.EQUAL) && !text.contains(Operator.UNEQUAL) && !text.contains(Operator.CONTAINS) && !text.contains(Operator.IN) && !text.contains(Operator.GTE) && !text.contains(Operator.GT) && !text.contains(Operator.LTE) && !text.contains(Operator.LT)){ continue; } if(ciList.size() > 0){ ConditionItem leftCI = ciList.get(ciList.size() - 1); ConditionItem rightCI = getCIByNode(child, beginId); ciList.add(rightCI); beginId = beginId + 1; ConditionItem pCI = new ConditionItem(); pCI.setId("ci" + beginId); ciList.add(pCI); beginId = beginId + 1; pCI.setLeafFlag(false); ChildrenInfo cInfo = new ChildrenInfo(); pCI.setChildrenInfo(cInfo); cInfo.setLeftCIName(leftCI.getId()); cInfo.setConnector(connector); cInfo.setRightCIName(rightCI.getId()); }else{ ConditionItem ci = getCIByNode(child, beginId); ciList.add(ci); beginId = beginId + 1; } } } return ciList; } /** * 获取参照的查询模板 * @param refAbName: 参照属性名 * @param clause: 属性参照的业务类型中的属性 * @param operator * @param ordinaryValue * @return */ private QueryTemplate getRefQT(String refAbName, String clause, String operator, String ordinaryValue) { QueryTemplate qt = new QueryTemplate(); List clauseList = new ArrayList(); clauseList.add("OID"); qt.setClauseList(clauseList); AttribItem refAb = ApProvider.getAbItemByName(refAbName); OtherInfo otherInfo = OtherInfo.getOtherInfoByText(refAb.other); int refFlag = otherInfo.getRefFlag(); String type = otherInfo.getRefTypeName(); if(refFlag == 0){ qt.setType(QTConstants.TYPE_BTM); qt.setBtmType(type); }else if(refFlag == 1){ qt.setType(QTConstants.TYPE_LINK); qt.setLinkType(type); } Condition condition = new Condition(); qt.setCondition(condition); condition.setRootCIName("ci1"); HashMap ciMap = new HashMap(); condition.setCIMap(ciMap); ConditionItem ci = new ConditionItem(); ci.setId("ci1"); ciMap.put(ci.getId(), ci); ci.setLeafFlag(true); LeafInfo leafInfo = new LeafInfo(); if(clause.contains(".")){ int fpIndex = clause.indexOf("."); String refAbName_ = clause.substring(0, fpIndex); clause = clause.substring(fpIndex + 1); leafInfo.setClause(refAbName_); leafInfo.setOperator(Operator.IN); QueryTemplate qt_ = getRefQT(refAbName_, clause, operator, ordinaryValue); LeafValue lValue = new LeafValue(); lValue.setQueryTemplate(qt_); leafInfo.setValue(lValue); qt.setId("qt_" + refAbName + "_" + refAbName_); }else{ leafInfo.setClause(clause); leafInfo.setOperator(operator); LeafValue lValue = new LeafValue(); lValue.setOrdinaryValue(ordinaryValue); leafInfo.setValue(lValue); qt.setId("qt_" + refAbName + "_" + clause); } ci.setLeafInfo(leafInfo); condition.setCIMap(ciMap); return qt; } /** * 高级查询 * 把Node内容解析成ConditionItem * @param nextNode * @param id * @return */ private ConditionItem getCIByNode(DefaultMutableTreeNode nextNode, int id){ String text = (String) nextNode.getUserObject(); ConditionItem ci = new ConditionItem(); ci.setId("ci" + id); ci.setLeafFlag(true); LeafInfo leafInfo = new LeafInfo(); ci.setLeafInfo(leafInfo); String operator = null; if(text.contains(Operator.CONTAINS)){ operator = Operator.CONTAINS; }else if(text.contains(Operator.UNEQUAL)){ operator = Operator.UNEQUAL; }else if(text.contains(Operator.GTE)){ operator = Operator.GTE; }else if(text.contains(Operator.GT)){ operator = Operator.GT; }else if(text.contains(Operator.LTE)){ operator = Operator.LTE; }else if(text.contains(Operator.LT)){ operator = Operator.LT; } else if(text.contains(Operator.EQUAL)){ operator = Operator.EQUAL; }else if(text.contains(Operator.IN)){ operator = Operator.IN; } int operatorIndex = text.indexOf(operator); String clause = text.substring(0, operatorIndex).trim(); String ordinaryValue = ""; if (text.length() > operatorIndex + 1) { ordinaryValue = text.substring(operatorIndex + 2).trim(); } //属性为参照属性 if(clause.contains(".")){ int fpIndex = clause.indexOf("."); String refAbName = clause.substring(0, fpIndex); clause = clause.substring(fpIndex + 1); leafInfo.setClause(refAbName); leafInfo.setOperator(Operator.IN); QueryTemplate qt = getRefQT(refAbName, clause, operator, ordinaryValue); LeafValue lValue = new LeafValue(); lValue.setQueryTemplate(qt); leafInfo.setValue(lValue); //属性为非参照属性 }else{ leafInfo.setClause(clause); leafInfo.setOperator(operator); LeafValue lValue = new LeafValue(); leafInfo.setValue(lValue); lValue.setOrdinaryValue(ordinaryValue); } return ci; } /** * 返回普通或者 高级 * rdGeneral: 0 * rdSenior : 1 * @return */ public short getRadioFlag(){ short flag = 0; if(rdSenior.isSelected()){ flag = 1; } return flag; } /** * 根据查询模板设置UI * @param qtWrapper */ public void setQTUI(QTInfo qtWrapper){ cp_centerPanel.removeAll(); QueryTemplate qt = null; try { qt = OQTool.getQTByDoc(DocumentHelper.parseText(qtWrapper.qtText), qtWrapper.qtName); } catch (DocumentException e1) { e1.printStackTrace(); } catch (VCIError e) { // TODO Auto-generated catch block e.printStackTrace(); } Condition condition = qt.getCondition(); if(condition == null){ cp_centerPanel.updateUI(); return; } Map ciMap = qt.getCondition().getCIMap(); String rootName = qt.getCondition().getRootCIName(); ConditionItem rootCI = ciMap.get(rootName); int levelFlag = qtWrapper.levelFlag; if(levelFlag == 0){ rdGeneral.setSelected(true); //当rdGeneral的Selected状态由false变为true时 , initCP_centerPanel //会设为默认的(将模板定义中的条件都加入), 此时先将默认的移除, 再加入qt的条件 //---START--- cp_centerPanel.removeAll(); cp_centerPanel.updateUI(); //---END--- ConditionItem lCI = null; ConditionItem rCI = null; if(!rootCI.isLeaf()){ lCI = ciMap.get(rootCI.getChildrenInfo().getLeftCIName()); rCI = ciMap.get(rootCI.getChildrenInfo().getRightCIName()); } List ciList = getCILeafList(ciMap, lCI, rootCI, rCI); for(int i = 0; i < ciList.size(); i++){ ConditionItem ci = ciList.get(i); LeafInfo leafInfo = ci.getLeafInfo(); GridBagConstraints g = new GridBagConstraints(); g.anchor = GridBagConstraints.WEST; g.gridx = 0; g.gridy = i; g.insets = new Insets(1, 1, 1, 1); final JButton btnRemove = new JButton("x"); cp_centerPanel.add(btnRemove, g); //获取leafInfo中的clause(有参照时加上参照)和value Map map = OQTool.getClauseAndValue(leafInfo); String attName = map.get("clause"); String value = map.get("value"); final JLabel labName = new JLabel(attName); g.gridx = 1; cp_centerPanel.add(labName, g); g.gridx = 2; final JComboBox combCons = new JComboBox(); combCons.setPreferredSize(new Dimension(80, 20)); if(attName.contains(".")){ attName = attName.substring(attName.lastIndexOf(".") + 1, attName.length()); } String attType = OQTool.getAbItemDataType(attName); if(attType.equalsIgnoreCase("VTInteger") || attType.equalsIgnoreCase("VTDouble") || attType.equalsIgnoreCase("VTLong")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); }else if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.LTE); }else{ combCons.addItem(Operator.EQUAL); combCons.addItem(Operator.UNEQUAL); combCons.addItem(Operator.CONTAINS); combCons.addItem(Operator.IN); combCons.addItem(Operator.NOTIN); combCons.addItem(Operator.GTE); combCons.addItem(Operator.GT); combCons.addItem(Operator.LTE); combCons.addItem(Operator.LT); } String operator = leafInfo.getOperator(); if(operator.equals(Operator.EQUAL)){ combCons.setSelectedItem(Operator.EQUAL); }else if(operator.equals(Operator.UNEQUAL)){ combCons.setSelectedItem(Operator.UNEQUAL); }else if(operator.equals(Operator.CONTAINS) || operator.equals(Operator.LIKE)){ combCons.setSelectedItem(Operator.CONTAINS); }else if(operator.equals(Operator.IN)){ combCons.setSelectedItem(Operator.IN); }else if(operator.equals(Operator.NOTIN)){ combCons.setSelectedItem(Operator.NOTIN); }else if(operator.equals(Operator.GTE)){ combCons.setSelectedItem(Operator.GTE); }else if(operator.equals(Operator.GT)){ combCons.setSelectedItem(Operator.GT); }else if(operator.equals(Operator.LTE)){ combCons.setSelectedItem(Operator.LTE); }else if(operator.equals(Operator.LT)){ combCons.setSelectedItem(Operator.LT); } cp_centerPanel.add(combCons, g); g.gridx = 3; //add by zhangweiwei 2014/12/09 start final JTextField tfValue = new JTextField(80); final JXDatePicker datePicker = new JXDatePicker(); datePicker.setPreferredSize(tfValue.getPreferredSize()); final Chooser chooser = Chooser.getInstance(); chooser.setLayout(cp_centerPanel.getLayout()); if(attType.equalsIgnoreCase("VTDateTime") || attType.equalsIgnoreCase("VTDate") || attType.equalsIgnoreCase("VTTime")){ cp_centerPanel.add(chooser, g); try { if (value == null || value.equalsIgnoreCase("null")) { // datePicker.setDate(null); JTextField text = new JTextField(value); text.setPreferredSize(tfValue.getPreferredSize()); chooser.register(text); } else { // datePicker.setDate(Tool.df.parse(value)); JTextField text = new JTextField(value); text.setPreferredSize(tfValue.getPreferredSize()); chooser.register(text); } //datePicker. } catch (Exception e1) { e1.printStackTrace(); } }else{ cp_centerPanel.add(tfValue, g); tfValue.setText(value); } g.gridx =4; final JButton chooseButton = new JButton("选择查询模板"); cp_centerPanel.add(chooseButton, g); g.gridwidth = GridBagConstraints.REMAINDER; cp_centerPanel.updateUI(); chooseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // TODO 后续窗口调整 显示查询模板窗口 SelectorQTDialog eid = new SelectorQTDialog(cp_centerPanel); QTInfo item = eid.getItem(); String attr = eid.getAttr(); //选中的查询模板 tfValue.setText(item.qtName + ";" + attr); } }); btnRemove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { cp_centerPanel.remove(btnRemove); cp_centerPanel.remove(labName); cp_centerPanel.remove(combCons); cp_centerPanel.remove(tfValue); cp_centerPanel.remove(chooser); cp_centerPanel.remove(chooseButton); cp_centerPanel.updateUI(); } }); //add by zhangweiwei 2014/12/09 end } }else if(levelFlag == 1){ rdSenior.setSelected(true); Document doc = null; try { if(qtWrapper.qtUIText != null && !qtWrapper.qtUIText.equals("")){ doc = DocumentHelper.parseText(qtWrapper.qtUIText); } } catch (DocumentException e) { e.printStackTrace(); } seniorTree = OQTool.parseDocToTree(doc); initSeniorTree(); seniorTree.updateUI(); } cp_centerPanel.updateUI(); } /** * 按先左后右顺序返回ciMap的叶子节点 * @param ciMap * @return */ private List getCILeafList(Map ciMap, ConditionItem left, ConditionItem root, ConditionItem right){ List ciList = new ArrayList(); //针对只有一个查询节点的情况 if(root.isLeaf()){ ciList.add(root); } if(left != null){ if(left.isLeaf()){ ciList.add(left); }else{ ConditionItem left_ = ciMap.get(left.getChildrenInfo().getLeftCIName()); ConditionItem root_ = left; ConditionItem right_ = ciMap.get(left.getChildrenInfo().getRightCIName()); ciList.addAll(getCILeafList(ciMap, left_, root_, right_)); } } if(right != null){ if(right.isLeaf()){ ciList.add(right); }else{ ConditionItem left_ = ciMap.get(right.getChildrenInfo().getLeftCIName()); ConditionItem root_ = left; ConditionItem right_ = ciMap.get(right.getChildrenInfo().getRightCIName()); ciList.addAll(getCILeafList(ciMap, left_, root_, right_)); } } return ciList; } /** * 初始高级查询树 */ private void initSeniorTree(){ if(seniorTree == null){ seniorRoot = new DefaultMutableTreeNode(AND); seniorTree = new JTree(seniorRoot); } seniorTree.addMouseListener(new MouseListener() { @Override public void mouseReleased(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseClicked(MouseEvent e) { } }); GridBagConstraints g1 = new GridBagConstraints(); g1.gridx = 0; g1.gridy = 0; g1.weightx = 1.0; g1.weighty = 1.0; g1.fill = GridBagConstraints.BOTH; seniorTree.setAutoscrolls(true); seniorTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); cp_centerPanel.add(seniorTree, g1); } /** * 增加逻辑条件 */ private void addCondition(){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) seniorTree.getLastSelectedPathComponent(); if(selectedNode == null){ JOptionPane.showMessageDialog(this, "请选择条件项或逻辑运算符", "选择条件", JOptionPane.INFORMATION_MESSAGE); return; } //对逻辑条件增加逻辑子节点 String obj = (String) selectedNode.getUserObject(); if (obj.equals(AND) || obj.equals(OR)) { DefaultMutableTreeNode node = new DefaultMutableTreeNode(AND); selectedNode.add(node); seniorTree.updateUI(); }else{ JOptionPane.showMessageDialog(this, "只能对逻辑条件增加逻辑条件", "只能对逻辑条件增加逻辑条件", JOptionPane.INFORMATION_MESSAGE); return; } } /** * 修改条件项或逻辑条件 */ private void modifyCondition(){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) seniorTree.getLastSelectedPathComponent(); if(selectedNode == null){ JOptionPane.showMessageDialog(this, "请选择条件项或逻辑运算符", "选择条件", JOptionPane.INFORMATION_MESSAGE); return; } //变更与或 if(selectedNode.getUserObject().equals(AND)){ selectedNode.setUserObject(OR); }else if(selectedNode.getUserObject().equals(OR)){ selectedNode.setUserObject(AND); //变更条件 }else{ String attName = (String) selectedNode.getUserObject(); if(attName.contains(".")){ attName = attName.substring(attName.lastIndexOf(".") + 1, attName.length()); } ConditionDialog dialog = new ConditionDialog(attName); dialog.setVisible(true); JComboBox combCons = dialog.getCombCons(); int disposeEvent = dialog.getDisposeEvent(); if(disposeEvent == ConditionDialog.disposeByOK){ String cons = dialog.getCons(); String value = dialog.getValue(); //add by caill start 2015.12.25 String preUserObj = (String)selectedNode.getUserObject(); int size=combCons.getModel().getSize(); List list = new ArrayList(); Map map = new HashMap(); String str = null; String a = null; String userObj = null; int index = -1; for(int i = 0; i0){ Collections.sort(list); index = list.get(0); str = map.get(index); int length = str.length(); a = preUserObj.substring(0, index+length); //将文本框值之前的字符串分割出来,防止文本框中含有跟不等式关键字相同的字符而影响下面的判断 userObj = trimUserObj(a); }else{ userObj = trimUserObj(preUserObj); } //add by caill end //String userObj = trimUserObj((String)selectedNode.getUserObject()); String all = userObj + " " + cons + " " + value; selectedNode.setUserObject(all); seniorTree.updateUI(); } } seniorTree.updateUI(); } /** * 删除条件 */ private void deleteCondition(){ DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) seniorTree.getLastSelectedPathComponent(); if(selectedNode == null){ JOptionPane.showMessageDialog(this, "请选择条件项或逻辑运算符", "选择条件", JOptionPane.INFORMATION_MESSAGE); return; } if (selectedNode.isRoot()) { JOptionPane.showMessageDialog(this, "不可删除根节点", "不可删除根节点", JOptionPane.INFORMATION_MESSAGE); return; } int option = JOptionPane.showConfirmDialog(cp_centerPanel, "删除该条件", "删除该条件", JOptionPane.YES_NO_OPTION); if (option != JOptionPane.YES_OPTION) { return; } ((DefaultMutableTreeNode) selectedNode.getParent()).remove(selectedNode); seniorTree.updateUI(); } /** * 获取当前选择的查询模板定义名字 * @return */ public String getSelectedQTDName(){ if(combQTD == null || combQTD.getSelectedItem() == null || ((QTDWrapper)combQTD.getSelectedItem()).qtd == null){ JOptionPane.showMessageDialog(this, "无查询模板", "无查询模板", JOptionPane.INFORMATION_MESSAGE); return null; } return ((QTDWrapper) combQTD.getSelectedItem()).qtd.name; } public boolean isLinkTypeSelected(){ if(rdLinkType.isSelected()){ return true; }else{ return false; } } /** * 获取业务类型选择按钮 * @return */ public JRadioButton getRadioBtm(){ return rdBtm; } /** * 获取链接类型选择按钮 * @return */ public JRadioButton getRadioLinkType(){ return rdLinkType; } }