/**
|
* @author xchao
|
* @time 2012-4-26
|
* @desc
|
* @version
|
* @since
|
*/
|
package com.vci.client.ui.swing.components.table.test;
|
|
|
import java.awt.BorderLayout;
|
import java.awt.Component;
|
import java.awt.Dimension;
|
import java.awt.Frame;
|
import java.awt.GridBagConstraints;
|
import java.awt.GridBagLayout;
|
import java.awt.Insets;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseEvent;
|
import java.beans.PropertyChangeEvent;
|
import java.beans.PropertyChangeListener;
|
import java.text.DecimalFormat;
|
import java.text.MessageFormat;
|
import java.util.HashMap;
|
import java.util.LinkedList;
|
import java.util.Map;
|
import java.util.regex.Pattern;
|
|
import javax.swing.DefaultCellEditor;
|
import javax.swing.Icon;
|
import javax.swing.JFrame;
|
import javax.swing.JLabel;
|
import javax.swing.JTable;
|
import javax.swing.JToggleButton;
|
import javax.swing.SwingUtilities;
|
import javax.swing.border.TitledBorder;
|
import javax.swing.table.TableColumn;
|
import javax.swing.table.TableColumnModel;
|
import javax.swing.text.JTextComponent;
|
|
import com.vci.client.ui.swing.VCISwingUtil;
|
import com.vci.client.ui.swing.components.SpecialCharChooseInferface;
|
import com.vci.client.ui.swing.components.VCIJButton;
|
import com.vci.client.ui.swing.components.VCIJCalendarPanel;
|
import com.vci.client.ui.swing.components.VCIJComboBox;
|
import com.vci.client.ui.swing.components.VCIJFrame;
|
import com.vci.client.ui.swing.components.VCIJIntegerTextField;
|
import com.vci.client.ui.swing.components.VCIJLabel;
|
import com.vci.client.ui.swing.components.VCIJOptionPane;
|
import com.vci.client.ui.swing.components.VCIJPanel;
|
import com.vci.client.ui.swing.components.VCIJRealTextField;
|
import com.vci.client.ui.swing.components.VCIJScrollPane;
|
import com.vci.client.ui.swing.components.VCIJTextArea;
|
import com.vci.client.ui.swing.components.VCIJTextContextMenu;
|
import com.vci.client.ui.swing.components.VCIJTextField;
|
import com.vci.client.ui.swing.components.table.IndexColumnTableCellRender;
|
import com.vci.client.ui.swing.components.table.VCIJTableCheckEvent;
|
import com.vci.client.ui.swing.components.table.VCIJTableCheckListener;
|
import com.vci.client.ui.swing.components.table.VCIJTablePanel;
|
import com.vci.client.common.ClientLog4j;
|
|
public class VCIJTablePanelTest extends VCIJFrame implements ActionListener {
|
|
/**
|
*
|
*/
|
private static final long serialVersionUID = 1752987097209928396L;
|
public VCIJTablePanelTest(){
|
init();
|
}
|
|
private void init(){
|
initActionMap();
|
initComponents();
|
}
|
|
private Map<String, Runnable> actionMap = new HashMap<String, Runnable>();
|
private void initActionMap(){
|
actionMap.put("startEditor", new Runnable() {
|
@Override
|
public void run() {
|
startEditor();
|
}
|
});
|
actionMap.put("endEditor", new Runnable() {
|
@Override
|
public void run() {
|
endEditor();
|
}
|
});
|
}
|
private void initComponents(){
|
setLayout(new BorderLayout());
|
add(new VCIJScrollPane(createNowthSearchPanel()), BorderLayout.NORTH);
|
add(createVCIJTablePanel(), BorderLayout.CENTER);
|
add(createSourthPanel(), BorderLayout.SOUTH);
|
}
|
|
private VCIJPanel createNowthSearchPanel(){
|
VCIJPanel pal = new VCIJPanel();
|
pal.setLayout(new GridBagLayout());
|
GridBagConstraints c = new GridBagConstraints();
|
VCIJLabel lblCode = new VCIJLabel("编码:");
|
VCIJTextField txtCode = new VCIJTextField("", 40);
|
txtCode.setRequired(true);
|
|
VCIJLabel lblName = new VCIJLabel("名称:");
|
VCIJTextField txtName = new VCIJTextField("", 40);
|
|
VCIJLabel lblDesc = new VCIJLabel("描述:");
|
VCIJIntegerTextField txtDesc = new VCIJIntegerTextField("");txtDesc.setRequired(true);
|
|
VCIJLabel lblOther = new VCIJLabel("其它:");
|
VCIJRealTextField txtOther = new VCIJRealTextField("");txtOther.setRequired(true);
|
|
VCIJLabel lblStartTime = new VCIJLabel("开始时间:");
|
VCIJTextField txtStartTime = new VCIJTextField("", 40);
|
|
VCIJLabel lblEndTime = new VCIJLabel("结束时间:");
|
VCIJTextField txtEndTime = new VCIJTextField("", 40);
|
|
VCIJLabel lblDescDeial = new VCIJLabel("复杂描述:");
|
VCIJTextArea txtDescDeial = createVCIJTextArea();
|
txtDescDeial.setLineWrap(true);
|
txtDescDeial.setWrapStyleWord(true);
|
VCIJScrollPane jspDescDeial = new VCIJScrollPane();
|
jspDescDeial.getViewport().add(txtDescDeial);
|
jspDescDeial.setBorder(txtDescDeial.getBorder());
|
VCIJButton btnSearch = new VCIJButton("查询", createImageIcon("search.png"));
|
VCIJButton btnSearchAdvance = new VCIJButton("高级查询", createImageIcon("search_advance.png"));
|
VCIJButton btnClear = new VCIJButton("清空查询条件", createImageIcon("clear.gif"));
|
|
VCIJButton btnStartEditor = VCISwingUtil.createVCIJButton("startEditor", "开始编辑", "", "accept.png", this);
|
VCIJButton btnStopEditor = VCISwingUtil.createVCIJButton("endEditor", "结束编辑", "", "cancel.png", this);
|
JToggleButton btn = new JToggleButton("ToggleButton", createImageIcon("add.png"), false);
|
|
c = getGridBagConstraints(c, 0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblCode, c);
|
c = getGridBagConstraints(c, 1, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtCode, c);
|
|
c = getGridBagConstraints(c, 2, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblName, c);
|
c = getGridBagConstraints(c, 3, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtName, c);
|
|
c = getGridBagConstraints(c, 0, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblDesc, c);
|
c = getGridBagConstraints(c, 1, 1, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtDesc, c);
|
|
c = getGridBagConstraints(c, 2, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblOther, c);
|
c = getGridBagConstraints(c, 3, 1, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtOther, c);
|
|
c = getGridBagConstraints(c, 0, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblStartTime, c);
|
c = getGridBagConstraints(c, 1, 2, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtStartTime, c);
|
|
c = getGridBagConstraints(c, 2, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblEndTime, c);
|
c = getGridBagConstraints(c, 3, 2, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(txtEndTime, c);
|
|
c = getGridBagConstraints(c, 0, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(lblDescDeial, c);
|
c = getGridBagConstraints(c, 1, 3, 10, 3, 0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH);
|
pal.add(jspDescDeial, c);
|
|
VCIJPanel palBtn = new VCIJPanel();
|
palBtn.add(btnSearch);
|
palBtn.add(btnSearchAdvance);
|
palBtn.add(btnClear);
|
|
palBtn.add(btnStartEditor);
|
palBtn.add(btnStopEditor);
|
palBtn.add(btn);
|
c = getGridBagConstraints(c, 3, 15, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE);
|
pal.add(palBtn, c);
|
return pal;
|
}
|
|
private GridBagConstraints getGridBagConstraints(GridBagConstraints c, int gridx, int gridy,
|
int gridwidth, int gridheight, double weightx, double weighty,
|
int anchor, int fill){
|
c.gridx = gridx;
|
c.gridy = gridy;
|
c.gridheight = gridheight;
|
c.gridwidth = gridwidth;
|
c.weightx = weightx;
|
c.weighty = weighty;
|
c.fill = fill;
|
c.anchor = anchor;
|
c.insets = new Insets(1, 1, 1, 1);
|
c.ipadx = 1;
|
c.ipady = 1;
|
return c;
|
}
|
|
private Icon createImageIcon(String icon){
|
if(icon == null || icon.equals("")) return null;
|
return VCISwingUtil.createImageIconFromImagePath(icon);
|
}
|
private VCIJTextArea textArea = createVCIJTextArea();
|
private VCIJTablePanel<TestPO> tablePanel = null;
|
private VCIJTablePanel<TestPO> createVCIJTablePanel(){
|
TestDataProvider dataProvider = new TestDataProvider();
|
tablePanel = new VCIJTablePanel<TestPO>(dataProvider);
|
tablePanel.setCustomButtons(this.createCustomButtons());
|
tablePanel.setShowPaging(true);
|
tablePanel.setShowExport(true);
|
tablePanel.setPageSizeList(new int[]{100,200,300,500,1000,2000,3000,5000,10000});
|
|
// tablePanel.setEnableMouseClickEffect(false);
|
// tablePanel.setEnableMouseMoveEffect(false);
|
LinkedList<Integer> editableColumnList = new LinkedList<Integer>();
|
editableColumnList.add(3);editableColumnList.add(4);
|
editableColumnList.add(5);editableColumnList.add(6);
|
editableColumnList.add(7);editableColumnList.add(8);
|
tablePanel.addCheckListener(new VCIJTableCheckListener() {
|
@Override
|
public void actionPerformed(VCIJTableCheckEvent e) {
|
VCIJOptionPane.showMessage(tablePanel, e.getSource().getClass().getName());
|
}
|
});
|
tablePanel.setEdiableColumnList(editableColumnList);
|
tablePanel.setReorderingAllowed(true);
|
tablePanel.setLastClickRowToChecked(true);
|
tablePanel.buildTablePanel();
|
tablePanel.refreshTableData();
|
tablePanel.getTable().getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(new VCIJComboBox(new Object[]{"1","2"})));
|
tablePanel.getTable().getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(new VCIJTextField(true)));
|
tablePanel.getTable().addMouseListener(new MouseAdapter() {
|
public void mouseClicked(MouseEvent e) {
|
int column = tablePanel.getTable().columnAtPoint(e.getPoint());
|
if(column == 0) return;
|
StringBuffer sb = new StringBuffer();
|
sb.append("------>");
|
sb.append("row:");
|
sb.append(tablePanel.getTable().getSelectedRow());
|
int[] rows = tablePanel.getTable().getSelectedRows();
|
sb.append("rows:");
|
for(int r : rows){
|
sb.append(r);
|
}
|
for(int row : rows){
|
TestPO obj = tablePanel.getSpecialObjectByRowIndex(row);
|
sb.append("\n")
|
.append(obj.getId()).append(",")
|
.append(obj.getName()).append(",")
|
.append(obj.getDesc()).append(",")
|
.append(obj.getOther()).append(",");
|
|
sb.append("\n").append(getTestBOString(obj));
|
}
|
sb.append("<------\n");
|
textArea.setText(sb.toString());
|
System.out.println(tablePanel.getTableSorter().getSortingStatus(3));
|
}
|
});
|
|
final DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
|
IndexColumnTableCellRender<TestPO> cellRenderer = new IndexColumnTableCellRender<TestPO>(tablePanel){
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
Component compt = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
|
if(compt instanceof javax.swing.JLabel){
|
JLabel lbl = (JLabel)compt;
|
if(value instanceof Double){
|
double d = (Double)value;
|
lbl.setText(decimalFormat.format(d));
|
} else if(value instanceof Float){
|
float f = (Float)value;
|
lbl.setText(decimalFormat.format(f));
|
} else if(value instanceof Long){
|
long l = (Long)value;
|
lbl.setText(decimalFormat.format(l));
|
}
|
}
|
return compt;
|
}
|
};
|
int start = tablePanel.isShowCheckbox() ? 1 : 0;
|
for(int i = start; i < tablePanel.getTable().getColumnCount(); i++){
|
tablePanel.getTable().getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
|
}
|
return tablePanel;
|
}
|
|
boolean defaultValue = false;// 是否显示默认值(如果指定了format,按format显示,未指定时,默认按yyyy-MM-dd 00:00:00,即:仅日期)
|
boolean hasDate = true;
|
VCIJCalendarPanel calendar1 = new VCIJCalendarPanel(true, defaultValue, true, true);
|
VCIJCalendarPanel calendar2 = new VCIJCalendarPanel(true, defaultValue, true, false);
|
VCIJCalendarPanel calendar3 = new VCIJCalendarPanel(true, defaultValue, false, true);
|
VCIJCalendarPanel calendar4 = new VCIJCalendarPanel(true, defaultValue, false, true);
|
VCIJCalendarPanel calendar5 = new VCIJCalendarPanel(true, defaultValue, false, true, "HH:00:00");
|
private VCIJPanel createSourthPanel(){
|
VCIJPanel pal = new VCIJPanel();
|
pal.setLayout(new GridBagLayout());
|
GridBagConstraints c = new GridBagConstraints();
|
pal.setBorder(new TitledBorder("SouthPanel"));
|
textArea.setLineWrap(true);
|
textArea.setWrapStyleWord(true);
|
pal.add(calendar1, getGridBagConstraints(c, 0, 0, 1, 1, 1.0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
pal.add(calendar2, getGridBagConstraints(c, 1, 0, 1, 1, 1.0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
pal.add(calendar3, getGridBagConstraints(c, 2, 0, 1, 1, 1.0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
pal.add(calendar4, getGridBagConstraints(c, 3, 0, 1, 1, 1.0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
pal.add(calendar5, getGridBagConstraints(c, 4, 0, 1, 1, 1.0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
calendar5.setItemStyleString("minseczeroFlag=1");
|
|
PropertyChangeListener pcl = new PropertyChangeListener() {
|
@Override
|
public void propertyChange(PropertyChangeEvent evt) {
|
System.out.println(MessageFormat.format("{0}:{1}->{2}",
|
evt.getPropertyName(), evt.getOldValue(), evt.getNewValue()));
|
}
|
};
|
calendar1.addPropertyChangeListener(pcl);
|
calendar2.addPropertyChangeListener(pcl);
|
calendar3.addPropertyChangeListener(pcl);
|
calendar4.addPropertyChangeListener(pcl);
|
calendar5.addPropertyChangeListener(pcl);
|
|
VCIJScrollPane jspTextArea = new VCIJScrollPane();
|
jspTextArea.getViewport().add(textArea);
|
jspTextArea.setPreferredSize(new Dimension(getWidth(), 100));
|
pal.add(jspTextArea, getGridBagConstraints(c, 0, 1, 5, 10, 1.0, 1.0, GridBagConstraints.EAST, GridBagConstraints.BOTH));
|
return pal;
|
}
|
|
private LinkedList<VCIJButton> createCustomButtons(){
|
LinkedList<VCIJButton> btns = new LinkedList<VCIJButton>();
|
String[][] btnNames = new String[][]{
|
{"添加","add.png"},
|
{"修改","edit.png"},
|
{"删除","delete.png"},
|
{"自定义业务按钮1","save.gif"},
|
{"自定义业务按钮2","save.gif"},
|
{"自定义业务按钮3","save.gif"},
|
{"自定义业务按钮4","save.gif"}
|
};
|
int length = btnNames.length;
|
for (int i = 0; i < length; i++) {
|
VCIJButton btn = new VCIJButton(btnNames[i][0], createImageIcon(btnNames[i][1]));
|
btn.addActionListener(this);
|
btns.add(btn);
|
}
|
return btns;
|
}
|
|
|
private void startEditor(){
|
tablePanel.setCellEditable(true);
|
System.out.println(
|
tablePanel.getTable().getColumnModel().getColumn(3).getCellEditor()
|
);
|
}
|
|
|
private void endEditor(){
|
|
Pattern ptnNumFloat = Pattern.compile("[0-9]+(\\.[0-9]+)?");
|
System.out.println(ptnNumFloat.matcher("123").matches());
|
System.out.println(ptnNumFloat.matcher("123.").matches());
|
System.out.println(ptnNumFloat.matcher("123.1234").matches());
|
|
cancelCellEditor();
|
tablePanel.setCellEditable(false);
|
}
|
|
private void cancelCellEditor(){
|
TableColumnModel tcm = tablePanel.getTable().getColumnModel();
|
int columnCount = tcm.getColumnCount();
|
for (int i = 0; i < columnCount; i++) {
|
TableColumn tc = tcm.getColumn(i);
|
if(tc.getCellEditor() != null){
|
tc.getCellEditor().stopCellEditing();
|
}
|
}
|
}
|
|
|
|
/*********************************************************************************/
|
public void actionPerformed(ActionEvent e) {
|
String actionCommand = e.getActionCommand();
|
if(actionMap.containsKey(actionCommand)){
|
actionMap.get(actionCommand).run();
|
return;
|
}
|
TestPO[] objs = tablePanel.getSelectedRowObjects().toArray(new TestPO[]{});
|
StringBuffer sb = new StringBuffer();
|
for(TestPO obj : objs){
|
sb.append(getTestBOString(obj)).append("\n");
|
}
|
String text = sb.toString();
|
textArea.setText(text);
|
System.out.println(text);
|
}
|
|
private String getTestBOString(TestPO obj){
|
String res =
|
obj.getId() + "\t" +
|
obj.getName() + "\t" +
|
obj.getDesc() + "\t" +
|
obj.getOther() + "\t"
|
;
|
return res;
|
}
|
|
public static void main(String[] args) {
|
SwingUtilities.invokeLater(new Runnable() {
|
public void run() {
|
ClientLog4j.logger.debug(System.currentTimeMillis());
|
VCISwingUtil.setLookAndFeel();
|
VCISwingUtil.setFont();
|
VCIJTablePanelTest frame = new VCIJTablePanelTest();
|
VCISwingUtil.setClientMainFrame(frame);
|
frame.setTitle("VCIJTablePanel Test Demo");
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
Dimension size = new Dimension(800, 600);
|
frame.setPreferredSize(size);
|
frame.setSize(size);
|
frame.setLocationRelativeTo(null);
|
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
|
frame.setVisible(true);
|
final Frame framef = frame;
|
VCIJTextContextMenu.setSpecialCharChooseInferface(new SpecialCharChooseInferface() {
|
public Object execute(JTextComponent textField) {
|
SpecialCharChooseDialog dialog = new SpecialCharChooseDialog(framef, true, textField);
|
dialog.setVisible(true);
|
return null;
|
}
|
});
|
}
|
});
|
}
|
|
protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
|
VCIJButton button = new VCIJButton(name);
|
gridbag.setConstraints(button, c);
|
add(button);
|
}
|
|
|
private VCIJTextArea createVCIJTextArea(){
|
VCIJTextArea area = new VCIJTextArea();
|
area.setWrapStyleWord(true);
|
area.setLineWrap(false);
|
int width = getWidth();
|
int height = 20;
|
Dimension size = new Dimension(width, height);
|
area.setPreferredSize(size);
|
area.setMaximumSize(size);
|
area.setMinimumSize(size);
|
area.setSize(size);
|
return area;
|
}
|
}
|