dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
149dd0 1 package com.iailab.netsdk.demo.frame.AutoRegister;
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.awt.event.WindowAdapter;
9 import java.awt.event.WindowEvent;
10
11 import javax.swing.JButton;
12 import javax.swing.JDialog;
13 import javax.swing.JLabel;
14 import javax.swing.JOptionPane;
15 import javax.swing.JPanel;
16 import javax.swing.JPasswordField;
17 import javax.swing.JTextField;
18
19 import com.iailab.netsdk.common.BorderEx;
20 import com.iailab.netsdk.common.DeviceManagerListener;
21 import com.iailab.netsdk.common.Res;
22
23 /**
24  * 在树上修改设备
25  */
26 public class ModifyDeviceDialog extends JDialog{
27     private static final long serialVersionUID = 1L;
28     
29     private DeviceManagerListener listener;
30     public void addDeviceManagerListener(DeviceManagerListener listener) {
31         this.listener = listener;
32     }
33     
34     private String deviceId = ""; 
35     private String username = "";
36     private String password = "";
37     
38     public ModifyDeviceDialog(String deviceId, String username, String password){
39         setTitle(Res.string().getModifyDevice());
40         setLayout(new BorderLayout());
41         setModal(true);    
42         pack();
43         setSize(220, 180);
44         setResizable(false);
45         setLocationRelativeTo(null);
46         setDefaultCloseOperation(DISPOSE_ON_CLOSE);   // 释放窗体
47         
48         this.deviceId = deviceId;
49         this.username = username;
50         this.password = password;
51         
52         ModifyDevicePanel addDevicePanel = new ModifyDevicePanel();
53         add(addDevicePanel, BorderLayout.CENTER);
54         
55         addWindowListener(new WindowAdapter() {
56             public void windowClosing(WindowEvent e){
57                 dispose();
58             }
59         });
60     }
61     
62     /*
63      * 修改设备面板
64      */
65     private class ModifyDevicePanel extends JPanel {
66         private static final long serialVersionUID = 1L;
67         
68         public ModifyDevicePanel() {
69             BorderEx.set(this, "", 2);
70             setLayout(new FlowLayout());
71             
72             JLabel deviceIdLabel = new JLabel(Res.string().getDeviceID(), JLabel.CENTER);
73             JLabel usernameLabel = new JLabel(Res.string().getUserName(), JLabel.CENTER);
74             JLabel passwordLabel = new JLabel(Res.string().getPassword(), JLabel.CENTER);
75             
76             deviceIdLabel.setPreferredSize(new Dimension(60, 21));
77             usernameLabel.setPreferredSize(new Dimension(60, 21));
78             passwordLabel.setPreferredSize(new Dimension(60, 21));
79             
80             deviceIdTextField = new JTextField();
81             usernameTextField = new JTextField();
82             passwordPasswordField = new JPasswordField();
83             
84             deviceIdTextField.setPreferredSize(new Dimension(120, 20));
85             usernameTextField.setPreferredSize(new Dimension(120, 20));
86             passwordPasswordField.setPreferredSize(new Dimension(120, 20));
87             
88             JButton modifyDeviceBtn = new JButton(Res.string().getModify());
89             JButton cancelBtn = new JButton(Res.string().getCancel());
90             
91             modifyDeviceBtn.setPreferredSize(new Dimension(90, 21));
92             cancelBtn.setPreferredSize(new Dimension(90, 21));
93             
94             add(deviceIdLabel);
95             add(deviceIdTextField);
96             add(usernameLabel);
97             add(usernameTextField);
98             add(passwordLabel);
99             add(passwordPasswordField);
100             add(modifyDeviceBtn);
101             add(cancelBtn);
102             
103             deviceIdTextField.setText(deviceId);
104             usernameTextField.setText(username);
105             passwordPasswordField.setText(password);
106             
107             // 修改
108             modifyDeviceBtn.addActionListener(new ActionListener() {            
109                 @Override
110                 public void actionPerformed(ActionEvent arg0) {
111                     if(deviceIdTextField.getText().equals("")) {
112                         JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getDeviceID(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
113                         return;
114                     }
115                     
116                     if(usernameTextField.getText().equals("")) {
117                         JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getUserName(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
118                         return;
119                     }
120                     
121                     if((new String(passwordPasswordField.getPassword()).trim()).equals("")) {
122                         JOptionPane.showMessageDialog(null, Res.string().getInput() + Res.string().getPassword(), Res.string().getErrorMessage(), JOptionPane.ERROR_MESSAGE);
123                         return;
124                     }
125                     
126                     dispose();
127                     listener.onDeviceManager(deviceIdTextField.getText(), 
128                                              usernameTextField.getText(), 
129                                              new String(passwordPasswordField.getPassword()).trim());                    
130                 }
131             });
132             
133             // 取消,关闭
134             cancelBtn.addActionListener(new ActionListener() {            
135                 @Override
136                 public void actionPerformed(ActionEvent arg0) {
137                     dispose();                        
138                 }
139             });
140         }
141     }
142     
143     private JTextField deviceIdTextField;
144     private JTextField usernameTextField;
145     private JPasswordField passwordPasswordField;
146 }