dongyukun
9 天以前 95066d1bdaf67f414f01d1686e7ea9c0ebc07e26
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
package com.iailab.netsdk.demo.frame.Attendance;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JTextField;
 
import com.iailab.netsdk.common.BorderEx;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.demo.module.AttendanceModule;
import com.iailab.netsdk.demo.module.AttendanceModule.OPERATE_TYPE;
import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
import com.iailab.netsdk.lib.NetSDKLib;
 
/**
 * 考勤机操作对话框
 */
public class AttendanceOperateShareDialog extends JDialog{
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private OPERATE_TYPE emType = OPERATE_TYPE.UNKNOWN;                    // 操作类型
    private boolean bSuccess = false;                                    // 接口调用结果
    
    public AttendanceOperateShareDialog(OPERATE_TYPE emType, UserData userData) { 
        this(emType, userData, "");
    }
    
    public AttendanceOperateShareDialog(OPERATE_TYPE emType, String fingerPrintId) { 
        this(emType, null, fingerPrintId);
    }
    
    public AttendanceOperateShareDialog(OPERATE_TYPE emType, UserData userData, String fingerPrintId) { 
        
        setTitle(Res.string().getPersonOperate());
        setLayout(new BorderLayout());
        setModal(true);
        pack();
        setSize(300, 200);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        //////////人员信息面板  /////////////////
        
        JPanel personInfoPanel = new JPanel();
        BorderEx.set(personInfoPanel, "", 4);
        Dimension dimLable = new Dimension(80, 20);
        JLabel userIdLabel = new JLabel(Res.string().getUserId());
        JLabel userNameLabel = new JLabel(Res.string().getUserName(true));
        JLabel cardNoLabel = new JLabel(Res.string().getCardNo());
        JLabel    fingerPrintIdLabel = new JLabel(Res.string().getFingerPrintId());
        userIdLabel.setPreferredSize(dimLable);
        userNameLabel.setPreferredSize(dimLable);
        cardNoLabel.setPreferredSize(dimLable);
        fingerPrintIdLabel.setPreferredSize(new Dimension(85, 20));
        
        Dimension dimValue = new Dimension(150, 20);
        userIdTextField = new JTextField();
        userNameTextField = new JTextField();
        cardNoTextField = new JTextField();
        fingerPrintIdTextField = new JTextField();
        userIdTextField.setPreferredSize(dimValue);
        userNameTextField.setPreferredSize(dimValue);
        cardNoTextField.setPreferredSize(dimValue);
        fingerPrintIdTextField.setPreferredSize(dimValue);
        
        // 数据处理
        if (userData != null) {
            if (userData.userId != null) {
                userIdTextField.setText(userData.userId);
            }
            
            if (userData.userName != null) {
                userNameTextField.setText(userData.userName);
            }
            
            if (userData.cardNo != null) {
                cardNoTextField.setText(userData.cardNo);
            }
        }
        
        if (!fingerPrintId.isEmpty()) {
            fingerPrintIdTextField.setText(fingerPrintId);
        }
        
        if (emType == OPERATE_TYPE.DELETE_FINGERPRINT_BY_ID) { // 根据信息ID删除用户
            JPanel fingerPrintPanel = new JPanel();
            fingerPrintPanel.add(fingerPrintIdLabel);
            fingerPrintPanel.add(fingerPrintIdTextField);
            personInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 40));
            personInfoPanel.add(fingerPrintPanel);
        }else {
            personInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
            personInfoPanel.add(userIdLabel);
            personInfoPanel.add(userIdTextField);
            personInfoPanel.add(userNameLabel);
            personInfoPanel.add(userNameTextField);
            personInfoPanel.add(cardNoLabel);
            personInfoPanel.add(cardNoTextField);
            
            if (emType == OPERATE_TYPE.DELETE_FINGERPRINT_BY_USERID
                    || emType == OPERATE_TYPE.DELETE_USER) {
                JLabel promptLabel = new JLabel(" " + Res.string().getDeleteFingerPrintPrompt() + " ");
                promptLabel.setEnabled(false);
                personInfoPanel.add(promptLabel);
            }
        }
        
        //////////功能面板  /////////////////
        JPanel functionPanel = new JPanel();
        confirmBtn = new JButton(Res.string().getConfirm());
        cancelBtn = new JButton(Res.string().getCancel());
        confirmBtn.setPreferredSize(new Dimension(100, 20));
        cancelBtn.setPreferredSize(new Dimension(100, 20));
        
        functionPanel.add(confirmBtn);
        functionPanel.add(cancelBtn);
        
        add(personInfoPanel, BorderLayout.CENTER);
        add(functionPanel, BorderLayout.SOUTH);
        
        operateListener = new UserOperateListener();
        confirmBtn.addActionListener(operateListener);
        cancelBtn.addActionListener(operateListener);
        
        this.emType = emType;
        switch(emType) {
            case ADD_USER:
                setTitle(Res.string().getAddPerson());
                confirmBtn.setText(Res.string().getAdd());
                break;
            case MODIFIY_USER:
                setTitle(Res.string().getModifyPerson());
                confirmBtn.setText(Res.string().getModify());
                userIdTextField.setEnabled(false);
                break;
            case DELETE_USER:
                setTitle(Res.string().getDelPerson());
                confirmBtn.setText(Res.string().getDelete());
                userIdTextField.setEnabled(false);
                userNameTextField.setEnabled(false);
                cardNoTextField.setEnabled(false);
                break;
            case DELETE_FINGERPRINT_BY_USERID:
            case DELETE_FINGERPRINT_BY_ID:
                setTitle(Res.string().getDeleteFingerPrint());
                confirmBtn.setText(Res.string().getDelete());
                userIdTextField.setEnabled(false);
                userNameTextField.setEnabled(false);
                cardNoTextField.setEnabled(false);
                fingerPrintIdTextField.setEditable(false);
            default:
                break;
        }
    }
    
    public boolean checkDataValidity() {
 
        if (emType == OPERATE_TYPE.ADD_USER) {
            if (userIdTextField.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getUserId(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                return false;
            }
    
            try {
                if (userIdTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_COMMON_STRING_32-1) {
                    JOptionPane.showMessageDialog(null, Res.string().getUserIdExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                    return false;
                }
            }catch (Exception e){
                
            }
        }
        
        try {
            if (userNameTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_ATTENDANCE_USERNAME_LEN-1) {
                JOptionPane.showMessageDialog(null, Res.string().getUserNameExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                return false;
            }
            
            if (cardNoTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_COMMON_STRING_32-1) {
                JOptionPane.showMessageDialog(null, Res.string().getCardNoExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }catch (Exception e){
            
        }
        
        
        return true;
    }
    
    public UserData getUserData() {
        UserData userData = new UserData();
        userData.cardNo = userIdTextField.getText();
        userData.userName = userNameTextField.getText();
        userData.cardNo = cardNoTextField.getText();
        return userData;
    }
    
    private class UserOperateListener implements ActionListener {                
        @Override
        public void actionPerformed(ActionEvent arg0) {
    
            if (arg0.getSource() == cancelBtn) {
                dispose();
            }else if (arg0.getSource() == confirmBtn) {
                switch(emType) {
                    case ADD_USER:
                        if (!checkDataValidity()) {
                            return;
                        }
                        bSuccess = AttendanceModule.addUser(userIdTextField.getText(), userNameTextField.getText(), cardNoTextField.getText());
                        break;
                    case MODIFIY_USER:
                        if (!checkDataValidity()) {
                            return;
                        }
                        bSuccess = AttendanceModule.modifyUser(userIdTextField.getText(), userNameTextField.getText(), cardNoTextField.getText());
                        break;
                    case DELETE_USER:
                        bSuccess = AttendanceModule.deleteUser(userIdTextField.getText());
                        break;
                    case DELETE_FINGERPRINT_BY_USERID:
                        bSuccess = AttendanceModule.removeFingerByUserId(userIdTextField.getText());
                        break;
                    case DELETE_FINGERPRINT_BY_ID:
                        bSuccess = AttendanceModule.removeFingerRecord(Integer.parseInt(fingerPrintIdTextField.getText()));
                        break;
                    default:
                        System.err.println("Can't Deal Operate Type: " + emType);
                        break;
                }
                
                if(bSuccess) {
                    JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
                }
                dispose();
            }else {
                System.err.println("Unknown Event: " + arg0.getSource());
            }
        }
    }
    
    private UserOperateListener operateListener;    // 按键监听
    private JTextField userIdTextField;                // 用户ID
    private JTextField userNameTextField;            // 用户名
    private JTextField cardNoTextField;                // 卡号
    private JTextField    fingerPrintIdTextField;        // 信息ID
    private JButton confirmBtn;                        // 确认(根据emType类型变化)
    private JButton cancelBtn;                        // 取消
}