package home;
|
|
import javax.swing.*;
|
import java.awt.*;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.nio.charset.StandardCharsets;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
public class DataLogPanel extends JPanel {
|
/**
|
*
|
*/
|
private static final long serialVersionUID = 1L;
|
private MainFrame mainFrame;
|
private JTextArea logArea;
|
private JCheckBox asciiDisplayCheck, autoSaveCheck, showTimeCheck;
|
private JButton toggleBtn, clearBtn;
|
|
// 用于存储接收到的数据
|
private StringBuilder receivedData = new StringBuilder();
|
private boolean isPaused = false;
|
|
public DataLogPanel(MainFrame mainFrame) {
|
this.mainFrame = mainFrame;
|
initializeUI();
|
}
|
|
private void initializeUI() {
|
setLayout(new BorderLayout());
|
setBorder(BorderFactory.createTitledBorder(getString("data.log")));
|
|
logArea = new JTextArea(20, 30);
|
logArea.setEditable(false);
|
JScrollPane scrollPane = new JScrollPane(logArea);
|
|
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
|
asciiDisplayCheck = new JCheckBox(getString("ascii.display"));
|
autoSaveCheck = new JCheckBox(getString("auto.save"));
|
showTimeCheck = new JCheckBox(getString("show.time"));
|
|
toggleBtn = ButtonUtils.createBlueButton(getString("start"), 25);
|
toggleBtn.setPreferredSize(new Dimension(70, 25));
|
clearBtn = ButtonUtils.createBlueButton(getString("clear"), 25);
|
clearBtn.setPreferredSize(new Dimension(70, 25));
|
|
// 添加按钮监听器
|
toggleBtn.addActionListener(e -> toggleDisplay());
|
clearBtn.addActionListener(e -> clearLog());
|
|
controlPanel.add(asciiDisplayCheck);
|
controlPanel.add(autoSaveCheck);
|
controlPanel.add(showTimeCheck);
|
controlPanel.add(toggleBtn);
|
controlPanel.add(clearBtn);
|
|
add(scrollPane, BorderLayout.CENTER);
|
add(controlPanel, BorderLayout.SOUTH);
|
}
|
|
/**
|
* 添加日志数据 - 原始方法
|
*/
|
public void addLogData(byte[] data) {
|
if (isPaused || data == null || data.length == 0) {
|
return;
|
}
|
|
// 根据显示格式处理数据
|
String displayData;
|
if (asciiDisplayCheck.isSelected()) {
|
// ASCII显示格式
|
displayData = bytesToAscii(data);
|
} else {
|
// HEX显示格式
|
displayData = bytesToHex(data);
|
}
|
|
// 添加时间戳
|
String finalData;
|
if (showTimeCheck.isSelected()) {
|
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
|
finalData = "[" + timestamp + "] " + displayData + "\n";
|
} else {
|
finalData = displayData + "\n";
|
}
|
|
// 更新显示
|
SwingUtilities.invokeLater(() -> {
|
logArea.append(finalData);
|
logArea.setCaretPosition(logArea.getDocument().getLength());
|
});
|
|
// 自动保存
|
if (autoSaveCheck.isSelected()) {
|
saveToFile(data, displayData);
|
}
|
}
|
|
/**
|
* 添加带标签的日志数据
|
* @param data 字节数组数据
|
* @param tag 数据标签(如"发送"、"接收"等)
|
*/
|
public void addLogData(byte[] data, String tag) {
|
if (isPaused || data == null || data.length == 0) {
|
return;
|
}
|
|
// 根据显示格式处理数据
|
String displayData;
|
if (asciiDisplayCheck.isSelected()) {
|
// ASCII显示格式
|
displayData = bytesToAscii(data);
|
} else {
|
// HEX显示格式
|
displayData = bytesToHex(data);
|
}
|
|
// 添加时间戳和标签
|
String finalData;
|
if (showTimeCheck.isSelected()) {
|
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
|
finalData = "[" + timestamp + "] [" + tag + "] " + displayData + "\n";
|
} else {
|
finalData = "[" + tag + "] " + displayData + "\n";
|
}
|
|
// 更新显示
|
SwingUtilities.invokeLater(() -> {
|
logArea.append(finalData);
|
logArea.setCaretPosition(logArea.getDocument().getLength());
|
});
|
|
// 自动保存
|
if (autoSaveCheck.isSelected()) {
|
saveToFileWithTag(data, displayData, tag);
|
}
|
}
|
|
/**
|
* 字节数组转ASCII字符串
|
*/
|
private String bytesToAscii(byte[] bytes) {
|
String message1 = new String(bytes, 0, bytes.length, StandardCharsets.UTF_8);
|
return message1;
|
}
|
|
/**
|
* 字节数组转十六进制字符串
|
*/
|
private String bytesToHex(byte[] bytes) {
|
StringBuilder sb = new StringBuilder();
|
for (byte b : bytes) {
|
sb.append(String.format("%02X ", b));
|
}
|
return sb.toString().trim();
|
}
|
|
/**
|
* 保存数据到文件
|
*/
|
private void saveToFile(byte[] data, String displayData) {
|
try {
|
String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
String fileName = "serial_data_" + dateStr + ".txt";
|
File file = new File(fileName);
|
|
String fileContent;
|
if (showTimeCheck.isSelected()) {
|
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
|
fileContent = "[" + timestamp + "] " + displayData + "\n";
|
} else {
|
fileContent = displayData + "\n";
|
}
|
|
try (FileOutputStream fos = new FileOutputStream(file, true)) {
|
fos.write(fileContent.getBytes("UTF-8"));
|
}
|
} catch (IOException e) {
|
System.err.println(getString("save.file.error") + ": " + e.getMessage());
|
}
|
}
|
|
/**
|
* 带标签保存数据到文件
|
*/
|
private void saveToFileWithTag(byte[] data, String displayData, String tag) {
|
try {
|
String dateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
|
String fileName = "serial_data_" + dateStr + ".txt";
|
File file = new File(fileName);
|
|
String fileContent;
|
if (showTimeCheck.isSelected()) {
|
String timestamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
|
fileContent = "[" + timestamp + "] [" + tag + "] " + displayData + "\n";
|
} else {
|
fileContent = "[" + tag + "] " + displayData + "\n";
|
}
|
|
try (FileOutputStream fos = new FileOutputStream(file, true)) {
|
fos.write(fileContent.getBytes("UTF-8"));
|
}
|
} catch (IOException e) {
|
System.err.println(getString("save.file.error") + ": " + e.getMessage());
|
}
|
}
|
|
/**
|
* 切换显示暂停/继续
|
*/
|
private void toggleDisplay() {
|
isPaused = !isPaused;
|
if (isPaused) {
|
toggleBtn.setText(getString("pause"));
|
} else {
|
toggleBtn.setText(getString("start"));
|
}
|
}
|
|
/**
|
* 清除日志
|
*/
|
private void clearLog() {
|
logArea.setText("");
|
receivedData.setLength(0);
|
}
|
|
public void updateLanguage() {
|
setBorder(BorderFactory.createTitledBorder(getString("data.log")));
|
asciiDisplayCheck.setText(getString("ascii.display"));
|
autoSaveCheck.setText(getString("auto.save"));
|
showTimeCheck.setText(getString("show.time"));
|
|
if (isPaused) {
|
toggleBtn.setText(getString("pause"));
|
} else {
|
toggleBtn.setText(getString("start"));
|
}
|
clearBtn.setText(getString("clear"));
|
|
revalidate();
|
repaint();
|
}
|
|
private String getString(String key) {
|
return mainFrame.getString(key);
|
}
|
|
/**
|
* 通用方法
|
*/
|
public void addLog(String logMessage) {
|
if (logArea != null) {
|
logArea.append(logMessage + "\n");
|
logArea.setCaretPosition(logArea.getDocument().getLength());
|
}
|
}
|
}
|