KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > qfs > apps > qflog > logview > LogDetailView


1 // {{{ copyright
2

3 /********************************************************************
4  *
5  * $Id: LogDetailView.java,v 1.13 2000/07/05 14:07:44 gs Exp $
6  *
7  * The contents of this file are subject to the Mozilla Public
8  * License Version 1.1 (the "License"); you may not use this file
9  * except in compliance with the License. You may obtain a copy of
10  * the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS
13  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  * implied. See the License for the specific language governing
15  * rights and limitations under the License.
16  *
17  * The Original Code is qfs.de code.
18  *
19  * The Initial Developer of the Original Code is Gregor Schmid.
20  * Portions created by Gregor Schmid are
21  * Copyright (C) 1999 Quality First Software, Gregor Schmid.
22  * All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  *******************************************************************/

27
28 // }}}
29

30 package de.qfs.apps.qflog.logview;
31
32 // {{{ imports
33

34 import java.awt.Color JavaDoc;
35 import java.awt.GridBagLayout JavaDoc;
36 import java.awt.GridBagConstraints JavaDoc;
37 import java.awt.BorderLayout JavaDoc;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.FocusEvent JavaDoc;
41 import java.awt.event.FocusListener JavaDoc;
42 import java.awt.event.KeyAdapter JavaDoc;
43 import java.awt.event.KeyEvent JavaDoc;
44
45 import java.text.SimpleDateFormat JavaDoc;
46
47 import java.util.Date JavaDoc;
48
49 import javax.swing.BorderFactory JavaDoc;
50 import javax.swing.Icon JavaDoc;
51 import javax.swing.JLabel JavaDoc;
52 import javax.swing.JPanel JavaDoc;
53 import javax.swing.JScrollPane JavaDoc;
54 import javax.swing.JTextArea JavaDoc;
55 import javax.swing.KeyStroke JavaDoc;
56 import javax.swing.border.Border JavaDoc;
57
58 import de.qfs.lib.gui.Borders;
59 import de.qfs.lib.gui.SwingUtil;
60 import de.qfs.lib.log.Log;
61 import de.qfs.lib.log.LogEntry;
62 import de.qfs.lib.log.Logger;
63 import de.qfs.lib.util.MapResourceBundle;
64
65 // }}}
66

67 /**
68  * Simple view that displays the details of a LogEntry.
69  *
70  * @author Gregor Schmid
71  * @version $Revision: 1.13 $
72  */

73 public class LogDetailView extends JPanel JavaDoc
74 {
75     // {{{ variables
76

77     /**
78      * The Logger used for logging.
79      */

80     private final static Logger logger = new Logger (LogDetailView.class);
81
82     /**
83      * DateFormat used for timestamp output.
84      */

85     private static SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("HH:mm:ss.SSS");
86
87     /**
88      * The names of the icon resources.
89      */

90     private final static String JavaDoc iconNames[] = {
91         "logTableView.icon.err", "logTableView.icon.errDetail",
92         "logTableView.icon.wrn", "logTableView.icon.wrnDetail",
93         "logTableView.icon.msg", "logTableView.icon.msgDetail",
94         "logTableView.icon.mtd", "logTableView.icon.mtdDetail",
95         "logTableView.icon.dbg", "logTableView.icon.dbgDetail"};
96
97     /**
98      * The icons to render.
99      */

100     private final static Icon JavaDoc[] icons = new Icon JavaDoc[iconNames.length];
101
102     /**
103      * Label to display the level.
104      */

105     JLabel JavaDoc level;
106
107     /**
108      * Label to display the timestamp.
109      */

110     JLabel JavaDoc time;
111
112     /**
113      * Label to display the thread.
114      */

115     JLabel JavaDoc thread;
116
117     /**
118      * Label to display the class.
119      */

120     JLabel JavaDoc clazz;
121
122     /**
123      * Label to display the method.
124      */

125     JLabel JavaDoc method;
126
127     /**
128      * Text area to display the message.
129      */

130     MessageView detail;
131
132     /**
133      * Whether logging is enabled (true) or disabled (false);
134      */

135     private boolean logging;
136
137     /**
138      * The resources to use.
139      */

140     private transient MapResourceBundle resources;
141
142     // }}}
143

144     // {{{ constructor
145

146     /**
147      * Create a new LogDetailView
148      */

149     public LogDetailView ()
150     {
151     }
152
153     // }}}
154

155     // {{{ setLoggingEnabled
156

157     /**
158      * Enable or disable logging output caused directly or indirectly by
159      * LogDetailView methods. If logging is disabled, all calls to methods
160      * that might create log messages will be protected with {@link
161      * de.qfs.lib.log.Log#excludeThread de.qfs.lib.log.Log.excludeThread}.
162      * This is necessary, if the LogFilterTreeModel is used inside the
163      * application whose logs it is supposed to filter. <p>
164      *
165      * The default value is false.
166      *
167      * @param enable True to enable logging, false to disable it.
168      */

169     public final void setLoggingEnabled(boolean enable)
170     {
171         logging = enable;
172     }
173
174     // }}}
175

176     // {{{ init
177

178     /**
179      * Initialize the view.
180      */

181     protected void init()
182     {
183         if (logging && logger.level >= Log.MTD) {
184             logger.log(Log.MTD, "init()", "");
185         }
186
187         initIcons();
188
189         setLayout(new BorderLayout JavaDoc ());
190
191         JPanel JavaDoc top = new JPanel JavaDoc ();
192         top.setLayout(new BorderLayout JavaDoc ());
193         top.setBorder(null);
194
195         String JavaDoc title = resources.getString("logTableView.detail.title",
196                                            "Detail");
197         JLabel JavaDoc titleLabel = new JLabel JavaDoc (title);
198         titleLabel.setBorder(BorderFactory.createEtchedBorder());
199         top.add(titleLabel, BorderLayout.NORTH);
200
201         add(top, BorderLayout.NORTH);
202
203         JPanel JavaDoc center = new JPanel JavaDoc ();
204         center.setBorder(BorderFactory.createCompoundBorder
205                          (BorderFactory.createLoweredBevelBorder(),
206                           BorderFactory.createEmptyBorder(5, 5, 5, 5)));
207
208         GridBagLayout JavaDoc layout = new GridBagLayout JavaDoc ();
209         center.setLayout(layout);
210
211         GridBagConstraints JavaDoc cons = new GridBagConstraints JavaDoc();
212
213         level = new JLabel JavaDoc (" ");
214         cons.gridx = 0;
215         cons.gridy = 0;
216         cons.gridwidth = 1;
217         cons.weightx = 1.0;
218         cons.weighty = 0;
219         cons.anchor = GridBagConstraints.NORTHWEST;
220         layout.setConstraints(level, cons);
221         center.add(level);
222
223         time = new JLabel JavaDoc (" ");
224         cons.gridx = 1;
225         cons.gridy = 0;
226         cons.gridwidth = 1;
227         cons.weightx = 1.0;
228         cons.weighty = 0;
229         cons.anchor = GridBagConstraints.NORTHWEST;
230         layout.setConstraints(time, cons);
231         center.add(time);
232
233         thread = new JLabel JavaDoc (" ");
234         cons.gridx = 2;
235         cons.gridy = 0;
236         cons.gridwidth = 2;
237         cons.weightx = 2.0;
238         cons.weighty = 0;
239         cons.anchor = GridBagConstraints.NORTHWEST;
240         layout.setConstraints(thread, cons);
241         center.add(thread);
242
243         clazz = new JLabel JavaDoc (" ");
244         cons.gridx = 0;
245         cons.gridy = 1;
246         cons.gridwidth = 2;
247         cons.weightx = 2.0;
248         cons.weighty = 0;
249         cons.anchor = GridBagConstraints.NORTHWEST;
250         layout.setConstraints(clazz, cons);
251         center.add(clazz);
252
253         method = new JLabel JavaDoc (" ");
254         cons.gridx = 2;
255         cons.gridy = 1;
256         cons.gridwidth = 2;
257         cons.weightx = 2.0;
258         cons.weighty = 0;
259         cons.anchor = GridBagConstraints.NORTHWEST;
260         layout.setConstraints(method, cons);
261         center.add(method);
262
263         detail = new MessageView ("");
264         cons.gridx = 0;
265         cons.gridy = 2;
266         cons.gridwidth = 4;
267         cons.weightx = 4.0;
268         cons.weighty = 1.0;
269         cons.fill = GridBagConstraints.BOTH;
270         cons.anchor = GridBagConstraints.NORTHWEST;
271         layout.setConstraints(detail, cons);
272         center.add(detail);
273
274         add(center, BorderLayout.CENTER);
275     }
276
277     // }}}
278
// {{{ cleanup
279

280     /**
281      * Clear everything that might prevent garbage collection.
282      */

283     public void cleanup()
284     {
285         if (logging && logger.level >= Log.MTD) {
286             logger.log(Log.MTD, "cleanup()", "");
287         }
288
289         SwingUtil.cleanup(this);
290
291         detail.cleanup();
292         detail = null;
293
294         resources = null;
295     }
296
297     // }}}
298
// {{{ finalize
299

300     /**
301      * Finalize the LogDetailView.
302      */

303     public void finalize()
304     {
305         if (logging && logger.level >= Log.MTD) {
306             logger.log(Log.MTD, "finalize()", "");
307         }
308     }
309
310     // }}}
311

312     // {{{ setResources
313

314     /**
315      * Set the resources to get the icons from.
316      *
317      * @param rb MapResourceBundle containing the resources to use.
318      */

319     public void setResources(MapResourceBundle rb)
320     {
321         resources = rb;
322     }
323
324     // }}}
325
// {{{ setEntry
326

327     /**
328      * Set the entry to display.
329      *
330      * @param entry The LogEntry to display.
331      */

332     public void setEntry(LogEntry entry)
333     {
334         if (entry != null) {
335             Icon JavaDoc icon = null;
336             if (icons[0] != null
337                 && entry.getLevel() >= 0
338                 && entry.getLevel() < icons.length) {
339                 icon = icons[entry.getLevel() - 1];
340             }
341             level.setIcon(icon);
342             level.setText(icon == null ? "" + entry.getLevel() : null);
343             time.setText(df.format (new Date JavaDoc (entry.getTimestamp())));
344             thread.setText(entry.getThread());
345             method.setText(entry.getMethod());
346             clazz.setText(entry.getClazz());
347             detail.getTextArea().setText(entry.getMessage());
348             detail.getTextArea().setCaretPosition(0);
349         } else {
350             level.setText(" ");
351             time.setText(" ");
352             thread.setText(" ");
353             method.setText(" ");
354             clazz.setText(" ");
355             detail.getTextArea().setText(" ");
356         }
357     }
358
359     // }}}
360

361     // {{{ initIcons
362

363     /**
364      * Fetch the icons.
365      */

366     private void initIcons()
367     {
368         if (icons[0] == null && resources != null) {
369             for (int i = 0; i < icons.length; i++) {
370                 icons[i] = resources.getIcon(iconNames[i], null);
371             }
372         }
373     }
374
375     // }}}
376

377     // {{{ class MessageView
378

379     // {{{ static MessageView variables
380

381     /**
382      * The Logger used for logging MessageView methods.
383      */

384     private final static Logger mvLogger = new Logger (MessageView.class);
385
386     // }}}
387

388     /**
389      * Scroll pane with a text area that draws a border around itself, when
390      * the text area has the focus.
391      */

392     private class MessageView
393         extends JScrollPane JavaDoc
394         implements FocusListener JavaDoc
395     {
396         // {{{ variables
397

398         /**
399          * The non focus border.
400          */

401         private Border JavaDoc defaultBorder;
402
403         /**
404          * The border used when the MessageView has the focus.
405          */

406         private Border JavaDoc focusBorder;
407
408         /**
409          * The JTextArea of the MessageView.
410          */

411         private JTextArea JavaDoc textArea;
412
413         /**
414          * Listner for scrolling.
415          */

416         private KeyAdapter JavaDoc scrollListener = new KeyAdapter JavaDoc () {
417             public void keyPressed (KeyEvent JavaDoc e) {
418                 if (e.getKeyCode() == e.VK_UP &&
419                     e.getModifiers() == 0) {
420                     SwingUtil.scrollDownLine(MessageView.this);
421                     e.consume();
422                 } else if (e.getKeyCode() == e.VK_DOWN &&
423                            e.getModifiers() == 0) {
424                     SwingUtil.scrollUpLine(MessageView.this);
425                     e.consume();
426                 }
427             }
428         };
429
430         // }}}
431

432         // {{{ constructor
433

434         /**
435          * Create a new MessageView.
436          *
437          * @param contents The text to display in the view.
438          */

439         public MessageView (String JavaDoc contents)
440         {
441             textArea = new JTextArea JavaDoc (contents) {
442                 public boolean isManagingFocus()
443                 {
444                     return false;
445                 }
446             };
447             textArea.setLineWrap(true);
448             textArea.setEditable(false);
449             textArea.addFocusListener(this);
450
451             setViewportView(textArea);
452
453             defaultBorder = BorderFactory.createCompoundBorder
454                 (BorderFactory.createEmptyBorder(1, 1, 1, 1),
455                  BorderFactory.createLoweredBevelBorder());
456             focusBorder = BorderFactory.createCompoundBorder
457                 (Borders.STIPPLE,
458                  BorderFactory.createLoweredBevelBorder());
459             this.setBorder(defaultBorder);
460
461             textArea.addKeyListener(scrollListener);
462         }
463
464         // }}}
465

466         // {{{ getTextArea
467

468         /**
469          * Get the JTextArea of the MessageView.
470          *
471          * @return The JTextArea of the MessageView.
472          */

473         public JTextArea JavaDoc getTextArea()
474         {
475             return textArea;
476         }
477
478         // }}}
479
// {{{ focusGained
480

481         /**
482          * Draw a focus border around the MessageView.
483          */

484         public void focusGained(FocusEvent JavaDoc e)
485         {
486             this.setBorder(focusBorder);
487         }
488
489
490         // }}}
491
// {{{ focusLost
492

493         /**
494          * Remove the focus border from the MessageView.
495          */

496         public void focusLost(FocusEvent JavaDoc e)
497         {
498             this.setBorder(defaultBorder);
499         }
500
501         // }}}
502

503         // {{{ cleanup
504

505         /**
506          * Clear everything that might prevent garbage collection.
507          */

508         public void cleanup()
509         {
510             if (logging && mvLogger.level >= Log.MTD) {
511                 mvLogger.log(Log.MTD, "cleanup()", "");
512             }
513             setViewportView(null);
514             this.setBorder(null);
515             defaultBorder = null;
516             focusBorder = null;
517
518             textArea.removeKeyListener(scrollListener);
519             scrollListener = null;
520             textArea.removeFocusListener(this);
521             textArea = null;
522
523             resources = null;
524         }
525
526         // }}}
527
// {{{ finalize
528

529         /**
530          * Finalize the MessageView.
531          */

532         public void finalize()
533         {
534             if (logging && mvLogger.level >= Log.MTD) {
535                 mvLogger.log(Log.MTD, "finalize()", "");
536             }
537         }
538
539         // }}}
540
}
541
542     // }}}
543
}
544
Popular Tags