From 56e32456c9e30871fbebeb4364e61e7672447f7a Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Tue, 12 May 2026 13:11:53 +0900 Subject: [PATCH] fix: preserve log copy formatting --- .../java/core/packetproxy/gui/GUILog.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/main/java/core/packetproxy/gui/GUILog.java b/src/main/java/core/packetproxy/gui/GUILog.java index aa8f18e5..862b4d3d 100644 --- a/src/main/java/core/packetproxy/gui/GUILog.java +++ b/src/main/java/core/packetproxy/gui/GUILog.java @@ -17,6 +17,8 @@ import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Toolkit; +import java.awt.datatransfer.StringSelection; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.JScrollPane; @@ -43,7 +45,7 @@ public static GUILog getInstance() { } public GUILog() { - text = new JTextPane(); + text = new PlainTextCopyTextPane(); text.setEditable(false); scrollPane = new JScrollPane(text); scrollPane.getVerticalScrollBar().setUnitIncrement(16); @@ -61,7 +63,7 @@ public void append(String s) { synchronized (thread_lock) { StyledDocument doc = text.getStyledDocument(); - doc.insertString(doc.getLength(), s + "\n\r", null); + doc.insertString(doc.getLength(), s + "\n", null); } } catch (BadLocationException ex) { @@ -76,10 +78,28 @@ public void appendErr(String s) { StyleConstants.setBackground(keyWord, new Color(240, 150, 150)); StyleConstants.setBold(keyWord, true); StyledDocument doc = text.getStyledDocument(); - doc.insertString(doc.getLength() - 1, s + "\n", keyWord); + doc.insertString(doc.getLength(), s + "\n", keyWord); } } catch (BadLocationException ex) { } } + + private static class PlainTextCopyTextPane extends JTextPane { + + private static final long serialVersionUID = -7625487841725414375L; + + @Override + public void copy() { + var selected = getSelectedText(); + if (selected == null || selected.isEmpty()) { + super.copy(); + return; + } + + var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); + var selection = new StringSelection(selected); + clipboard.setContents(selection, selection); + } + } }