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
package com.vci.client.omd.enumManager.ui;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
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.corba.omd.etm.EnumChild;
 
public class EmItemDialog extends JDialog{
 
    /**
     * 
     */
    private static final long serialVersionUID = -1924696083112318808L;
    private EnumChild emChild;
    private JPanel centerPanel, southPanel;
    private JTextField tfName, tfValue;
    private JTextArea taDescrip;
    private JButton btnOK, btnCancel;
    private String type;
    private int length;
    
    public EmItemDialog(EnumChild emChild, String type, int length){
        this.emChild = emChild;
        this.type = type;
        this.length = length;
        initUI();
        initData();
        addListener();
    }
    
    private void initUI(){
        this.setTitle("枚举项");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width/4, screenSize.height/4);
        this.setSize(600, 400);//add by caill 2016.1.14调整弹出页面大小
        this.setModal(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        
        //centerPanel:枚举项名, 枚举项值, 描述
        centerPanel = new JPanel();
        //southPanel:确定, 取消
        southPanel = new JPanel();
        
        setLayout(new BorderLayout());
        add(centerPanel, BorderLayout.CENTER);
        add(southPanel, BorderLayout.SOUTH);
        
        centerPanel.setLayout(new GridBagLayout());
        GridBagConstraints g = new GridBagConstraints();
        g.gridx = 0;
        g.gridy = 0;
        g.anchor = GridBagConstraints.WEST;//add by caill 2015.11.26将第一列控件设置为左对齐
        g.insets = new Insets(20, 15, 10, 15);//add by caill 2016.1.14调整控件间距
        centerPanel.add(new JLabel("枚举项名:"), g);
        GridBagConstraints g1 = new GridBagConstraints();
        g1.gridx = 1;
        g1.gridy = 0;
        g1.gridwidth = GridBagConstraints.REMAINDER;
        g1.anchor = GridBagConstraints.WEST;//add by caill 2015.11.26将第二列控件设置为左对齐
        g1.insets = new Insets(20, 15, 10, 15);
        tfName = new JTextField(30);
        centerPanel.add(tfName, g1);
        g.gridy = 1;
        centerPanel.add(new JLabel("枚举项值:"), g);
        g1.gridy = 1;
        tfValue = new JTextField(30);
        centerPanel.add(tfValue, g1);
        g.gridy = 2;
        centerPanel.add(new JLabel("描述:"), g);
        g1.gridy = 2;
        taDescrip = new JTextArea(3,30);
        taDescrip.setPreferredSize(new Dimension(110, 40));
        taDescrip.setLineWrap(true);
        centerPanel.add(new JScrollPane(taDescrip), g1);
        
        btnOK = new JButton("确定");
        btnCancel = new JButton("取消");
        southPanel.add(btnOK);
        southPanel.add(btnCancel);
    }
    
    private void initData(){
        if(emChild == null){
            return;
        }
        
        tfName.setText(emChild.name);
        tfValue.setText(emChild.value);
        taDescrip.setText(emChild.description);
    }
    private void addListener(){
        btnOK.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
 
                if(!checkName()){
                    return;
                }
                if(!checkValue()){
                    return;
                }
                if(emChild == null){
                    emChild = new EnumChild();
                    emChild.name = tfName.getText();
                    emChild.value = tfValue.getText();
                    emChild.description = taDescrip.getText();
                    EnumItemPanel.getInstance().emChildList.add(emChild);
                }else{
                    for(int i = 0; i < EnumItemPanel.getInstance().emChildList.size(); i++){
                        if(EnumItemPanel.getInstance().emChildList.get(i).name.equals(emChild.name)){
                            EnumItemPanel.getInstance().emChildList.get(i).name = tfName.getText();
                            EnumItemPanel.getInstance().emChildList.get(i).value = tfValue.getText();
                            EnumItemPanel.getInstance().emChildList.get(i).description = taDescrip.getText();
                        }
                    }
                }
                
                EnumItemPanel.getInstance().initTableData();
                dispose();
            }
        });
        
        btnCancel.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }
    
    private boolean checkName(){
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(this, "请输入枚举项名", "请输入枚举项名", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        String name = tfName.getText().trim();
        
        for(int i = 0; i < EnumItemPanel.getInstance().emChildList.size(); i++){
            EnumChild emChild = EnumItemPanel.getInstance().emChildList.get(i);
            
            if(this.emChild == null){
                if(emChild.name.equals(name)){
                    JOptionPane.showMessageDialog(this, "该枚举项已经存在", "该枚举项已经存在", JOptionPane.WARNING_MESSAGE);
                    return false;
                }
            }else{
                if(!emChild.name.equals(this.emChild.name) && emChild.name.equals(name)){
                    JOptionPane.showMessageDialog(this, "该枚举项已经存在", "该枚举项已经存在", JOptionPane.WARNING_MESSAGE);
                    return false;
                }
            }
            
        }
        return true;
    }
    
    private boolean checkValue(){
        if(tfValue.getText().equals("")){
            JOptionPane.showMessageDialog(this, "请输入枚举项值", "请输入枚举项值", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        String text = tfValue.getText();
        if(text.length() > length){
            JOptionPane.showMessageDialog(this, "超过最大长度", "超过最大长度", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(type.equals("Integer")){
            if(!text.matches("-?[1-9][0-9]*")){
                JOptionPane.showMessageDialog(this, "枚举项值只能为Integer", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE);
                return false;
            }
        }else if(type.equals("String")){
//            if(!text.matches("([a-z A-Z]*[0-9]*)*") && !text.matches("-?[1-9][0-9]*")){
//                JOptionPane.showMessageDialog(this, "枚举项值只能为String", "枚举项值类型错误", JOptionPane.WARNING_MESSAGE);
//                return false;
//            }
        }
        return true;
    }
}