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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package com.iailab.netsdk.demo.frame.Attendance;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.HashMap;
import java.util.Vector;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
 
import com.iailab.netsdk.common.BorderEx;
import com.iailab.netsdk.common.Res;
import com.iailab.netsdk.common.Res.LanguageType;
import com.iailab.netsdk.demo.module.AttendanceModule.AccessEventInfo;
import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
import com.iailab.netsdk.lib.NetSDKLib.NET_ACCESS_DOOROPEN_METHOD;
 
public class AttendanceShowPanel extends JPanel {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static int userIndex = 0;
    public static int eventIndex = 0;
    
    public AttendanceShowPanel() {
        setLayout(new BorderLayout());
        
        userShowPanel = new UserInfoShowPanel();
        eventShowPanel = new EventInfoShowPanel();
        
        JSplitPane splitPane = new JSplitPane();
        splitPane.setDividerSize(0);
        splitPane.setBorder(null);
        splitPane.add(userShowPanel, JSplitPane.LEFT);
        splitPane.add(eventShowPanel, JSplitPane.RIGHT);
        
        add(splitPane);
    }
    
    public void clearup() {
        userShowPanel.clearData();
        eventShowPanel.clearEvent();
    }
    
    /**
     * 用户信息显示界面
     * */
    public class UserInfoShowPanel extends JPanel {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public static final int INDEX = 0;
        public static final int USER_ID = 1;
        public static final int USER_NAME = 2;
        public static final int CARD_NO = 3;
        public static final int FINGERPRINT_ID = 4;
        public static final int FINGERPRINT_DATA = 5;
        
        public final static int QUERY_SHOW_COUNT = 15; // 查询人数
        private int realRows = 0;        // 实际显示个数
        
        public UserInfoShowPanel() {
            BorderEx.set(this, Res.string().getUserList(), 1);
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(395, 400));
            Vector<String> columnNames =  new Vector<String>();
            columnNames.add(Res.string().getIndex());             // 序号
            columnNames.add(Res.string().getUserId());             // 用户编号
            columnNames.add(Res.string().getUserName());         // 用户名
            columnNames.add(Res.string().getCardNo());             // 卡号
            
            tableModel = new DefaultTableModel(null, columnNames);
            table = new JTable(tableModel) {
                private static final long serialVersionUID = 1L;
 
                public boolean isCellEditable(int rowIndex, int columnIndex) { // 不可编辑
                    return false;
                }
            };
 
            tableModel.setRowCount(QUERY_SHOW_COUNT);    // 设置最小显示行
            
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行
            
            table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
            table.getColumnModel().getColumn(USER_ID).setPreferredWidth(150);
            table.getColumnModel().getColumn(USER_NAME).setPreferredWidth(150);
            table.getColumnModel().getColumn(CARD_NO).setPreferredWidth(150);
            
            ((DefaultTableCellRenderer)
                    table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
            
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            
            JPanel functionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            prePageBtn = new JButton(Res.string().getPreviousPage());
            nextPageBtn = new JButton(Res.string().getNextPage());
            prePageBtn.setPreferredSize(new Dimension(120, 20));
            nextPageBtn.setPreferredSize(new Dimension(120, 20));
            
            prePageBtn.setEnabled(false);
            nextPageBtn.setEnabled(false);
            
            functionPanel.add(prePageBtn);
            functionPanel.add(new JLabel("    "));
            functionPanel.add(nextPageBtn);
            
            add(scrollPane, BorderLayout.CENTER);
            add(functionPanel, BorderLayout.SOUTH);
        }
        
        public int getRows(){
            return realRows;
        }
        
        public UserData GetSelectedItem() {
            int currentRow = table.getSelectedRow(); //获得所选的单行
            if(currentRow < 0 || currentRow + 1 > realRows) {
                return null;
            }
            UserData userData = new UserData();
            
            userData.userId = (String) tableModel.getValueAt(currentRow, 1);
            userData.userName = (String) tableModel.getValueAt(currentRow, 2);
            userData.cardNo = (String) tableModel.getValueAt(currentRow, 3);
            
            return userData;
        }
        
        public void updateSelectedItem(UserData userData) {
            
            int currentRow = table.getSelectedRow(); //获得所选的单行
            if(currentRow < 0 || currentRow + 1 > realRows) {
                return;
            }
            
//            tableModel.setValueAt(userData.userId, currentRow, 1);
            tableModel.setValueAt(userData.userName, currentRow, 2);
            tableModel.setValueAt(userData.cardNo, currentRow, 3);
            table.updateUI();
        }
        
        public void insertData(UserData[] arrUserData) {
            if (arrUserData == null) {
                return;
            }
            
            realRows = 0;
            tableModel.setRowCount(0);
            
            for (UserData userData : arrUserData) {
                insertUserData(userData);
            }
            
            tableModel.setRowCount(QUERY_SHOW_COUNT);
            table.updateUI();
            
            setButtonEnable(true);
        }
        
        public void setButtonEnable(boolean b) {
            if (b) {
                if (UserData.nTotalUser - userIndex > 0) {
                    nextPageBtn.setEnabled(true);
                }else {
                    nextPageBtn.setEnabled(false);
                }
                
                if (userIndex - QUERY_SHOW_COUNT > 0) {
                    prePageBtn.setEnabled(true);
                }else {
                    prePageBtn.setEnabled(false);
                }
            }else {
                prePageBtn.setEnabled(false);
                nextPageBtn.setEnabled(false);
            }
        }
        
        public void insertData(UserData userData) {
            if (userData == null) {
                return;
            }
            
            clearData();
            tableModel.setRowCount(0);
            
            insertUserData(userData);
            
            tableModel.setRowCount(QUERY_SHOW_COUNT);
            table.updateUI();
            
            setButtonEnable(false);
        }
        
        
        private void insertUserData(UserData userData) {
            ++userIndex;
            ++realRows;
            Vector<String> vector = new Vector<String>();
            vector.add(String.valueOf(userIndex));
            vector.add(userData.userId);
            vector.add(userData.userName);
            vector.add(userData.cardNo);
            
            tableModel.addRow(vector);
        }
            
        public void clearData() {
            realRows = 0;
            userIndex = 0;
            tableModel.setRowCount(0);
            tableModel.setRowCount(QUERY_SHOW_COUNT);
            table.updateUI();
            prePageBtn.setEnabled(false);
            nextPageBtn.setEnabled(false);
        }
        
        private JTable table = null;
        private DefaultTableModel tableModel = null;
        public JButton prePageBtn;
        public JButton nextPageBtn;
    }
    
    /**
     * 门禁事件显示界面
     * */
    public class EventInfoShowPanel extends JPanel {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private static final int INDEX = 0;
        private static final int USER_ID = 1;
        private static final int CARD_NO = 2;
        private static final int EVENT_TIME = 3;
        private static final int DOOR_OPEN_METHOD = 4;
        
        private final static int MIN_SHOW_LINES = 17;
        private final static int MAX_SHOW_LINES = 50;
        
        public EventInfoShowPanel() {
            BorderEx.set(this, Res.string().getEventInfo(), 1);
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(395, 400));
            
            Vector<String> columnNames =  new Vector<String>();
            columnNames.add(Res.string().getIndex());             // 序号
            columnNames.add(Res.string().getUserId());             // 用户编号
            columnNames.add(Res.string().getCardNo());             // 卡号
            columnNames.add(Res.string().getEventTime());         // 事件时间
            columnNames.add(Res.string().getDoorOpenMethod());     // 开门方式
            
            tableModel = new DefaultTableModel(null, columnNames);
            table = new JTable(tableModel) {
                private static final long serialVersionUID = 1L;
 
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return false;
                }
            };
 
            tableModel.setRowCount(MIN_SHOW_LINES);    // 设置最小显示行
            
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行
            
            table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
            table.getColumnModel().getColumn(USER_ID).setPreferredWidth(150);
            table.getColumnModel().getColumn(CARD_NO).setPreferredWidth(150);
            table.getColumnModel().getColumn(EVENT_TIME).setPreferredWidth(150);
            table.getColumnModel().getColumn(DOOR_OPEN_METHOD).setPreferredWidth(120);
            
            ((DefaultTableCellRenderer)
                    table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
            
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            
            JScrollPane scrollPane = new JScrollPane(table);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            add(scrollPane, BorderLayout.CENTER);
        }
        
        public void clearEvent() {
            eventIndex = 0;
            tableModel.setRowCount(0);
            tableModel.setRowCount(MIN_SHOW_LINES);
            table.updateUI();
        }
        
        public void insertEvent(AccessEventInfo accessEventInfo) {
            if (accessEventInfo == null) {
                return;
            }
            ++eventIndex;
            tableModel.insertRow(0, convertEventData(accessEventInfo));
            if (eventIndex <= MIN_SHOW_LINES) {
                tableModel.setRowCount(MIN_SHOW_LINES);
            }else if (eventIndex >= MAX_SHOW_LINES){
                tableModel.setRowCount(MAX_SHOW_LINES);
            }
            
            table.updateUI();
        }
        
        private Vector<String> convertEventData(AccessEventInfo accessEventInfo) {    
            Vector<String> vector = new Vector<String>();
            vector.add(String.valueOf(eventIndex));
            vector.add(accessEventInfo.userId);
            vector.add(accessEventInfo.cardNo);
            vector.add(accessEventInfo.eventTime.replace("/", "-"));
            String openDoor = openDoorMethodMap.get(accessEventInfo.openDoorMethod);
            if (openDoor == null) {
                openDoor = Res.string().getUnKnow();
            }
            vector.add(openDoor);
        
            return vector;
        }
        
        private JTable table = null;
        private DefaultTableModel tableModel = null;
    }
    
    private static HashMap<Integer, String> openDoorMethodMap = new HashMap<Integer, String>() {
        
        private static final long serialVersionUID = 1L;
 
        {
            put(NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_FINGERPRINT, Res.string().getFingerPrint());
            put(NET_ACCESS_DOOROPEN_METHOD.NET_ACCESS_DOOROPEN_METHOD_CARD, Res.string().getCard());
        }
    };
    
    public UserInfoShowPanel userShowPanel;
    public EventInfoShowPanel eventShowPanel;
    
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        Res.string().switchLanguage(LanguageType.English);
        
        AttendanceShowPanel demo = new AttendanceShowPanel();
        JFrame frame = new JFrame();
        frame.setSize(800, 560);
        frame.add(demo);
        System.out.println("AttendanceShowPanel Test");
        frame.setVisible(true);        
    }
}