dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.Attendance;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JLabel;
12 import javax.swing.JOptionPane;
13 import javax.swing.JPanel;
14 import javax.swing.JTextField;
15
16 import com.iailab.netsdk.common.BorderEx;
17 import com.iailab.netsdk.common.Res;
18 import com.iailab.netsdk.demo.module.AttendanceModule;
19 import com.iailab.netsdk.demo.module.AttendanceModule.OPERATE_TYPE;
20 import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
21 import com.iailab.netsdk.lib.NetSDKLib;
22
23 /**
24  * 考勤机操作对话框
25  */
26 public class AttendanceOperateShareDialog extends JDialog{
27     
28     /**
29      * 
30      */
31     private static final long serialVersionUID = 1L;
32     private OPERATE_TYPE emType = OPERATE_TYPE.UNKNOWN;                    // 操作类型
33     private boolean bSuccess = false;                                    // 接口调用结果
34     
35     public AttendanceOperateShareDialog(OPERATE_TYPE emType, UserData userData) { 
36         this(emType, userData, "");
37     }
38     
39     public AttendanceOperateShareDialog(OPERATE_TYPE emType, String fingerPrintId) { 
40         this(emType, null, fingerPrintId);
41     }
42     
43     public AttendanceOperateShareDialog(OPERATE_TYPE emType, UserData userData, String fingerPrintId) { 
44         
45         setTitle(Res.string().getPersonOperate());
46         setLayout(new BorderLayout());
47         setModal(true);
48         pack();
49         setSize(300, 200);
50         setResizable(false);
51         setLocationRelativeTo(null);
52         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
53         
54         //////////人员信息面板  /////////////////
55         
56         JPanel personInfoPanel = new JPanel();
57         BorderEx.set(personInfoPanel, "", 4);
58         Dimension dimLable = new Dimension(80, 20);
59         JLabel userIdLabel = new JLabel(Res.string().getUserId());
60         JLabel userNameLabel = new JLabel(Res.string().getUserName(true));
61         JLabel cardNoLabel = new JLabel(Res.string().getCardNo());
62         JLabel    fingerPrintIdLabel = new JLabel(Res.string().getFingerPrintId());
63         userIdLabel.setPreferredSize(dimLable);
64         userNameLabel.setPreferredSize(dimLable);
65         cardNoLabel.setPreferredSize(dimLable);
66         fingerPrintIdLabel.setPreferredSize(new Dimension(85, 20));
67         
68         Dimension dimValue = new Dimension(150, 20);
69         userIdTextField = new JTextField();
70         userNameTextField = new JTextField();
71         cardNoTextField = new JTextField();
72         fingerPrintIdTextField = new JTextField();
73         userIdTextField.setPreferredSize(dimValue);
74         userNameTextField.setPreferredSize(dimValue);
75         cardNoTextField.setPreferredSize(dimValue);
76         fingerPrintIdTextField.setPreferredSize(dimValue);
77         
78         // 数据处理
79         if (userData != null) {
80             if (userData.userId != null) {
81                 userIdTextField.setText(userData.userId);
82             }
83             
84             if (userData.userName != null) {
85                 userNameTextField.setText(userData.userName);
86             }
87             
88             if (userData.cardNo != null) {
89                 cardNoTextField.setText(userData.cardNo);
90             }
91         }
92         
93         if (!fingerPrintId.isEmpty()) {
94             fingerPrintIdTextField.setText(fingerPrintId);
95         }
96         
97         if (emType == OPERATE_TYPE.DELETE_FINGERPRINT_BY_ID) { // 根据信息ID删除用户
98             JPanel fingerPrintPanel = new JPanel();
99             fingerPrintPanel.add(fingerPrintIdLabel);
100             fingerPrintPanel.add(fingerPrintIdTextField);
101             personInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 40));
102             personInfoPanel.add(fingerPrintPanel);
103         }else {
104             personInfoPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
105             personInfoPanel.add(userIdLabel);
106             personInfoPanel.add(userIdTextField);
107             personInfoPanel.add(userNameLabel);
108             personInfoPanel.add(userNameTextField);
109             personInfoPanel.add(cardNoLabel);
110             personInfoPanel.add(cardNoTextField);
111             
112             if (emType == OPERATE_TYPE.DELETE_FINGERPRINT_BY_USERID
113                     || emType == OPERATE_TYPE.DELETE_USER) {
114                 JLabel promptLabel = new JLabel(" " + Res.string().getDeleteFingerPrintPrompt() + " ");
115                 promptLabel.setEnabled(false);
116                 personInfoPanel.add(promptLabel);
117             }
118         }
119         
120         //////////功能面板  /////////////////
121         JPanel functionPanel = new JPanel();
122         confirmBtn = new JButton(Res.string().getConfirm());
123         cancelBtn = new JButton(Res.string().getCancel());
124         confirmBtn.setPreferredSize(new Dimension(100, 20));
125         cancelBtn.setPreferredSize(new Dimension(100, 20));
126         
127         functionPanel.add(confirmBtn);
128         functionPanel.add(cancelBtn);
129         
130         add(personInfoPanel, BorderLayout.CENTER);
131         add(functionPanel, BorderLayout.SOUTH);
132         
133         operateListener = new UserOperateListener();
134         confirmBtn.addActionListener(operateListener);
135         cancelBtn.addActionListener(operateListener);
136         
137         this.emType = emType;
138         switch(emType) {
139             case ADD_USER:
140                 setTitle(Res.string().getAddPerson());
141                 confirmBtn.setText(Res.string().getAdd());
142                 break;
143             case MODIFIY_USER:
144                 setTitle(Res.string().getModifyPerson());
145                 confirmBtn.setText(Res.string().getModify());
146                 userIdTextField.setEnabled(false);
147                 break;
148             case DELETE_USER:
149                 setTitle(Res.string().getDelPerson());
150                 confirmBtn.setText(Res.string().getDelete());
151                 userIdTextField.setEnabled(false);
152                 userNameTextField.setEnabled(false);
153                 cardNoTextField.setEnabled(false);
154                 break;
155             case DELETE_FINGERPRINT_BY_USERID:
156             case DELETE_FINGERPRINT_BY_ID:
157                 setTitle(Res.string().getDeleteFingerPrint());
158                 confirmBtn.setText(Res.string().getDelete());
159                 userIdTextField.setEnabled(false);
160                 userNameTextField.setEnabled(false);
161                 cardNoTextField.setEnabled(false);
162                 fingerPrintIdTextField.setEditable(false);
163             default:
164                 break;
165         }
166     }
167     
168     public boolean checkDataValidity() {
169
170         if (emType == OPERATE_TYPE.ADD_USER) {
171             if (userIdTextField.getText().isEmpty()) {
172                 JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getUserId(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
173                 return false;
174             }
175     
176             try {
177                 if (userIdTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_COMMON_STRING_32-1) {
178                     JOptionPane.showMessageDialog(null, Res.string().getUserIdExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
179                     return false;
180                 }
181             }catch (Exception e){
182                 
183             }
184         }
185         
186         try {
187             if (userNameTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_ATTENDANCE_USERNAME_LEN-1) {
188                 JOptionPane.showMessageDialog(null, Res.string().getUserNameExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
189                 return false;
190             }
191             
192             if (cardNoTextField.getText().getBytes("UTF-8").length > NetSDKLib.MAX_COMMON_STRING_32-1) {
193                 JOptionPane.showMessageDialog(null, Res.string().getCardNoExceedLength(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
194                 return false;
195             }
196         }catch (Exception e){
197             
198         }
199         
200         
201         return true;
202     }
203     
204     public UserData getUserData() {
205         UserData userData = new UserData();
206         userData.cardNo = userIdTextField.getText();
207         userData.userName = userNameTextField.getText();
208         userData.cardNo = cardNoTextField.getText();
209         return userData;
210     }
211     
212     private class UserOperateListener implements ActionListener {                
213         @Override
214         public void actionPerformed(ActionEvent arg0) {
215     
216             if (arg0.getSource() == cancelBtn) {
217                 dispose();
218             }else if (arg0.getSource() == confirmBtn) {
219                 switch(emType) {
220                     case ADD_USER:
221                         if (!checkDataValidity()) {
222                             return;
223                         }
224                         bSuccess = AttendanceModule.addUser(userIdTextField.getText(), userNameTextField.getText(), cardNoTextField.getText());
225                         break;
226                     case MODIFIY_USER:
227                         if (!checkDataValidity()) {
228                             return;
229                         }
230                         bSuccess = AttendanceModule.modifyUser(userIdTextField.getText(), userNameTextField.getText(), cardNoTextField.getText());
231                         break;
232                     case DELETE_USER:
233                         bSuccess = AttendanceModule.deleteUser(userIdTextField.getText());
234                         break;
235                     case DELETE_FINGERPRINT_BY_USERID:
236                         bSuccess = AttendanceModule.removeFingerByUserId(userIdTextField.getText());
237                         break;
238                     case DELETE_FINGERPRINT_BY_ID:
239                         bSuccess = AttendanceModule.removeFingerRecord(Integer.parseInt(fingerPrintIdTextField.getText()));
240                         break;
241                     default:
242                         System.err.println("Can't Deal Operate Type: " + emType);
243                         break;
244                 }
245                 
246                 if(bSuccess) {
247                     JOptionPane.showMessageDialog(null, Res.string().getSucceed(), Res.string().getPromptMessage(), JOptionPane.INFORMATION_MESSAGE);
248                 } else {
249                     JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
250                 }
251                 dispose();
252             }else {
253                 System.err.println("Unknown Event: " + arg0.getSource());
254             }
255         }
256     }
257     
258     private UserOperateListener operateListener;    // 按键监听
259     private JTextField userIdTextField;                // 用户ID
260     private JTextField userNameTextField;            // 用户名
261     private JTextField cardNoTextField;                // 卡号
262     private JTextField    fingerPrintIdTextField;        // 信息ID
263     private JButton confirmBtn;                        // 确认(根据emType类型变化)
264     private JButton cancelBtn;                        // 取消
265 }