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
;
15 import javax
.swing
.JLabel
;
16 import javax
.swing
.JPanel
;
17 import javax
.swing
.border
.BevelBorder
;
18 import javax
.swing
.border
.EmptyBorder
;
21 * Status panel tracker, as Swing does not have one!
25 public class StatusPanel
27 implements TallyListener
29 /** The state of the debugger. */
30 protected final DebuggerState debuggerState
;
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
;
45 protected final JLabel latencyLabel
;
47 /** General message. */
48 protected final JLabel message
;
50 /** The maximum latency. */
51 private volatile int _maxLatency
;
54 * Initializes the panel.
56 * @param __debuggerState The state of the debugger.
57 * @throws NullPointerException On null arguments.
60 public StatusPanel(DebuggerState __debuggerState
)
61 throws NullPointerException
63 if (__debuggerState
== null)
64 throw new NullPointerException("NARG");
67 this.debuggerState
= __debuggerState
;
69 // Make sure it is always visible
70 Font baseLabelFont
= new JLabel().getFont();
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
);
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
;
95 JLabel sentLabel
= new JLabel();
96 this.__pretty(sentLabel
);
97 this.sentLabel
= sentLabel
;
100 JLabel waitingLabel
= new JLabel();
101 this.__pretty(waitingLabel
);
102 this.waitingLabel
= waitingLabel
;
105 JLabel latencyLabel
= new JLabel();
106 this.__pretty(latencyLabel
);
107 this.latencyLabel
= latencyLabel
;
110 JLabel message
= new JLabel();
111 this.__pretty(message
);
112 this.message
= message
;
115 this.add(stateLabel
);
116 this.add(receivedLabel
);
118 this.add(waitingLabel
);
119 this.add(latencyLabel
);
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.
136 public void setMessage(String __message
)
138 this.message
.setText(__message
);
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
));
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
));
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)",
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.
188 private void __pretty(JLabel __label
)
189 throws NullPointerException
192 throw new NullPointerException("NARG");
194 __label
.setBorder(new BevelBorder(BevelBorder
.LOWERED
));