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
package com.iailab.netsdk.demo.frame.Gate;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;
 
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
 
import com.sun.jna.Memory;
 
import com.iailab.netsdk.common.BorderEx;
import com.iailab.netsdk.common.DateChooserJButton;
import com.iailab.netsdk.common.PaintPanel;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.demo.module.GateModule;
import com.iailab.netsdk.lib.ToolKits;
 
public class ModifyCardDialog extends JDialog{
 
    private static final long serialVersionUID = 1L;
 
    private Memory memory = null;
    
    private Vector<String> vector = null;
    
    private String picPath = "";
    
    public ModifyCardDialog(Vector<String> v){
        setTitle(Res.string().getModify() + Res.string().getCardInfo());
        setLayout(new BorderLayout());
        setModal(true);    
        pack();
        setSize(520, 390);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
        
        CardInfoPanel cardInfoPanel = new CardInfoPanel();
        ImagePanel imagePanel = new ImagePanel();
        
        add(cardInfoPanel, BorderLayout.CENTER);
        add(imagePanel, BorderLayout.EAST);
        
        this.vector = v;
        showCardInfo();
        
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                clear();
                dispose();
            }
        });
    }
    
    /**
     * 卡信息
     */
    private class CardInfoPanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
        
        public CardInfoPanel() {
            BorderEx.set(this, Res.string().getCardInfo(), 4);
            
            JLabel cardNoLabel = new JLabel(Res.string().getCardNo() + ":", JLabel.CENTER);
            JLabel userIdLabel = new JLabel(Res.string().getUserId() + ":", JLabel.CENTER);
            JLabel cardNameLabel = new JLabel(Res.string().getCardName() + ":", JLabel.CENTER);
            JLabel cardPasswdLabel = new JLabel(Res.string().getCardPassword() + ":", JLabel.CENTER);
            JLabel cardStatusLabel = new JLabel(Res.string().getCardStatus() + ":", JLabel.CENTER);
            JLabel cardTypeLabel = new JLabel(Res.string().getCardType() + ":", JLabel.CENTER);
            JLabel useTimesLabel = new JLabel(Res.string().getUseTimes() + ":", JLabel.CENTER);
            JLabel validPeriodLabel = new JLabel(Res.string().getValidPeriod() + ":", JLabel.CENTER);
            
            Dimension dimension = new Dimension();
            dimension.width = 85;
            dimension.height = 20;
            cardNoLabel.setPreferredSize(dimension);
            userIdLabel.setPreferredSize(dimension);
            cardNameLabel.setPreferredSize(dimension);
            cardPasswdLabel.setPreferredSize(dimension);
            cardStatusLabel.setPreferredSize(dimension);
            cardTypeLabel.setPreferredSize(dimension);
            useTimesLabel.setPreferredSize(dimension);
            validPeriodLabel.setPreferredSize(dimension);
            
            cardNoTextField = new JTextField();
            userIdTextField = new JTextField();
            cardNameTextField = new JTextField();
            cardPasswdField = new JPasswordField();        
            cardStatusComboBox = new JComboBox(Res.string().getCardStatusList());
            cardTypeComboBox = new JComboBox(Res.string().getCardTypeList());    
            useTimesTextField = new JTextField();
            firstEnterCheckBox = new JCheckBox(Res.string().getIsFirstEnter());
            enableCheckBox = new JCheckBox(Res.string().getEnable());
            enableCheckBox.setSelected(true);
            enableCheckBox.setVisible(false);
            startTimeBtn = new DateChooserJButton();
            endTimeBtn = new DateChooserJButton();
            
            cardNoTextField.setPreferredSize(new Dimension(145, 20));
            userIdTextField.setPreferredSize(new Dimension(145, 20));
            cardNameTextField.setPreferredSize(new Dimension(145, 20));
            cardPasswdField.setPreferredSize(new Dimension(145, 20));
            useTimesTextField.setPreferredSize(new Dimension(145, 20));
            cardStatusComboBox.setPreferredSize(new Dimension(145, 20));
            cardTypeComboBox.setPreferredSize(new Dimension(145, 20));
            startTimeBtn.setPreferredSize(new Dimension(145, 20));
            endTimeBtn.setPreferredSize(new Dimension(145, 20));
            firstEnterCheckBox.setPreferredSize(new Dimension(170, 20));
            enableCheckBox.setPreferredSize(new Dimension(70, 20));
            
            JLabel nullLabel1 = new JLabel();
            JLabel nullLabel2 = new JLabel();
            JLabel nullLabel3 = new JLabel();
            nullLabel1.setPreferredSize(new Dimension(5, 20));
            nullLabel2.setPreferredSize(new Dimension(30, 20));
            nullLabel3.setPreferredSize(new Dimension(85, 20));
            
            cardNoTextField.setEditable(false);
            userIdTextField.setEditable(false);
            
            modifyBtn = new JButton(Res.string().getModify());
            cancelBtn = new JButton(Res.string().getCancel());
            JLabel nullLabel4 = new JLabel();
            nullLabel4.setPreferredSize(new Dimension(250, 20));
            modifyBtn.setPreferredSize(new Dimension(110, 20));
            cancelBtn.setPreferredSize(new Dimension(110, 20));
            
            add(cardNoLabel);
            add(cardNoTextField);
            add(userIdLabel);
            add(userIdTextField);
            
            add(cardNameLabel);
            add(cardNameTextField);
            add(cardPasswdLabel);
            add(cardPasswdField);
            
            add(cardStatusLabel);
            add(cardStatusComboBox);
            add(cardTypeLabel);
            add(cardTypeComboBox);
            
            add(useTimesLabel);
            add(useTimesTextField);
            add(nullLabel1);
            add(firstEnterCheckBox);
            add(nullLabel2);
            add(enableCheckBox);
            
            add(validPeriodLabel);
            add(startTimeBtn);
            add(nullLabel3);
            add(endTimeBtn);
            
            add(nullLabel4);
            add(modifyBtn);
            add(cancelBtn);
            
            // 修改
            modifyBtn.addActionListener(new ActionListener() {        
                @Override
                public void actionPerformed(ActionEvent arg0) {    
                    try {
                        if (cardNameTextField.getText().getBytes("UTF-8").length > 63) {
                            JOptionPane.showMessageDialog(null, Res.string().getCardNameExceedLength() + "(63)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                        
                        if (new String(cardPasswdField.getPassword()).getBytes("UTF-8").length > 63) {
                            JOptionPane.showMessageDialog(null, Res.string().getCardPasswdExceedLength() + "(63)", Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            return;
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }     
                    
                    int useTimes = 0;
                    if(useTimesTextField.getText().isEmpty()) {
                        useTimes = 0;
                    } else {
                        useTimes = Integer.parseInt(useTimesTextField.getText());
                    }
                    
                    if(GateModule.modifyCard(Integer.parseInt(vector.get(3).toString()), cardNoTextField.getText(), 
                                            userIdTextField.getText(), cardNameTextField.getText(), 
                                            new String(cardPasswdField.getPassword()), 
                                            Res.string().getCardStatusInt(cardStatusComboBox.getSelectedIndex()), 
                                            Res.string().getCardTypeInt(cardTypeComboBox.getSelectedIndex()), 
                                            useTimes, firstEnterCheckBox.isSelected() ? 1:0, 
                                            enableCheckBox.isSelected() ? 1:0, startTimeBtn.getText(), endTimeBtn.getText())) {
                        if(memory == null) {
                            JOptionPane.showMessageDialog(null, Res.string().getSucceedModifyCard(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
                            dispose();
                        } else {
                            if(GateModule.modifyFaceInfo(userIdTextField.getText(), memory)) {
                                JOptionPane.showMessageDialog(null, Res.string().getSucceedModifyCardAndPerson(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
                                dispose();
                            } else {
                                JOptionPane.showMessageDialog(null, Res.string().getSucceedModifyCardButFailedModifyPerson() + " : " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                            }
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, Res.string().getFailedModifyCard() + " : " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    }    
                }
            });
            
            // 取消
            cancelBtn.addActionListener(new ActionListener() {        
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    clear();
                    dispose();                
                }
            });
        }
    }
    
    /**
     * 选择图片
     */
    private class ImagePanel extends JPanel {
 
        private static final long serialVersionUID = 1L;
        
        public ImagePanel() {
            BorderEx.set(this, Res.string().getPersonPicture(), 4);
            Dimension dimension = new Dimension();
            dimension.width = 250;
            setPreferredSize(dimension);
            setLayout(new BorderLayout());
            
            addImagePanel = new PaintPanel();   // 添加的人员信息图片显示
            selectImageBtn = new JButton(Res.string().getSelectPicture());
            add(addImagePanel, BorderLayout.CENTER);
            add(selectImageBtn, BorderLayout.SOUTH);
            
            // 选择图片,获取图片的信息
            selectImageBtn.addActionListener(new ActionListener() {                
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // 选择图片,获取图片路径,并在界面显示
                    picPath = ToolKits.openPictureFile(addImagePanel);
                            
                    if(!picPath.isEmpty()) {
                        try {
                            memory = ToolKits.readPictureFile(picPath);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }        
                }
            });
        }
    }
    
    /**
     * 界面显示要修改的卡信息
     */
    private void showCardInfo() {
        // 卡号
        cardNoTextField.setText(vector.get(1).toString());
        
        // 卡名
        cardNameTextField.setText(vector.get(2).toString());
        
        // 用户ID
        userIdTextField.setText(vector.get(4).toString());
        
        // 卡密码
        cardPasswdField.setText(vector.get(5).toString());
        
        // 卡状态
        cardStatusComboBox.setSelectedIndex(Res.string().getCardStatusChomBoxIndex(vector.get(6).toString()));
        
        // 卡类型
        cardTypeComboBox.setSelectedIndex(Res.string().getCardTypeChomBoxIndex(vector.get(7).toString()));
        
        // 使用次数
        useTimesTextField.setText(vector.get(8).toString());
        
        // 是否首卡
        if(vector.get(9).toString().equals(Res.string().getFirstEnter())) {
            firstEnterCheckBox.setSelected(true);
        } else {
            firstEnterCheckBox.setSelected(false);
        }
        
        // 是否有效
        if(vector.get(10).toString().equals(Res.string().getValid())) {
            enableCheckBox.setSelected(true);
        } else {
            enableCheckBox.setSelected(false);
        }
        
        // 有效开始时间
        startTimeBtn.setText(vector.get(11).toString());
        
        // 有效结束时间
        endTimeBtn.setText(vector.get(12).toString());
    }
    
    private void clear() {
        memory = null;
        vector = null;
        picPath = "";
    }
    
    private PaintPanel addImagePanel;
    private JButton selectImageBtn;
    
    private JTextField cardNoTextField;
    private JTextField userIdTextField;
    private JTextField cardNameTextField;
    private JPasswordField cardPasswdField;        
    private JComboBox cardStatusComboBox;
    private JComboBox cardTypeComboBox;    
    private JTextField useTimesTextField;
    private JCheckBox firstEnterCheckBox;
    private JCheckBox enableCheckBox;
    private DateChooserJButton startTimeBtn;
    private DateChooserJButton endTimeBtn;
    
    private JButton modifyBtn;
    private JButton cancelBtn;
}