1 20 21 package org.jivesoftware.smack.debugger; 22 23 import java.awt.*; 24 import java.awt.datatransfer.*; 25 import java.awt.event.*; 26 import java.io.*; 27 28 import javax.swing.*; 29 30 import org.jivesoftware.smack.*; 31 import org.jivesoftware.smack.packet.*; 32 import org.jivesoftware.smack.util.*; 33 34 40 public class LiteDebugger implements SmackDebugger { 41 42 private static final String NEWLINE = "\n"; 43 44 private JFrame frame = null; 45 private XMPPConnection connection = null; 46 47 private PacketListener listener = null; 48 49 private Writer writer; 50 private Reader reader; 51 private ReaderListener readerListener; 52 private WriterListener writerListener; 53 54 public LiteDebugger(XMPPConnection connection, Writer writer, Reader reader) { 55 this.connection = connection; 56 this.writer = writer; 57 this.reader = reader; 58 createDebug(); 59 } 60 61 64 private void createDebug() { 65 frame = new JFrame("Smack Debug Window -- " + connection.getServiceName() + ":" + 66 connection.getPort()); 67 68 frame.addWindowListener(new WindowAdapter() { 70 public void windowClosing(WindowEvent evt) { 71 rootWindowClosing(evt); 72 } 73 }); 74 75 JTabbedPane tabbedPane = new JTabbedPane(); 79 80 JPanel allPane = new JPanel(); 81 allPane.setLayout(new GridLayout(3, 1)); 82 tabbedPane.add("All", allPane); 83 84 final JTextArea sentText1 = new JTextArea(); 86 final JTextArea sentText2 = new JTextArea(); 87 sentText1.setEditable(false); 88 sentText2.setEditable(false); 89 sentText1.setForeground(new Color(112, 3, 3)); 90 sentText2.setForeground(new Color(112, 3, 3)); 91 allPane.add(new JScrollPane(sentText1)); 92 tabbedPane.add("Sent", new JScrollPane(sentText2)); 93 94 JPopupMenu menu = new JPopupMenu(); 96 JMenuItem menuItem1 = new JMenuItem("Copy"); 97 menuItem1.addActionListener(new ActionListener() { 98 public void actionPerformed(ActionEvent e) { 99 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 101 clipboard.setContents(new StringSelection(sentText1.getText()), null); 103 } 104 }); 105 106 JMenuItem menuItem2 = new JMenuItem("Clear"); 107 menuItem2.addActionListener(new ActionListener() { 108 public void actionPerformed(ActionEvent e) { 109 sentText1.setText(""); 110 sentText2.setText(""); 111 } 112 }); 113 114 MouseListener popupListener = new PopupListener(menu); 116 sentText1.addMouseListener(popupListener); 117 sentText2.addMouseListener(popupListener); 118 menu.add(menuItem1); 119 menu.add(menuItem2); 120 121 final JTextArea receivedText1 = new JTextArea(); 123 final JTextArea receivedText2 = new JTextArea(); 124 receivedText1.setEditable(false); 125 receivedText2.setEditable(false); 126 receivedText1.setForeground(new Color(6, 76, 133)); 127 receivedText2.setForeground(new Color(6, 76, 133)); 128 allPane.add(new JScrollPane(receivedText1)); 129 tabbedPane.add("Received", new JScrollPane(receivedText2)); 130 131 menu = new JPopupMenu(); 133 menuItem1 = new JMenuItem("Copy"); 134 menuItem1.addActionListener(new ActionListener() { 135 public void actionPerformed(ActionEvent e) { 136 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 138 clipboard.setContents(new StringSelection(receivedText1.getText()), null); 140 } 141 }); 142 143 menuItem2 = new JMenuItem("Clear"); 144 menuItem2.addActionListener(new ActionListener() { 145 public void actionPerformed(ActionEvent e) { 146 receivedText1.setText(""); 147 receivedText2.setText(""); 148 } 149 }); 150 151 popupListener = new PopupListener(menu); 153 receivedText1.addMouseListener(popupListener); 154 receivedText2.addMouseListener(popupListener); 155 menu.add(menuItem1); 156 menu.add(menuItem2); 157 158 final JTextArea interpretedText1 = new JTextArea(); 160 final JTextArea interpretedText2 = new JTextArea(); 161 interpretedText1.setEditable(false); 162 interpretedText2.setEditable(false); 163 interpretedText1.setForeground(new Color(1, 94, 35)); 164 interpretedText2.setForeground(new Color(1, 94, 35)); 165 allPane.add(new JScrollPane(interpretedText1)); 166 tabbedPane.add("Interpreted", new JScrollPane(interpretedText2)); 167 168 menu = new JPopupMenu(); 170 menuItem1 = new JMenuItem("Copy"); 171 menuItem1.addActionListener(new ActionListener() { 172 public void actionPerformed(ActionEvent e) { 173 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 175 clipboard.setContents(new StringSelection(interpretedText1.getText()), null); 177 } 178 }); 179 180 menuItem2 = new JMenuItem("Clear"); 181 menuItem2.addActionListener(new ActionListener() { 182 public void actionPerformed(ActionEvent e) { 183 interpretedText1.setText(""); 184 interpretedText2.setText(""); 185 } 186 }); 187 188 popupListener = new PopupListener(menu); 190 interpretedText1.addMouseListener(popupListener); 191 interpretedText2.addMouseListener(popupListener); 192 menu.add(menuItem1); 193 menu.add(menuItem2); 194 195 frame.getContentPane().add(tabbedPane); 196 197 frame.setSize(550, 400); 198 frame.setVisible(true); 199 200 ObservableReader debugReader = new ObservableReader(reader); 202 readerListener = new ReaderListener() { 203 public void read(String str) { 204 int index = str.lastIndexOf(">"); 205 if (index != -1) { 206 receivedText1.append(str.substring(0, index + 1)); 207 receivedText2.append(str.substring(0, index + 1)); 208 receivedText1.append(NEWLINE); 209 receivedText2.append(NEWLINE); 210 if (str.length() > index) { 211 receivedText1.append(str.substring(index + 1)); 212 receivedText2.append(str.substring(index + 1)); 213 } 214 } 215 else { 216 receivedText1.append(str); 217 receivedText2.append(str); 218 } 219 } 220 }; 221 debugReader.addReaderListener(readerListener); 222 223 ObservableWriter debugWriter = new ObservableWriter(writer); 225 writerListener = new WriterListener() { 226 public void write(String str) { 227 sentText1.append(str); 228 sentText2.append(str); 229 if (str.endsWith(">")) { 230 sentText1.append(NEWLINE); 231 sentText2.append(NEWLINE); 232 } 233 } 234 }; 235 debugWriter.addWriterListener(writerListener); 236 237 reader = debugReader; 240 writer = debugWriter; 241 242 listener = new PacketListener() { 246 public void processPacket(Packet packet) { 247 interpretedText1.append(packet.toXML()); 248 interpretedText2.append(packet.toXML()); 249 interpretedText1.append(NEWLINE); 250 interpretedText2.append(NEWLINE); 251 } 252 }; 253 } 254 255 261 public void rootWindowClosing(WindowEvent evt) { 262 connection.removePacketListener(listener); 263 ((ObservableReader)reader).removeReaderListener(readerListener); 264 ((ObservableWriter)writer).removeWriterListener(writerListener); 265 } 266 267 270 private class PopupListener extends MouseAdapter { 271 JPopupMenu popup; 272 273 PopupListener(JPopupMenu popupMenu) { 274 popup = popupMenu; 275 } 276 277 public void mousePressed(MouseEvent e) { 278 maybeShowPopup(e); 279 } 280 281 public void mouseReleased(MouseEvent e) { 282 maybeShowPopup(e); 283 } 284 285 private void maybeShowPopup(MouseEvent e) { 286 if (e.isPopupTrigger()) { 287 popup.show(e.getComponent(), e.getX(), e.getY()); 288 } 289 } 290 } 291 292 public Reader newConnectionReader(Reader newReader) { 293 ((ObservableReader)reader).removeReaderListener(readerListener); 294 ObservableReader debugReader = new ObservableReader(newReader); 295 debugReader.addReaderListener(readerListener); 296 reader = debugReader; 297 return reader; 298 } 299 300 public Writer newConnectionWriter(Writer newWriter) { 301 ((ObservableWriter)writer).removeWriterListener(writerListener); 302 ObservableWriter debugWriter = new ObservableWriter(newWriter); 303 debugWriter.addWriterListener(writerListener); 304 writer = debugWriter; 305 return writer; 306 } 307 308 public void userHasLogged(String user) { 309 boolean isAnonymous = "".equals(StringUtils.parseName(user)); 310 String title = 311 "Smack Debug Window -- " 312 + (isAnonymous ? "" : StringUtils.parseBareAddress(user)) 313 + "@" 314 + connection.getServiceName() 315 + ":" 316 + connection.getPort(); 317 title += "/" + StringUtils.parseResource(user); 318 frame.setTitle(title); 319 } 320 321 public Reader getReader() { 322 return reader; 323 } 324 325 public Writer getWriter() { 326 return writer; 327 } 328 329 public PacketListener getReaderListener() { 330 return listener; 331 } 332 333 public PacketListener getWriterListener() { 334 return null; 335 } 336 } 337 | Popular Tags |