1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
import uk.ac.ukc.iscream.core.*; |
5 |
import java.util.Date; |
6 |
import java.text.DateFormat; |
7 |
import java.util.Locale; |
8 |
import javax.swing.*; |
9 |
import javax.swing.border.*; |
10 |
import java.awt.Color; |
11 |
|
12 |
/** |
13 |
* The SimpleGUILogger is an implementation of the LoggerImpl defined |
14 |
* in the associated interface. It's only purpose is to simply print |
15 |
* all the logging information it receives to a window frame. |
16 |
* |
17 |
* All very simple really... |
18 |
* |
19 |
* @author $Author: ajm4 $ |
20 |
* @version $Id: SimpleSwingLogger.java,v 1.1 2000/11/27 17:34:04 ajm4 Exp $ |
21 |
*/ |
22 |
class SimpleSwingLogger extends JFrame implements LoggerImpl { |
23 |
|
24 |
//---FINAL ATTRIBUTES--- |
25 |
|
26 |
/** |
27 |
* The current CVS revision of this class |
28 |
*/ |
29 |
public final String REVISION = "$Revision: 1.1 $"; |
30 |
|
31 |
private final int width = 700; |
32 |
private final int height = 400; |
33 |
|
34 |
//---STATIC METHODS--- |
35 |
|
36 |
//---CONSTRUCTORS--- |
37 |
|
38 |
/** |
39 |
* Creates a new Simple Swing Logger. |
40 |
*/ |
41 |
public SimpleSwingLogger() { |
42 |
_verbosityLevel = Integer.parseInt(System.getProperty("uk.ac.ukc.iscream.Verbosity")); |
43 |
System.out.println(this.toString() + ": opening window"); |
44 |
|
45 |
// set up the Frame |
46 |
setTitle("I-Scream Logger"); |
47 |
setSize(width, height); |
48 |
getContentPane().setBackground(Color.white); |
49 |
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
50 |
Box box = Box.createVerticalBox(); |
51 |
|
52 |
// create the i-scream logo at the top |
53 |
JLabel iscream = new JLabel(new ImageIcon("i-scream.gif")); |
54 |
iscream.setBackground(Color.white); |
55 |
Box header = Box.createHorizontalBox(); |
56 |
header.add(Box.createHorizontalGlue()); |
57 |
header.add(iscream); |
58 |
header.add(Box.createHorizontalGlue()); |
59 |
|
60 |
// set up the text area |
61 |
_textArea.setEditable(false); |
62 |
JScrollPane text = new JScrollPane(_textArea); |
63 |
text.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
64 |
text.setBackground(Color.white); |
65 |
|
66 |
// build the frame |
67 |
box.add(header); |
68 |
box.add(text); |
69 |
getContentPane().add(box); |
70 |
|
71 |
// show the window |
72 |
show(); |
73 |
write(this.toString(), Logger.SYSINIT, "started"); |
74 |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
75 |
} |
76 |
|
77 |
//---PUBLIC METHODS--- |
78 |
|
79 |
/** |
80 |
* The write() method takes a message, formats it using the |
81 |
* formatLogLine() method, and then outputs it to the screen |
82 |
* using the text area. The source is usually the |
83 |
* calling object, referenced by `this'. The method has been |
84 |
* made synchronized to avoid it being called by two different |
85 |
* objects and the output ending up merged on the screen. |
86 |
* |
87 |
* @param source A string representation of the calling object. |
88 |
* @param verbosity the verbosity of this message |
89 |
* @param message The text to be logged. |
90 |
*/ |
91 |
public synchronized void write(String source, int verbosity, String message) { |
92 |
if (verbosity <= _verbosityLevel) { |
93 |
String line = formatLogLine(source, message); |
94 |
_textArea.insert(line + "\n",0); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
100 |
* method to provide clean logging (every class should have this). |
101 |
* |
102 |
* @return the name of this class and its CVS revision |
103 |
*/ |
104 |
public String toString() { |
105 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
106 |
//return "ScreenLogger" + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
107 |
} |
108 |
//---PRIVATE METHODS--- |
109 |
|
110 |
/** |
111 |
* This method generates a nicely formatted line for the log, |
112 |
* including the date/time and the source of the message. The date |
113 |
* and time are formatted using the DateFormat class, and the source |
114 |
* class is formatted using the toString() method found in every |
115 |
* source file. This is then prepended to the message and returned. |
116 |
* |
117 |
* @param source A string representation of the calling object. |
118 |
* @param message The message to be logged. |
119 |
* @return The string to be written to the log. |
120 |
*/ |
121 |
private String formatLogLine(String source, String message){ |
122 |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
123 |
return "[" + date + "] " + source + ": " + message; |
124 |
} |
125 |
|
126 |
//---ACCESSOR/MUTATOR METHODS--- |
127 |
|
128 |
//---ATTRIBUTES--- |
129 |
|
130 |
/** |
131 |
* A text area to write log messages to |
132 |
*/ |
133 |
private JTextArea _textArea = new JTextArea(); |
134 |
|
135 |
|
136 |
/** |
137 |
* The verbosity level of this instance |
138 |
*/ |
139 |
private int _verbosityLevel; |
140 |
|
141 |
//---STATIC ATTRIBUTES--- |
142 |
|
143 |
} |