KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > debugger > LiteDebugger


1 /**
2  * $RCSfile$
3  * $Revision: 2730 $
4  * $Date: 2005-08-26 23:23:36 -0300 (Fri, 26 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

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 /**
35  * The LiteDebugger is a very simple debugger that allows to debug sent, received and
36  * interpreted messages.
37  *
38  * @author Gaston Dombiak
39  */

40 public class LiteDebugger implements SmackDebugger {
41
42     private static final String JavaDoc 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     /**
62      * Creates the debug process, which is a GUI window that displays XML traffic.
63      */

64     private void createDebug() {
65         frame = new JFrame("Smack Debug Window -- " + connection.getServiceName() + ":" +
66                 connection.getPort());
67
68         // Add listener for window closing event
69
frame.addWindowListener(new WindowAdapter() {
70             public void windowClosing(WindowEvent evt) {
71                 rootWindowClosing(evt);
72             }
73         });
74
75         // We'll arrange the UI into four tabs. The first tab contains all data, the second
76
// client generated XML, the third server generated XML, and the fourth is packet
77
// data from the server as seen by Smack.
78
JTabbedPane tabbedPane = new JTabbedPane();
79
80         JPanel allPane = new JPanel();
81         allPane.setLayout(new GridLayout(3, 1));
82         tabbedPane.add("All", allPane);
83
84         // Create UI elements for client generated XML traffic.
85
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         // Add pop-up menu.
95
JPopupMenu menu = new JPopupMenu();
96         JMenuItem menuItem1 = new JMenuItem("Copy");
97         menuItem1.addActionListener(new ActionListener() {
98             public void actionPerformed(ActionEvent e) {
99                 // Get the clipboard
100
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
101                 // Set the sent text as the new content of the clipboard
102
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         // Add listener to the text area so the popup menu can come up.
115
MouseListener popupListener = new PopupListener(menu);
116         sentText1.addMouseListener(popupListener);
117         sentText2.addMouseListener(popupListener);
118         menu.add(menuItem1);
119         menu.add(menuItem2);
120
121         // Create UI elements for server generated XML traffic.
122
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         // Add pop-up menu.
132
menu = new JPopupMenu();
133         menuItem1 = new JMenuItem("Copy");
134         menuItem1.addActionListener(new ActionListener() {
135             public void actionPerformed(ActionEvent e) {
136                 // Get the clipboard
137
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
138                 // Set the sent text as the new content of the clipboard
139
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         // Add listener to the text area so the popup menu can come up.
152
popupListener = new PopupListener(menu);
153         receivedText1.addMouseListener(popupListener);
154         receivedText2.addMouseListener(popupListener);
155         menu.add(menuItem1);
156         menu.add(menuItem2);
157
158         // Create UI elements for interpreted XML traffic.
159
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         // Add pop-up menu.
169
menu = new JPopupMenu();
170         menuItem1 = new JMenuItem("Copy");
171         menuItem1.addActionListener(new ActionListener() {
172             public void actionPerformed(ActionEvent e) {
173                 // Get the clipboard
174
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
175                 // Set the sent text as the new content of the clipboard
176
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         // Add listener to the text area so the popup menu can come up.
189
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         // Create a special Reader that wraps the main Reader and logs data to the GUI.
201
ObservableReader debugReader = new ObservableReader(reader);
202         readerListener = new ReaderListener() {
203                     public void read(String JavaDoc 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         // Create a special Writer that wraps the main Writer and logs data to the GUI.
224
ObservableWriter debugWriter = new ObservableWriter(writer);
225         writerListener = new WriterListener() {
226                     public void write(String JavaDoc 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         // Assign the reader/writer objects to use the debug versions. The packet reader
238
// and writer will use the debug versions when they are created.
239
reader = debugReader;
240         writer = debugWriter;
241
242         // Create a thread that will listen for all incoming packets and write them to
243
// the GUI. This is what we call "interpreted" packet data, since it's the packet
244
// data as Smack sees it and not as it's coming in as raw XML.
245
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     /**
256      * Notification that the root window is closing. Stop listening for received and
257      * transmitted packets.
258      *
259      * @param evt the event that indicates that the root window is closing
260      */

261     public void rootWindowClosing(WindowEvent evt) {
262         connection.removePacketListener(listener);
263         ((ObservableReader)reader).removeReaderListener(readerListener);
264         ((ObservableWriter)writer).removeWriterListener(writerListener);
265     }
266
267     /**
268      * Listens for debug window popup dialog events.
269      */

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 JavaDoc user) {
309         boolean isAnonymous = "".equals(StringUtils.parseName(user));
310         String JavaDoc 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