潘志宝
5 天以前 4d7e3bb9a93ac0bdba9075e5efa536a165f8aae9
提交 | 用户 | 时间
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 import java.util.Vector;
9
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JLabel;
13 import javax.swing.JOptionPane;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JSplitPane;
17 import javax.swing.JTable;
18 import javax.swing.JTextField;
19 import javax.swing.ListSelectionModel;
20 import javax.swing.table.DefaultTableCellRenderer;
21 import javax.swing.table.DefaultTableModel;
22
23 import com.iailab.netsdk.common.Base64;
24 import com.iailab.netsdk.common.BorderEx;
25 import com.iailab.netsdk.common.Res;
26 import com.iailab.netsdk.demo.module.AttendanceModule;
27 import com.iailab.netsdk.demo.module.AttendanceModule.OPERATE_TYPE;
28 import com.iailab.netsdk.demo.module.AttendanceModule.UserData;
29
30 public class OperateByUserIdDialog extends JDialog{
31     /**
32      * 
33      */
34     private static final long serialVersionUID = 1L;
35     private UserData userData;
36     
37     public OperateByUserIdDialog(UserData userData) {
38         setTitle(Res.string().getOperateByUserId());
39         setLayout(new BorderLayout());
40         setModal(true);
41         pack();
42         setSize(570, 383);
43         setResizable(false);
44         setLocationRelativeTo(null);
45         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
46         
47         ////////// 用户信息 (不可改变)/////////////////
48         JPanel userInfoPanel = new JPanel();
49         userInfoPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
50         BorderEx.set(userInfoPanel, Res.string().getUserInfo(), 2);
51
52         JLabel userIdLabel = new JLabel(Res.string().getUserId(), JLabel.CENTER);
53         JTextField userIdTextField = new JTextField(userData.userId);
54         JLabel userNameLabel = new JLabel(Res.string().getUserName(true), JLabel.CENTER);
55         JTextField userNameTextField = new JTextField(userData.userName);
56         JLabel cardNoLabel = new JLabel(Res.string().getCardNo(), JLabel.CENTER);
57         JTextField cardNoTextField = new JTextField(userData.cardNo);
58         
59         userIdTextField.setEnabled(false);
60         userNameTextField.setEnabled(false);
61         cardNoTextField.setEnabled(false);
62         
63         Dimension dimLable = new Dimension(55, 20);
64         userIdLabel.setPreferredSize(dimLable);
65         userNameLabel.setPreferredSize(dimLable);
66         cardNoLabel.setPreferredSize(dimLable);
67         Dimension dimValue = new Dimension(100, 20);
68         userIdTextField.setPreferredSize(dimValue);
69         userNameTextField.setPreferredSize(dimValue);
70         cardNoTextField.setPreferredSize(dimValue);
71         
72         userInfoPanel.add(userIdLabel);
73         userInfoPanel.add(userIdTextField);
74         userInfoPanel.add(userNameLabel);
75         userInfoPanel.add(userNameTextField);
76         userInfoPanel.add(cardNoLabel);
77         userInfoPanel.add(cardNoTextField);
78         
79         ////////// 信息功能 /////////////////
80         JPanel functionPanel = new JPanel();
81         BorderEx.set(functionPanel, Res.string().getOperateByUserId(), 2);
82         searchFingerPrintBtn = new JButton(Res.string().getSearchFingerPrint());
83         addFingerPrintBtn = new JButton(Res.string().getAddFingerPrint());
84         deleteFingerPrintBtn = new JButton(Res.string().getDeleteFingerPrint());
85         
86         searchFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
87         addFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
88         deleteFingerPrintBtn.setPreferredSize(new Dimension(150, 20));
89
90         functionPanel.add(searchFingerPrintBtn);
91         functionPanel.add(addFingerPrintBtn);
92         functionPanel.add(deleteFingerPrintBtn);
93         
94         //////////布局 /////////////////
95         JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
96         splitPane.setDividerSize(0);
97         splitPane.setBorder(null);
98         splitPane.add(userInfoPanel, JSplitPane.TOP);
99         splitPane.add(functionPanel, JSplitPane.BOTTOM);
100          add(splitPane, BorderLayout.NORTH);
101         
102          fingerPrintShowPanel = new FingerPrintShowPanel();
103          add(fingerPrintShowPanel, BorderLayout.CENTER);
104          
105         listener = new UserIdOperateActionListener();
106         searchFingerPrintBtn.addActionListener(listener);
107         addFingerPrintBtn.addActionListener(listener);
108         deleteFingerPrintBtn.addActionListener(listener);
109         
110         this.userData = userData;
111     }
112     
113     public void searchFingerPrint() {
114         clearTable();
115         boolean bSuccess = AttendanceModule.getFingerByUserId(userData.userId, userData);
116         if (bSuccess){
117             fingerPrintShowPanel.insertData(userData);
118         } else {
119             JOptionPane.showMessageDialog(null, Res.string().getFailed(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
120         }
121     }
122     
123     public void clearTable() {
124         fingerPrintShowPanel.clearData();
125     }
126     
127     public void addFingerPrint(int fingerPrintId, byte[] fingerPrintData) {
128         fingerPrintShowPanel.insertData(fingerPrintId, fingerPrintData);
129     }
130     
131     /**
132      * 按键监听实现类
133      */
134     private class UserIdOperateActionListener implements ActionListener {    
135
136         @Override
137         public void actionPerformed(ActionEvent arg0) {
138
139             OPERATE_TYPE emType = getOperateType(arg0.getSource());
140             switch(emType) {
141                 case SEARCH_FINGERPRINT_BY_USERID:
142                     searchFingerPrint();
143                     break;
144                 case ADD_FINGERPRINT:
145                     new AddFingerPrintDialog(userData.userId).setVisible(true);
146                     break;
147                 case DELETE_FINGERPRINT_BY_USERID:
148                     new AttendanceOperateShareDialog(emType, userData).setVisible(true);
149                     break;
150                 default:
151                     break;
152             }
153         }
154         
155         private OPERATE_TYPE getOperateType(Object btn) {
156             OPERATE_TYPE type = OPERATE_TYPE.UNKNOWN;
157             if (btn == searchFingerPrintBtn) { // 查找信息
158                 type = OPERATE_TYPE.SEARCH_FINGERPRINT_BY_USERID;
159             }else if (btn == addFingerPrintBtn) {    // 添加信息
160                 type = OPERATE_TYPE.ADD_FINGERPRINT;
161             }else if (btn == deleteFingerPrintBtn) {    // 删除信息(用户ID)
162                 type = OPERATE_TYPE.DELETE_FINGERPRINT_BY_USERID;
163             }else {
164                 System.err.println("Unknown Event: " + btn);
165             }
166             
167             return type;
168             
169         }
170     }
171     
172     /**
173      * 信息信息显示界面
174      * */
175     public class FingerPrintShowPanel extends JPanel {
176         /**
177          * 
178          */
179         private static final long serialVersionUID = 1L;
180         public static final int INDEX = 0;
181         public static final int USER_ID = 1;
182         public static final int FINGERPRINT_ID = 2;
183         public static final int FINGERPRINT_DATA = 3;
184         
185         public final static int MAX_FINGERPRINT_NUM = 10; // 最大信息个数, 也做为显示个数
186         private int realRows = 0;        // 实际显示个数
187         
188         public FingerPrintShowPanel() {
189             BorderEx.set(this, Res.string().getFingerPrintInfo(), 1);
190             setLayout(new BorderLayout());
191             setPreferredSize(new Dimension(550, 375));
192             Vector<String> columnNames =  new Vector<String>();
193             columnNames.add(Res.string().getIndex());             // 序号
194             columnNames.add(Res.string().getUserId());             // 用户编号
195             columnNames.add(Res.string().getFingerPrintId());    // 信息ID
196             columnNames.add(Res.string().getFingerPrintData());    // 信息
197             
198             tableModel = new DefaultTableModel(null, columnNames);
199             table = new JTable(tableModel) {
200                 private static final long serialVersionUID = 1L;
201
202                 public boolean isCellEditable(int rowIndex, int columnIndex) { // 不可编辑
203                     return false;
204                 }
205             };
206
207             tableModel.setRowCount(MAX_FINGERPRINT_NUM);    // 设置最小显示行
208             
209             table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 只能选中一行(其实无意义)
210             
211             table.getColumnModel().getColumn(INDEX).setPreferredWidth(80);
212             table.getColumnModel().getColumn(USER_ID).setPreferredWidth(100);
213             table.getColumnModel().getColumn(FINGERPRINT_ID).setPreferredWidth(100);
214             table.getColumnModel().getColumn(FINGERPRINT_DATA).setPreferredWidth(8888);
215             
216             ((DefaultTableCellRenderer)
217                     table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
218             
219             table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
220             
221             JScrollPane scrollPane = new JScrollPane(table);
222             scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
223             scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
224             
225             add(scrollPane, BorderLayout.CENTER);
226         }
227         
228         public void insertData(UserData userData) {
229             if (userData.nFingerPrintIDs == null) {
230                 return;
231             }
232             
233             clearData();
234             tableModel.setRowCount(0);
235             for (int i = 0; i < userData.nFingerPrintIDs.length; ++i) {
236                 insertFingerPrintData(userData.nFingerPrintIDs[i], userData.szFingerPrintInfo[i]);
237             }
238             
239             tableModel.setRowCount(MAX_FINGERPRINT_NUM);
240             table.updateUI();
241         }
242         
243         public void insertData(int fingerPrintId, byte[] fingerPrintData) {
244             tableModel.setRowCount(realRows);
245             insertFingerPrintData(fingerPrintId, fingerPrintData);
246             tableModel.setRowCount(MAX_FINGERPRINT_NUM);
247             table.updateUI();
248         }
249         
250         private void insertFingerPrintData(int fingerPrintId, byte[] fingerPrintData) {
251             ++realRows;
252             Vector<String> vector = new Vector<String>();
253             vector.add(String.valueOf(realRows));
254             vector.add(userData.userId);
255             vector.add(String.valueOf(fingerPrintId));
256             vector.add(formatFingerPrintData(fingerPrintData));
257             tableModel.addRow(vector);
258         }
259         
260         private String formatFingerPrintData(byte[] fingerPrintData) {
261             String formatData = Base64.getEncoder().encodeToString(fingerPrintData);
262             return formatData;
263         }
264             
265         public void clearData() {
266             realRows = 0;
267             tableModel.setRowCount(0);
268             tableModel.setRowCount(MAX_FINGERPRINT_NUM);
269             table.updateUI();
270         }
271         
272         private JTable table = null;
273         private DefaultTableModel tableModel = null;
274     }
275     
276     public JButton searchFingerPrintBtn;
277     private JButton addFingerPrintBtn;
278     private JButton deleteFingerPrintBtn;
279     private UserIdOperateActionListener listener;
280     private FingerPrintShowPanel fingerPrintShowPanel;
281 }