826220679@qq.com
2025-08-07 4d6cd980c5c69e4d9d150669c89734642297e0cd
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
package dell_targets;
 
import targets.LocationTag;
import targets.TagType;
 
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
 
public class AddTagDialog extends JDialog {
    private ResourceBundle messages;
    private JTextField idField;
    private JComboBox<String> typeComboBox;
    private JTextField nameField;
    private boolean confirmed = false;
    private List<TagType> tagTypes;
 
    public AddTagDialog(Frame parent, ResourceBundle messages) {
        super(parent, messages.getString("ADD_TAG"), true);
        this.messages = messages;
 
        setLayout(new BorderLayout());
        setSize(400, 200);
        setLocationRelativeTo(parent);
 
        JPanel formPanel = new JPanel(new GridLayout(3, 2, 10, 10));
        formPanel.setBorder(new EmptyBorder(15, 15, 15, 15));
 
        // É豸±àºÅ
        JLabel idLabel = new JLabel(messages.getString("DEVICE_NUMBER") + ": *");
        idField = new JTextField();
        formPanel.add(idLabel);
        formPanel.add(idField);
 
        // É豸ÀàÐÍ
        JLabel typeLabel = new JLabel(messages.getString("DEVICE_TYPE") + ": *");
        typeComboBox = new JComboBox<>();
        loadTagTypes();
        formPanel.add(typeLabel);
        formPanel.add(typeComboBox);
 
        // É豸Ãû³Æ
        JLabel nameLabel = new JLabel(messages.getString("DEVICE_NAME") + ":");
        nameField = new JTextField();
        formPanel.add(nameLabel);
        formPanel.add(nameField);
 
        add(formPanel, BorderLayout.CENTER);
 
        // °´Å¥Ãæ°å
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        JButton okButton = new JButton(messages.getString("OK"));
        JButton cancelButton = new JButton(messages.getString("CANCEL"));
 
        okButton.addActionListener(e -> {
            if (validateInput()) {
                confirmed = true;
                dispose();
            }
        });
 
        cancelButton.addActionListener(e -> dispose());
 
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }
 
    private void loadTagTypes() {
        try {
            tagTypes = Dell_tagType.getTagTypes();
            for (TagType type : tagTypes) {
                typeComboBox.addItem(type.getTagType());
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("LOAD_TAG_TYPES_ERROR") + e.getMessage(),
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
        }
    }
 
    private boolean validateInput() {
        String id = idField.getText().trim();
        if (id.isEmpty()) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("DEVICE_ID_REQUIRED"),
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        // ¼ì²éHEX¸ñʽºÍ³¤¶È
        if (!id.matches("[0-9A-Fa-f]{1,10}")) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("INVALID_HEX_FORMAT"),
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        // ¼ì²éÃû³Æ³¤¶È
        String name = nameField.getText().trim();
        if (name.length() > 10) {
            JOptionPane.showMessageDialog(this, 
                    messages.getString("DEVICE_NAME_TOO_LONG"),
                    messages.getString("ERROR"), 
                    JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
 
    public boolean isConfirmed() {
        return confirmed;
    }
 
    public LocationTag getTag() {
        LocationTag tag = new LocationTag();
        tag.setDeviceNumber(idField.getText().trim());
        tag.setDeviceType((String) typeComboBox.getSelectedItem());
        
        String name = nameField.getText().trim();
        tag.setDeviceName(name.isEmpty() ? messages.getString("UNBOUND") : name); // ¿ÕʱĬÈÏ"δ°ó¶¨"
        
        // ÉèÖÃĬÈÏÖµ
        tag.setOnlineStatus("0"); // Ä¬ÈÏÀëÏß
        tag.setDeviceBattery("0"); // Ä¬ÈϵçÁ¿0
        tag.setIpAndPort(""); // Ä¬ÈϿյØÖ·¶Ë¿Ú
        
        return tag;
    }
}