houzhongjian
2024-12-27 12db859ea1485e676b6e11927946cb50f79e27a8
提交 | 用户 | 时间
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.JLabel;
11 import javax.swing.JOptionPane;
12 import javax.swing.JPanel;
13 import javax.swing.JSplitPane;
14 import javax.swing.JTextField;
15 import javax.swing.SwingUtilities;
16 import javax.swing.SwingWorker;
17
18 import com.iailab.netsdk.common.BorderEx;
19 import com.iailab.netsdk.common.Res;
20 import com.iailab.netsdk.demo.frame.Attendance.AttendanceShowPanel.UserInfoShowPanel;
21 import com.iailab.netsdk.demo.module.AttendanceModule;
22 import com.iailab.netsdk.demo.module.AttendanceModule.OPERATE_TYPE;
23 import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
24
25 /**
26  * 考勤机操作面板
27  */
28 public class AttendanceFunctionOperatePanel extends JPanel{
29     /**
30      * 
31      */
32     private static final long serialVersionUID = 1L;
33     public static boolean bLogout = false;
34     public AttendanceShowPanel showPanel;    // 显示面板
35     private AttendanceFunctionOperatePanel target = this; // 为了传值
36     
37     public AttendanceFunctionOperatePanel(AttendanceShowPanel showPanel) {
38         
39         setLayout(new BorderLayout());
40         setPreferredSize(new Dimension(800, 120));
41         
42         listener = new UserOperateActionListener();
43         
44         userPanel = new FunctionOperatePanel();
45         subscribePanel = new SubscribePanel(showPanel.eventShowPanel);
46         
47         JSplitPane splitPane = new JSplitPane();
48         splitPane.setDividerSize(0);
49         splitPane.setBorder(null);
50         splitPane.add(userPanel, JSplitPane.LEFT);
51         splitPane.add(subscribePanel, JSplitPane.RIGHT);
52         
53         add(splitPane, BorderLayout.CENTER);
54             
55         this.showPanel = showPanel;
56         this.showPanel.userShowPanel.prePageBtn.addActionListener(listener);
57         this.showPanel.userShowPanel.nextPageBtn.addActionListener(listener);
58     }
59     
60     public void setButtonEnable(boolean b) {
61         bLogout=!b;
62         userPanel.setButtonEnable(b);
63         subscribePanel.setButtonEnable(b);
64     }
65     
66     public void setSearchEnable(boolean b) {
67         showPanel.userShowPanel.setButtonEnable(b);
68         userPanel.searchPersonBtn.setEnabled(b);
69     }
70     
71     public void insertData(UserData[] arrUserData) {
72         showPanel.userShowPanel.insertData(arrUserData);
73     }
74     
75     public void insertData(UserData userData) {
76         showPanel.userShowPanel.insertData(userData);
77     }
78     
79     /**
80      * 总的功能操作面板
81      */
82     public class FunctionOperatePanel extends JPanel{
83         /**
84          * 
85          */
86         private static final long serialVersionUID = 1L;
87         public FunctionOperatePanel() {
88 //            BorderEx.set(this, Res.string().getOperateByUser(), 1);
89             setLayout(new BorderLayout());
90             setPreferredSize(new Dimension(600, 60));
91             
92             ////////// 查询条件 /////////////////
93             JLabel userIdLabel = new JLabel(Res.string().getUserId(), JLabel.CENTER);
94             userIdTextField = new JTextField();
95             userIdLabel.setPreferredSize(new Dimension(80, 20));
96             userIdTextField.setPreferredSize(new Dimension(110, 20));
97             
98             ////////// 功能面板 /////////////////
99             // 用户功能面板
100             JPanel userFunctionPanel = new JPanel();
101             userFunctionPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
102             BorderEx.set(userFunctionPanel, Res.string().getUserOperate(), 1);
103             searchPersonBtn = new JButton(Res.string().getSearch());
104             addPersonBtn = new JButton(Res.string().getAdd());
105             modifyPersonBtn = new JButton(Res.string().getModify());
106             deletePersonBtn = new JButton(Res.string().getDelete());
107
108             searchPersonBtn.setPreferredSize(new Dimension(90, 20));
109             addPersonBtn.setPreferredSize(new Dimension(90, 20));
110             modifyPersonBtn.setPreferredSize(new Dimension(90, 20));
111             deletePersonBtn.setPreferredSize(new Dimension(90, 20));
112             
113             userFunctionPanel.add(userIdLabel);
114             userFunctionPanel.add(userIdTextField);
115             userFunctionPanel.add(searchPersonBtn);
116             userFunctionPanel.add(addPersonBtn);
117             userFunctionPanel.add(modifyPersonBtn);
118             userFunctionPanel.add(deletePersonBtn);
119             
120             // 信息功能面板
121             JPanel fingerPrintFunctionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
122             BorderEx.set(fingerPrintFunctionPanel, Res.string().getFingerPrintOperate(), 1);
123             operateByUserIdBtn = new JButton(Res.string().getOperateByUserId());
124             operateByFingerPrintIdBtn = new JButton(Res.string().getOperateByFingerPrintId());
125
126             operateByUserIdBtn.setPreferredSize(new Dimension(260, 20));
127             operateByFingerPrintIdBtn.setPreferredSize(new Dimension(260, 20));
128             fingerPrintFunctionPanel.add(operateByUserIdBtn);
129             fingerPrintFunctionPanel.add(operateByFingerPrintIdBtn);
130             
131             JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
132             splitPane.setDividerSize(0);
133             splitPane.setBorder(null);
134             splitPane.add(userFunctionPanel, JSplitPane.TOP);
135             splitPane.add(fingerPrintFunctionPanel, JSplitPane.BOTTOM);
136              add(splitPane, BorderLayout.CENTER);
137             
138              searchPersonBtn.addActionListener(listener);
139             addPersonBtn.addActionListener(listener);
140             modifyPersonBtn.addActionListener(listener);
141             deletePersonBtn.addActionListener(listener);
142             operateByUserIdBtn.addActionListener(listener);
143             operateByFingerPrintIdBtn.addActionListener(listener);
144             
145             setButtonEnable(false);
146         }
147     
148         public void setButtonEnable(boolean b) {
149             searchPersonBtn.setEnabled(b);
150             addPersonBtn.setEnabled(b);
151             modifyPersonBtn.setEnabled(b);
152             deletePersonBtn.setEnabled(b);
153             operateByUserIdBtn.setEnabled(b);
154             operateByFingerPrintIdBtn.setEnabled(b);
155         }
156         
157         public void searchPerson(OPERATE_TYPE type) { // flush 为 true 时 强制刷新
158             if (type == OPERATE_TYPE.SEARCH_USER && !userIdTextField.getText().isEmpty()) {
159                 UserData userData = AttendanceModule.getUser(userIdTextField.getText());
160                 if (userData == null) {
161                     JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
162                     return;
163                 }
164                 showPanel.userShowPanel.insertData(userData);                
165             }else {
166                 setSearchEnable(false);
167                 new SearchPersonSwingWorker(type, target).execute();
168             }
169         }
170         
171         private JTextField userIdTextField;
172         public JButton searchPersonBtn;
173         private JButton addPersonBtn;
174         private JButton modifyPersonBtn;
175         private JButton deletePersonBtn;
176         private JButton operateByUserIdBtn;
177         private JButton operateByFingerPrintIdBtn;
178     }
179     
180     /**
181      * 按键监听实现类
182      */
183     private class UserOperateActionListener implements ActionListener {    
184
185         @Override
186         public void actionPerformed(ActionEvent arg0) {
187
188             OPERATE_TYPE emType = getOperateType(arg0.getSource());
189             switch(emType) {
190                 case SEARCH_USER:
191                 case PRE_SEARCH_USER:
192                 case NEXT_SEARCH_USER:
193                     SwingUtilities.invokeLater(new SearchRunnable(emType));
194                     break;    
195                 case ADD_USER: 
196                     new AttendanceOperateShareDialog(emType, null, "").setVisible(true);
197                     break;
198                 case MODIFIY_USER:
199                 case DELETE_USER:
200                 case FINGERPRINT_OPEARTE_BY_USERID:
201                     UserData userData = showPanel.userShowPanel.GetSelectedItem();
202                     if(userData == null) {
203                         JOptionPane.showMessageDialog(null, Res.string().getSelectPerson(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
204                         return;
205                     }
206                     if (emType == OPERATE_TYPE.FINGERPRINT_OPEARTE_BY_USERID) {
207                         new OperateByUserIdDialog(userData).setVisible(true);
208                     }else {
209                         new AttendanceOperateShareDialog(emType, userData).setVisible(true);
210                     }
211                     
212                     break;
213                 case FINGERPRINT_OPEARTE_BY_ID:
214                     new OperateByFingerPrintIdDialog().setVisible(true);
215                 default:
216                     break;
217             }
218         }
219         
220         private OPERATE_TYPE getOperateType(Object btn) {
221             OPERATE_TYPE type = OPERATE_TYPE.UNKNOWN;
222             
223             if (btn == userPanel.searchPersonBtn) { // 查找人员
224                 type = OPERATE_TYPE.SEARCH_USER;
225             }else if (btn == showPanel.userShowPanel.prePageBtn) { // 上一页查找人员
226                 type = OPERATE_TYPE.PRE_SEARCH_USER;
227             }else if (btn == showPanel.userShowPanel.nextPageBtn) { // 下一页查找人员
228                 type = OPERATE_TYPE.NEXT_SEARCH_USER;
229             }else if (btn == userPanel.addPersonBtn) { // 添加人员
230                 type = OPERATE_TYPE.ADD_USER;
231             }else if (btn == userPanel.modifyPersonBtn) { // 修改人员
232                 type = OPERATE_TYPE.MODIFIY_USER;
233             }else if (btn == userPanel.deletePersonBtn) {    // 删除人员
234                 type = OPERATE_TYPE.DELETE_USER;
235             }else if (btn == userPanel.operateByUserIdBtn) {        // 通过用户ID操作信息
236                 type = OPERATE_TYPE.FINGERPRINT_OPEARTE_BY_USERID;
237             }else if (btn == userPanel.operateByFingerPrintIdBtn) {    // 通过信息ID操作信息
238                 type = OPERATE_TYPE.FINGERPRINT_OPEARTE_BY_ID;
239             }else {
240                 System.err.println("Unknown Event: " + btn);
241             }
242             
243             return type;
244             
245         }
246     }
247     
248     public class SearchRunnable implements Runnable {
249         private OPERATE_TYPE searchType;
250         public SearchRunnable(OPERATE_TYPE searchType) {
251             this.searchType = searchType;
252         }
253         
254         @Override
255         public void run() {
256             userPanel.searchPerson(searchType);
257         }
258     }
259     
260     /**
261      * 人员搜索工作线程(完成异步搜索)
262      */
263     public class SearchPersonSwingWorker extends SwingWorker<UserData[], Object> {
264         private AttendanceFunctionOperatePanel operatePanel;
265         private int offset = 0;
266         private OPERATE_TYPE type;
267         public SearchPersonSwingWorker(OPERATE_TYPE type, AttendanceFunctionOperatePanel operatePanel) {
268             this.operatePanel = operatePanel;
269             this.type = type;
270         }
271         
272         protected UserData[] doInBackground() throws Exception {
273
274             switch(type) {
275                 case SEARCH_USER:
276                     offset = 0;
277                     break;
278                 case PRE_SEARCH_USER:
279                     offset = UserInfoShowPanel.QUERY_SHOW_COUNT * ((AttendanceShowPanel.userIndex-1)/UserInfoShowPanel.QUERY_SHOW_COUNT - 1);
280                     break;
281                 case NEXT_SEARCH_USER:
282                     offset = AttendanceShowPanel.userIndex;
283                     break;
284                 default:
285                     break;
286             }
287             
288             UserData[] arrUserData = AttendanceModule.findUser(offset, UserInfoShowPanel.QUERY_SHOW_COUNT);
289             
290             return arrUserData;
291         }
292         
293         @Override
294         protected void done() {    
295             if (bLogout) {
296                 return;
297             }
298             
299             try {
300                 
301                 UserData[] arrUserData = get();
302                 if (arrUserData == null) {
303                     JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
304                     return;
305                 }
306                 
307                 if (type == OPERATE_TYPE.SEARCH_USER || 
308                         type == OPERATE_TYPE.PRE_SEARCH_USER) { // 更新userIndex
309                     AttendanceShowPanel.userIndex = offset;
310                 }
311                 operatePanel.insertData(arrUserData);                
312             } catch (Exception e) {
313 //                e.printStackTrace();
314             }finally {
315                 operatePanel.setSearchEnable(true);
316             }
317             
318         }
319     }
320     
321     private UserOperateActionListener listener;
322     public FunctionOperatePanel userPanel;
323     public SubscribePanel subscribePanel;
324 }