田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
 * @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;
    }
}