Jay
2024-11-08 02722a3f9eca857ce7fffea352e9f7ee692a1b71
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
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 java.util.Vector;
 
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
 
import com.iailab.netsdk.common.Base64;
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;
 
public class OperateByUserIdDialog extends JDialog{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private UserData userData;
    
    public OperateByUserIdDialog(UserData userData) {
        setTitle(Res.string().getOperateByUserId());
        setLayout(new BorderLayout());
        setModal(true);
        pack();
        setSize(570, 383);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        ////////// 用户信息 (不可改变)/////////////////
        JPanel userInfoPanel = new JPanel();
        userInfoPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        BorderEx.set(userInfoPanel, Res.string().getUserInfo(), 2);
 
        JLabel userIdLabel = new JLabel(Res.string().getUserId(), JLabel.CENTER);
        JTextField userIdTextField = new JTextField(userData.userId);
        JLabel userNameLabel = new JLabel(Res.string().getUserName(true), JLabel.CENTER);
        JTextField userNameTextField = new JTextField(userData.userName);
        JLabel cardNoLabel = new JLabel(Res.string().getCardNo(), JLabel.CENTER);
        JTextField cardNoTextField = new JTextField(userData.cardNo);
        
        userIdTextField.setEnabled(false);
        userNameTextField.setEnabled(false);
        cardNoTextField.setEnabled(false);
        
        Dimension dimLable = new Dimension(55, 20);
        userIdLabel.setPreferredSize(dimLable);
        userNameLabel.setPreferredSize(dimLable);
        cardNoLabel.setPreferredSize(dimLable);
        Dimension dimValue = new Dimension(100, 20);
        userIdTextField.setPreferredSize(dimValue);
        userNameTextField.setPreferredSize(dimValue);
        cardNoTextField.setPreferredSize(dimValue);
        
        userInfoPanel.add(userIdLabel);
        userInfoPanel.add(userIdTextField);
        userInfoPanel.add(userNameLabel);
        userInfoPanel.add(userNameTextField);
        userInfoPanel.add(cardNoLabel);
        userInfoPanel.add(cardNoTextField);
        
        ////////// 信息功能 /////////////////
        JPanel functionPanel = new JPanel();
        BorderEx.set(functionPanel, Res.string().getOperateByUserId(), 2);
        searchFingerPrintBtn = new JButton(Res.string().getSearchFingerPrint());
        addFingerPrintBtn = new JButton(Res.string().getAddFingerPrint());
        deleteFingerPrintBtn = new JButton(Res.string().getDeleteFingerPrint());
        
        searchFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
        addFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
        deleteFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
 
        functionPanel.add(searchFingerPrintBtn);
        functionPanel.add(addFingerPrintBtn);
        functionPanel.add(deleteFingerPrintBtn);
        
        //////////布局 /////////////////
        JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        splitPane.setDividerSize(0);
        splitPane.setBorder(null);
        splitPane.add(userInfoPanel, JSplitPane.TOP);
        splitPane.add(functionPanel, JSplitPane.BOTTOM);
         add(splitPane, BorderLayout.NORTH);
        
         fingerPrintShowPanel = new FingerPrintShowPanel();
         add(fingerPrintShowPanel, BorderLayout.CENTER);
         
        listener = new UserIdOperateActionListener();
        searchFingerPrintBtn.addActionListener(listener);
        addFingerPrintBtn.addActionListener(listener);
        deleteFingerPrintBtn.addActionListener(listener);
        
        this.userData = userData;
    }
    
    public void searchFingerPrint() {
        clearTable();
        boolean bSuccess = AttendanceModule.getFingerByUserId(userData.userId, userData);
        if (bSuccess){
            fingerPrintShowPanel.insertData(userData);
        } else {
            JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
        }
    }
    
    public void clearTable() {
        fingerPrintShowPanel.clearData();
    }
    
    public void addFingerPrint(int fingerPrintId, byte[] fingerPrintData) {
        fingerPrintShowPanel.insertData(fingerPrintId, fingerPrintData);
    }
    
    /**
     * 按键监听实现类
     */
    private class UserIdOperateActionListener implements ActionListener {    
 
        @Override
        public void actionPerformed(ActionEvent arg0) {
 
            OPERATE_TYPE emType = getOperateType(arg0.getSource());
            switch(emType) {
                case SEARCH_FINGERPRINT_BY_USERID:
                    searchFingerPrint();
                    break;
                case ADD_FINGERPRINT:
                    new AddFingerPrintDialog(userData.userId).setVisible(true);
                    break;
                case DELETE_FINGERPRINT_BY_USERID:
                    new AttendanceOperateShareDialog(emType, userData).setVisible(true);
                    break;
                default:
                    break;
            }
        }
        
        private OPERATE_TYPE getOperateType(Object btn) {
            OPERATE_TYPE type = OPERATE_TYPE.UNKNOWN;
            if (btn == searchFingerPrintBtn) { // 查找信息
                type = OPERATE_TYPE.SEARCH_FINGERPRINT_BY_USERID;
            }else if (btn == addFingerPrintBtn) {    // 添加信息
                type = OPERATE_TYPE.ADD_FINGERPRINT;
            }else if (btn == deleteFingerPrintBtn) {    // 删除信息(用户ID)
                type = OPERATE_TYPE.DELETE_FINGERPRINT_BY_USERID;
            }else {
                System.err.println("Unknown Event: " + btn);
            }
            
            return type;
            
        }
    }
    
    /**
     * 信息信息显示界面
     * */
    public class FingerPrintShowPanel 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 FINGERPRINT_ID = 2;
        public static final int FINGERPRINT_DATA = 3;
        
        public final static int MAX_FINGERPRINT_NUM = 10; // 最大信息个数, 也做为显示个数
        private int realRows = 0;        // 实际显示个数
        
        public FingerPrintShowPanel() {
            BorderEx.set(this, Res.string().getFingerPrintInfo(), 1);
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(550, 375));
            Vector<String> columnNames =  new Vector<String>();
            columnNames.add(Res.string().getIndex());             // 序号
            columnNames.add(Res.string().getUserId());             // 用户编号
            columnNames.add(Res.string().getFingerPrintId());    // 信息ID
            columnNames.add(Res.string().getFingerPrintData());    // 信息
            
            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(MAX_FINGERPRINT_NUM);    // 设置最小显示行
            
            table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行(其实无意义)
            
            table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
            table.getColumnModel().getColumn(USER_ID).setPreferredWidth(100);
            table.getColumnModel().getColumn(FINGERPRINT_ID).setPreferredWidth(100);
            table.getColumnModel().getColumn(FINGERPRINT_DATA).setPreferredWidth(8888);
            
            ((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 insertData(UserData userData) {
            if (userData.nFingerPrintIDs == null) {
                return;
            }
            
            clearData();
            tableModel.setRowCount(0);
            for (int i = 0; i < userData.nFingerPrintIDs.length; ++i) {
                insertFingerPrintData(userData.nFingerPrintIDs[i], userData.szFingerPrintInfo[i]);
            }
            
            tableModel.setRowCount(MAX_FINGERPRINT_NUM);
            table.updateUI();
        }
        
        public void insertData(int fingerPrintId, byte[] fingerPrintData) {
            tableModel.setRowCount(realRows);
            insertFingerPrintData(fingerPrintId, fingerPrintData);
            tableModel.setRowCount(MAX_FINGERPRINT_NUM);
            table.updateUI();
        }
        
        private void insertFingerPrintData(int fingerPrintId, byte[] fingerPrintData) {
            ++realRows;
            Vector<String> vector = new Vector<String>();
            vector.add(String.valueOf(realRows));
            vector.add(userData.userId);
            vector.add(String.valueOf(fingerPrintId));
            vector.add(formatFingerPrintData(fingerPrintData));
            tableModel.addRow(vector);
        }
        
        private String formatFingerPrintData(byte[] fingerPrintData) {
            String formatData = Base64.getEncoder().encodeToString(fingerPrintData);
            return formatData;
        }
            
        public void clearData() {
            realRows = 0;
            tableModel.setRowCount(0);
            tableModel.setRowCount(MAX_FINGERPRINT_NUM);
            table.updateUI();
        }
        
        private JTable table = null;
        private DefaultTableModel tableModel = null;
    }
    
    public JButton searchFingerPrintBtn;
    private JButton addFingerPrintBtn;
    private JButton deleteFingerPrintBtn;
    private UserIdOperateActionListener listener;
    private FingerPrintShowPanel fingerPrintShowPanel;
}