wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package com.vci.client.omd.attribpool.ui;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
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.JTextArea;
import javax.swing.JTextField;
 
import com.vci.client.common.datatype.VTDouble;
import com.vci.corba.omd.atm.AttribItem;
 
public class VTDoublePanel extends VTDataTypePanel{
 
    /**
     * 
     */
    private static final long serialVersionUID = -2697390825347128628L;
    private JCheckBox cbNull;
//    private JComboBox combDim;
    private JTextField tfAcc, tfLen, tfDefValue;
    private JTextField tfRage;
    private JButton btnLar, btnSma, btnLarE, btnSmaE, btnEqu, btnRage, btnUnEqu;
    private JTextArea taRage;
    private JButton btnRemove;
    private HashMap<String, String> detailInfoMap = null;
    private JPanel rightPanel1, rightPanel2, rightPanel3;
    private JComboBox combRage1, combRage2;
    private JTextField tfRage1, tfRage2;
    private JButton btnRageAdd;
    private boolean rageFlag = false;
    
    public VTDoublePanel(){
        initUI();
        addListener();
    }
    
    private void initUI(){
        
        this.setLayout(new GridBagLayout());
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        
        GridBagConstraints gb = new GridBagConstraints();
        gb.gridx = 0;
        gb.gridy = 0;
        gb.weightx = 1.0;
        gb.weighty = 1.0;
        gb.fill = GridBagConstraints.BOTH;
        
        this.add(leftPanel, gb);
        gb.gridx = 1;
        this.add(rightPanel, gb);
        leftPanel.setPreferredSize(new Dimension(this.getSize().width/2, 
                this.getSize().height));
        rightPanel.setPreferredSize(new Dimension(this.getSize().width/2, 
                this.getSize().height));
        
        //设置左边panel的控件
        leftPanel.setBorder(BorderFactory.createTitledBorder("VTDouble"));
        leftPanel.setLayout(new BorderLayout());        
        JPanel leftPanel2 = new JPanel();
        
        JScrollPane spLP1 = new JScrollPane();
        spLP1.setBorder(null);    
        JScrollPane spLP2 = new JScrollPane();
        spLP2.setBorder(null);
        spLP2.setViewportView(leftPanel2);
        
        leftPanel.add(spLP1, BorderLayout.NORTH);
        leftPanel.add(spLP2, BorderLayout.CENTER);
        
        cbNull = new JCheckBox("允许为空");
        cbNull.setSelected(true);
//        cbFixLen = new JCheckBox("定长");
//        leftPanel1.add(cbFixLen);
        
        leftPanel2.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.insets = new Insets(15, 10, 5, 10);
        g.gridx = 0;
        g.gridy = 0;
        g.anchor = GridBagConstraints.WEST;//add by caill 2016.1.15将“长度”一列左对齐    
        leftPanel2.add(cbNull,g);    //add by caill 2016.1.15将“允许为空”放到“精度”的上面
        g.gridy = 1;
        leftPanel2.add(new JLabel("精度"), g);
        GridBagConstraints g1 = new GridBagConstraints();
        g1.insets = new Insets(15, 10, 5, 10);
        g1.anchor = GridBagConstraints.WEST;    //add by caill 2016.1.15将文本框左对齐
        g1.gridx = 1;
        g1.gridy = 1;
        //g1.weightx = 1.0;
        g1.gridwidth = GridBagConstraints.REMAINDER;
        tfAcc = new JTextField(30);      //add by caill 2016.1.15增加文本框长度
        tfAcc.setText(String.valueOf(VTDouble.accDefault));
        leftPanel2.add(tfAcc, g1);
        g.gridy = 2;
        leftPanel2.add(new JLabel("长度"), g);
        tfLen = new JTextField(30);
        tfLen.setText(String.valueOf(VTDouble.lenDefault));
        g1.gridy = 2;
        leftPanel2.add(tfLen, g1);
        //屏蔽使用量纲---start
//        g.gridy = 2;
//        leftPanel2.add(new JLabel("使用量纲"), g);
//        combDim = new JComboBox();
//        combDim.addItem("kg");
//        combDim.addItem("km");
//        combDim.setPreferredSize(new Dimension(120, 20));
//        g1.gridy = 2;
//        leftPanel2.add(combDim, g1);
        //屏蔽使用量纲---end
        
        g.gridy = 3;
        leftPanel2.add(new JLabel("默认值"), g);
        tfDefValue = new JTextField(30);
        tfDefValue.setText(String.valueOf(VTDouble.defaultValue));
        g1.gridy = 3;
        leftPanel2.add(tfDefValue, g1);
        
        //设置右边panel的控件
        rightPanel.setBorder(BorderFactory.createTitledBorder("值域"));
        rightPanel.setLayout(new GridBagLayout());
        
        rightPanel1 = new JPanel();
        rightPanel2 = new JPanel();
        rightPanel3 = new JPanel();
        
        GridBagConstraints gRight = new GridBagConstraints();
        gRight.gridx = 0;
        gRight.gridy = 0;
        gRight.weightx = 1.0;
        gRight.weighty = 1.0;
        gRight.fill = GridBagConstraints.BOTH;
        
        JScrollPane spRP1 = new JScrollPane();
        spRP1.setBorder(null);
        spRP1.setViewportView(rightPanel1);
        JScrollPane spRP2 = new JScrollPane();
        spRP2.setBorder(null);
        spRP2.setViewportView(rightPanel2);
        JScrollPane spRP3 = new JScrollPane();
        spRP3.setBorder(null);
        spRP3.setViewportView(rightPanel3);
        
        rightPanel.add(spRP1, gRight);
        gRight.gridy = 1;
        rightPanel.add(spRP2, gRight);
        gRight.gridy = 2;
        rightPanel.add(spRP3, gRight);
        
        //值域输入框
        tfRage = new JTextField(30);//add by caill 2016.1.15调整值域输入框大小
        //tfRage.setPreferredSize(new Dimension(200, 30));
        rightPanel1.add(tfRage);
        //当选择btnRage时, 值域输入框切换为区间选择框
        combRage1 = new JComboBox();
        combRage2 = new JComboBox();
        combRage1.setPreferredSize(new Dimension(50, 20));
        combRage2.setPreferredSize(new Dimension(50, 20));
        combRage1.addItem("(");
        combRage1.addItem("[");
        combRage2.addItem(")");
        combRage2.addItem("]");
        tfRage1 = new JTextField(5);
        tfRage2 = new JTextField(5);
        btnRageAdd = new JButton("+");        
        
        btnLar = new JButton(">");
        btnSma = new JButton("<");
        btnLarE = new JButton(">=");
        btnSmaE = new JButton("<=");
        btnEqu = new JButton("=");
        btnUnEqu = new JButton("!=");
        btnRage = new JButton("([])");
        
        rightPanel2.setLayout(new GridBagLayout());
        GridBagConstraints gRP2 = new GridBagConstraints();
        gRP2.gridx = 0;
        gRP2.gridy = 0;
        rightPanel2.add(btnLar, gRP2);
        gRP2.gridx = 1;
        rightPanel2.add(btnSma, gRP2);
        gRP2.gridx = 2;
        rightPanel2.add(btnLarE, gRP2);
        gRP2.gridx = 3;
        gRP2.gridwidth = 0;
        rightPanel2.add(btnSmaE, gRP2);
        gRP2.gridx = 0;
        gRP2.gridy = 1;
        gRP2.gridwidth = 1;
        rightPanel2.add(btnEqu, gRP2);
        gRP2.gridx = 1;
        rightPanel2.add(btnUnEqu, gRP2);
        gRP2.gridx = 2;
        gRP2.gridwidth = 0;
        rightPanel2.add(btnRage, gRP2);
        
        rightPanel3.setLayout(new GridBagLayout());
        taRage = new JTextArea(4, 20);
        taRage.setLineWrap(true);
        GridBagConstraints gr3 = new GridBagConstraints();
        gr3.insets = new Insets(5, 5, 5, 5);    //add by caill 2016.1.16调整文本域和“-”之间的距离
        gr3.gridx = 0;
        gr3.gridy = 0;
        gr3.weightx = 0;
        rightPanel3.add(new JScrollPane(taRage), gr3);
        gr3.anchor = GridBagConstraints.NORTHWEST;
        gr3.gridx = 1;
        gr3.gridwidth = GridBagConstraints.REMAINDER;
        gr3.insets = new Insets(5, 5, 5, 38);//add by caill 2016.1.16调整文本域和“-”的位置
        btnRemove = new JButton("-");
        rightPanel3.add(btnRemove, gr3);
    }
    
    public void addListener(){
        btnLar.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                String rageStr = tfRage.getText();
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = ">" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
                
            }
        });
        
        btnSma.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                String rageStr = tfRage.getText();
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = "<" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
                
            }
        });
        
        btnEqu.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                String rageStr = tfRage.getText();
                if(rageStr.equals("")){
                    return;
                }
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = "=" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
            }
        });
        
        btnLarE.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                String rageStr = tfRage.getText();
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = ">=" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
                
            }
        });
        
        btnSmaE.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                String rageStr = tfRage.getText();
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = "<=" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
                
            }
        });
        
        btnUnEqu.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    rageFlag = false;
                    rightPanel1.removeAll();
                    rightPanel1.add(tfRage);
                    rightPanel1.updateUI();
                    return;
                }
                
                String rageStr = tfRage.getText();
                double rageDou;
                if(rageStr.equals("")){
                    return;
                }
                try{
                    rageDou = Double.parseDouble(rageStr);
                }catch (NumberFormatException e1){
                    JOptionPane.showMessageDialog(null, "请输入数字");
                    tfRage.setText("");
                    return;
                }
                
                String str = "!=" + rageDou;
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
                
            }
        });
        
        //将输入框切换为区间选择框
        btnRage.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                if(rageFlag){
                    return;
                }else{
                    rageFlag = true;
                    rightPanel1.removeAll();
                    rightPanel1.add(combRage1);
                    rightPanel1.add(tfRage1);
                    rightPanel1.add(new JLabel(","));
                    rightPanel1.add(tfRage2);
                    rightPanel1.add(combRage2);
                    rightPanel1.add(btnRageAdd);
                    rightPanel1.updateUI();
                }
                
            }
        });
        
        btnRageAdd.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                String rage1 = tfRage1.getText();
                if(rage1.equals("") || !rage1.matches("[-+]?[0-9]*.*[0-9]*")){
                    JOptionPane.showMessageDialog(null, "请输入数字(左)");
                    tfRage1.setText("");
                    return;
                }
                
                String rage2 = tfRage2.getText();
                if(rage2.equals("") || !rage2.matches("[-+]?[0-9]*.*[0-9]*")){
                    JOptionPane.showMessageDialog(null, "请输入数字(右)");
                    tfRage2.setText("");
                    return;
                }
                if(Double.valueOf(rage1) >= Double.valueOf(rage2)){
                    JOptionPane.showMessageDialog(null, "无效区间,请重新输入");
                    return;
                }
                String str = combRage1.getSelectedItem() + rage1 + "," + rage2 + combRage2.getSelectedItem();
                if(taRage.getText().equals("")){
                    taRage.setText(str);
                }else if(taRage.getText().contains(str)){
                    JOptionPane.showMessageDialog(null, "该值域规则已经存在");
                }else{
                    taRage.setText(taRage.getText() + "\n" + str);
                }
            }
        });
        btnRemove.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                int lastN = taRage.getText().lastIndexOf("\n");
                if(lastN > 0){
                    taRage.setText(taRage.getText().substring(0, lastN));
                }else{
                    taRage.setText("");
                }
                
            }
        });
    }
    
    /**
     * 获取VTDataType的详细信息
     * @return
     */
    public HashMap<String, String> getDetailInfo(){
        detailInfoMap = new HashMap<String, String>();
        String otherValue = "";
        
        detailInfoMap.put(VTDataTypePanel.DEFVALUE, tfDefValue.getText());
        detailInfoMap.put(VTDataTypePanel.RAGE, taRage.getText().replace("\n", ";"));
        
        if(cbNull.isSelected()){
            otherValue += VTDataTypePanel.ALLOWNULL + " = yes;";
        }else{
            otherValue += VTDataTypePanel.ALLOWNULL + " = no;";
        }
        
//        if(cbFixLen.isSelected()){
//            otherValue += VTDataTypePanel.ISFIXLEN + " = yes;";
//        }else{
//            otherValue += VTDataTypePanel.ISFIXLEN + " = no;";
//        }
        otherValue += VTDataTypePanel.ACCURACY + " = " + 
                tfAcc.getText() + ";";
        otherValue += VTDataTypePanel.LENGTH + " = " + 
                tfLen.getText() + ";";
//        otherValue += VTDataTypePanel.DIMENSION + " = " + 
//                (String)combDim.getSelectedItem() + ";";
        otherValue = otherValue.substring(0, otherValue.lastIndexOf(";"));
        detailInfoMap.put(VTDataTypePanel.OTHER, otherValue);
        return detailInfoMap;
    }
    
    /**
     * 根据当前状态(查看,创建,修改), 设置相关组件的显示情况
     * @param flag
     */
    public void updataUIStatus(AttribItem abItem, int flag){
        if(abItem != null){
            tfDefValue.setText(abItem.defValue);
            taRage.setText(abItem.rage.replace(";", "\n"));
            String other = abItem.other;
            String accuracy = getOtherValueByType(other, "accuracy");
            tfAcc.setText(accuracy);
            String length = getOtherValueByType(other, "length");
            tfLen.setText(length);
//            String dimension = getOtherValueByType(other, "dimension");
//            combDim.setSelectedItem(dimension);
        }
        if(flag == 0){
            cbNull.setEnabled(false);
//            cbFixLen.setEnabled(false);
//            combDim.setEnabled(false);
            tfDefValue.setEditable(false);
            
            
            //值域输入组件群
            {
                tfRage.setVisible(false);
                btnLar.setVisible(false);
                btnSma.setVisible(false);
                btnLarE.setVisible(false);
                btnSmaE.setVisible(false);
                btnEqu.setVisible(false);
                btnRage.setVisible(false);
                btnUnEqu.setVisible(false);
            }
            
            //值域显示组件群
            {
                taRage.setEditable(false);
                btnRemove.setVisible(false);
            }
        }
        
        if(flag == 1 || flag == 2){
            cbNull.setEnabled(true);
//            cbFixLen.setEnabled(true);
//            combDim.setEnabled(true);
            tfDefValue.setEditable(true);
            
            
            //值域输入组件群
            {
                tfRage.setVisible(true);
                btnLar.setVisible(true);
                btnSma.setVisible(true);
                btnLarE.setVisible(true);
                btnSmaE.setVisible(true);
                btnEqu.setVisible(true);
                btnRage.setVisible(true);
                btnUnEqu.setVisible(true);
            }
            
            //值域显示组件群
            {
                taRage.setEditable(true);
                btnRemove.setVisible(true);
            }
        }
        
    }
}