Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2003-2007 Jive Software. 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.net.URL;
 
 
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 EnhancedDebuggerWindow.

This class also provides information about Smack like for example the Smack version and the installed providers.

Author(s):
Gaston Dombiak
 
 public class EnhancedDebuggerWindow {
 
     private static EnhancedDebuggerWindow instance;
 
     private static ImageIcon connectionCreatedIcon;
     private static ImageIcon connectionActiveIcon;
     private static ImageIcon connectionClosedIcon;
     private static ImageIcon connectionClosedOnErrorIcon;
 
     public static boolean PERSISTED_DEBUGGER = false;
    
Keeps the max number of rows to keep in the tables. A value less than 0 means that packets will never be removed. If you are planning to use this debugger in a production environment then you should set a lower value (e.g. 50) to prevent the debugger from consuming all the JVM memory.
 
     public static int MAX_TABLE_ROWS = 150;
 
     {
         URL url;
 
         url =
                 Thread.currentThread().getContextClassLoader().getResource(
                         "images/trafficlight_off.png");
         if (url != null) {
              = new ImageIcon(url);
         }
         url =
                 Thread.currentThread().getContextClassLoader().getResource(
                         "images/trafficlight_green.png");
         if (url != null) {
              = new ImageIcon(url);
         }
         url =
                 Thread.currentThread().getContextClassLoader().getResource(
                         "images/trafficlight_red.png");
         if (url != null) {
              = new ImageIcon(url);
         }
         url = Thread.currentThread().getContextClassLoader().getResource("images/warning.png");
         if (url != null) {
             = new ImageIcon(url);
        }
    }
    private JFrame frame = null;
    private JTabbedPane tabbedPane = null;
    private EnhancedDebuggerWindow() {
    }

    
Returns the unique EnhancedDebuggerWindow instance available in the system.

Returns:
the unique EnhancedDebuggerWindow instance
    public static EnhancedDebuggerWindow getInstance() {
        if ( == null) {
             = new EnhancedDebuggerWindow();
        }
        return ;
    }

    
Adds the new specified debugger to the list of debuggers to show in the main window.

Parameters:
debugger the new debugger to show in the debug window
    synchronized static void addDebugger(EnhancedDebugger debugger) {
        getInstance().showNewDebugger(debugger);
    }

    
Shows the new debugger in the debug window.

Parameters:
debugger the new debugger to show
    private void showNewDebugger(EnhancedDebugger debugger) {
        if ( == null) {
            createDebug();
        }
        debugger.tabbedPane.setName("XMPPConnection_" + .getComponentCount());
        .add(debugger.tabbedPane.getComponentCount() - 1);
        .setTitle(
                "Smack Debug Window -- Total connections: " + (.getComponentCount() - 1));
        // Keep the added debugger for later access
        .add(debugger);
    }

    
Notification that a user has logged in to the server. A new title will be set to the tab of the given debugger.

Parameters:
debugger the debugger whose connection logged in to the server
user the user@host/resource that has just logged in
    synchronized static void userHasLogged(EnhancedDebugger debuggerString user) {
        int index = getInstance()..indexOfComponent(debugger.tabbedPane);
                index,
                user);
                index,
                );
    }

    
Notification that the connection was properly closed.

Parameters:
debugger the debugger whose connection was properly closed.
    synchronized static void connectionClosed(EnhancedDebugger debugger) {
                getInstance()..indexOfComponent(debugger.tabbedPane),
                );
    }

    
Notification that the connection was closed due to an exception.

Parameters:
debugger the debugger whose connection was closed due to an exception.
e the exception.
    synchronized static void connectionClosedOnError(EnhancedDebugger debuggerException e) {
        int index = getInstance()..indexOfComponent(debugger.tabbedPane);
                index,
                "XMPPConnection closed due to the exception: " + e.getMessage());
                index,
                );
    }
    synchronized static void connectionEstablished(EnhancedDebugger debugger) {
                getInstance()..indexOfComponent(debugger.tabbedPane),
                );
    }
    
    
Creates the main debug window that provides information about Smack and also shows a tab panel for each connection that is being debugged.
    @SuppressWarnings({ "rawtypes""unchecked" })
	private void createDebug() {
         = new JFrame("Smack Debug Window");
        if (!) {
            // Add listener for window closing event
            .addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    rootWindowClosing(evt);
                }
            });
        }
        // 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. 
         = new JTabbedPane();
        // Create the Smack info panel 
        JPanel informationPanel = new JPanel();
        informationPanel.setLayout(new BoxLayout(informationPanel.));
        // Add the Smack version label
        JPanel versionPanel = new JPanel();
        versionPanel.setLayout(new BoxLayout(versionPanel.));
        versionPanel.setMaximumSize(new Dimension(2000, 31));
        versionPanel.add(new JLabel(" Smack version: "));
        JFormattedTextField field = new JFormattedTextField(SmackConfiguration.getVersion());
        field.setEditable(false);
        field.setBorder(null);
        versionPanel.add(field);
        informationPanel.add(versionPanel);
        // Add the list of installed IQ Providers
        JPanel iqProvidersPanel = new JPanel();
        iqProvidersPanel.setLayout(new GridLayout(1, 1));
        iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers"));
        Vector<Stringproviders = new Vector<String>();
        for (Object provider : ProviderManager.getIQProviders()) {
            if (provider.getClass() == Class.class) {
                providers.add(((Class<?>) provider).getName());
            }
            else {
                providers.add(provider.getClass().getName());
            }
        }
        // Sort the collection of providers
        Collections.sort(providers);
        JList list = new JList(providers);
        iqProvidersPanel.add(new JScrollPane(list));
        informationPanel.add(iqProvidersPanel);
        // Add the list of installed Extension Providers
        JPanel extensionProvidersPanel = new JPanel();
        extensionProvidersPanel.setLayout(new GridLayout(1, 1));
        extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers"));
        providers = new Vector<String>();
        for (Object provider : ProviderManager.getExtensionProviders()) {
            if (provider.getClass() == Class.class) {
                providers.add(((Class<?>) provider).getName());
            }
            else {
                providers.add(provider.getClass().getName());
            }
        }
        // Sort the collection of providers
        Collections.sort(providers);
        list = new JList(providers);
        extensionProvidersPanel.add(new JScrollPane(list));
        informationPanel.add(extensionProvidersPanel);
        .add("Smack Info"informationPanel);
        // Add pop-up menu.
        JPopupMenu menu = new JPopupMenu();
        // Add a menu item that allows to close the current selected tab
        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 (.getSelectedIndex() < .getComponentCount() - 1) {
                    int index = .getSelectedIndex();
                    // Notify to the debugger to stop debugging
                    EnhancedDebugger debugger = .get(index);
                    debugger.cancel();
                    // Remove the debugger from the root window
                    .remove(debugger.tabbedPane);
                    .remove(debugger);
                    // Update the root window title
                    .setTitle(
                            "Smack Debug Window -- Total connections: "
                                    + (.getComponentCount() - 1));
                }
            }
        });
        menu.add(menuItem);
        // 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) {
                ArrayList<EnhancedDebuggerdebuggersToRemove = new ArrayList<EnhancedDebugger>();
                // Remove all the debuggers of which their connections are no longer valid
                for (int index = 0; index < .getComponentCount() - 1; index++) {
                    EnhancedDebugger debugger = .get(index);
                    if (!debugger.isConnectionActive()) {
                        // Notify to the debugger to stop debugging
                        debugger.cancel();
                        debuggersToRemove.add(debugger);
                    }
                }
                for (EnhancedDebugger debugger : debuggersToRemove) {
                    // Remove the debugger from the root window
                    .remove(debugger.tabbedPane);
                    .remove(debugger);
                }
                // Update the root window title
                .setTitle(
                        "Smack Debug Window -- Total connections: "
                                + (.getComponentCount() - 1));
            }
        });
        menu.add(menuItem);
        // Add listener to the text area so the popup menu can come up.
        .addMouseListener(new PopupListener(menu));
        .setSize(650, 400);
        if (!) {
            .setVisible(true);
        }
    }

    
Notification that the root window is closing. Stop listening for received and transmitted packets in all the debugged connections.

Parameters:
evt the event that indicates that the root window is closing
    public void rootWindowClosing(WindowEvent evt) {
        // Notify to all the debuggers to stop debugging
        for (EnhancedDebugger debugger : ) {
            debugger.cancel();
        }
        // Release any reference to the debuggers
        .removeAll();
        // Release the default instance
         = null;
    }

    
Listens for debug window popup dialog events.
    private class PopupListener extends MouseAdapter {
        JPopupMenu popup;
        PopupListener(JPopupMenu popupMenu) {
             = popupMenu;
        }
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }
        public void mouseReleased(MouseEvent e) {
            maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                .show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }
    public void setVisible(boolean visible) {
        if ( != null) {
            .setVisible(visible);
        }
    }
    public boolean isVisible() {
        return  != null && .isVisible();
    }
New to GrepCode? Check out our FAQ X