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;
|
}
|
}
|