1 20 21 package org.jivesoftware.smackx.debugger; 22 23 import java.awt.*; 24 import java.awt.event.*; 25 import java.net.*; 26 import java.util.*; 27 28 import javax.swing.*; 29 30 import org.jivesoftware.smack.SmackConfiguration; 31 import org.jivesoftware.smack.provider.ProviderManager; 32 33 43 class EnhancedDebuggerWindow { 44 45 private static EnhancedDebuggerWindow instance; 46 47 private static ImageIcon connectionCreatedIcon; 48 private static ImageIcon connectionActiveIcon; 49 private static ImageIcon connectionClosedIcon; 50 private static ImageIcon connectionClosedOnErrorIcon; 51 52 { 53 URL url; 54 55 url = 56 Thread.currentThread().getContextClassLoader().getResource( 57 "images/trafficlight_off.png"); 58 if (url != null) { 59 connectionCreatedIcon = new ImageIcon(url); 60 } 61 url = 62 Thread.currentThread().getContextClassLoader().getResource( 63 "images/trafficlight_green.png"); 64 if (url != null) { 65 connectionActiveIcon = new ImageIcon(url); 66 } 67 url = 68 Thread.currentThread().getContextClassLoader().getResource( 69 "images/trafficlight_red.png"); 70 if (url != null) { 71 connectionClosedIcon = new ImageIcon(url); 72 } 73 url = Thread.currentThread().getContextClassLoader().getResource("images/warning.png"); 74 if (url != null) { 75 connectionClosedOnErrorIcon = new ImageIcon(url); 76 } 77 78 } 79 80 private JFrame frame = null; 81 private JTabbedPane tabbedPane = null; 82 private java.util.List debuggers = new ArrayList(); 83 84 private EnhancedDebuggerWindow() { 85 } 86 87 92 private static EnhancedDebuggerWindow getInstance() { 93 if (instance == null) { 94 instance = new EnhancedDebuggerWindow(); 95 } 96 return instance; 97 } 98 99 104 synchronized static void addDebugger(EnhancedDebugger debugger) { 105 getInstance().showNewDebugger(debugger); 106 } 107 108 113 private void showNewDebugger(EnhancedDebugger debugger) { 114 if (frame == null) { 115 createDebug(); 116 } 117 debugger.tabbedPane.setName("Connection_" + tabbedPane.getComponentCount()); 118 tabbedPane.add(debugger.tabbedPane, tabbedPane.getComponentCount() - 1); 119 tabbedPane.setIconAt(tabbedPane.indexOfComponent(debugger.tabbedPane), connectionCreatedIcon); 120 frame.setTitle( 121 "Smack Debug Window -- Total connections: " + (tabbedPane.getComponentCount() - 1)); 122 debuggers.add(debugger); 124 } 125 126 133 synchronized static void userHasLogged(EnhancedDebugger debugger, String user) { 134 int index = getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane); 135 getInstance().tabbedPane.setTitleAt( 136 index, 137 user); 138 getInstance().tabbedPane.setIconAt( 139 index, 140 connectionActiveIcon); 141 } 142 143 148 synchronized static void connectionClosed(EnhancedDebugger debugger) { 149 getInstance().tabbedPane.setIconAt( 150 getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane), 151 connectionClosedIcon); 152 } 153 154 160 synchronized static void connectionClosedOnError(EnhancedDebugger debugger, Exception e) { 161 int index = getInstance().tabbedPane.indexOfComponent(debugger.tabbedPane); 162 getInstance().tabbedPane.setToolTipTextAt( 163 index, 164 "Connection closed due to the exception: " + e.getMessage()); 165 getInstance().tabbedPane.setIconAt( 166 index, 167 connectionClosedOnErrorIcon); 168 } 169 170 174 private void createDebug() { 175 176 frame = new JFrame("Smack Debug Window"); 177 178 frame.addWindowListener(new WindowAdapter() { 180 public void windowClosing(WindowEvent evt) { 181 rootWindowClosing(evt); 182 } 183 }); 184 185 tabbedPane = new JTabbedPane(); 188 189 JPanel informationPanel = new JPanel(); 191 informationPanel.setLayout(new BoxLayout(informationPanel, BoxLayout.Y_AXIS)); 192 193 JPanel versionPanel = new JPanel(); 195 versionPanel.setLayout(new BoxLayout(versionPanel, BoxLayout.X_AXIS)); 196 versionPanel.setMaximumSize(new Dimension(2000, 31)); 197 versionPanel.add(new JLabel(" Smack version: ")); 198 JFormattedTextField field = new JFormattedTextField(SmackConfiguration.getVersion()); 199 field.setEditable(false); 200 field.setBorder(null); 201 versionPanel.add(field); 202 informationPanel.add(versionPanel); 203 204 JPanel iqProvidersPanel = new JPanel(); 206 iqProvidersPanel.setLayout(new GridLayout(1, 1)); 207 iqProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed IQ Providers")); 208 Vector providers = new Vector(); 209 for (Iterator it = ProviderManager.getIQProviders(); it.hasNext();) { 210 Object provider = it.next(); 211 if (provider.getClass() == Class .class) { 212 providers.add(((Class ) provider).getName()); 213 } 214 else { 215 providers.add(provider.getClass().getName()); 216 } 217 } 218 Collections.sort(providers); 220 JList list = new JList(providers); 221 iqProvidersPanel.add(new JScrollPane(list)); 222 informationPanel.add(iqProvidersPanel); 223 224 JPanel extensionProvidersPanel = new JPanel(); 226 extensionProvidersPanel.setLayout(new GridLayout(1, 1)); 227 extensionProvidersPanel.setBorder(BorderFactory.createTitledBorder("Installed Extension Providers")); 228 providers = new Vector(); 229 for (Iterator it = ProviderManager.getExtensionProviders(); it.hasNext();) { 230 Object provider = it.next(); 231 if (provider.getClass() == Class .class) { 232 providers.add(((Class ) provider).getName()); 233 } 234 else { 235 providers.add(provider.getClass().getName()); 236 } 237 } 238 Collections.sort(providers); 240 list = new JList(providers); 241 extensionProvidersPanel.add(new JScrollPane(list)); 242 informationPanel.add(extensionProvidersPanel); 243 244 tabbedPane.add("Smack Info", informationPanel); 245 246 JPopupMenu menu = new JPopupMenu(); 248 JMenuItem menuItem = new JMenuItem("Close"); 250 menuItem.addActionListener(new ActionListener() { 251 public void actionPerformed(ActionEvent e) { 252 if (tabbedPane.getSelectedIndex() < tabbedPane.getComponentCount() - 1) { 254 int index = tabbedPane.getSelectedIndex(); 255 EnhancedDebugger debugger = (EnhancedDebugger)debuggers.get(index); 257 debugger.cancel(); 258 tabbedPane.remove(debugger.tabbedPane); 260 debuggers.remove(debugger); 261 frame.setTitle( 263 "Smack Debug Window -- Total connections: " 264 + (tabbedPane.getComponentCount() - 1)); 265 } 266 } 267 }); 268 menu.add(menuItem); 269 menuItem = new JMenuItem("Close All Not Active"); 271 menuItem.addActionListener(new ActionListener() { 272 public void actionPerformed(ActionEvent e) { 273 ArrayList debuggersToRemove = new ArrayList(); 274 for (int index=0; index < tabbedPane.getComponentCount()-1; index++) { 276 EnhancedDebugger debugger = (EnhancedDebugger)debuggers.get(index); 277 if (!debugger.isConnectionActive()) { 278 debugger.cancel(); 280 debuggersToRemove.add(debugger); 281 } 282 } 283 for (Iterator it=debuggersToRemove.iterator(); it.hasNext();) { 284 EnhancedDebugger debugger = (EnhancedDebugger)it.next(); 285 tabbedPane.remove(debugger.tabbedPane); 287 debuggers.remove(debugger); 288 } 289 frame.setTitle( 291 "Smack Debug Window -- Total connections: " 292 + (tabbedPane.getComponentCount() - 1)); 293 } 294 }); 295 menu.add(menuItem); 296 tabbedPane.addMouseListener(new PopupListener(menu)); 298 299 frame.getContentPane().add(tabbedPane); 300 301 frame.setSize(650, 400); 302 frame.setVisible(true); 303 304 } 305 306 312 public void rootWindowClosing(WindowEvent evt) { 313 for (Iterator it = debuggers.iterator(); it.hasNext();) { 315 EnhancedDebugger debugger = (EnhancedDebugger)it.next(); 316 debugger.cancel(); 317 } 318 debuggers.removeAll(debuggers); 320 instance = null; 322 } 323 324 327 private class PopupListener extends MouseAdapter { 328 JPopupMenu popup; 329 330 PopupListener(JPopupMenu popupMenu) { 331 popup = popupMenu; 332 } 333 334 public void mousePressed(MouseEvent e) { 335 maybeShowPopup(e); 336 } 337 338 public void mouseReleased(MouseEvent e) { 339 maybeShowPopup(e); 340 } 341 342 private void maybeShowPopup(MouseEvent e) { 343 if (e.isPopupTrigger()) { 344 popup.show(e.getComponent(), e.getX(), e.getY()); 345 } 346 } 347 } 348 } 349 | Popular Tags |