/** * $RCSfile$ * $Revision$ * $Date$ * * Copyright 2003-2004 Jive Software. * * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jivesoftware.smackx.debugger; import java.awt.*; import java.awt.datatransfer.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.Date; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import org.jivesoftware.smack.*; import org.jivesoftware.smack.debugger.*; import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.util.*; /** * 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.
*
* 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.
*
* @author Gaston Dombiak
*/
public class EnhancedDebugger implements SmackDebugger {
private static final String NEWLINE = "\n";
private static ImageIcon packetReceivedIcon;
private static ImageIcon packetSentIcon;
private static ImageIcon presencePacketIcon;
private static ImageIcon iqPacketIcon;
private static ImageIcon messagePacketIcon;
private static ImageIcon unknownPacketTypeIcon;
{
URL url;
// Load the image icons
url =
Thread.currentThread().getContextClassLoader().getResource("images/nav_left_blue.png");
if (url != null) {
packetReceivedIcon = new ImageIcon(url);
}
url =
Thread.currentThread().getContextClassLoader().getResource("images/nav_right_red.png");
if (url != null) {
packetSentIcon = new ImageIcon(url);
}
url =
Thread.currentThread().getContextClassLoader().getResource("images/photo_portrait.png");
if (url != null) {
presencePacketIcon = new ImageIcon(url);
}
url =
Thread.currentThread().getContextClassLoader().getResource(
"images/question_and_answer.png");
if (url != null) {
iqPacketIcon = new ImageIcon(url);
}
url = Thread.currentThread().getContextClassLoader().getResource("images/message.png");
if (url != null) {
messagePacketIcon = new ImageIcon(url);
}
url = Thread.currentThread().getContextClassLoader().getResource("images/unknown.png");
if (url != null) {
unknownPacketTypeIcon = new ImageIcon(url);
}
}
private DefaultTableModel messagesTable = null;
private JTextArea messageTextArea = null;
private JFormattedTextField userField = null;
private JFormattedTextField statusField = null;
private XMPPConnection connection = null;
private PacketListener packetReaderListener = null;
private PacketListener packetWriterListener = null;
private ConnectionListener connListener = null;
private Writer writer;
private Reader reader;
private ReaderListener readerListener;
private WriterListener writerListener;
private Date creationTime = new Date();
// Statistics variables
private DefaultTableModel statisticsTable = null;
private int sentPackets = 0;
private int receivedPackets = 0;
private int sentIQPackets = 0;
private int receivedIQPackets = 0;
private int sentMessagePackets = 0;
private int receivedMessagePackets = 0;
private int sentPresencePackets = 0;
private int receivedPresencePackets = 0;
private int sentOtherPackets = 0;
private int receivedOtherPackets = 0;
JTabbedPane tabbedPane;
public EnhancedDebugger(XMPPConnection connection, Writer writer, Reader reader) {
this.connection = connection;
this.writer = writer;
this.reader = reader;
createDebug();
EnhancedDebuggerWindow.addDebugger(this);
}
/**
* Creates the debug process, which is a GUI window that displays XML traffic.
*/
private void createDebug() {
// We'll arrange the UI into six tabs. The first tab contains all data, the second
// client generated XML, the third server generated XML, the fourth allows to send
// ad-hoc messages and the fifth contains connection information.
tabbedPane = new JTabbedPane();
// Add the All Packets, Sent, Received and Interpreted panels
addBasicPanels();
// Add the panel to send ad-hoc messages
addAdhocPacketPanel();
// Add the connection information panel
addInformationPanel();
// Create a thread that will listen for all incoming packets and write them to
// the GUI. This is what we call "interpreted" packet data, since it's the packet
// data as Smack sees it and not as it's coming in as raw XML.
packetReaderListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) {
addReadPacketToTable(dateFormatter, packet);
}
};
// Create a thread that will listen for all outgoing packets and write them to
// the GUI.
packetWriterListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) {
addSentPacketToTable(dateFormatter, packet);
}
};
// Create a thread that will listen for any connection closed event
connListener = new ConnectionListener() {
public void connectionClosed() {
statusField.setValue("Closed");
EnhancedDebuggerWindow.connectionClosed(EnhancedDebugger.this);
}
public void connectionClosedOnError(Exception e) {
statusField.setValue("Closed due to an exception");
EnhancedDebuggerWindow.connectionClosedOnError(EnhancedDebugger.this, e);
}
};
}
private void addBasicPanels() {
JPanel allPane = new JPanel();
allPane.setLayout(new GridLayout(2, 1));
tabbedPane.add("All Packets", allPane);
tabbedPane.setToolTipTextAt(0, "Sent and received packets processed by Smack");
messagesTable =
new DefaultTableModel(
new Object[] { "Hide", "Timestamp", "", "", "Message", "Id", "Type", "To", "From" },
0) {
public boolean isCellEditable(int rowIndex, int mColIndex) {
return false;
}
public Class getColumnClass(int columnIndex) {
if (columnIndex == 2 || columnIndex == 3) {
return Icon.class;
}
return super.getColumnClass(columnIndex);
}
};
JTable table = new JTable(messagesTable);
// Allow only single a selection
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Hide the first column
table.getColumnModel().getColumn(0).setMaxWidth(0);
table.getColumnModel().getColumn(0).setMinWidth(0);
table.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
table.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
// Set the column "timestamp" size
table.getColumnModel().getColumn(1).setMaxWidth(300);
table.getColumnModel().getColumn(1).setPreferredWidth(70);
// Set the column "direction" icon size
table.getColumnModel().getColumn(2).setMaxWidth(50);
table.getColumnModel().getColumn(2).setPreferredWidth(30);
// Set the column "packet type" icon size
table.getColumnModel().getColumn(3).setMaxWidth(50);
table.getColumnModel().getColumn(3).setPreferredWidth(30);
// Set the column "Id" size
table.getColumnModel().getColumn(5).setMaxWidth(100);
table.getColumnModel().getColumn(5).setPreferredWidth(55);
// Set the column "type" size
table.getColumnModel().getColumn(6).setMaxWidth(200);
table.getColumnModel().getColumn(6).setPreferredWidth(50);
// Set the column "to" size
table.getColumnModel().getColumn(7).setMaxWidth(300);
table.getColumnModel().getColumn(7).setPreferredWidth(90);
// Set the column "from" size
table.getColumnModel().getColumn(8).setMaxWidth(300);
table.getColumnModel().getColumn(8).setPreferredWidth(90);
// Create a table listener that listen for row selection events
SelectionListener selectionListener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(selectionListener);
table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);
allPane.add(new JScrollPane(table));
messageTextArea = new JTextArea();
messageTextArea.setEditable(false);
// Add pop-up menu.
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem1 = new JMenuItem("Copy");
menuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get the clipboard
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// Set the sent text as the new content of the clipboard
clipboard.setContents(new StringSelection(messageTextArea.getText()), null);
}
});
menu.add(menuItem1);
// Add listener to the text area so the popup menu can come up.
messageTextArea.addMouseListener(new PopupListener(menu));
allPane.add(new JScrollPane(messageTextArea));
// Create UI elements for client generated XML traffic.
final JTextArea sentText = new JTextArea();
sentText.setEditable(false);
sentText.setForeground(new Color(112, 3, 3));
tabbedPane.add("Raw Sent Packets", new JScrollPane(sentText));
tabbedPane.setToolTipTextAt(1, "Raw text of the sent packets");
// Add pop-up menu.
menu = new JPopupMenu();
menuItem1 = new JMenuItem("Copy");
menuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get the clipboard
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// Set the sent text as the new content of the clipboard
clipboard.setContents(new StringSelection(sentText.getText()), null);
}
});
JMenuItem menuItem2 = new JMenuItem("Clear");
menuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sentText.setText("");
}
});
// Add listener to the text area so the popup menu can come up.
sentText.addMouseListener(new PopupListener(menu));
menu.add(menuItem1);
menu.add(menuItem2);
// Create UI elements for server generated XML traffic.
final JTextArea receivedText = new JTextArea();
receivedText.setEditable(false);
receivedText.setForeground(new Color(6, 76, 133));
tabbedPane.add("Raw Received Packets", new JScrollPane(receivedText));
tabbedPane.setToolTipTextAt(
2,
"Raw text of the received packets before Smack process them");
// Add pop-up menu.
menu = new JPopupMenu();
menuItem1 = new JMenuItem("Copy");
menuItem1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Get the clipboard
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// Set the sent text as the new content of the clipboard
clipboard.setContents(new StringSelection(receivedText.getText()), null);
}
});
menuItem2 = new JMenuItem("Clear");
menuItem2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
receivedText.setText("");
}
});
// Add listener to the text area so the popup menu can come up.
receivedText.addMouseListener(new PopupListener(menu));
menu.add(menuItem1);
menu.add(menuItem2);
// Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() {
public void read(String str) {
int index = str.lastIndexOf(">");
if (index != -1) {
receivedText.append(str.substring(0, index + 1));
receivedText.append(NEWLINE);
if (str.length() > index) {
receivedText.append(str.substring(index + 1));
}
}
else {
receivedText.append(str);
}
}
};
debugReader.addReaderListener(readerListener);
// Create a special Writer that wraps the main Writer and logs data to the GUI.
ObservableWriter debugWriter = new ObservableWriter(writer);
writerListener = new WriterListener() {
public void write(String str) {
sentText.append(str);
if (str.endsWith(">")) {
sentText.append(NEWLINE);
}
}
};
debugWriter.addWriterListener(writerListener);
// Assign the reader/writer objects to use the debug versions. The packet reader
// and writer will use the debug versions when they are created.
reader = debugReader;
writer = debugWriter;
}
private void addAdhocPacketPanel() {
// Create UI elements for sending ad-hoc messages.
final JTextArea adhocMessages = new JTextArea();
adhocMessages.setEditable(true);
adhocMessages.setForeground(new Color(1, 94, 35));
tabbedPane.add("Ad-hoc message", new JScrollPane(adhocMessages));
tabbedPane.setToolTipTextAt(3, "Panel that allows you to send adhoc packets");
// Add pop-up menu.
JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Message");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
adhocMessages.setText(
"
* * 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. * */ private class AdHocPacket extends Packet { private String text; /** * Create a new AdHocPacket with the text to send. The passed text must be a valid text to * send to the server, no validation will be done on the passed text. * * @param text the whole text of the packet to send */ public AdHocPacket(String text) { this.text = text; } public String toXML() { return text; } } /** * Listens for debug window popup dialog events. */ private class PopupListener extends MouseAdapter { JPopupMenu popup; PopupListener(JPopupMenu popupMenu) { popup = popupMenu; } public void mousePressed(MouseEvent e) { maybeShowPopup(e); } public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); } } } private class SelectionListener implements ListSelectionListener { JTable table; // It is necessary to keep the table since it is not possible // to determine the table from the event's source SelectionListener(JTable table) { this.table = table; } public void valueChanged(ListSelectionEvent e) { if (table.getSelectedRow() == -1) { // Clear the messageTextArea since there is none packet selected messageTextArea.setText(null); } else { // Set the detail of the packet in the messageTextArea messageTextArea.setText( (String) table.getModel().getValueAt(table.getSelectedRow(), 0)); // Scroll up to the top messageTextArea.setCaretPosition(0); } } } }