1 |
ajm |
1.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 |
ajm |
1.2 |
import javax.swing.border.*; |
10 |
|
|
import java.awt.Color; |
11 |
ajm |
1.1 |
|
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 |
ajm |
1.3 |
* @version $Id: SimpleSwingLogger.java,v 1.2 2000/11/27 21:57:08 ajm4 Exp $ |
21 |
ajm |
1.1 |
*/ |
22 |
ajm |
1.2 |
class SimpleSwingLogger extends JFrame implements LoggerImpl { |
23 |
ajm |
1.1 |
|
24 |
|
|
//---FINAL ATTRIBUTES--- |
25 |
|
|
|
26 |
|
|
/** |
27 |
|
|
* The current CVS revision of this class |
28 |
|
|
*/ |
29 |
ajm |
1.3 |
public final String REVISION = "$Revision: 1.2 $"; |
30 |
ajm |
1.1 |
|
31 |
ajm |
1.2 |
private final int width = 700; |
32 |
|
|
private final int height = 400; |
33 |
|
|
|
34 |
ajm |
1.1 |
//---STATIC METHODS--- |
35 |
|
|
|
36 |
|
|
//---CONSTRUCTORS--- |
37 |
|
|
|
38 |
|
|
/** |
39 |
ajm |
1.2 |
* Creates a new Simple Swing Logger. |
40 |
ajm |
1.1 |
*/ |
41 |
ajm |
1.2 |
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 |
ajm |
1.3 |
JLabel comment = new JLabel(" I-Scream System Logging Console"); |
55 |
|
|
comment.setForeground( new Color(0, 0, 102)); |
56 |
ajm |
1.2 |
iscream.setBackground(Color.white); |
57 |
ajm |
1.3 |
comment.setBackground(Color.white); |
58 |
ajm |
1.2 |
Box header = Box.createHorizontalBox(); |
59 |
ajm |
1.3 |
header.add(comment); |
60 |
ajm |
1.2 |
header.add(Box.createHorizontalGlue()); |
61 |
|
|
header.add(iscream); |
62 |
ajm |
1.3 |
//header.add(Box.createHorizontalGlue()); |
63 |
ajm |
1.2 |
|
64 |
|
|
// set up the text area |
65 |
|
|
_textArea.setEditable(false); |
66 |
|
|
JScrollPane text = new JScrollPane(_textArea); |
67 |
|
|
text.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 102)), " Messages ")); |
68 |
|
|
text.setBackground(Color.white); |
69 |
|
|
|
70 |
|
|
// build the frame |
71 |
|
|
box.add(header); |
72 |
|
|
box.add(text); |
73 |
|
|
getContentPane().add(box); |
74 |
|
|
|
75 |
|
|
// show the window |
76 |
|
|
show(); |
77 |
|
|
write(this.toString(), Logger.SYSINIT, "started"); |
78 |
ajm |
1.1 |
write(this.toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
//---PUBLIC METHODS--- |
82 |
|
|
|
83 |
|
|
/** |
84 |
ajm |
1.2 |
* The write() method takes a message, formats it using the |
85 |
|
|
* formatLogLine() method, and then outputs it to the screen |
86 |
|
|
* using the text area. The source is usually the |
87 |
|
|
* calling object, referenced by `this'. The method has been |
88 |
|
|
* made synchronized to avoid it being called by two different |
89 |
|
|
* objects and the output ending up merged on the screen. |
90 |
|
|
* |
91 |
|
|
* @param source A string representation of the calling object. |
92 |
|
|
* @param verbosity the verbosity of this message |
93 |
|
|
* @param message The text to be logged. |
94 |
|
|
*/ |
95 |
ajm |
1.1 |
public synchronized void write(String source, int verbosity, String message) { |
96 |
ajm |
1.2 |
if (verbosity <= _verbosityLevel) { |
97 |
|
|
String line = formatLogLine(source, message); |
98 |
|
|
_textArea.insert(line + "\n",0); |
99 |
ajm |
1.1 |
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
104 |
|
|
* method to provide clean logging (every class should have this). |
105 |
|
|
* |
106 |
|
|
* @return the name of this class and its CVS revision |
107 |
|
|
*/ |
108 |
|
|
public String toString() { |
109 |
|
|
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
110 |
|
|
//return "ScreenLogger" + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
111 |
|
|
} |
112 |
|
|
//---PRIVATE METHODS--- |
113 |
|
|
|
114 |
ajm |
1.2 |
/** |
115 |
|
|
* This method generates a nicely formatted line for the log, |
116 |
|
|
* including the date/time and the source of the message. The date |
117 |
|
|
* and time are formatted using the DateFormat class, and the source |
118 |
|
|
* class is formatted using the toString() method found in every |
119 |
|
|
* source file. This is then prepended to the message and returned. |
120 |
|
|
* |
121 |
|
|
* @param source A string representation of the calling object. |
122 |
|
|
* @param message The message to be logged. |
123 |
|
|
* @return The string to be written to the log. |
124 |
|
|
*/ |
125 |
ajm |
1.1 |
private String formatLogLine(String source, String message){ |
126 |
|
|
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(new Date()); |
127 |
|
|
return "[" + date + "] " + source + ": " + message; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
131 |
|
|
|
132 |
ajm |
1.2 |
//---ATTRIBUTES--- |
133 |
ajm |
1.1 |
|
134 |
ajm |
1.2 |
/** |
135 |
|
|
* A text area to write log messages to |
136 |
|
|
*/ |
137 |
ajm |
1.1 |
private JTextArea _textArea = new JTextArea(); |
138 |
|
|
|
139 |
|
|
|
140 |
ajm |
1.2 |
/** |
141 |
|
|
* The verbosity level of this instance |
142 |
|
|
*/ |
143 |
|
|
private int _verbosityLevel; |
144 |
ajm |
1.1 |
|
145 |
|
|
//---STATIC ATTRIBUTES--- |
146 |
|
|
|
147 |
|
|
} |