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
package com.netsdk.demo.frame.Attendance;
 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
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.JTextArea;
import javax.swing.JTextField;
 
import com.netsdk.common.Base64;
import com.netsdk.common.BorderEx;
import com.netsdk.common.Res;
import com.netsdk.demo.module.AttendanceModule;
import com.netsdk.demo.module.AttendanceModule.OPERATE_TYPE;
import com.netsdk.demo.module.AttendanceModule.UserData;
 
/**
 * 通过信息ID操作信息对话框
 */
public class OperateByFingerPrintIdDialog extends JDialog{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public OperateByFingerPrintIdDialog() {
        setTitle(Res.string().getOperateByFingerPrintId());
        setLayout(new BorderLayout());
        setModal(true);
        pack();
        setSize(600, 500);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        ////////// 查询条件 /////////////////
        JLabel fingerPrintIdLabel = new JLabel(Res.string().getFingerPrintId(), JLabel.CENTER);
        fingerPrintIdTextField = new JTextField();
        fingerPrintIdLabel.setPreferredSize(new Dimension(85, 20));
        fingerPrintIdTextField.setPreferredSize(new Dimension(100, 20));
        
        ////////// 信息功能 /////////////////
        searchFingerPrintBtn = new JButton(Res.string().getSearchFingerPrint());
        deleteFingerPrintBtn = new JButton(Res.string().getDeleteFingerPrint());
        
        searchFingerPrintBtn.setPreferredSize(new Dimension(140, 20));
        deleteFingerPrintBtn.setPreferredSize(new Dimension(140, 20));
 
        JPanel functionPanel = new JPanel();
        BorderEx.set(functionPanel, Res.string().getOperateByFingerPrintId(), 1);
        functionPanel.add(fingerPrintIdLabel);
        functionPanel.add(fingerPrintIdTextField);
        functionPanel.add(searchFingerPrintBtn);
        functionPanel.add(deleteFingerPrintBtn);
        
        //////////信息信息 /////////////////
        JPanel fingerPrintPanel = new JPanel();
        BorderEx.set(fingerPrintPanel, Res.string().getFingerPrintInfo(), 1);
        fingerPrintPanel.setLayout(null);
        JLabel userIdLabel = new JLabel(Res.string().getUserId());
        userId = new JLabel();
        JLabel fingerPrintDataLabel = new JLabel(Res.string().getFingerPrintData());
        fingerPrintData = new JTextArea();
        fingerPrintData.setBackground(null);
        fingerPrintData.setEditable(false);
        fingerPrintData.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(fingerPrintData);
        userIdLabel.setBounds(30, 30, 90, 20);
        userId.setBounds(150, 30, 300, 20);
        fingerPrintDataLabel.setBounds(30, 60, 150, 20);
        fingerPrintData.setBounds(30, 80, 600, 20);
        scrollPane.setBounds(30, 80, 550, 300);
        scrollPane.setBorder(null);
        
        fingerPrintPanel.add(userIdLabel);
        fingerPrintPanel.add(userId);
        fingerPrintPanel.add(fingerPrintDataLabel);
        fingerPrintPanel.add(scrollPane);
        
        add(functionPanel, BorderLayout.NORTH);
        add(fingerPrintPanel, BorderLayout.CENTER);
        
        fingerPrintIdTextField.addKeyListener(new KeyListener() {
            
            public void keyTyped(KeyEvent e) {
                  int key = e.getKeyChar();
                  if (key < 48 || key > 57) {
                      e.consume();
                  }
            }
 
            public void keyPressed(KeyEvent e) {}
            
            public void keyReleased(KeyEvent e) {}
        });
        
        listener = new FingerPrintIdOperateActionListener();
        searchFingerPrintBtn.addActionListener(listener);
        deleteFingerPrintBtn.addActionListener(listener);
    }
    
    public String getFingerPrintId() {
        
        if (fingerPrintIdTextField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, Res.string().getInput()+Res.string().getFingerPrintId(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return null;
        }
        
        try {
            Integer.parseInt(fingerPrintIdTextField.getText());
        }catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, Res.string().getFingerPrintIdIllegal(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return null;
        }
        return fingerPrintIdTextField.getText();
    }
    
    public void searchFingerPrint() {
        clearFingerPrintInfo();
        
        String fingerPrintId = getFingerPrintId();
        if (fingerPrintId == null) {
            return;
        }
        UserData userData = AttendanceModule.getFingerRecord(Integer.parseInt(fingerPrintId));
        if (userData == null) {
            JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (userData.szFingerPrintInfo[0].length == 0) {
            JOptionPane.showMessageDialog(null, Res.string().getFingerPrintIdNotExist(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        dealFingerPrintInfo(userData);
    }
    
    public void dealFingerPrintInfo(UserData userData) {
        userId.setText(userData.userId);
        fingerPrintData.setText(formatFingerPrintData(userData.szFingerPrintInfo[0]));
    }
    
    private String formatFingerPrintData(byte[] fingerPrintData) {
        String formatData = Base64.getEncoder().encodeToString(fingerPrintData);
        return formatData;
    }
    
    public void clearFingerPrintInfo() {
        userId.setText("");
        fingerPrintData.setText("");
    }
    
    /**
     * 按键监听实现类
     */
    private class FingerPrintIdOperateActionListener implements ActionListener {    
 
        @Override
        public void actionPerformed(ActionEvent arg0) {
 
            OPERATE_TYPE emType = getOperateType(arg0.getSource());
            switch(emType) {
                case SEARCH_FINGERPRINT_BY_ID:
                    searchFingerPrint();
                    break;
                case DELETE_FINGERPRINT_BY_ID:
                    String fingerPrintId = getFingerPrintId();
                    if (fingerPrintId == null) {
                        return;
                    }
                    new AttendanceOperateShareDialog(emType, fingerPrintId).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_ID;
            }else if (btn == deleteFingerPrintBtn) {    // 删除信息
                type = OPERATE_TYPE.DELETE_FINGERPRINT_BY_ID;
            }else {
                System.err.println("Unknown Event: " + btn);
            }
            
            return type;
            
        }
    }
    
    private JTextField fingerPrintIdTextField;
    public JButton searchFingerPrintBtn;
    private JButton deleteFingerPrintBtn;
    private JLabel userId;
    private JTextArea fingerPrintData;
    private FingerPrintIdOperateActionListener listener;
}