package com.vci.client.ui.swing.components.table.export; 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.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.regex.Pattern; import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListCellRenderer; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JList; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.PlainDocument; 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.VCIJList; import com.vci.client.ui.swing.components.VCIJOptionPane; import com.vci.client.ui.swing.components.VCIJPanel; import com.vci.client.ui.swing.components.VCIJRadioButton; import com.vci.client.ui.swing.components.VCIJScrollPane; import com.vci.client.ui.swing.components.VCIJTextField; import com.vci.client.ui.swing.components.table.AbstractVCIJTableDataProvider; import com.vci.client.ui.swing.components.table.VCIJTablePanel; /** * *

Title:

*

Description:

*

Copyright: Copyright (c) 2012

*

Company: VCI

* @author xchao * @time 2012-5-10 * @version 1.0 */ public class VCIJTablePanelExportFilterDialog extends VCIJDialog { /** * */ private static final long serialVersionUID = 5179592609825598347L; /************************ 导出相关的 *********************************/ private VCIJComboBox cbxExportTypes = new VCIJComboBox(); private LinkedHashMap exportSupportTypes = new LinkedHashMap(); private VCIJRadioButton rbtnChecked = new VCIJRadioButton("选择", true); private VCIJRadioButton rbtnAll = new VCIJRadioButton("全部", false); private VCIJRadioButton rbtnPageNum = new VCIJRadioButton("页码"); private SpecialFormatPageRangeTextField txtPageNum = new SpecialFormatPageRangeTextField("1", 10); private VCIJLabel lblPateNum = new VCIJLabel("(输入页码或页面范围。如:1-10)"); private VCIJTablePanelExportFilterDialogActionListener actionListener = new VCIJTablePanelExportFilterDialogActionListener(this); // 实例初始化 { exportSupportTypes.put("Excel(*.xls|*.xlsx)", VCISwingUtil.createImageIcon("export_excel.png")); // exportSupportTypes.put("Word(*.doc|*.docx)", VCISwingUtil.createImageIcon("export_word.png")); // exportSupportTypes.put("PDF(*.pdf)", VCISwingUtil.createImageIcon("export_pdf.png")); } private VCIJTablePanel tablePanel = null; public VCIJTablePanelExportFilterDialog(VCIJTablePanel tablePanel){ super(VCISwingUtil.getClientMainFrame()); this.tablePanel = tablePanel; this.init(); } private void init(){ initSizeAndLocation(); initComponents(); } private void initSizeAndLocation(){ setTitle("数据导出属性选择窗口"); setSize(new Dimension(700, 550)); Point location = VCISwingUtil.getClientMainFrame().getLocation(); location.setLocation(location.getX() + 30, location.getY() + 30); setLocation(location); } private void initComponents(){ setLayout(new BorderLayout()); add(createCenterPanel(), BorderLayout.CENTER); add(createBottomButtonPanel(), BorderLayout.SOUTH); } private VCIJPanel createCenterPanel(){ VCIJPanel pal = new VCIJPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridwidth = 4; c.gridheight = 1; c.weightx = 0; c.weighty = 0; c.fill = GridBagConstraints.EAST; c.anchor = GridBagConstraints.NORTHWEST; pal.add(createCheckBoxPanel(), c); c.gridx = 0; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.NORTHWEST; VCIJScrollPane jspScrollListLeft = new VCIJScrollPane(createLeftList()); jspScrollListLeft.setPreferredSize(new Dimension(100, 100)); pal.add(jspScrollListLeft, c); c.gridx = 1; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0; c.weighty = 0; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; pal.add(createMoveLeftRightButtonPanel(), c); c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.NORTHEAST; VCIJScrollPane jspScrollListRight = new VCIJScrollPane(createRightList()); jspScrollListRight.setPreferredSize(new Dimension(100, 100)); pal.add(jspScrollListRight, c); c.gridx = 3; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0; c.weighty = 0; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; pal.add(createMoveUpDownButtonPanel(), c); return pal; } private VCIJPanel createMoveLeftRightButtonPanel(){ VCIJPanel pal = new VCIJPanel(); pal.setLayout(new BoxLayout(pal, BoxLayout.Y_AXIS)); VCIJButton btn = createVCIJButton("move_left_all_to_right", "", "", "control-fast-forward.png"); pal.add(btn); pal.add(new VCIJLabel(" ")); pal.add(new VCIJLabel(" ")); btn = createVCIJButton("move_left_part_to_right", "", "", "arrow-right.png"); pal.add(btn); pal.add(new VCIJLabel(" ")); pal.add(new VCIJLabel(" ")); btn = createVCIJButton("move_right_part_to_left", "", "", "arrow-left.png"); pal.add(btn); pal.add(new VCIJLabel(" ")); pal.add(new VCIJLabel(" ")); btn = createVCIJButton("move_right_all_to_left", "", "", "control-rewind.png"); pal.add(btn); return pal; } private VCIJPanel createMoveUpDownButtonPanel(){ VCIJPanel pal = new VCIJPanel(); pal.setLayout(new BoxLayout(pal, BoxLayout.Y_AXIS)); VCIJButton btn = createVCIJButton("move_down_to_up", "", "", "arrow-up.png"); pal.add(btn); pal.add(new VCIJLabel(" ")); pal.add(new VCIJLabel(" ")); btn = createVCIJButton("move_up_to_down", "", "", "arrow-down.png"); pal.add(btn); pal.add(new VCIJLabel(" ")); pal.add(new VCIJLabel(" ")); return pal; } private VCIJPanel createCheckBoxPanel(){ VCIJPanel pal = new VCIJPanel(new FlowLayout(FlowLayout.LEADING)); pal.add(new VCIJLabel("导出类型:")); DefaultComboBoxModel cbxModel = new DefaultComboBoxModel(); Iterator keys = this.exportSupportTypes.keySet().iterator(); while(keys.hasNext()){ String key = keys.next(); cbxModel.addElement(key); } this.cbxExportTypes.setModel(cbxModel); this.cbxExportTypes.setRenderer(new DefaultListCellRenderer(){ /** * */ private static final long serialVersionUID = -994757979130204768L; public Component getListCellRendererComponent(JList list, Object value,int index, boolean isSelected, boolean cellHasFocus){ Component compt = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); this.setIcon(exportSupportTypes.get(value)); return compt; } }); pal.add(this.cbxExportTypes); ButtonGroup rbtnGroup = new ButtonGroup(); rbtnGroup.add(rbtnChecked); rbtnGroup.add(rbtnAll); rbtnGroup.add(rbtnPageNum); pal.add(rbtnChecked); pal.add(rbtnAll); pal.add(rbtnPageNum); pal.add(txtPageNum); pal.add(lblPateNum); txtPageNum.setText(String.valueOf(tablePanel.getPageIndex())); ActionListener rbtnActionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { txtPageNum.setEditable(!rbtnAll.isSelected()); } }; rbtnAll.addActionListener(rbtnActionListener); rbtnPageNum.addActionListener(rbtnActionListener); rbtnActionListener.actionPerformed(null); return pal; } private VCIJList listLeft = new VCIJList(); private VCIJList createLeftList(){ VCIJList list = new VCIJList(); list.setFont(VCISwingUtil.FONT_SONTTI_PLAN_14); DefaultListModel model = new DefaultListModel(); for(int i = 1; i < this.tablePanel.getTable().getColumnCount(); i++){ String column = this.tablePanel.getTable().getColumnName(i); if(column.equals("序号")) continue; model.addElement(column); } list.setModel(model); list.setCellRenderer(listRender); listLeft = list; listLeft.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2){ actionListener.move_left_part_to_right(); listLeft.updateUI(); } } }); return list; } private VCIJList listRight= new VCIJList(); private VCIJList createRightList(){ VCIJList list = new VCIJList(); DefaultListModel model = new DefaultListModel(); list.setFont(VCISwingUtil.FONT_SONTTI_PLAN_14); list.setCellRenderer(listRender); list.setModel(model); listRight = list; listRight.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if(e.getClickCount() == 2){ actionListener.move_right_part_to_left(); listRight.updateUI(); } } }); return list; } private VCIJPanel createBottomButtonPanel(){ VCIJPanel pal = new VCIJPanel(); VCIJButton btnOk = createVCIJButton("ok", "确定", "确定", "accept.png"); VCIJButton btnCancel = createVCIJButton("cancel", "取消", "取消", "cancel.png"); pal.add(btnOk); pal.add(btnCancel); return pal; } private VCIJButton createVCIJButton(String actionCommand, String text, String toolTip, String icon){ VCIJButton btn = VCISwingUtil.createVCIJButton(actionCommand, text, toolTip, icon, actionListener); btn.setIcon(VCISwingUtil.createImageIcon(icon)); return btn; } DefaultListCellRenderer listRender = new DefaultListCellRenderer(){ /** * */ private static final long serialVersionUID = 4705852866494910106L; public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { Component compt = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); setIcon(VCISwingUtil.createImageIcon("attribute.png")); return compt; } }; public VCIJRadioButton getRbtnChecked() { return rbtnChecked; } public VCIJRadioButton getRbtnPageNum() { return rbtnPageNum; } public SpecialFormatPageRangeTextField getTxtPageNum() { return txtPageNum; } public VCIJList getListLeft() { return listLeft; } public void setListLeft(VCIJList listLeft) { this.listLeft = listLeft; } public VCIJList getListRight() { return listRight; } public void setListRight(VCIJList listRight) { this.listRight = listRight; } public boolean isValidPageForamt(){ boolean res = false; if(rbtnChecked.isSelected() || rbtnAll.isSelected()) { res = true; return res; } String pageNum = txtPageNum.getText(); if(pageNum.equals("")){ VCIJOptionPane.showError(this, "页码不能为空"); } else{ int pageCount = tablePanel.getPageCount(); String[] pages = txtPageNum.getText().split("-"); // 用dobule接收输入的数据,防止输入超大值(大于Integer.MAX_VALUE) // 转换成Integer,进行比较,及在提示内容中去掉double类型数据可能会出现的小数点 if(pages.length == 1){ double pageD = Double.parseDouble(pages[0]); if(pageD > Integer.MAX_VALUE){ VCIJOptionPane.showError(this, "起始页码 " + String.valueOf(pageD) + " 不得大于 " + Integer.MAX_VALUE); } else{ int page = (int)pageD; if(page > pageCount){ VCIJOptionPane.showError(this, "输入的页码 " + page + " 不得大于总页数 " + pageCount); } else if(page > Integer.MAX_VALUE){ VCIJOptionPane.showError(this, "输入的页码 " + page + " 不得大于 " + Integer.MAX_VALUE); } else { res = true; } } } else{ double pageStartD = Double.parseDouble(pages[0]); double pageEndD = Double.parseDouble(pages[1]); if(pageStartD > Integer.MAX_VALUE){ VCIJOptionPane.showError(this, "起始页码 " + pageStartD + " 不得大于 " + Integer.MAX_VALUE); } else if(pageEndD > Integer.MAX_VALUE){ VCIJOptionPane.showError(this, "结束页码 " + pageEndD + " 不得大于 " + Integer.MAX_VALUE); } else{ int pageStart = (int)pageStartD; int pageEnd = (int)pageEndD; if(pageStart > pageCount){ VCIJOptionPane.showError(this, "起始页码 " + pageStart + " 不得大于总页数 " + pageCount); } else if(pageEnd > pageCount){ VCIJOptionPane.showError(this, "结束页码 " + pageEnd + " 不得大于总页数 " + pageCount); } else if(pageStart > pageEnd){ VCIJOptionPane.showError(this, "起始页码 " + pageStart + " 不得大于结束页码 " + pageEnd); } else{ res = true; } } } } if(!res){ pageNumRequestFocus(); } return res; } /** * 返回需要导出数据的PageIndex列表 *

Description:

* * @author xchao * @time 2012-6-15 * @return */ public Integer[] getPageIndexs(){ Integer[] res = {}; LinkedList list = new LinkedList(); int start = 1; int end = tablePanel.getPageCount(); if(rbtnPageNum.isSelected()){ String[] pages = txtPageNum.getText().split("-"); if(pages.length == 1){ start = end = Integer.parseInt(pages[0]); }else{ start = Integer.parseInt(pages[0]); end = Integer.parseInt(pages[1]); } } for(int i = start; i <= end; i++){ list.add(i); } res = list.toArray(new Integer[]{}); return res; } /** * 返回要导出数据的属性名称列表 *

Description:

* * @author xchao * @time 2012-6-15 * @return */ public LinkedList getExportFields(){ LinkedList list = new LinkedList(); DefaultListModel model = (DefaultListModel)getListRight().getModel(); int size = model.getSize(); for(int i = 0; i < size; i++){ list.add(model.getElementAt(i).toString()); } return list; } private void pageNumRequestFocus(){ txtPageNum.requestFocus(); txtPageNum.selectAll(); } class SpecialFormatPageRangeTextField extends VCIJTextField{ /** * */ private static final long serialVersionUID = -870828004389831472L; public SpecialFormatPageRangeTextField(String value, int columns){ super(value, columns); addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { this_keyReleased(e); } }); } private void this_keyReleased(KeyEvent e){ String text = getText(); if(text.equals("")) return; if(text.startsWith("0")){ if(text.length() != 1){ text = text.substring(2); } else { text = ""; } } // 数值不能以-.或.开头 else if(text.startsWith("-")){ if(text.length() != 1){ text = "1-" + text.substring(2); } else { text = "1-"; } } else if(text.startsWith(".")){ text = text.substring(1); } else if(text.indexOf("-") != text.lastIndexOf("-")){ text = text.substring(0, text.length() - 1); } setText(text); } @Override protected Document createDefaultModel(){ return new NumberPlainDocument(); } } class NumberPlainDocument extends PlainDocument{ /** * */ private static final long serialVersionUID = 7590021361237646854L; public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { if (str == null) { return; } char[] chars = str.toCharArray(); LinkedList charsList = new LinkedList(); for(char c : chars){ // 匹配数字 if(pNum.matcher(String.valueOf(c)).matches()){ charsList.add(c); } // 匹配减号 else if(pMinus.matcher(String.valueOf(c)).matches()){ charsList.add(c); } } char[] charsNew = new char[charsList.size()]; for(int i = 0; i < charsNew.length; i++){ charsNew[i] = charsList.get(i); } super.insertString(offs, new String(charsNew), a); } Pattern pNum = Pattern.compile("\\d+"); Pattern pMinus = Pattern.compile("-?"); } public VCIJTablePanel getTablePanel() { return tablePanel; } public void setTablePanel(VCIJTablePanel tablePanel) { this.tablePanel = tablePanel; } }