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 import java.util.Vector;
13
14 import javax.swing.DefaultComboBoxModel;
15 import javax.swing.JButton;
16 import javax.swing.JComboBox;
17 import javax.swing.JFrame;
18 import javax.swing.JLabel;
19 import javax.swing.JOptionPane;
20 import javax.swing.JPanel;
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.FunctionList;
28 import com.netsdk.common.LoginPanel;
29 import com.netsdk.common.Res;
30 import com.netsdk.demo.module.LoginModule;
31 import com.netsdk.demo.module.TalkModule;
32 import com.netsdk.lib.NetSDKLib;
33 import com.netsdk.lib.NetSDKLib.LLong;
34 import com.netsdk.lib.ToolKits;
35
36 /**
37  * Talk Demo
38  */
39 class TalkFrame extends JFrame {
40     private static final long serialVersionUID = 1L;
41
42     // device channel list
43     private Vector<String> chnlist = new Vector<String>();
44     
45     // device disconnect callback instance
46     private static DisConnect disConnect = new DisConnect(); 
47     
48     // talk frame (this)
49     private static JFrame frame = new JFrame();
50     
51     public TalkFrame() {
52         setTitle(Res.string().getTalk());
53         setLayout(new BorderLayout());
54         pack();
55         setSize(400, 450);
56         setResizable(false);
57         setLocationRelativeTo(null);
58         LoginModule.init(disConnect, null);
59         
60         try {
61             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
62         } catch (Exception e) {
63             e.printStackTrace();
64         } 
65      
66         loginPanel = new TalkLoginPanel();
67         talkPanel = new TalkPanel();
68         
69         add(loginPanel, BorderLayout.CENTER);
70         add(talkPanel, BorderLayout.SOUTH);
71         
72         loginPanel.addLoginBtnActionListener(new ActionListener() {
73             @Override
74             public void actionPerformed(ActionEvent e) {               
75                 if(loginPanel.checkLoginText()) {
76                     if(login()) {
77                         frame = ToolKits.getFrame(e);
78                         frame.setTitle(Res.string().getTalk() + " : " + Res.string().getOnline());
79                     }
80                 }
81             }
82         });
83         
84         loginPanel.addLogoutBtnActionListener(new ActionListener() {        
85             @Override
86             public void actionPerformed(ActionEvent e) {
87                 frame.setTitle(Res.string().getTalk());
88                 logout();    
89             }
90         });
91         
92         addWindowListener(new WindowAdapter() {
93             public void windowClosing(WindowEvent e) {
94                 TalkModule.stopTalk();
95                 LoginModule.logout();
96                 LoginModule.cleanup();
97                 dispose();    
98                 
99                 SwingUtilities.invokeLater(new Runnable() {
100                     public void run() {
101                         FunctionList demo = new FunctionList();
102                         demo.setVisible(true);
103                     }
104                 });
105             }
106         });
107     }
108     
109     /////////////////function///////////////////
110     // device disconnect callback class 
111     // set it's instance by call CLIENT_Init, when device disconnect sdk will call it.
112     private static class DisConnect implements NetSDKLib.fDisConnect {
113         public void invoke(LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
114             System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
115
116             SwingUtilities.invokeLater(new Runnable() {
117                 public void run() {
118                     frame.setTitle(Res.string().getTalk() + " : " + Res.string().getDisConnectReconnecting());
119                 }
120             });
121         }
122     }
123
124     public boolean login() {
125
126         if(LoginModule.login(loginPanel.ipTextArea.getText(), 
127                     Integer.parseInt(loginPanel.portTextArea.getText()), 
128                     loginPanel.nameTextArea.getText(), 
129                     new String(loginPanel.passwordTextArea.getPassword()))) {
130         
131                 loginPanel.setButtonEnable(true);
132         
133                 for(int i = 1; i < LoginModule.m_stDeviceInfo.byChanNum + 1; i++) {
134                     chnlist.add(Res.string().getChannel() + " " + String.valueOf(i));
135                 }
136                 
137                 talkPanel.talkEnable();
138                                 
139         }else {
140             JOptionPane.showMessageDialog(null, Res.string().getLoginFailed() + ", " + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
141             return false;
142         }
143         
144         return true;
145     }
146
147     public void logout() {
148         TalkModule.stopTalk();
149         LoginModule.logout();
150         
151         loginPanel.setButtonEnable(false);
152         chnlist.clear();
153         talkPanel.initTalkEnable();
154     }
155     
156     private class TalkPanel extends JPanel {
157         private static final long serialVersionUID = 1L;
158         
159         public TalkPanel() {
160             BorderEx.set(this, Res.string().getTalk(), 2);
161             setLayout(new GridLayout(3, 1));
162             setPreferredSize(new Dimension(350, 220));
163             
164             transmitPanel = new JPanel();
165             chnPanel = new JPanel();
166             talkBtnPanel = new JPanel();
167             
168             transmitLabel = new JLabel(Res.string().getTransmitType());
169             transmitLabel.setPreferredSize(new Dimension(100, 25));
170             transmitComboBox = new JComboBox();
171             transmitComboBox.setPreferredSize(new Dimension(150, 25));
172             transmitPanel.add(transmitLabel);
173             transmitPanel.add(transmitComboBox);
174             
175             chnlabel = new JLabel(Res.string().getTransmitChannel());
176             chnlabel.setPreferredSize(new Dimension(100, 25));
177             chnComboBox = new JComboBox();    
178             chnComboBox.setPreferredSize(new Dimension(150, 25)); 
179             chnPanel.add(chnlabel);
180             chnPanel.add(chnComboBox);
181             
182             startTalkBtn = new JButton(Res.string().getStartTalk());
183             startTalkBtn.setPreferredSize(new Dimension(100, 20));
184             JLabel nullLabel = new JLabel("      ");
185             stopTalkBtn = new JButton(Res.string().getStopTalk());
186             stopTalkBtn.setPreferredSize(new Dimension(100, 20));
187             talkBtnPanel.add(startTalkBtn);
188             talkBtnPanel.add(nullLabel);
189             talkBtnPanel.add(stopTalkBtn);
190             
191             add(transmitPanel);
192             add(chnPanel);
193             add(talkBtnPanel);
194             
195             initTalkEnable();
196             
197             startTalkBtn.addActionListener(new ActionListener() {            
198                 @Override
199                 public void actionPerformed(ActionEvent e) {
200                     
201                     if(TalkModule.startTalk(transmitComboBox.getSelectedIndex(), 
202                             chnComboBox.getSelectedIndex())) {
203                         setButtonEnable(false);
204                     }else {
205                         JOptionPane.showMessageDialog(null, Res.string().getTalkFailed() + "," + ToolKits.getErrorCodeShow(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
206                     }
207                 }
208             });
209             
210             stopTalkBtn.addActionListener(new ActionListener() {            
211                 @Override
212                 public void actionPerformed(ActionEvent e) {
213                     
214                     TalkModule.stopTalk();
215                     setButtonEnable(true);
216                 }
217             });
218             
219             transmitComboBox.addItemListener(new ItemListener() {            
220                 @Override
221                 public void itemStateChanged(ItemEvent e) {
222
223                     if (e.getStateChange() == ItemEvent.SELECTED) {
224                         if (transmitComboBox.getSelectedIndex() == 1) {
225                             chnComboBox.setModel(new DefaultComboBoxModel(chnlist));
226                             chnComboBox.setEnabled(true);
227                         }else {
228                             chnComboBox.setModel(new DefaultComboBoxModel());
229                             chnComboBox.setEnabled(false);
230                         }
231                     }
232                 }
233             });
234             
235         }
236         
237         public void talkEnable() {
238             
239             String[] transmit = {Res.string().getLocalTransmitType(), Res.string().getRemoteTransmitType()};
240             transmitComboBox.setModel(new DefaultComboBoxModel(transmit));
241             setButtonEnable(true);
242         }
243         
244         public void initTalkEnable() {
245             
246             chnComboBox.setModel(new DefaultComboBoxModel());
247             transmitComboBox.setModel(new DefaultComboBoxModel());
248             chnComboBox.setEnabled(false);
249              transmitComboBox.setEnabled(false);
250             startTalkBtn.setEnabled(false);
251             stopTalkBtn.setEnabled(false);
252         }
253
254         private void setButtonEnable(boolean bln) {
255             
256              transmitComboBox.setEnabled(bln);
257              if (bln && transmitComboBox.getSelectedIndex() == 1) {
258                 chnComboBox.setEnabled(true);
259             }else {
260                 chnComboBox.setEnabled(false);
261             }
262              startTalkBtn.setEnabled(bln);
263             stopTalkBtn.setEnabled(!bln);
264         }
265         
266         private JPanel transmitPanel;
267         private JPanel chnPanel;
268         private JPanel talkBtnPanel;
269         private JLabel transmitLabel;
270         private JComboBox transmitComboBox;
271         private JLabel chnlabel;
272         private JComboBox chnComboBox;    
273         private JButton startTalkBtn;
274         private JButton stopTalkBtn;
275     }
276
277     private class TalkLoginPanel extends LoginPanel {
278     
279         private static final long serialVersionUID = 1L;
280
281         public TalkLoginPanel() {
282             setLayout(new GridLayout(3, 1));
283             removeAll();
284             JPanel ipPanel = new JPanel();
285             JPanel userPanel = new JPanel();
286             JPanel btnPanel = new JPanel();
287             JLabel nullLabel = new JLabel("          ");
288             JLabel nullLabel1 = new JLabel("          ");
289             
290             resetSize();
291     
292             ipPanel.add(ipLabel);
293             ipPanel.add(ipTextArea);
294             ipPanel.add(portLabel);
295             ipPanel.add(portTextArea);
296             
297             userPanel.add(nameLabel);
298             userPanel.add(nameTextArea);
299             userPanel.add(passwordLabel);
300             userPanel.add(passwordTextArea);
301                 
302             btnPanel.add(nullLabel);
303             btnPanel.add(loginBtn);
304             btnPanel.add(nullLabel1);
305             btnPanel.add(logoutBtn);
306
307             add(ipPanel);
308             add(userPanel);
309             add(btnPanel);
310         }
311         
312         private void resetSize() {
313             
314             ipLabel.setPreferredSize(new Dimension(70, 20));
315             portLabel.setPreferredSize(new Dimension(70, 20));
316             nameLabel.setText(Res.string().getUserName());
317             nameLabel.setPreferredSize(new Dimension(70, 20));
318             passwordLabel.setPreferredSize(new Dimension(70, 20));
319
320             ipTextArea.setPreferredSize(new Dimension(90, 20));
321             portTextArea.setPreferredSize(new Dimension(90, 20));
322             nameTextArea.setPreferredSize(new Dimension(90, 20));
323             passwordTextArea.setPreferredSize(new Dimension(90, 20));
324         }
325     }
326     
327     private TalkLoginPanel loginPanel;
328     private TalkPanel talkPanel;
329     
330 }
331
332 public class Talk {  
333     public static void main(String[] args) {    
334         SwingUtilities.invokeLater(new Runnable() {
335             public void run() {
336                 TalkFrame demo = new TalkFrame();
337                 demo.setVisible(true);
338             }
339         });        
340     }
341 };
342