From 22fc1b376995191406b56a0e756067de711fdc09 Mon Sep 17 00:00:00 2001 From: vito Date: Fri, 11 Oct 2024 17:29:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-135687=20=E4=BC=98=E5=8C=96=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=201.=20=E6=B7=BB=E5=8A=A0=E5=BA=8F=E5=8F=B7=202.=20?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=98=BE=E7=A4=BA=E4=BC=98=E5=8C=96=203.=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=BB=9A=E5=8A=A8=E4=BF=9D=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../debug/RemoteDesignNetWorkTablePane.java | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java index 21cb2ab049..8fd082209a 100644 --- a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java +++ b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java @@ -4,6 +4,7 @@ import com.fanruan.workplace.http.debug.RequestInfo; import com.fine.theme.icon.LazyIcon; import com.formdev.flatlaf.util.ScaledEmptyBorder; import com.fr.design.gui.ibutton.UIButton; +import com.fr.design.gui.ibutton.UIToggleButton; import com.fr.event.Event; import com.fr.event.EventDispatcher; import com.fr.event.Listener; @@ -34,18 +35,22 @@ import static com.fanruan.workplace.http.debug.RemoteDesignDebugEvent.REMOTE_HTT */ public class RemoteDesignNetWorkTablePane extends JPanel { private static final int K = 1024; + private static final int MS = 1000; private static final int TWO = 2; private JTable uiTable; private DefaultTableModel model; + private int count = 0; + private boolean scrollRectToVisible = false; private final Listener remoteDesignDebugListener = new Listener() { @Override public void on(Event event, RequestInfo requestInfo) { model.addRow(new Object[]{ - requestInfo.getStatus(), + count++, dateFormat(requestInfo.getDate()), requestInfo.getPath().substring(WorkContext.getCurrent().getPath().length() - 1), - requestInfo.getConsume() + "ms", + requestInfo.getStatus(), + simpleTime(requestInfo.getConsume()), simpleSize(requestInfo.getRequestSize()), simpleSize(requestInfo.getResponseSize()), requestInfo.getSendBody(), @@ -69,9 +74,10 @@ public class RemoteDesignNetWorkTablePane extends JPanel { private void initTable() { model = new DefaultTableModel(); - model.addColumn("status"); + model.addColumn("No."); model.addColumn("time"); model.addColumn("path"); + model.addColumn("status"); model.addColumn("cost"); model.addColumn("request size"); model.addColumn("response size"); @@ -86,7 +92,8 @@ public class RemoteDesignNetWorkTablePane extends JPanel { jToolBar.setBorder(new EmptyBorder(5, 0, 10, 0)); UIButton run = new UIButton(new LazyIcon("run")); UIButton forbid = new UIButton(new LazyIcon("forbid")); - UIButton refresh = new UIButton(new LazyIcon("remove")); + UIButton remove = new UIButton(new LazyIcon("remove")); + UIToggleButton refresh = new UIToggleButton(new LazyIcon("refresh")); run.setEnabled(false); run.setToolTipText("Start Record"); run.addActionListener(e -> { @@ -100,10 +107,16 @@ public class RemoteDesignNetWorkTablePane extends JPanel { run.setEnabled(true); forbid.setEnabled(false); }); - refresh.setToolTipText("Clear Records"); - refresh.addActionListener(e -> model.setRowCount(0)); + remove.setToolTipText("Clear Records"); + remove.addActionListener(e -> { + model.setRowCount(0); + count = 0; + }); + refresh.setToolTipText("Always Scroll To Visible"); + refresh.addActionListener(e -> scrollRectToVisible = !scrollRectToVisible); jToolBar.add(run); jToolBar.add(forbid); + jToolBar.add(remove); jToolBar.add(refresh); add(jToolBar, BorderLayout.NORTH); } @@ -120,20 +133,23 @@ public class RemoteDesignNetWorkTablePane extends JPanel { EventDispatcher.listen(REMOTE_HTTP_REQUEST, remoteDesignDebugListener); } - private static void adjustColumnWidths(JTable table) { + private void adjustColumnWidths(JTable table) { + int row = table.getRowCount() - 1; + // 从最后一行来调整大小 for (int column = 0; column < table.getColumnCount() - TWO; column++) { TableColumn tableColumn = table.getColumnModel().getColumn(column); int preferredWidth = 20; int maxWidth = tableColumn.getMaxWidth(); tableColumn.setMinWidth(0); - // 从最后一行来调整大小 - int row = table.getRowCount() - 1; TableCellRenderer cellRenderer = table.getCellRenderer(row, column); Component component = table.prepareRenderer(cellRenderer, row, column); int width = component.getPreferredSize().width + table.getIntercellSpacing().width; preferredWidth = Math.max(preferredWidth, Math.min(width, maxWidth)); tableColumn.setPreferredWidth(preferredWidth); } + if (scrollRectToVisible) { + table.scrollRectToVisible(table.getCellRect(row, 0, true)); + } } private static String dateFormat(Date date) { @@ -141,6 +157,17 @@ public class RemoteDesignNetWorkTablePane extends JPanel { return dateFormat.format(date); } + private static String simpleTime(long time) { + if (time < 0) { + return time + ""; + } else if (time < MS) { + return time + " ms"; + } else { + DecimalFormat df = new DecimalFormat("#.0"); + return df.format((float) time / MS) + " s"; + } + } + private static String simpleSize(long bytes) { if (bytes < 0) { return bytes + ""; From ad45176383b919cdae1e013223a8f4cf2639adf0 Mon Sep 17 00:00:00 2001 From: vito Date: Fri, 11 Oct 2024 20:32:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?REPORT-135687=20=E4=BC=98=E5=8C=96=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=201.=20=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F=E6=94=AF?= =?UTF-8?q?=E6=8C=81=EF=BC=8C=E5=BA=8F=E5=8F=B7=EF=BC=8C=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=92=8C=E5=A4=A7=E5=B0=8F=E6=8E=92=E5=BA=8F=202.=20=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=BC=82=E6=AD=A5=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/debug/RemoteDesignNetWorkHelper.java | 83 +++++++++++++++++++ .../debug/RemoteDesignNetWorkTablePane.java | 67 +++++---------- .../RemoteDesignNetWorkTableRowSorter.java | 32 +++++++ 3 files changed, 137 insertions(+), 45 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkHelper.java create mode 100644 designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTableRowSorter.java diff --git a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkHelper.java b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkHelper.java new file mode 100644 index 0000000000..ba08291379 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkHelper.java @@ -0,0 +1,83 @@ +package com.fr.design.remote.ui.debug; + +import com.fr.stable.StringUtils; + +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * 帮助类 + * + * @author vito + * @since 11.0 + * Created on 2024/10/11 + */ +public class RemoteDesignNetWorkHelper { + private static final int UNIT = 1000; + private static final int UNIT_BYTES = 1024; + private static final String SECOND = "s"; + private static final int K = 1024; + private static final int MS = 1000; + + static String dateFormat(Date date) { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + return dateFormat.format(date); + } + + static String simpleTime(long time) { + if (time < 0) { + return time + ""; + } else if (time < MS) { + return time + " ms"; + } else { + DecimalFormat df = new DecimalFormat("#.0"); + return df.format((float) time / MS) + " s"; + } + } + + static String simpleSize(long bytes) { + if (bytes < 0) { + return bytes + ""; + } else if (bytes < K) { + return bytes + " B"; + } else { + DecimalFormat df = new DecimalFormat("#.00"); + return df.format((float) bytes / K) + " K"; + } + } + + static long parseCostToMS(String timeStr) { + String[] split = timeStr.split(" "); + if (split.length > 1) { + double number = Double.parseDouble(split[0]); + String unit = split[1].toLowerCase(); + + if (StringUtils.equals(unit, SECOND)) { + return (long) (number * UNIT); + } + return (long) number; + } + return 0; + } + + static long parseSizeToBytes(String sizeStr) { + String[] split = sizeStr.split(" "); + if (split.length > 1) { + double number = Double.parseDouble(split[0]); + String unit = split[1].toUpperCase(); + + switch (unit) { + case "K": + return (long) (number * UNIT_BYTES); + case "M": + return (long) (number * UNIT_BYTES * UNIT_BYTES); + case "G": + return (long) (number * UNIT_BYTES * UNIT_BYTES * UNIT_BYTES); + default: + return (long) number; + } + } + return 0; + } +} diff --git a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java index 8fd082209a..c29633aa5c 100644 --- a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java +++ b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTablePane.java @@ -14,17 +14,19 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JToolBar; +import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import java.awt.BorderLayout; import java.awt.Component; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.concurrent.atomic.AtomicLong; import static com.fanruan.workplace.http.debug.RemoteDesignDebugEvent.REMOTE_HTTP_REQUEST; +import static com.fr.design.remote.ui.debug.RemoteDesignNetWorkHelper.dateFormat; +import static com.fr.design.remote.ui.debug.RemoteDesignNetWorkHelper.simpleSize; +import static com.fr.design.remote.ui.debug.RemoteDesignNetWorkHelper.simpleTime; /** * 远程设计网络调试面板 @@ -34,29 +36,29 @@ import static com.fanruan.workplace.http.debug.RemoteDesignDebugEvent.REMOTE_HTT * Created on 2024/9/24 */ public class RemoteDesignNetWorkTablePane extends JPanel { - private static final int K = 1024; - private static final int MS = 1000; private static final int TWO = 2; private JTable uiTable; private DefaultTableModel model; - private int count = 0; + private final AtomicLong count = new AtomicLong(0); private boolean scrollRectToVisible = false; private final Listener remoteDesignDebugListener = new Listener() { @Override public void on(Event event, RequestInfo requestInfo) { - model.addRow(new Object[]{ - count++, - dateFormat(requestInfo.getDate()), - requestInfo.getPath().substring(WorkContext.getCurrent().getPath().length() - 1), - requestInfo.getStatus(), - simpleTime(requestInfo.getConsume()), - simpleSize(requestInfo.getRequestSize()), - simpleSize(requestInfo.getResponseSize()), - requestInfo.getSendBody(), - requestInfo.getReturnBody(), + SwingUtilities.invokeLater(() -> { + model.addRow(new Object[]{ + count.incrementAndGet(), + dateFormat(requestInfo.getDate()), + requestInfo.getPath().substring(WorkContext.getCurrent().getPath().length() - 1), + requestInfo.getStatus(), + simpleTime(requestInfo.getConsume()), + simpleSize(requestInfo.getRequestSize()), + simpleSize(requestInfo.getResponseSize()), + requestInfo.getSendBody(), + requestInfo.getReturnBody(), + }); + adjustColumnWidths(uiTable); }); - adjustColumnWidths(uiTable); } }; @@ -84,6 +86,7 @@ public class RemoteDesignNetWorkTablePane extends JPanel { model.addColumn("request"); model.addColumn("response"); uiTable = new JTable(model); + uiTable.setRowSorter(new RemoteDesignNetWorkTableRowSorter(model)); add(new JScrollPane(uiTable), BorderLayout.CENTER); } @@ -110,10 +113,10 @@ public class RemoteDesignNetWorkTablePane extends JPanel { remove.setToolTipText("Clear Records"); remove.addActionListener(e -> { model.setRowCount(0); - count = 0; + count.set(0); }); refresh.setToolTipText("Always Scroll To Visible"); - refresh.addActionListener(e -> scrollRectToVisible = !scrollRectToVisible); + refresh.addChangeListener(e -> scrollRectToVisible = !scrollRectToVisible); jToolBar.add(run); jToolBar.add(forbid); jToolBar.add(remove); @@ -152,31 +155,5 @@ public class RemoteDesignNetWorkTablePane extends JPanel { } } - private static String dateFormat(Date date) { - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - return dateFormat.format(date); - } - - private static String simpleTime(long time) { - if (time < 0) { - return time + ""; - } else if (time < MS) { - return time + " ms"; - } else { - DecimalFormat df = new DecimalFormat("#.0"); - return df.format((float) time / MS) + " s"; - } - } - - private static String simpleSize(long bytes) { - if (bytes < 0) { - return bytes + ""; - } else if (bytes < K) { - return bytes + " B"; - } else { - DecimalFormat df = new DecimalFormat("#.00"); - return df.format((float) bytes / K) + " K"; - } - } } diff --git a/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTableRowSorter.java b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTableRowSorter.java new file mode 100644 index 0000000000..231acc72c7 --- /dev/null +++ b/designer-base/src/main/java/com/fr/design/remote/ui/debug/RemoteDesignNetWorkTableRowSorter.java @@ -0,0 +1,32 @@ +package com.fr.design.remote.ui.debug; + +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableRowSorter; +import java.util.Comparator; + +/** + * 行排序器 + * + * @author vito + * @since 11.0 + * Created on 2024/10/11 + */ +public class RemoteDesignNetWorkTableRowSorter extends TableRowSorter { + + public RemoteDesignNetWorkTableRowSorter(DefaultTableModel model) { + super(model); + setComparator(0, (Comparator) Long::compareTo); + setComparator(4, Comparator.comparingLong(RemoteDesignNetWorkHelper::parseCostToMS)); + setComparator(5, Comparator.comparingLong(RemoteDesignNetWorkHelper::parseSizeToBytes)); + setComparator(6, Comparator.comparingLong(RemoteDesignNetWorkHelper::parseSizeToBytes)); + } + + @Override + public boolean isSortable(int column) { + return column == 0 + || column == 4 + || column == 5 + || column == 6; + } + +}