dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
提交 | 用户 | 时间
ce910c 1 package com.netsdk.demo.frame;
H 2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.GridLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.ItemEvent;
9 import java.awt.event.ItemListener;
10 import java.awt.event.WindowAdapter;
11 import java.awt.event.WindowEvent;
12
13 import javax.swing.JButton;
14 import javax.swing.JCheckBox;
15 import javax.swing.JDialog;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 import javax.swing.JOptionPane;
19 import javax.swing.JPanel;
20 import javax.swing.JSplitPane;
21 import javax.swing.SwingUtilities;
22 import javax.swing.UIManager;
23
24 import com.sun.jna.Pointer;
25
26 import com.netsdk.common.BorderEx;
27 import com.netsdk.common.DateChooserJButton;
28 import com.netsdk.common.FunctionList;
29 import com.netsdk.common.LoginPanel;
30 import com.netsdk.common.Res;
31 import com.netsdk.demo.module.DeviceControlModule;
32 import com.netsdk.demo.module.LoginModule;
33 import com.netsdk.lib.NetSDKLib;
34 import com.netsdk.lib.NetSDKLib.LLong;
35 import com.netsdk.lib.ToolKits;
36
37 /**
38  * Device Control Demo
39  */
40 class DeviceControlFrame extends JFrame {
41     private static final long serialVersionUID = 1L;
42     
43     // device disconnect callback instance
44     private DisConnect disConnect = new DisConnect(); 
45     
46     // device control frame (this)
47     private static JFrame frame = new JFrame();
48     
49     public DeviceControlFrame() {
50         setTitle(Res.string().getDeviceControl());
51         setLayout(new BorderLayout());
52         pack();
53         setSize(550, 350);
54         setResizable(false);
55         setLocationRelativeTo(null);
56         LoginModule.init(disConnect, null);
57         
58         try {
59             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
60         } catch (Exception e) {
61             e.printStackTrace();
62         } 
63      
64         loginPanel = new DeviceControlLoginPanel();
65         deviceCtlPanel = new DeviceControlPanel();
66         
67         add(loginPanel, BorderLayout.NORTH);
68         add(deviceCtlPanel, BorderLayout.CENTER);
69         
70         loginPanel.addLoginBtnActionListener(new ActionListener() {
71             @Override
72             public void actionPerformed(ActionEvent e) {               
73                 if(loginPanel.checkLoginText()) {
74                     if(login()) {
75                         frame = ToolKits.getFrame(e);
76                         frame.setTitle(Res.string().getDeviceControl() + " : " + Res.string().getOnline());
77                     }
78                 }
79             }
80         });
81         
82         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
83             @Override
84             public void actionPerformed(ActionEvent e) {
85                 frame.setTitle(Res.string().getDeviceControl());
86                 logout();    
87             }
88         });
89         
90         addWindowListener(new WindowAdapter() {
91             public void windowClosing(WindowEvent e) {
92                 LoginModule.logout();
93                 LoginModule.cleanup();
94                 dispose();    
95                 
96                 SwingUtilities.invokeLater(new Runnable() {
97                     public void run() {
98                         FunctionList demo = new FunctionList();
99                         demo.setVisible(true);
100                     }
101                 });
102             }
103         });
104     }
105     
106     /////////////////function///////////////////
107     // device disconnect callback class 
108     // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
109     private class DisConnect implements NetSDKLib.fDisConnect {
110         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
111             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
112
113             SwingUtilities.invokeLater(new Runnable() {
114                 public void run() {
115                     JOptionPane.showMessageDialog(null, Res.string().getDisConnect(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
116                     frame.setTitle(Res.string().getDeviceControl());
117                     logout();
118                 }
119             });
120         }
121     }
122
123     public boolean login() {
124
125         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
126                     Integer.parseInt(loginPanel.portTextArea.getText()), 
127                     loginPanel.nameTextArea.getText(), 
128                     new String(loginPanel.passwordTextArea.getPassword()))) {
129         
130                 loginPanel.setButtonEnable(true);
131                 deviceCtlPanel.setButtonEnabled(true);
132                                 
133         }else {
134             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
135             return false;
136         }
137         
138         return true;
139     }
140
141     public void logout() {
142         
143         LoginModule.logout();
144         
145         loginPanel.setButtonEnable(false);
146         deviceCtlPanel.resetButtonEnabled();
147     }
148     
149     private class DeviceControlPanel extends JPanel {
150         private static final long serialVersionUID = 1L;
151         
152         public DeviceControlPanel() {
153             BorderEx.set(this, Res.string().getDeviceControl(), 2);
154             setLayout(new BorderLayout());
155             setPreferredSize(new Dimension(350, 220));
156             setResizable(false);
157             JLabel nullLable = new JLabel();
158             
159             currentTimeCheckBox = new JCheckBox(Res.string().getCurrentTime());
160             
161             getDateChooser = new DateChooserJButton();
162             setDateChooser = new DateChooserJButton(2000, 2037);
163             
164             rebootBtn = new JButton(Res.string().getReboot());
165             getTimeBtn = new JButton(Res.string().getGetTime());
166             setTimeBtn = new JButton(Res.string().getSetTime());
167             
168             nullLable.setPreferredSize(currentTimeCheckBox.getPreferredSize());
169             getDateChooser.setPreferredSize(new Dimension(150, 20));
170             setDateChooser.setPreferredSize(new Dimension(150, 20));
171             rebootBtn.setPreferredSize(new Dimension(100, 20));
172             getTimeBtn.setPreferredSize(new Dimension(100, 20));
173             setTimeBtn.setPreferredSize(new Dimension(100, 20));
174             
175             JPanel rebootPanel = new JPanel();
176             BorderEx.set(rebootPanel, Res.string().getDeviceReboot(), 2);
177             
178             rebootPanel.add(rebootBtn);
179             
180             JPanel timePanel = new JPanel(new GridLayout(2,1));
181             BorderEx.set(timePanel, Res.string().getSyncTime(), 2);
182             
183             JPanel getPanel = new JPanel();
184             JPanel setPanel = new JPanel();
185             
186             getPanel.add(nullLable);
187             getPanel.add(getDateChooser);
188             getPanel.add(getTimeBtn);
189             
190             setPanel.add(currentTimeCheckBox);
191             setPanel.add(setDateChooser);
192             setPanel.add(setTimeBtn);
193             
194             timePanel.add(getPanel);
195             timePanel.add(setPanel);
196             
197             JSplitPane splitPane = new JSplitPane();
198             splitPane.setDividerSize(0);
199             splitPane.setBorder(null);
200             splitPane.add(rebootPanel, JSplitPane.LEFT);
201             splitPane.add(timePanel, JSplitPane.RIGHT);
202              add(splitPane);
203             
204             getDateChooser.setEnabled(false);
205             setButtonEnabled(false);
206
207             rebootBtn.addActionListener(new ActionListener() {            
208                 @Override
209                 public void actionPerformed(ActionEvent e) {
210                     
211                     OptionDialog optionDialog = new OptionDialog();
212                     optionDialog.setVisible(true);
213                 }
214             });
215             
216             getTimeBtn.addActionListener(new ActionListener() {            
217                 @Override
218                 public void actionPerformed(ActionEvent e) {
219                     String date = DeviceControlModule.getTime();
220                     if (date == null) {
221                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
222                     }else {
223                         getDateChooser.setText(date);
224                     }
225                     
226                 }
227             });
228             
229             setTimeBtn.addActionListener(new ActionListener() {            
230                 @Override
231                 public void actionPerformed(ActionEvent e) {
232                     String date = null;
233                     if (!currentTimeCheckBox.isSelected()) {
234                         date = setDateChooser.getText();
235                     }
236                     if (!DeviceControlModule.setTime(date)) {
237                         JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
238                     }
239                     else {
240                         JOptionPane.showMessageDialog(null, Res.string().getOperateSuccess(), Res.string().getPromptMessage(), JOptionPane.PLAIN_MESSAGE);
241                     }
242                 }
243             });
244             
245             currentTimeCheckBox.addItemListener(new ItemListener() {
246                 @Override
247                 public void itemStateChanged(ItemEvent e) {
248                     JCheckBox jcb = (JCheckBox)e.getItem();
249                     if (jcb.isSelected()) {
250                         setDateChooser.setEnabled(false);
251                     }else {
252                         setDateChooser.setEnabled(true);
253                     }
254                 }
255             });
256             
257         }
258         
259         public void setButtonEnabled(boolean b) {
260             
261             currentTimeCheckBox.setEnabled(b);
262             setDateChooser.setEnabled(b);
263             rebootBtn.setEnabled(b);
264             getTimeBtn.setEnabled(b);
265             setTimeBtn.setEnabled(b);
266         }
267         
268         public void resetButtonEnabled() {
269             currentTimeCheckBox.setSelected(false);
270             setButtonEnabled(false);
271         }
272         
273         private class OptionDialog extends JDialog {
274             private static final long serialVersionUID = 1L;
275
276             public OptionDialog() {
277                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
278                 pack();
279                 setSize(250, 100);
280                 setLocationRelativeTo(null);
281                 setModal(true);
282                 setTitle(Res.string().getDeviceReboot());
283                 
284                 JLabel messageLable = new JLabel(Res.string().getRebootTips()); 
285                 confirmBtn = new JButton(Res.string().getConfirm());
286                 cancelBtn = new JButton(Res.string().getCancel());
287                 
288                 JPanel messagePanel = new JPanel();
289                 messagePanel.add(messageLable);
290                 
291                 JPanel btnPanel = new JPanel();
292                 btnPanel.add(cancelBtn);
293                 btnPanel.add(confirmBtn);
294                 
295                 add(messagePanel, BorderLayout.NORTH);
296                 add(btnPanel, BorderLayout.CENTER);
297                 
298                 addListener();
299                 
300             }
301             
302             private void addListener() {
303               confirmBtn.addActionListener(new ActionListener() {
304
305                     @Override
306                     public void actionPerformed(ActionEvent e) {
307                         cancelBtn.setEnabled(false);
308                         if (!DeviceControlModule.reboot()) {
309                             JOptionPane.showMessageDialog(null, ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
310                         }
311                         else {
312                             JOptionPane.showMessageDialog(null, Res.string().getOperateSuccess(), Res.string().getPromptMessage(), JOptionPane.PLAIN_MESSAGE);
313                         }
314                         dispose();
315                     }
316                 });
317                 
318                 cancelBtn.addActionListener(new ActionListener() {
319
320                     @Override
321                     public void actionPerformed(ActionEvent e) {
322                         dispose();
323                     }
324                 });
325             }
326             
327             private JButton confirmBtn;
328             private JButton cancelBtn;
329             
330         }
331         
332         private JButton rebootBtn;
333         private DateChooserJButton getDateChooser;
334         private JButton getTimeBtn;
335         private JCheckBox currentTimeCheckBox;
336         private DateChooserJButton setDateChooser;
337         private JButton setTimeBtn; 
338     }
339
340     private class DeviceControlLoginPanel extends LoginPanel {
341     
342         private static final long serialVersionUID = 1L;
343
344         public DeviceControlLoginPanel() {
345             setLayout(new GridLayout(3, 1));
346             removeAll();
347             JPanel ipPanel = new JPanel();
348             JPanel userPanel = new JPanel();
349             JPanel btnPanel = new JPanel();
350             
351             resetSize();
352     
353             ipPanel.add(ipLabel);
354             ipPanel.add(ipTextArea);
355             ipPanel.add(portLabel);
356             ipPanel.add(portTextArea);
357             
358             userPanel.add(nameLabel);
359             userPanel.add(nameTextArea);
360             userPanel.add(passwordLabel);
361             userPanel.add(passwordTextArea);
362                 
363             btnPanel.add(loginBtn);
364             btnPanel.add(new JLabel("  "));
365             btnPanel.add(logoutBtn);
366
367             add(ipPanel);
368             add(userPanel);
369             add(btnPanel);
370         }
371         
372         private void resetSize() {
373             
374             ipLabel.setPreferredSize(new Dimension(70, 25));
375             portLabel.setPreferredSize(new Dimension(70, 25));
376             nameLabel.setText(Res.string().getUserName());
377             nameLabel.setPreferredSize(new Dimension(70, 25));
378             passwordLabel.setPreferredSize(new Dimension(70, 25));
379
380             loginBtn.setPreferredSize(new Dimension(100, 20));
381             logoutBtn.setPreferredSize(new Dimension(100, 20));
382             
383             ipTextArea.setPreferredSize(new Dimension(100, 20));
384             portTextArea.setPreferredSize(new Dimension(100, 20));
385             nameTextArea.setPreferredSize(new Dimension(100, 20));
386             passwordTextArea.setPreferredSize(new Dimension(100, 20));
387         }
388     }
389     
390     private DeviceControlLoginPanel loginPanel;
391     
392     private DeviceControlPanel deviceCtlPanel;
393     
394 }
395
396 public class DeviceControl {  
397     public static void main(String[] args) {    
398         SwingUtilities.invokeLater(new Runnable() {
399             public void run() {    
400                 DeviceControlFrame demo = new DeviceControlFrame();
401                 demo.setVisible(true);
402             }
403         });        
404     }
405 };
406