2003-11-15 21:24:42 +01:00
|
|
|
/**
|
2004-11-03 00:37:00 +01:00
|
|
|
* $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.
|
|
|
|
*/
|
2003-11-15 21:24:42 +01:00
|
|
|
|
|
|
|
package org.jivesoftware.smackx.debugger;
|
|
|
|
|
2003-12-19 19:21:16 +01:00
|
|
|
import org.jivesoftware.smack.SmackConfiguration;
|
2003-11-15 21:24:42 +01:00
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
|
|
|
|
2005-10-17 22:55:09 +02:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Vector;
|
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
/**
|
2005-10-17 22:55:09 +02:00
|
|
|
* The EnhancedDebuggerWindow is the main debug window that will show all the EnhancedDebuggers.
|
|
|
|
* For each connection to debug there will be an EnhancedDebugger that will be shown in the
|
2003-11-15 21:24:42 +01:00
|
|
|
* EnhancedDebuggerWindow.<p>
|
2005-10-17 22:55:09 +02:00
|
|
|
* <p/>
|
2003-11-15 21:24:42 +01:00
|
|
|
* This class also provides information about Smack like for example the Smack version and the
|
2005-10-17 22:55:09 +02:00
|
|
|
* installed providers.
|
|
|
|
*
|
2003-11-15 21:24:42 +01:00
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2005-10-17 22:55:09 +02:00
|
|
|
public class EnhancedDebuggerWindow {
|
2003-11-15 21:24:42 +01:00
|
|
|
|
|
|
|
private static EnhancedDebuggerWindow instance;
|
|
|
|
|
2004-01-02 16:13:55 +01:00
|
|
|
private static ImageIcon connectionCreatedIcon;
|
|
|
|
private static ImageIcon connectionActiveIcon;
|
|
|
|
private static ImageIcon connectionClosedIcon;
|
|
|
|
private static ImageIcon connectionClosedOnErrorIcon;
|
2005-10-17 22:55:09 +02:00
|
|
|
|
|
|
|
public static boolean PERSISTED_DEBUGGER = false;
|
2005-10-18 21:03:29 +02:00
|
|
|
/**
|
|
|
|
* Keeps the max number of rows to keep in the tables. A value less than 0 means that packets
|
2006-02-08 21:54:36 +01:00
|
|
|
* will never be removed. If you are planning to use this debugger in a
|
2005-10-18 21:03:29 +02:00
|
|
|
* production environment then you should set a lower value (e.g. 50) to prevent the debugger
|
|
|
|
* from consuming all the JVM memory.
|
|
|
|
*/
|
2006-02-08 21:54:36 +01:00
|
|
|
public static int MAX_TABLE_ROWS = 150;
|
2005-10-17 22:55:09 +02:00
|
|
|
|
2004-01-02 16:13:55 +01:00
|
|
|
{
|
|
|
|
URL url;
|
2005-10-17 22:55:09 +02:00
|
|
|
|
2004-01-05 00:31:54 +01:00
|
|
|
url =
|
2005-10-17 22:55:09 +02:00
|
|
|
Thread.currentThread().getContextClassLoader().getResource(
|
|
|
|
"images/trafficlight_off.png");
|
2004-01-02 16:13:55 +01:00
|
|
|
if (url != null) {
|
|
|
|
connectionCreatedIcon = new ImageIcon(url);
|
2004-01-05 00:31:54 +01:00
|
|
|
}
|
|
|
|
url =
|
2005-10-17 22:55:09 +02:00
|
|
|
Thread.currentThread().getContextClassLoader().getResource(
|
|
|
|
"images/trafficlight_green.png");
|
2004-01-02 16:13:55 +01:00
|
|
|
if (url != null) {
|
|
|
|
connectionActiveIcon = new ImageIcon(url);
|
|
|
|
}
|
2004-01-05 00:31:54 +01:00
|
|
|
url =
|
2005-10-17 22:55:09 +02:00
|
|
|
Thread.currentThread().getContextClassLoader().getResource(
|
|
|
|
"images/trafficlight_red.png");
|
2004-01-02 16:13:55 +01:00
|
|
|
if (url != null) {
|
|
|
|
connectionClosedIcon = new ImageIcon(url);
|
|
|
|
}
|
2004-01-05 00:31:54 +01:00
|
|
|
url = Thread.currentThread().getContextClassLoader().getResource("images/warning.png");
|
2004-01-02 16:13:55 +01:00
|
|
|
if (url != null) {
|
|
|
|
connectionClosedOnErrorIcon = new ImageIcon(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
private JFrame frame = null;
|
|
|
|
private JTabbedPane tabbedPane = null;
|
2006-09-14 21:16:40 +02:00
|
|
|
private java.util.List<EnhancedDebugger> debuggers = new ArrayList<EnhancedDebugger>();
|
2003-11-15 21:24:42 +01:00
|
|
|
|
|
|
|
private EnhancedDebuggerWindow() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-17 22:55:09 +02:00
|
|
|
* Returns the unique EnhancedDebuggerWindow instance available in the system.
|
|
|
|
*
|
|
|
|
* @return the unique EnhancedDebuggerWindow instance
|
2003-11-15 21:24:42 +01:00
|
|
|
*/
|
2005-10-17 22:55:09 +02:00
|
|
|
public static EnhancedDebuggerWindow getInstance() {
|
2003-11-15 21:24:42 +01:00
|
|
|
if (instance == null) {
|
|
|
|
instance = new EnhancedDebuggerWindow();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the new specified debugger to the list of debuggers to show in the main window.
|
2005-10-17 22:55:09 +02:00
|
|
|
*
|
2003-11-15 21:24:42 +01:00
|
|
|
* @param debugger the new debugger to show in the debug window
|
|
|
|
*/
|
|
|
|
synchronized static void addDebugger(EnhancedDebugger debugger) {
|
|
|
|
getInstance().showNewDebugger(debugger);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-17 22:55:09 +02:00
|
|
|
* Shows the new debugger in the debug window.
|
|
|
|
*
|
2003-11-15 21:24:42 +01:00
|
|
|
* @param debugger the new debugger to show
|
|
|
|
*/
|
|
|
|
private void showNewDebugger(EnhancedDebugger debugger) {
|
|
|
|
if (frame == null) {
|
|
|
|
createDebug();
|
|
|
|
}
|
|
|
|
debugger.tabbedPane.setName("Connection_" + tabbedPane.getComponentCount());
|
|
|
|
tabbedPane.add(debugger.tabbedPane, tabbedPane.getComponentCount() - 1);
|
2004-01-02 16:13:55 +01:00
|
|
|
tabbedPane.setIconAt(tabbedPane.indexOfComponent(debugger.tabbedPane), connectionCreatedIcon);
|
2003-11-15 21:24:42 +01:00
|
|
|
frame.setTitle(
|
2005-10-17 22:55:09 +02:00
|
|
|
"Smack Debug Window -- Total connections: " + (tabbedPane.getComponentCount() - 1));
|
2004-01-12 17:09:07 +01:00
|
|
|
// Keep the added debugger for later access
|
|
|
|
debuggers.add(debugger);
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-10-17 22:55:09 +02:00
|
|
|
* Notification that a user has logged in to the server. A new title will be set
|
|
|
|
* to the tab of the given debugger.
|
|
|
|
*
|
2003-11-15 21:24:42 +01:00
|
|
|
* @param debugger the debugger whose connection logged in to the server
|
2005-10-17 22:55:09 +02:00
|
|
|
* @param user the user@host/resource that has just logged in
|
2003-11-15 21:24:42 +01:00
|
|
|
*/
|
|
|
|
synchronized static void userHasLogged(EnhancedDebugger debugger, String user) {
|
2004-01-02 16:13:55 +01:00
|
|
|
int index = getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane);
|
2003-11-15 21:24:42 +01:00
|
|
|
getInstance().tabbedPane.setTitleAt(
|
2005-10-17 22:55:09 +02:00
|
|
|
index,
|
|
|
|
user);
|
2004-01-02 16:13:55 +01:00
|
|
|
getInstance().tabbedPane.setIconAt(
|
2005-10-17 22:55:09 +02:00
|
|
|
index,
|
|
|
|
connectionActiveIcon);
|
2004-01-02 16:13:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification that the connection was properly closed.
|
|
|
|
*
|
|
|
|
* @param debugger the debugger whose connection was properly closed.
|
|
|
|
*/
|
|
|
|
synchronized static void connectionClosed(EnhancedDebugger debugger) {
|
|
|
|
getInstance().tabbedPane.setIconAt(
|
2005-10-17 22:55:09 +02:00
|
|
|
getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane),
|
|
|
|
connectionClosedIcon);
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification that the connection was closed due to an exception.
|
|
|
|
*
|
|
|
|
* @param debugger the debugger whose connection was closed due to an exception.
|
2005-10-17 22:55:09 +02:00
|
|
|
* @param e the exception.
|
2003-11-15 21:24:42 +01:00
|
|
|
*/
|
|
|
|
synchronized static void connectionClosedOnError(EnhancedDebugger debugger, Exception e) {
|
|
|
|
int index = getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane);
|
|
|
|
getInstance().tabbedPane.setToolTipTextAt(
|
2005-10-17 22:55:09 +02:00
|
|
|
index,
|
|
|
|
"Connection closed due to the exception: " + e.getMessage());
|
2004-01-02 16:13:55 +01:00
|
|
|
getInstance().tabbedPane.setIconAt(
|
2005-10-17 22:55:09 +02:00
|
|
|
index,
|
|
|
|
connectionClosedOnErrorIcon);
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
|
|
|
|
2006-09-14 21:16:40 +02:00
|
|
|
synchronized static void connectionEstablished(EnhancedDebugger debugger) {
|
|
|
|
getInstance().tabbedPane.setIconAt(
|
|
|
|
getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane),
|
|
|
|
connectionActiveIcon);
|
|
|
|
}
|
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
/**
|
|
|
|
* Creates the main debug window that provides information about Smack and also shows
|
2005-10-17 22:55:09 +02:00
|
|
|
* a tab panel for each connection that is being debugged.
|
2003-11-15 21:24:42 +01:00
|
|
|
*/
|
|
|
|
private void createDebug() {
|
|
|
|
|
|
|
|
frame = new JFrame("Smack Debug Window");
|
|
|
|
|
2005-10-17 22:55:09 +02:00
|
|
|
if (!PERSISTED_DEBUGGER) {
|
|
|
|
// Add listener for window closing event
|
|
|
|
frame.addWindowListener(new WindowAdapter() {
|
|
|
|
public void windowClosing(WindowEvent evt) {
|
|
|
|
rootWindowClosing(evt);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2004-01-12 17:09:07 +01:00
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
// We'll arrange the UI into tabs. The last tab contains Smack's information.
|
|
|
|
// All the connection debugger tabs will be shown before the Smack info tab.
|
|
|
|
tabbedPane = new JTabbedPane();
|
|
|
|
|
|
|
|
// Create the Smack info panel
|
|
|
|
JPanel informationPanel = new JPanel();
|
|
|
|
informationPanel.setLayout(new BoxLayout(informationPanel, BoxLayout.Y_AXIS));
|
|
|
|
|
|
|
|
// Add the Smack version label
|
|
|
|
JPanel versionPanel = new JPanel();
|
|
|
|
versionPanel.setLayout(new BoxLayout(versionPanel, BoxLayout.X_AXIS));
|
|
|
|
versionPanel.setMaximumSize(new Dimension(2000, 31));
|
|
|
|
versionPanel.add(new JLabel(" Smack version: "));
|
2004-03-15 22:56:18 +01:00
|
|
|
JFormattedTextField field = new JFormattedTextField(SmackConfiguration.getVersion());
|
2003-11-15 21:24:42 +01:00
|
|
|
field.setEditable(false);
|
|
|
|
field.setBorder(null);
|
|
|
|
versionPanel.add(field);
|
|
|
|
informationPanel.add(versionPanel);
|
|
|
|
|
|
|
|
// Add the list of installed IQ Providers
|
2004-01-15 14:13:27 +01:00
|
|
|
JPanel iqProvidersPanel = new JPanel();
|
|
|
|
iqProvidersPanel.setLayout(new GridLayout(1, 1));
|
|
|
|
iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers"));
|
2006-07-18 08:47:38 +02:00
|
|
|
Vector<String> providers = new Vector<String>();
|
2006-11-16 20:31:30 +01:00
|
|
|
for (Object provider : ProviderManager.getInstance().getIQProviders()) {
|
2005-02-07 22:37:23 +01:00
|
|
|
if (provider.getClass() == Class.class) {
|
|
|
|
providers.add(((Class) provider).getName());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
providers.add(provider.getClass().getName());
|
|
|
|
}
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
2005-02-07 22:37:23 +01:00
|
|
|
// Sort the collection of providers
|
|
|
|
Collections.sort(providers);
|
2003-11-15 21:24:42 +01:00
|
|
|
JList list = new JList(providers);
|
2004-01-15 14:13:27 +01:00
|
|
|
iqProvidersPanel.add(new JScrollPane(list));
|
|
|
|
informationPanel.add(iqProvidersPanel);
|
2003-11-15 21:24:42 +01:00
|
|
|
|
|
|
|
// Add the list of installed Extension Providers
|
2004-01-15 14:13:27 +01:00
|
|
|
JPanel extensionProvidersPanel = new JPanel();
|
|
|
|
extensionProvidersPanel.setLayout(new GridLayout(1, 1));
|
|
|
|
extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers"));
|
2006-07-18 08:47:38 +02:00
|
|
|
providers = new Vector<String>();
|
2006-11-16 20:31:30 +01:00
|
|
|
for (Object provider : ProviderManager.getInstance().getExtensionProviders()) {
|
2005-02-07 22:37:23 +01:00
|
|
|
if (provider.getClass() == Class.class) {
|
|
|
|
providers.add(((Class) provider).getName());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
providers.add(provider.getClass().getName());
|
|
|
|
}
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
2005-02-07 22:37:23 +01:00
|
|
|
// Sort the collection of providers
|
|
|
|
Collections.sort(providers);
|
2003-11-15 21:24:42 +01:00
|
|
|
list = new JList(providers);
|
2004-01-15 14:13:27 +01:00
|
|
|
extensionProvidersPanel.add(new JScrollPane(list));
|
|
|
|
informationPanel.add(extensionProvidersPanel);
|
2003-11-15 21:24:42 +01:00
|
|
|
|
|
|
|
tabbedPane.add("Smack Info", informationPanel);
|
|
|
|
|
|
|
|
// Add pop-up menu.
|
|
|
|
JPopupMenu menu = new JPopupMenu();
|
2004-06-14 18:58:58 +02:00
|
|
|
// Add a menu item that allows to close the current selected tab
|
2003-11-15 21:24:42 +01:00
|
|
|
JMenuItem menuItem = new JMenuItem("Close");
|
|
|
|
menuItem.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
// Remove the selected tab pane if it's not the Smack info pane
|
|
|
|
if (tabbedPane.getSelectedIndex() < tabbedPane.getComponentCount() - 1) {
|
2004-01-12 17:09:07 +01:00
|
|
|
int index = tabbedPane.getSelectedIndex();
|
|
|
|
// Notify to the debugger to stop debugging
|
2006-09-14 21:16:40 +02:00
|
|
|
EnhancedDebugger debugger = debuggers.get(index);
|
2004-01-12 17:09:07 +01:00
|
|
|
debugger.cancel();
|
|
|
|
// Remove the debugger from the root window
|
|
|
|
tabbedPane.remove(debugger.tabbedPane);
|
|
|
|
debuggers.remove(debugger);
|
|
|
|
// Update the root window title
|
2003-11-15 21:24:42 +01:00
|
|
|
frame.setTitle(
|
2005-10-17 22:55:09 +02:00
|
|
|
"Smack Debug Window -- Total connections: "
|
|
|
|
+ (tabbedPane.getComponentCount() - 1));
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menu.add(menuItem);
|
2004-06-14 18:58:58 +02:00
|
|
|
// Add a menu item that allows to close all the tabs that have their connections closed
|
|
|
|
menuItem = new JMenuItem("Close All Not Active");
|
|
|
|
menuItem.addActionListener(new ActionListener() {
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2006-09-14 21:16:40 +02:00
|
|
|
ArrayList<EnhancedDebugger> debuggersToRemove = new ArrayList<EnhancedDebugger>();
|
2004-06-14 18:58:58 +02:00
|
|
|
// Remove all the debuggers of which their connections are no longer valid
|
2005-10-17 22:55:09 +02:00
|
|
|
for (int index = 0; index < tabbedPane.getComponentCount() - 1; index++) {
|
2006-09-14 21:16:40 +02:00
|
|
|
EnhancedDebugger debugger = debuggers.get(index);
|
2004-06-14 18:58:58 +02:00
|
|
|
if (!debugger.isConnectionActive()) {
|
|
|
|
// Notify to the debugger to stop debugging
|
|
|
|
debugger.cancel();
|
|
|
|
debuggersToRemove.add(debugger);
|
|
|
|
}
|
|
|
|
}
|
2006-09-14 21:16:40 +02:00
|
|
|
for (EnhancedDebugger debugger : debuggersToRemove) {
|
2004-06-14 18:58:58 +02:00
|
|
|
// Remove the debugger from the root window
|
|
|
|
tabbedPane.remove(debugger.tabbedPane);
|
|
|
|
debuggers.remove(debugger);
|
|
|
|
}
|
|
|
|
// Update the root window title
|
|
|
|
frame.setTitle(
|
2005-10-17 22:55:09 +02:00
|
|
|
"Smack Debug Window -- Total connections: "
|
|
|
|
+ (tabbedPane.getComponentCount() - 1));
|
2004-06-14 18:58:58 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
menu.add(menuItem);
|
2003-11-15 21:24:42 +01:00
|
|
|
// Add listener to the text area so the popup menu can come up.
|
|
|
|
tabbedPane.addMouseListener(new PopupListener(menu));
|
|
|
|
|
|
|
|
frame.getContentPane().add(tabbedPane);
|
|
|
|
|
|
|
|
frame.setSize(650, 400);
|
|
|
|
|
2005-10-17 22:55:09 +02:00
|
|
|
if (!PERSISTED_DEBUGGER) {
|
|
|
|
frame.setVisible(true);
|
|
|
|
}
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|
|
|
|
|
2004-01-12 17:09:07 +01:00
|
|
|
/**
|
2005-10-17 22:55:09 +02:00
|
|
|
* Notification that the root window is closing. Stop listening for received and
|
2004-01-12 17:09:07 +01:00
|
|
|
* transmitted packets in all the debugged connections.
|
2005-10-17 22:55:09 +02:00
|
|
|
*
|
|
|
|
* @param evt the event that indicates that the root window is closing
|
2004-01-12 17:09:07 +01:00
|
|
|
*/
|
|
|
|
public void rootWindowClosing(WindowEvent evt) {
|
|
|
|
// Notify to all the debuggers to stop debugging
|
2006-09-14 21:16:40 +02:00
|
|
|
for (EnhancedDebugger debugger : debuggers) {
|
2004-01-12 17:09:07 +01:00
|
|
|
debugger.cancel();
|
|
|
|
}
|
|
|
|
// Release any reference to the debuggers
|
|
|
|
debuggers.removeAll(debuggers);
|
|
|
|
// Release the default instance
|
|
|
|
instance = null;
|
|
|
|
}
|
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
/**
|
|
|
|
* Listens for debug window popup dialog events.
|
|
|
|
*/
|
|
|
|
private class PopupListener extends MouseAdapter {
|
2005-10-17 22:55:09 +02:00
|
|
|
|
2003-11-15 21:24:42 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 22:55:09 +02:00
|
|
|
|
|
|
|
public void setVisible(boolean visible) {
|
|
|
|
if (frame != null) {
|
|
|
|
frame.setVisible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-18 21:03:29 +02:00
|
|
|
public boolean isVisible() {
|
2006-11-16 20:31:30 +01:00
|
|
|
return frame != null && frame.isVisible();
|
2005-10-18 21:03:29 +02:00
|
|
|
}
|
2003-11-15 21:24:42 +01:00
|
|
|
}
|