潘志宝
2024-12-10 29b971556c85da59d9f820dce13f8bce4d52056a
提交 | 用户 | 时间
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.util.HashMap;
7 import java.util.Vector;
8
9 import javax.swing.JButton;
10 import javax.swing.JFrame;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JScrollPane;
14 import javax.swing.JSplitPane;
15 import javax.swing.JTable;
16 import javax.swing.ListSelectionModel;
17 import javax.swing.UIManager;
18 import javax.swing.table.DefaultTableCellRenderer;
19 import javax.swing.table.DefaultTableModel;
20
21 import com.iailab.netsdk.common.BorderEx;
22 import com.iailab.netsdk.common.Res;
23 import com.iailab.netsdk.common.Res.LanguageType;
24 import com.iailab.netsdk.demo.module.AttendanceModule.AccessEventInfo;
25 import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
26 import com.iailab.netsdk.lib.NetSDKLib.NET_ACCESS_DOOROPEN_METHOD;
27
28 public class AttendanceShowPanel extends JPanel {
29     /**
30      * 
31      */
32     private static final long serialVersionUID = 1L;
33     public static int userIndex = 0;
34     public static int eventIndex = 0;
35     
36     public AttendanceShowPanel() {
37         setLayout(new BorderLayout());
38         
39         userShowPanel = new UserInfoShowPanel();
40         eventShowPanel = new EventInfoShowPanel();
41         
42         JSplitPane splitPane = new JSplitPane();
43         splitPane.setDividerSize(0);
44         splitPane.setBorder(null);
45         splitPane.add(userShowPanel, JSplitPane.LEFT);
46         splitPane.add(eventShowPanel, JSplitPane.RIGHT);
47         
48         add(splitPane);
49     }
50     
51     public void clearup() {
52         userShowPanel.clearData();
53         eventShowPanel.clearEvent();
54     }
55     
56     /**
57      * 用户信息显示界面
58      * */
59     public class UserInfoShowPanel extends JPanel {
60         /**
61          * 
62          */
63         private static final long serialVersionUID = 1L;
64         public static final int INDEX = 0;
65         public static final int USER_ID = 1;
66         public static final int USER_NAME = 2;
67         public static final int CARD_NO = 3;
68         public static final int FINGERPRINT_ID = 4;
69         public static final int FINGERPRINT_DATA = 5;
70         
71         public final static int QUERY_SHOW_COUNT = 15; // 查询人数
72         private int realRows = 0;        // 实际显示个数
73         
74         public UserInfoShowPanel() {
75             BorderEx.set(this, Res.string().getUserList(), 1);
76             setLayout(new BorderLayout());
77             setPreferredSize(new Dimension(395, 400));
78             Vector<String> columnNames =  new Vector<String>();
79             columnNames.add(Res.string().getIndex());             // 序号
80             columnNames.add(Res.string().getUserId());             // 用户编号
81             columnNames.add(Res.string().getUserName());         // 用户名
82             columnNames.add(Res.string().getCardNo());             // 卡号
83             
84             tableModel = new DefaultTableModel(null, columnNames);
85             table = new JTable(tableModel) {
86                 private static final long serialVersionUID = 1L;
87
88                 public boolean isCellEditable(int rowIndex, int columnIndex) { // 不可编辑
89                     return false;
90                 }
91             };
92
93             tableModel.setRowCount(QUERY_SHOW_COUNT);    // 设置最小显示行
94             
95             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行
96             
97             table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
98             table.getColumnModel().getColumn(USER_ID).setPreferredWidth(150);
99             table.getColumnModel().getColumn(USER_NAME).setPreferredWidth(150);
100             table.getColumnModel().getColumn(CARD_NO).setPreferredWidth(150);
101             
102             ((DefaultTableCellRenderer)
103                     table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
104             
105             table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
106             
107             JScrollPane scrollPane = new JScrollPane(table);
108             scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
109             scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
110             
111             JPanel functionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
112             prePageBtn = new JButton(Res.string().getPreviousPage());
113             nextPageBtn = new JButton(Res.string().getNextPage());
114             prePageBtn.setPreferredSize(new Dimension(120, 20));
115             nextPageBtn.setPreferredSize(new Dimension(120, 20));
116             
117             prePageBtn.setEnabled(false);
118             nextPageBtn.setEnabled(false);
119             
120             functionPanel.add(prePageBtn);
121             functionPanel.add(new JLabel("    "));
122             functionPanel.add(nextPageBtn);
123             
124             add(scrollPane, BorderLayout.CENTER);
125             add(functionPanel, BorderLayout.SOUTH);
126         }
127         
128         public int getRows(){
129             return realRows;
130         }
131         
132         public UserData GetSelectedItem() {
133             int currentRow = table.getSelectedRow(); //获得所选的单行
134             if(currentRow < 0 || currentRow + 1 > realRows) {
135                 return null;
136             }
137             UserData userData = new UserData();
138             
139             userData.userId = (String) tableModel.getValueAt(currentRow, 1);
140             userData.userName = (String) tableModel.getValueAt(currentRow, 2);
141             userData.cardNo = (String) tableModel.getValueAt(currentRow, 3);
142             
143             return userData;
144         }
145         
146         public void updateSelectedItem(UserData userData) {
147             
148             int currentRow = table.getSelectedRow(); //获得所选的单行
149             if(currentRow < 0 || currentRow + 1 > realRows) {
150                 return;
151             }
152             
153 //            tableModel.setValueAt(userData.userId, currentRow, 1);
154             tableModel.setValueAt(userData.userName, currentRow, 2);
155             tableModel.setValueAt(userData.cardNo, currentRow, 3);
156             table.updateUI();
157         }
158         
159         public void insertData(UserData[] arrUserData) {
160             if (arrUserData == null) {
161                 return;
162             }
163             
164             realRows = 0;
165             tableModel.setRowCount(0);
166             
167             for (UserData userData : arrUserData) {
168                 insertUserData(userData);
169             }
170             
171             tableModel.setRowCount(QUERY_SHOW_COUNT);
172             table.updateUI();
173             
174             setButtonEnable(true);
175         }
176         
177         public void setButtonEnable(boolean b) {
178             if (b) {
179                 if (UserData.nTotalUser - userIndex > 0) {
180                     nextPageBtn.setEnabled(true);
181                 }else {
182                     nextPageBtn.setEnabled(false);
183                 }
184                 
185                 if (userIndex - QUERY_SHOW_COUNT > 0) {
186                     prePageBtn.setEnabled(true);
187                 }else {
188                     prePageBtn.setEnabled(false);
189                 }
190             }else {
191                 prePageBtn.setEnabled(false);
192                 nextPageBtn.setEnabled(false);
193             }
194         }
195         
196         public void insertData(UserData userData) {
197             if (userData == null) {
198                 return;
199             }
200             
201             clearData();
202             tableModel.setRowCount(0);
203             
204             insertUserData(userData);
205             
206             tableModel.setRowCount(QUERY_SHOW_COUNT);
207             table.updateUI();
208             
209             setButtonEnable(false);
210         }
211         
212         
213         private void insertUserData(UserData userData) {
214             ++userIndex;
215             ++realRows;
216             Vector<String> vector = new Vector<String>();
217             vector.add(String.valueOf(userIndex));
218             vector.add(userData.userId);
219             vector.add(userData.userName);
220             vector.add(userData.cardNo);
221             
222             tableModel.addRow(vector);
223         }
224             
225         public void clearData() {
226             realRows = 0;
227             userIndex = 0;
228             tableModel.setRowCount(0);
229             tableModel.setRowCount(QUERY_SHOW_COUNT);
230             table.updateUI();
231             prePageBtn.setEnabled(false);
232             nextPageBtn.setEnabled(false);
233         }
234         
235         private JTable table = null;
236         private DefaultTableModel tableModel = null;
237         public JButton prePageBtn;
238         public JButton nextPageBtn;
239     }
240     
241     /**
242      * 门禁事件显示界面
243      * */
244     public class EventInfoShowPanel extends JPanel {
245         /**
246          * 
247          */
248         private static final long serialVersionUID = 1L;
249         private static final int INDEX = 0;
250         private static final int USER_ID = 1;
251         private static final int CARD_NO = 2;
252         private static final int EVENT_TIME = 3;
253         private static final int DOOR_OPEN_METHOD = 4;
254         
255         private final static int MIN_SHOW_LINES = 17;
256         private final static int MAX_SHOW_LINES = 50;
257         
258         public EventInfoShowPanel() {
259             BorderEx.set(this, Res.string().getEventInfo(), 1);
260             setLayout(new BorderLayout());
261             setPreferredSize(new Dimension(395, 400));
262             
263             Vector<String> columnNames =  new Vector<String>();
264             columnNames.add(Res.string().getIndex());             // 序号
265             columnNames.add(Res.string().getUserId());             // 用户编号
266             columnNames.add(Res.string().getCardNo());             // 卡号
267             columnNames.add(Res.string().getEventTime());         // 事件时间
268             columnNames.add(Res.string().getDoorOpenMethod());     // 开门方式
269             
270             tableModel = new DefaultTableModel(null, columnNames);
271             table = new JTable(tableModel) {
272                 private static final long serialVersionUID = 1L;
273
274                 public boolean isCellEditable(int rowIndex, int columnIndex) {
275                     return false;
276                 }
277             };
278
279             tableModel.setRowCount(MIN_SHOW_LINES);    // 设置最小显示行
280             
281             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行
282             
283             table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
284             table.getColumnModel().getColumn(USER_ID).setPreferredWidth(150);
285             table.getColumnModel().getColumn(CARD_NO).setPreferredWidth(150);
286             table.getColumnModel().getColumn(EVENT_TIME).setPreferredWidth(150);
287             table.getColumnModel().getColumn(DOOR_OPEN_METHOD).setPreferredWidth(120);
288             
289             ((DefaultTableCellRenderer)
290                     table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
291             
292             table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
293             
294             JScrollPane scrollPane = new JScrollPane(table);
295             scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
296             scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
297             add(scrollPane, BorderLayout.CENTER);
298         }
299         
300         public void clearEvent() {
301             eventIndex = 0;
302             tableModel.setRowCount(0);
303             tableModel.setRowCount(MIN_SHOW_LINES);
304             table.updateUI();
305         }
306         
307         public void insertEvent(AccessEventInfo accessEventInfo) {
308             if (accessEventInfo == null) {
309                 return;
310             }
311             ++eventIndex;
312             tableModel.insertRow(0, convertEventData(accessEventInfo));
313             if (eventIndex <= MIN_SHOW_LINES) {
314                 tableModel.setRowCount(MIN_SHOW_LINES);
315             }else if (eventIndex >= MAX_SHOW_LINES){
316                 tableModel.setRowCount(MAX_SHOW_LINES);
317             }
318             
319             table.updateUI();
320         }
321         
322         private Vector<String> convertEventData(AccessEventInfo accessEventInfo) {    
323             Vector<String> vector = new Vector<String>();
324             vector.add(String.valueOf(eventIndex));
325             vector.add(accessEventInfo.userId);
326             vector.add(accessEventInfo.cardNo);
327             vector.add(accessEventInfo.eventTime.replace("/", "-"));
328             String openDoor = openDoorMethodMap.get(accessEventInfo.openDoorMethod);
329             if (openDoor == null) {
330                 openDoor = Res.string().getUnKnow();
331             }
332             vector.add(openDoor);
333         
334             return vector;
335         }
336         
337         private JTable table = null;
338         private DefaultTableModel tableModel = null;
339     }
340     
341     private static HashMap<Integer, String> openDoorMethodMap = new HashMap<Integer, String>() {
342         
343         private static final long serialVersionUID = 1L;
344
345         {
346             put(NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_FINGERPRINT, Res.string().getFingerPrint());
347             put(NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_CARD, Res.string().getCard());
348         }
349     };
350     
351     public UserInfoShowPanel userShowPanel;
352     public EventInfoShowPanel eventShowPanel;
353     
354     public static void main(String[] args) {
355         try {
356             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
357         } catch (Exception e) {
358             e.printStackTrace();
359         }
360         
361         Res.string().switchLanguage(LanguageType.English);
362         
363         AttendanceShowPanel demo = new AttendanceShowPanel();
364         JFrame frame = new JFrame();
365         frame.setSize(800, 560);
366         frame.add(demo);
367         System.out.println("AttendanceShowPanel Test");
368         frame.setVisible(true);        
369     }
370 }