1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-24 13:27:59 +01:00

Added Thread Safe behavior in Debugger.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2992 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Derek DeMoro 2005-10-24 03:45:11 +00:00 committed by derek
parent e76f335910
commit a43b8a1cdb

View file

@ -20,29 +20,42 @@
package org.jivesoftware.smackx.debugger; package org.jivesoftware.smackx.debugger;
import java.awt.*; import org.jivesoftware.smack.ConnectionListener;
import java.awt.datatransfer.*; import org.jivesoftware.smack.PacketListener;
import java.awt.event.*; import org.jivesoftware.smack.XMPPConnection;
import java.io.*; import org.jivesoftware.smack.debugger.SmackDebugger;
import java.net.*; import org.jivesoftware.smack.packet.IQ;
import java.text.*; import org.jivesoftware.smack.packet.Message;
import java.util.Date; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.*; import javax.swing.event.ListSelectionEvent;
import javax.swing.table.*; import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.xml.transform.*; import javax.xml.transform.*;
import javax.xml.transform.stream.*; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.jivesoftware.smack.*; import java.awt.*;
import org.jivesoftware.smack.debugger.*; import java.awt.datatransfer.Clipboard;
import org.jivesoftware.smack.packet.*; import java.awt.datatransfer.StringSelection;
import org.jivesoftware.smack.util.*; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
/** /**
* The EnhancedDebugger is a debugger that allows to debug sent, received and interpreted messages * The EnhancedDebugger is a debugger that allows to debug sent, received and interpreted messages
* but also provides the ability to send ad-hoc messages composed by the user.<p> * but also provides the ability to send ad-hoc messages composed by the user.<p>
* * <p/>
* A new EnhancedDebugger will be created for each connection to debug. All the EnhancedDebuggers * A new EnhancedDebugger will be created for each connection to debug. All the EnhancedDebuggers
* will be shown in the same debug window provided by the class EnhancedDebuggerWindow. * will be shown in the same debug window provided by the class EnhancedDebuggerWindow.
* *
@ -63,23 +76,23 @@ public class EnhancedDebugger implements SmackDebugger {
URL url; URL url;
// Load the image icons // Load the image icons
url = url =
Thread.currentThread().getContextClassLoader().getResource("images/nav_left_blue.png"); Thread.currentThread().getContextClassLoader().getResource("images/nav_left_blue.png");
if (url != null) { if (url != null) {
packetReceivedIcon = new ImageIcon(url); packetReceivedIcon = new ImageIcon(url);
} }
url = url =
Thread.currentThread().getContextClassLoader().getResource("images/nav_right_red.png"); Thread.currentThread().getContextClassLoader().getResource("images/nav_right_red.png");
if (url != null) { if (url != null) {
packetSentIcon = new ImageIcon(url); packetSentIcon = new ImageIcon(url);
} }
url = url =
Thread.currentThread().getContextClassLoader().getResource("images/photo_portrait.png"); Thread.currentThread().getContextClassLoader().getResource("images/photo_portrait.png");
if (url != null) { if (url != null) {
presencePacketIcon = new ImageIcon(url); presencePacketIcon = new ImageIcon(url);
} }
url = url =
Thread.currentThread().getContextClassLoader().getResource( Thread.currentThread().getContextClassLoader().getResource(
"images/question_and_answer.png"); "images/question_and_answer.png");
if (url != null) { if (url != null) {
iqPacketIcon = new ImageIcon(url); iqPacketIcon = new ImageIcon(url);
} }
@ -157,8 +170,14 @@ public class EnhancedDebugger implements SmackDebugger {
// data as Smack sees it and not as it's coming in as raw XML. // data as Smack sees it and not as it's coming in as raw XML.
packetReaderListener = new PacketListener() { packetReaderListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa"); SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) {
addReadPacketToTable(dateFormatter, packet); public void processPacket(final Packet packet) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
addReadPacketToTable(dateFormatter, packet);
}
});
} }
}; };
@ -166,21 +185,37 @@ public class EnhancedDebugger implements SmackDebugger {
// the GUI. // the GUI.
packetWriterListener = new PacketListener() { packetWriterListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa"); SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) {
addSentPacketToTable(dateFormatter, packet); public void processPacket(final Packet packet) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
addSentPacketToTable(dateFormatter, packet);
}
});
} }
}; };
// Create a thread that will listen for any connection closed event // Create a thread that will listen for any connection closed event
connListener = new ConnectionListener() { connListener = new ConnectionListener() {
public void connectionClosed() { public void connectionClosed() {
statusField.setValue("Closed"); SwingUtilities.invokeLater(new Runnable() {
EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this); public void run() {
statusField.setValue("Closed");
EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this);
}
});
} }
public void connectionClosedOnError(Exception e) { public void connectionClosedOnError(final Exception e) {
statusField.setValue("Closed due to an exception"); SwingUtilities.invokeLater(new Runnable() {
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e); public void run() {
statusField.setValue("Closed due to an exception");
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
}
});
} }
}; };
} }
@ -192,20 +227,21 @@ public class EnhancedDebugger implements SmackDebugger {
tabbedPane.setToolTipTextAt(0, "Sent and received packets processed by Smack"); tabbedPane.setToolTipTextAt(0, "Sent and received packets processed by Smack");
messagesTable = messagesTable =
new DefaultTableModel( new DefaultTableModel(
new Object[] { "Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From" }, new Object[]{"Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From"},
0) { 0) {
public boolean isCellEditable(int rowIndex, int mColIndex) { public boolean isCellEditable(int rowIndex, int mColIndex) {
return false; return false;
} }
public Class getColumnClass(int columnIndex) {
if (columnIndex == 2 || columnIndex == 3) {
return Icon.class;
}
return super.getColumnClass(columnIndex);
}
}; public Class getColumnClass(int columnIndex) {
if (columnIndex == 2 || columnIndex == 3) {
return Icon.class;
}
return super.getColumnClass(columnIndex);
}
};
JTable table = new JTable(messagesTable); JTable table = new JTable(messagesTable);
// Allow only single a selection // Allow only single a selection
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@ -295,8 +331,8 @@ public class EnhancedDebugger implements SmackDebugger {
receivedText.setForeground(new Color(6, 76, 133)); receivedText.setForeground(new Color(6, 76, 133));
tabbedPane.add("Raw Received Packets", new JScrollPane(receivedText)); tabbedPane.add("Raw Received Packets", new JScrollPane(receivedText));
tabbedPane.setToolTipTextAt( tabbedPane.setToolTipTextAt(
2, 2,
"Raw text of the received packets before Smack process them"); "Raw text of the received packets before Smack process them");
// Add pop-up menu. // Add pop-up menu.
menu = new JPopupMenu(); menu = new JPopupMenu();
@ -325,32 +361,34 @@ public class EnhancedDebugger implements SmackDebugger {
// Create a special Reader that wraps the main Reader and logs data to the GUI. // Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader); ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() { readerListener = new ReaderListener() {
public void read(String str) { public void read(String str) {
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER && if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) { !EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible // Do not add content if the parent is not visible
return; return;
} }
int index = str.lastIndexOf(">"); int index = str.lastIndexOf(">");
if (index != -1) { if (index != -1) {
receivedText.append(str.substring(0, index + 1)); receivedText.append(str.substring(0, index + 1));
receivedText.append(NEWLINE); receivedText.append(NEWLINE);
if (str.length() > index) { if (str.length() > index) {
receivedText.append(str.substring(index + 1)); receivedText.append(str.substring(index + 1));
}
}
else {
receivedText.append(str);
}
} }
}; }
else {
receivedText.append(str);
}
}
};
debugReader.addReaderListener(readerListener); debugReader.addReaderListener(readerListener);
// Create a special Writer that wraps the main Writer and logs data to the GUI. // Create a special Writer that wraps the main Writer and logs data to the GUI.
ObservableWriter debugWriter = new ObservableWriter(writer); ObservableWriter debugWriter = new ObservableWriter(writer);
writerListener = new WriterListener() { writerListener = new WriterListener() {
public void write(String str) { public void write(final String str) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER && if (EnhancedDebuggerWindow.PERSISTED_DEBUGGER &&
!EnhancedDebuggerWindow.getInstance().isVisible()) { !EnhancedDebuggerWindow.getInstance().isVisible()) {
// Do not add content if the parent is not visible // Do not add content if the parent is not visible
@ -362,7 +400,11 @@ public class EnhancedDebugger implements SmackDebugger {
sentText.append(NEWLINE); sentText.append(NEWLINE);
} }
} }
}; });
}
};
debugWriter.addWriterListener(writerListener); debugWriter.addWriterListener(writerListener);
// Assign the reader/writer objects to use the debug versions. The packet reader // Assign the reader/writer objects to use the debug versions. The packet reader
@ -386,9 +428,9 @@ public class EnhancedDebugger implements SmackDebugger {
menuItem.addActionListener(new ActionListener() { menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
adhocMessages.setText( adhocMessages.setText(
"<message to=\"\" id=\"" "<message to=\"\" id=\""
+ StringUtils.randomString(5) + StringUtils.randomString(5)
+ "-X\"><body></body></message>"); + "-X\"><body></body></message>");
} }
}); });
menu.add(menuItem); menu.add(menuItem);
@ -397,9 +439,9 @@ public class EnhancedDebugger implements SmackDebugger {
menuItem.addActionListener(new ActionListener() { menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
adhocMessages.setText( adhocMessages.setText(
"<iq type=\"get\" to=\"\" id=\"" "<iq type=\"get\" to=\"\" id=\""
+ StringUtils.randomString(5) + StringUtils.randomString(5)
+ "-X\"><query xmlns=\"\"></query></iq>"); + "-X\"><query xmlns=\"\"></query></iq>");
} }
}); });
menu.add(menuItem); menu.add(menuItem);
@ -408,9 +450,9 @@ public class EnhancedDebugger implements SmackDebugger {
menuItem.addActionListener(new ActionListener() { menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
adhocMessages.setText( adhocMessages.setText(
"<iq type=\"set\" to=\"\" id=\"" "<iq type=\"set\" to=\"\" id=\""
+ StringUtils.randomString(5) + StringUtils.randomString(5)
+ "-X\"><query xmlns=\"\"></query></iq>"); + "-X\"><query xmlns=\"\"></query></iq>");
} }
}); });
menu.add(menuItem); menu.add(menuItem);
@ -419,7 +461,7 @@ public class EnhancedDebugger implements SmackDebugger {
menuItem.addActionListener(new ActionListener() { menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
adhocMessages.setText( adhocMessages.setText(
"<presence to=\"\" id=\"" + StringUtils.randomString(5) + "-X\"/>"); "<presence to=\"\" id=\"" + StringUtils.randomString(5) + "-X\"/>");
} }
}); });
menu.add(menuItem); menu.add(menuItem);
@ -462,56 +504,56 @@ public class EnhancedDebugger implements SmackDebugger {
label.setMinimumSize(new java.awt.Dimension(150, 14)); label.setMinimumSize(new java.awt.Dimension(150, 14));
label.setMaximumSize(new java.awt.Dimension(150, 14)); label.setMaximumSize(new java.awt.Dimension(150, 14));
connPanel.add( connPanel.add(
label, label,
new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0));
JFormattedTextField field = new JFormattedTextField(connection.getServiceName()); JFormattedTextField field = new JFormattedTextField(connection.getServiceName());
field.setMinimumSize(new java.awt.Dimension(150, 20)); field.setMinimumSize(new java.awt.Dimension(150, 20));
field.setMaximumSize(new java.awt.Dimension(150, 20)); field.setMaximumSize(new java.awt.Dimension(150, 20));
field.setEditable(false); field.setEditable(false);
field.setBorder(null); field.setBorder(null);
connPanel.add( connPanel.add(
field, field,
new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
// Add the Port information // Add the Port information
label = new JLabel("Port: "); label = new JLabel("Port: ");
label.setMinimumSize(new java.awt.Dimension(150, 14)); label.setMinimumSize(new java.awt.Dimension(150, 14));
label.setMaximumSize(new java.awt.Dimension(150, 14)); label.setMaximumSize(new java.awt.Dimension(150, 14));
connPanel.add( connPanel.add(
label, label,
new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0));
field = new JFormattedTextField(new Integer(connection.getPort())); field = new JFormattedTextField(new Integer(connection.getPort()));
field.setMinimumSize(new java.awt.Dimension(150, 20)); field.setMinimumSize(new java.awt.Dimension(150, 20));
field.setMaximumSize(new java.awt.Dimension(150, 20)); field.setMaximumSize(new java.awt.Dimension(150, 20));
field.setEditable(false); field.setEditable(false);
field.setBorder(null); field.setBorder(null);
connPanel.add( connPanel.add(
field, field,
new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
// Add the connection's User information // Add the connection's User information
label = new JLabel("User: "); label = new JLabel("User: ");
label.setMinimumSize(new java.awt.Dimension(150, 14)); label.setMinimumSize(new java.awt.Dimension(150, 14));
label.setMaximumSize(new java.awt.Dimension(150, 14)); label.setMaximumSize(new java.awt.Dimension(150, 14));
connPanel.add( connPanel.add(
label, label,
new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0));
userField = new JFormattedTextField(); userField = new JFormattedTextField();
userField.setMinimumSize(new java.awt.Dimension(150, 20)); userField.setMinimumSize(new java.awt.Dimension(150, 20));
userField.setMaximumSize(new java.awt.Dimension(150, 20)); userField.setMaximumSize(new java.awt.Dimension(150, 20));
userField.setEditable(false); userField.setEditable(false);
userField.setBorder(null); userField.setBorder(null);
connPanel.add( connPanel.add(
userField, userField,
new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
// Add the connection's creationTime information // Add the connection's creationTime information
label = new JLabel("Creation time: "); label = new JLabel("Creation time: ");
label.setMinimumSize(new java.awt.Dimension(150, 14)); label.setMinimumSize(new java.awt.Dimension(150, 14));
label.setMaximumSize(new java.awt.Dimension(150, 14)); label.setMaximumSize(new java.awt.Dimension(150, 14));
connPanel.add( connPanel.add(
label, label,
new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0));
field = new JFormattedTextField(new SimpleDateFormat("yyyy.MM.dd hh:mm:ss aaa")); field = new JFormattedTextField(new SimpleDateFormat("yyyy.MM.dd hh:mm:ss aaa"));
field.setMinimumSize(new java.awt.Dimension(150, 20)); field.setMinimumSize(new java.awt.Dimension(150, 20));
field.setMaximumSize(new java.awt.Dimension(150, 20)); field.setMaximumSize(new java.awt.Dimension(150, 20));
@ -519,16 +561,16 @@ public class EnhancedDebugger implements SmackDebugger {
field.setEditable(false); field.setEditable(false);
field.setBorder(null); field.setBorder(null);
connPanel.add( connPanel.add(
field, field,
new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
// Add the connection's creationTime information // Add the connection's creationTime information
label = new JLabel("Status: "); label = new JLabel("Status: ");
label.setMinimumSize(new java.awt.Dimension(150, 14)); label.setMinimumSize(new java.awt.Dimension(150, 14));
label.setMaximumSize(new java.awt.Dimension(150, 14)); label.setMaximumSize(new java.awt.Dimension(150, 14));
connPanel.add( connPanel.add(
label, label,
new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, 21, 0, new Insets(0, 0, 0, 0), 0, 0));
statusField = new JFormattedTextField(); statusField = new JFormattedTextField();
statusField.setMinimumSize(new java.awt.Dimension(150, 20)); statusField.setMinimumSize(new java.awt.Dimension(150, 20));
statusField.setMaximumSize(new java.awt.Dimension(150, 20)); statusField.setMaximumSize(new java.awt.Dimension(150, 20));
@ -536,8 +578,8 @@ public class EnhancedDebugger implements SmackDebugger {
statusField.setEditable(false); statusField.setEditable(false);
statusField.setBorder(null); statusField.setBorder(null);
connPanel.add( connPanel.add(
statusField, statusField,
new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0)); new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, 10, 2, new Insets(0, 0, 0, 0), 0, 0));
// Add the connection panel to the information panel // Add the connection panel to the information panel
informationPanel.add(connPanel, BorderLayout.NORTH); informationPanel.add(connPanel, BorderLayout.NORTH);
@ -547,20 +589,20 @@ public class EnhancedDebugger implements SmackDebugger {
packetsPanel.setBorder(BorderFactory.createTitledBorder("Transmitted Packets")); packetsPanel.setBorder(BorderFactory.createTitledBorder("Transmitted Packets"));
statisticsTable = statisticsTable =
new DefaultTableModel(new Object[][] { { "IQ", new Integer(0), new Integer(0)}, { new DefaultTableModel(new Object[][]{{"IQ", new Integer(0), new Integer(0)}, {
"Message", new Integer(0), new Integer(0) "Message", new Integer(0), new Integer(0)
}, { }, {
"Presence", new Integer(0), new Integer(0) "Presence", new Integer(0), new Integer(0)
}, { }, {
"Other", new Integer(0), new Integer(0) "Other", new Integer(0), new Integer(0)
}, { }, {
"Total", new Integer(0), new Integer(0) "Total", new Integer(0), new Integer(0)
} }
}, new Object[] { "Type", "Received", "Sent" }) { }, new Object[]{"Type", "Received", "Sent"}) {
public boolean isCellEditable(int rowIndex, int mColIndex) { public boolean isCellEditable(int rowIndex, int mColIndex) {
return false; return false;
} }
}; };
JTable table = new JTable(statisticsTable); JTable table = new JTable(statisticsTable);
// Allow only single a selection // Allow only single a selection
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
@ -574,7 +616,7 @@ public class EnhancedDebugger implements SmackDebugger {
} }
public Reader newConnectionReader(Reader newReader) { public Reader newConnectionReader(Reader newReader) {
((ObservableReader)reader).removeReaderListener(readerListener); ((ObservableReader) reader).removeReaderListener(readerListener);
ObservableReader debugReader = new ObservableReader(newReader); ObservableReader debugReader = new ObservableReader(newReader);
debugReader.addReaderListener(readerListener); debugReader.addReaderListener(readerListener);
reader = debugReader; reader = debugReader;
@ -582,19 +624,25 @@ public class EnhancedDebugger implements SmackDebugger {
} }
public Writer newConnectionWriter(Writer newWriter) { public Writer newConnectionWriter(Writer newWriter) {
((ObservableWriter)writer).removeWriterListener(writerListener); ((ObservableWriter) writer).removeWriterListener(writerListener);
ObservableWriter debugWriter = new ObservableWriter(newWriter); ObservableWriter debugWriter = new ObservableWriter(newWriter);
debugWriter.addWriterListener(writerListener); debugWriter.addWriterListener(writerListener);
writer = debugWriter; writer = debugWriter;
return writer; return writer;
} }
public void userHasLogged(String user) { public void userHasLogged(final String user) {
userField.setText(user); final EnhancedDebugger debugger = this;
EnhancedDebuggerWindow.userHasLogged(this, user); SwingUtilities.invokeLater(new Runnable() {
// Add the connection listener to the connection so that the debugger can be notified public void run() {
// whenever the connection is closed. userField.setText(user);
connection.addConnectionListener(connListener); EnhancedDebuggerWindow.userHasLogged(debugger, user);
// Add the connection listener to the connection so that the debugger can be notified
// whenever the connection is closed.
connection.addConnectionListener(connListener);
}
});
} }
public Reader getReader() { public Reader getReader() {
@ -637,7 +685,7 @@ public class EnhancedDebugger implements SmackDebugger {
* Adds the received packet detail to the messages table. * Adds the received packet detail to the messages table.
* *
* @param dateFormatter the SimpleDateFormat to use to format Dates * @param dateFormatter the SimpleDateFormat to use to format Dates
* @param packet the read packet to add to the table * @param packet the read packet to add to the table
*/ */
private void addReadPacketToTable(final SimpleDateFormat dateFormatter, final Packet packet) { private void addReadPacketToTable(final SimpleDateFormat dateFormatter, final Packet packet) {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@ -680,16 +728,16 @@ public class EnhancedDebugger implements SmackDebugger {
} }
messagesTable.addRow( messagesTable.addRow(
new Object[] { new Object[]{
formatXML(packet.toXML()), formatXML(packet.toXML()),
dateFormatter.format(new Date()), dateFormatter.format(new Date()),
packetReceivedIcon, packetReceivedIcon,
packetTypeIcon, packetTypeIcon,
messageType, messageType,
packet.getPacketID(), packet.getPacketID(),
type, type,
"", "",
from }); from});
// Update the statistics table // Update the statistics table
updateStatistics(); updateStatistics();
} }
@ -700,7 +748,7 @@ public class EnhancedDebugger implements SmackDebugger {
* Adds the sent packet detail to the messages table. * Adds the sent packet detail to the messages table.
* *
* @param dateFormatter the SimpleDateFormat to use to format Dates * @param dateFormatter the SimpleDateFormat to use to format Dates
* @param packet the sent packet to add to the table * @param packet the sent packet to add to the table
*/ */
private void addSentPacketToTable(final SimpleDateFormat dateFormatter, final Packet packet) { private void addSentPacketToTable(final SimpleDateFormat dateFormatter, final Packet packet) {
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@ -743,16 +791,16 @@ public class EnhancedDebugger implements SmackDebugger {
} }
messagesTable.addRow( messagesTable.addRow(
new Object[] { new Object[]{
formatXML(packet.toXML()), formatXML(packet.toXML()),
dateFormatter.format(new Date()), dateFormatter.format(new Date()),
packetSentIcon, packetSentIcon,
packetTypeIcon, packetTypeIcon,
messageType, messageType,
packet.getPacketID(), packet.getPacketID(),
type, type,
to, to,
"" }); ""});
// Update the statistics table // Update the statistics table
updateStatistics(); updateStatistics();
@ -769,7 +817,8 @@ public class EnhancedDebugger implements SmackDebugger {
try { try {
tFactory.setAttribute("indent-number", new Integer(2)); tFactory.setAttribute("indent-number", new Integer(2));
} }
catch (IllegalArgumentException e) {} catch (IllegalArgumentException e) {
}
Transformer transformer = tFactory.newTransformer(); Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
@ -821,24 +870,22 @@ public class EnhancedDebugger implements SmackDebugger {
/** /**
* Stops debugging the connection. Removes any listener on the connection. * Stops debugging the connection. Removes any listener on the connection.
*
*/ */
void cancel() { void cancel() {
connection.removeConnectionListener(connListener); connection.removeConnectionListener(connListener);
connection.removePacketListener(packetReaderListener); connection.removePacketListener(packetReaderListener);
connection.removePacketWriterListener(packetWriterListener); connection.removePacketWriterListener(packetWriterListener);
((ObservableReader)reader).removeReaderListener(readerListener); ((ObservableReader) reader).removeReaderListener(readerListener);
((ObservableWriter)writer).removeWriterListener(writerListener); ((ObservableWriter) writer).removeWriterListener(writerListener);
messagesTable = null; messagesTable = null;
} }
/** /**
* An ad-hoc packet is like any regular packet but with the exception that it's intention is * An ad-hoc packet is like any regular packet but with the exception that it's intention is
* to be used only <b>to send packets</b>.<p> * to be used only <b>to send packets</b>.<p>
* * <p/>
* The whole text to send must be passed to the constructor. This implies that the client of * The whole text to send must be passed to the constructor. This implies that the client of
* this class is responsible for sending a valid text to the constructor. * this class is responsible for sending a valid text to the constructor.
*
*/ */
private class AdHocPacket extends Packet { private class AdHocPacket extends Packet {
@ -864,6 +911,7 @@ public class EnhancedDebugger implements SmackDebugger {
* Listens for debug window popup dialog events. * Listens for debug window popup dialog events.
*/ */
private class PopupListener extends MouseAdapter { private class PopupListener extends MouseAdapter {
JPopupMenu popup; JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) { PopupListener(JPopupMenu popupMenu) {
@ -886,6 +934,7 @@ public class EnhancedDebugger implements SmackDebugger {
} }
private class SelectionListener implements ListSelectionListener { private class SelectionListener implements ListSelectionListener {
JTable table; JTable table;
// It is necessary to keep the table since it is not possible // It is necessary to keep the table since it is not possible
@ -893,6 +942,7 @@ public class EnhancedDebugger implements SmackDebugger {
SelectionListener(JTable table) { SelectionListener(JTable table) {
this.table = table; this.table = table;
} }
public void valueChanged(ListSelectionEvent e) { public void valueChanged(ListSelectionEvent e) {
if (table.getSelectedRow() == -1) { if (table.getSelectedRow() == -1) {
// Clear the messageTextArea since there is none packet selected // Clear the messageTextArea since there is none packet selected
@ -901,7 +951,7 @@ public class EnhancedDebugger implements SmackDebugger {
else { else {
// Set the detail of the packet in the messageTextArea // Set the detail of the packet in the messageTextArea
messageTextArea.setText( messageTextArea.setText(
(String) table.getModel().getValueAt(table.getSelectedRow(), 0)); (String) table.getModel().getValueAt(table.getSelectedRow(), 0));
// Scroll up to the top // Scroll up to the top
messageTextArea.setCaretPosition(0); messageTextArea.setCaretPosition(0);
} }