Keep track of the maximum latency.
[SquirrelJME.git] / tools / squirreljme-debugger / src / main / java / cc / squirreljme / debugger / StatusPanel.java
blob39445e132347e99201a8c5bc0ae6c4c30eef2fb9
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.debugger;
12 import java.awt.Dimension;
13 import java.awt.FlowLayout;
14 import java.awt.Font;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.border.BevelBorder;
18 import javax.swing.border.EmptyBorder;
20 /**
21 * Status panel tracker, as Swing does not have one!
23 * @since 2024/01/19
25 public class StatusPanel
26 extends JPanel
27 implements TallyListener
29 /** The state of the debugger. */
30 protected final DebuggerState debuggerState;
32 /** State label. */
33 protected final JLabel stateLabel;
35 /** Received Label. */
36 protected final JLabel receivedLabel;
38 /** The number of packets that were sent. */
39 protected final JLabel sentLabel;
41 /** Waiting packets. */
42 protected final JLabel waitingLabel;
44 /** Latency. */
45 protected final JLabel latencyLabel;
47 /** General message. */
48 protected final JLabel message;
50 /** The maximum latency. */
51 private volatile int _maxLatency;
53 /**
54 * Initializes the panel.
56 * @param __debuggerState The state of the debugger.
57 * @throws NullPointerException On null arguments.
58 * @since 2024/01/19
60 public StatusPanel(DebuggerState __debuggerState)
61 throws NullPointerException
63 if (__debuggerState == null)
64 throw new NullPointerException("NARG");
66 // Store for tracking
67 this.debuggerState = __debuggerState;
69 // Make sure it is always visible
70 Font baseLabelFont = new JLabel().getFont();
71 this.setMinimumSize(
72 new Dimension(320, baseLabelFont.getSize()));
73 /*this.setPreferredSize(new Dimension(320, 16));*/
75 // Beveled border, which is generally natural
76 /*this.setBorder(new BevelBorder(BevelBorder.LOWERED));*/
77 this.setBorder(new EmptyBorder(0, 0, 0, 0));
79 // Flow layouts are clean
80 FlowLayout layout = new FlowLayout(FlowLayout.LEADING, 0, 0);
81 layout.setAlignOnBaseline(true);
82 this.setLayout(layout);
84 // State label
85 JLabel stateLabel = new JLabel("CONNECTED");
86 this.__pretty(stateLabel);
87 this.stateLabel = stateLabel;
89 // Received packets label
90 JLabel receivedLabel = new JLabel();
91 this.__pretty(receivedLabel);
92 this.receivedLabel = receivedLabel;
94 // Sent packets
95 JLabel sentLabel = new JLabel();
96 this.__pretty(sentLabel);
97 this.sentLabel = sentLabel;
99 // Waiting packets
100 JLabel waitingLabel = new JLabel();
101 this.__pretty(waitingLabel);
102 this.waitingLabel = waitingLabel;
104 // Latency
105 JLabel latencyLabel = new JLabel();
106 this.__pretty(latencyLabel);
107 this.latencyLabel = latencyLabel;
109 // Message
110 JLabel message = new JLabel();
111 this.__pretty(message);
112 this.message = message;
114 // Add labels
115 this.add(stateLabel);
116 this.add(receivedLabel);
117 this.add(sentLabel);
118 this.add(waitingLabel);
119 this.add(latencyLabel);
120 this.add(message);
122 // Add listeners to tallies to update stats
123 __debuggerState.receiveTally.addListener(this);
124 __debuggerState.sentTally.addListener(this);
125 __debuggerState.waitingTally.addListener(this);
126 __debuggerState.latency.addListener(this);
127 __debuggerState.disconnectedTally.addListener(this);
131 * Sets the message to display.
133 * @param __message The message.
134 * @since 2024/01/26
136 public void setMessage(String __message)
138 this.message.setText(__message);
142 * {@inheritDoc}
143 * @since 2024/01/19
145 @Override
146 public void updateTally(TallyTracker __which, int __old, int __new)
148 DebuggerState debuggerState = this.debuggerState;
150 // Received a packet? Or disconnected?
151 if (__which == debuggerState.receiveTally)
152 this.receivedLabel.setText(
153 String.format("Received: %d", __new));
155 // Sent a packet?
156 else if (__which == debuggerState.sentTally)
157 this.sentLabel.setText(
158 String.format("Sent: %d", __new));
160 // Waiting for packets
161 else if (__which == debuggerState.waitingTally)
162 this.waitingLabel.setText(
163 String.format("Waiting: %d", __new));
165 // Latency
166 else if (__which == debuggerState.latency)
168 int max = Math.max(__new, this._maxLatency);
169 this._maxLatency = max;
171 this.latencyLabel.setText(
172 String.format("Latency: %d ms (max %d ms)",
173 __new, max));
176 // Disconnected?
177 else if (__which == debuggerState.disconnectedTally)
178 this.stateLabel.setText("DISCONNECTED");
182 * Makes the border pretty.
184 * @param __label The label to use.
185 * @throws NullPointerException On null arguments.
186 * @since 2024/01/21
188 private void __pretty(JLabel __label)
189 throws NullPointerException
191 if (__label == null)
192 throw new NullPointerException("NARG");
194 __label.setBorder(new BevelBorder(BevelBorder.LOWERED));