ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package com.vci.client.omd.enumManager.ui;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
 
import com.vci.mw.InvocationUtility;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.client.omd.enumManager.toOutside.InterEM;
import com.vci.client.omd.enumManager.toOutside.InterEMManager;
import com.vci.client.omd.enumManager.wrapper.EnumChildWrapper;
import com.vci.client.ui.table.CheckBoxEditor;
import com.vci.client.ui.table.CheckBoxRenderer;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.etm.EnumChild;
import com.vci.corba.omd.etm.EnumItem;
 
public class EnumItemPanel extends JPanel{
 
    /**
     * 
     */
    private static final long serialVersionUID = -6093961120146139979L;
 
    private static EnumItemPanel enumItemPanel = null;
    private JPanel northPanel, centerPanel, southPanel;
    private JTextField tfName, tfLabel, tfLength;
    private JComboBox cbType;
    private JPanel cen_centerPanel, cen_southPanel;
    private JXTable emChildTable;
    private EmItemTableModel tableModel;
    private final int TABLE_HEADER_HEIGHT = 25;
    private final int ROW_HEIGHT = 30;
    private JButton btnCreate, btnModify, btnDelete;
    private JButton btnOK, btnCancel;
    public ArrayList<EnumChild> emChildList = new ArrayList<EnumChild>();
    public final String ENUMITEM = "enumItem";
    private EnumItem emItem = null;
    //0:显示; 1: 创建; 2: 修改
    //保存当前状态״̬
    private int flag;
    
    private EnumItemPanel(){
        initUI();
        initTableData();
        addListener();
    }
    
    public static EnumItemPanel getInstance(){
        if(enumItemPanel == null){
            enumItemPanel = new EnumItemPanel();
        }
        
        return enumItemPanel;
    }
    
    private void initUI(){
        this.setBackground(Color.WHITE);
        this.setLayout(new BorderLayout(0, 0));
        this.setBorder(BorderFactory.createTitledBorder("枚举信息"));
        
        //northPanel:名称, 标签, 返回类型
        northPanel = new JPanel();
        this.add(northPanel, BorderLayout.NORTH);
        
        northPanel.setLayout(new GridBagLayout());
        
        int gridy = 0;
        northPanel.add(new JLabel("名称"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3));
        tfName = new JTextField(30);
        northPanel.add(tfName, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3));
 
        gridy++;
        northPanel.add(new JLabel("标签"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3));
        tfLabel = new JTextField(30);
        northPanel.add(tfLabel, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3));
 
        gridy++;
        northPanel.add(new JLabel("类型"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3));
        cbType = new JComboBox();
        cbType.addItem("Integer");
        cbType.addItem("String");
        northPanel.add(cbType, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3));
 
        gridy++;
        northPanel.add(new JLabel("长度"), getGBC(0, gridy, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, 3));
        tfLength = new JTextField(30);
        northPanel.add(tfLength, getGBC(1, gridy, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 3));
 
//        
//        GridBagConstraints g = new GridBagConstraints();
//        /*g.anchor = GridBagConstraints.EAST;*/
//        g.anchor = GridBagConstraints.WEST;    //add by caill 2016.1.14 将第一列控件左对齐
//        g.insets = new Insets(10, 5, 0, 5);
//        g.gridx = 0;
//        g.gridy = 0;
//        northPanel.add(new JLabel("名称"), g);
//        
//        tfName = new JTextField(30);    //add by caill 2016.1.14将文本框长度增加
//        GridBagConstraints g1 = new GridBagConstraints();
//        g1.anchor = GridBagConstraints.WEST;
//        g1.insets = new Insets(10, 5, 0, 5);
//        g1.gridx = 1;
//        g1.gridy = 0;
//        g1.weighty = 1.0;
//        //g1.fill = GridBagConstraints.HORIZONTAL;
//        
//        g1.gridwidth = GridBagConstraints.REMAINDER;
//        northPanel.add(tfName, g1);
//        g.gridy = 1;
//        northPanel.add(new JLabel("标签"), g);
//        tfLabel = new JTextField(30);
//        g1.gridy = 1;
//        northPanel.add(tfLabel, g1);
//        g.gridy = 2;
//        northPanel.add(new JLabel("类型"), g);
//        cbType = new JComboBox();
//        cbType.addItem("Integer");
//        cbType.addItem("String");
//        g1.gridy = 2;
//        northPanel.add(cbType, g1);
//        g.gridy = 3;
//        northPanel.add(new JLabel("长度"), g);
//        tfLength = new JTextField(30);
//        g1.gridy = 3;
//        northPanel.add(tfLength, g1);
        
        //centerPanel:枚举项列表 ,增加枚举项,修改枚举项,删除枚举项按钮
        centerPanel = new JPanel();
        this.add(centerPanel, BorderLayout.CENTER);
        centerPanel.setLayout(new BorderLayout());
        //cen_centerPanel:枚举项列表
        cen_centerPanel = new JPanel();
        centerPanel.setBorder(BorderFactory.createTitledBorder("枚举项"));
        //cen_southPanel:增加枚举项,修改枚举项,删除枚举项按钮
        cen_southPanel = new JPanel();
        centerPanel.add(cen_centerPanel, BorderLayout.CENTER);
        centerPanel.add(cen_southPanel, BorderLayout.SOUTH);
        
        tableModel = new EmItemTableModel();
        emChildTable = new JXTable(tableModel);
        TableColumn column = emChildTable.getColumn(0);
        column.setCellEditor(new CheckBoxEditor(new JCheckBox()));
        column.setCellRenderer(new CheckBoxRenderer());
        column.setMinWidth(30);
        column.setMaxWidth(30);
        column.setPreferredWidth(30);
        
        emChildTable.setRowHeight(ROW_HEIGHT);
        emChildTable.setHorizontalScrollEnabled(true);
        emChildTable.setHighlighters(HighlighterFactory.createAlternateStriping());
        
        //排序后,引起其他页面的数据不显示
        emChildTable.setSortable(false);
        //设置表头高度
        JTableHeader tableHeader = emChildTable.getTableHeader();
        Dimension size = tableHeader.getPreferredSize();
        size.height = TABLE_HEADER_HEIGHT;
        tableHeader.setPreferredSize(size);
        
        cen_centerPanel.setBorder(new EmptyBorder(5, 5, 0, 5));
        cen_centerPanel.setAutoscrolls(true);
        cen_centerPanel.setLayout(new BorderLayout(5, 5));
        cen_centerPanel.add(new JScrollPane(emChildTable), BorderLayout.CENTER);
        
        btnCreate = new JButton("增加");
        btnModify = new JButton("修改");
        btnDelete = new JButton("删除");
        cen_southPanel.add(btnCreate);
        cen_southPanel.add(btnModify);
        cen_southPanel.add(btnDelete);
        
        //southPanel: 确定,取消按钮
        southPanel = new JPanel();
        btnOK = new JButton("确定");
        btnCancel = new JButton("取消");
        southPanel.add(btnOK);
        southPanel.add(btnCancel);
        this.add(southPanel, BorderLayout.SOUTH);
    }
    
    private GridBagConstraints getGBC(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, int padxy) {
        return new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, new Insets(padxy, padxy, padxy, padxy), padxy, padxy);
    }
 
    private void addListener(){
        btnCreate.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!checkLength()){
                    return;
                }
                int length = Integer.valueOf(tfLength.getText());
                EmItemDialog emItemDialog = new EmItemDialog(null, (String)cbType.getSelectedItem(), length);
                emItemDialog.setVisible(true);
            }
        });
        
        btnModify.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = emChildTable.getSelectedRow();
                if(row >= 0){
                    EnumChild emItem = ((EnumChildWrapper)emChildTable.getValueAt(row, 1)).emChild;
                    //TODO 用事件机制,对该emItem进行使用情况检查
                    if(!checkLength()){
                        return;
                    }
                    int length = Integer.valueOf(tfLength.getText());
                    EmItemDialog emItemDialog = new EmItemDialog(emItem, (String)cbType.getSelectedItem(), length);
                    emItemDialog.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "请选中数据", "无数据", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        
        btnDelete.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(getInstance(), "是否确认删除选择的枚举项", "确认框", JOptionPane.YES_NO_OPTION)){
                    return;
                }
                deleteEmItems();
                initTableData();
            }
        });
        
        btnOK.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                //创建枚举
                if(flag == 1){
                    if(!checkName()){
                        return;
                    }
                    emItem = new EnumItem();
                    emItem.name = tfName.getText();
                    emItem.label = tfLabel.getText();
                    emItem.type = (String)cbType.getSelectedItem();
                    if(tfLength.getText().equals("")){
                        emItem.length = 50;
                    }else{
                        emItem.length = Integer.valueOf(tfLength.getText());
                    }
                    if(!checkEMChildValue(emItem.type, emItem.length)){
                        return;
                    }
                    emItem.children = emChildList.toArray(new EnumChild[0]);
                    String userName = InvocationUtility.getInvocation().userName;
                    emItem.creator = userName;
                    emItem.modifier = userName;
                    try {
                        boolean addSuccess = ServiceProvider.getOMDService().getEnumService().addEmItem(emItem);
                        if(!addSuccess){
                            JOptionPane.showMessageDialog(null, "添加枚举失败", "添加枚举失败", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        JOptionPane.showMessageDialog(null, "添加枚举成功", "添加枚举成功", JOptionPane.INFORMATION_MESSAGE);
                        EnumManagerPanel.getInstance().initTableData();
                        EnumItem selected = EnumManagerPanel.getInstance().getSelectedEnum();
                        updateDataAndUI(selected, 0);
                        EnumManagerPanel.getInstance().setFlag(0);
                    } catch (VCIError e1) {
                        e1.printStackTrace();
                    }
                //修改枚举
                }else if(flag == 2){
                    if(emItem == null){
                        JOptionPane.showMessageDialog(null, "请选择要修改的枚举", "选择枚举", JOptionPane.WARNING_MESSAGE);
                    }
                    emItem.label = tfLabel.getText();
                    emItem.type = (String)cbType.getSelectedItem();
                    if(tfLength.getText().equals("")){
                        emItem.length = 0;
                    }else{
                        emItem.length = Integer.valueOf(tfLength.getText());
                    }
                    if(!checkEMChildValue(emItem.type, emItem.length)){
                        return;
                    }
                    emItem.children = emChildList.toArray(new EnumChild[0]);
                    emItem.modifier = InvocationUtility.getInvocation().userName;
                    try {
                        boolean mdSuccess = ServiceProvider.getOMDService().getEnumService().modifyEmItem(emItem);
                        if(!mdSuccess){
                            JOptionPane.showMessageDialog(null, "修改枚举失败", "修改枚举失败", JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        //更改使用枚举的属性值域
                        ArrayList<InterEM> interEMList = InterEMManager.getInstance().getInterEMList();
                        for(InterEM inter : interEMList){
                            inter.updateAtt(emItem);
                        }
                        JOptionPane.showMessageDialog(null, "修改枚举成功", "修改枚举成功", JOptionPane.INFORMATION_MESSAGE);
                        EnumManagerPanel.getInstance().initTableData();
                        EnumItem selected = EnumManagerPanel.getInstance().getSelectedEnum();
                        updateDataAndUI(selected, 0);
                        EnumManagerPanel.getInstance().setFlag(0);
                    } catch (VCIError e1) {
                        e1.printStackTrace();
                    }
                    
                }
                
            }
        });
        
        btnCancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(flag == 1){
                    updateDataAndUI(null, 0);
                }else if(flag == 2){
                    updateDataAndUI(emItem, 0);
                }
            }
        });
    }
    
    
    public void addCancelActionListener(ActionListener listener) {
        btnCancel.addActionListener(listener);
    }
    
    public void initTableData(){
        if(emChildList == null){
            return;
        }
        
        tableModel.setRowCount(emChildList.size());
        //重新设置table的Cell的可编辑性
        tableModel.setInitFlag(false);
        EnumChild emChild;
        for(int i = 0; i < emChildList.size(); i++){
            emChild = emChildList.get(i);
            emChildTable.setValueAt(new JCheckBox(), i, 0);
            emChildTable.setValueAt(new EnumChildWrapper(emChild), i, 1);
            emChildTable.setValueAt(emChild.value, i, 2);
            emChildTable.setValueAt(emChild.description, i, 3);
        }
        //重新设置table的Cell的可编辑性
        tableModel.setInitFlag(true);
        emChildTable.updateUI();
    }
    
    /**
     * @param emItem : 用来显示和修改属性项
     * @param flag : 当前状态(创建,修改,删除)
     */
    public void updateDataAndUI(EnumItem emItem, int flag){
        this.flag = flag;
        if(emItem != null){
            tfName.setText(emItem.name);
            tfLabel.setText(emItem.label);
            cbType.setSelectedItem(emItem.type);
            tfLength.setText(String.valueOf(emItem.length));
            emChildList = new ArrayList<EnumChild>();
            for(int i = 0; i < emItem.children.length; i++){
                emChildList.add(emItem.children[i]);
            }
            initTableData();
            this.emItem = emItem;
        }else{
            tfName.setText("");
            tfLabel.setText("");
            tfLength.setText("50");
            emChildList = new ArrayList<EnumChild>();
            initTableData();
        }
        
        if(flag == 0){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(false);
                tfLabel.setEditable(false);
                cbType.setEnabled(false);
                tfLength.setEditable(false);
            }
            
            btnCreate.setVisible(false);
            btnModify.setVisible(false);
            btnDelete.setVisible(false);
            btnOK.setVisible(false);
            btnCancel.setVisible(false);
        }
        
        
        if(flag == 1){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(true);
                tfLabel.setEditable(true);
                cbType.setEnabled(true);
                tfLength.setEditable(true);
            }
            btnCreate.setVisible(true);
            btnModify.setVisible(true);
            btnDelete.setVisible(true);
            btnOK.setVisible(true);
            btnCancel.setVisible(true);
        }
        
        if(flag == 2){
            //名称,标签,描述,属性类型  组件群
            {
                tfName.setEditable(false);
                tfLabel.setEditable(true);
                cbType.setEnabled(true);
                tfLength.setEditable(true);
            }
            btnCreate.setVisible(true);
            btnModify.setVisible(true);
            btnDelete.setVisible(true);
            btnOK.setVisible(true);
            btnCancel.setVisible(true);
        }
    }
    
    private boolean checkName(){
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(this, "请输入枚举名", "注意", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        String name = tfName.getText();
        if(!name.matches("[a-z A-Z]*")){
            JOptionPane.showMessageDialog(this, "枚举名只能为英文字母", "注意", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        
        try {
            if(ServiceProvider.getOMDService().getEnumService().checkRowIsExists(tfName.getText())){
                JOptionPane.showMessageDialog(this, "枚举名已经存在", "注意", JOptionPane.WARNING_MESSAGE);
                return false;
            }
        } catch (VCIError e) {
            e.printStackTrace();
        } catch (HeadlessException e) {
            e.printStackTrace();
        }
        return true;
    }
    
    /**
     * 删除checkBox选中的属性项,
     * 没有选中项时,提示
     */
    private void deleteEmItems(){
        boolean hasSeletedChildFlag = false;
        for(int i = 0; i < emChildTable.getRowCount(); i++){
            if(((JCheckBox)emChildTable.getValueAt(i, 0)).isSelected()){
                //TODO 用事件机制,对该emItem进行使用情况检查
                emChildList.remove((((EnumChildWrapper)emChildTable.getValueAt(i, 1)).emChild));
                hasSeletedChildFlag = true;
            }
        }
        
        if(!hasSeletedChildFlag){
            JOptionPane.showMessageDialog(this, "请选中要删除的枚举项", "未选枚举项", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /**
     * 检查枚举项的值类型
     */
    private boolean checkEMChildValue(String type, long length){
        String value;
        for(int i = 0; i < emChildList.size(); i++){
            value = emChildList.get(i).value;
            if(value.equals("")){
                JOptionPane.showMessageDialog(this, "枚举项值不能空", "枚举项值不能空", JOptionPane.WARNING_MESSAGE);
                return false;
            }
            if(value.length() > length){
                JOptionPane.showMessageDialog(this, "超过最大长度", "超过最大长度", JOptionPane.ERROR_MESSAGE);
                return false;
            }
            if(type.equals("Integer")){
                if(!value.matches("-?[1-9][0-9]*")){
                    JOptionPane.showMessageDialog(this, "枚举项值只能为Integer", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE);
                    return false;
                }
            }else if(type.equals("String")){
//                if(!value.matches("([a-z A-Z]*[0-9]*)*") && !value.matches("-?[1-9][0-9]*")){
//                    JOptionPane.showMessageDialog(this, "枚举项值只能为String", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE);
//                    return false;
//                }
            }
        }
        
        return true;
    }
    
    /**
     * 检查长度
     * @return
     */
    private boolean checkLength(){
        if(tfLength.getText().equals("")){
            JOptionPane.showMessageDialog(this, "请输入最大长度", "请输入最大长度", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(!tfLength.getText().matches("[1-9][0-9]*")){
            JOptionPane.showMessageDialog(this, "长度只能为数字", "长度只能为数字", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        return true;
    }
}