1*cdf0e10cSrcweir import java.awt.Font;
2*cdf0e10cSrcweir import java.awt.Rectangle;
3*cdf0e10cSrcweir import java.awt.Color;
4*cdf0e10cSrcweir import java.awt.Graphics;
5*cdf0e10cSrcweir import javax.swing.JScrollPane;
6*cdf0e10cSrcweir import javax.swing.JTextArea;
7*cdf0e10cSrcweir import javax.swing.JScrollBar;
8*cdf0e10cSrcweir 
9*cdf0e10cSrcweir 
10*cdf0e10cSrcweir 
11*cdf0e10cSrcweir /** A message area displays text in a scrollable text widget.  It is a
12*cdf0e10cSrcweir     singleton.  Other objects can access it directly to display messages.
13*cdf0e10cSrcweir */
14*cdf0e10cSrcweir public class MessageArea
15*cdf0e10cSrcweir     extends JScrollPane
16*cdf0e10cSrcweir {
17*cdf0e10cSrcweir     public static synchronized MessageArea Instance ()
18*cdf0e10cSrcweir     {
19*cdf0e10cSrcweir         if (saInstance == null)
20*cdf0e10cSrcweir             saInstance = new MessageArea ();
21*cdf0e10cSrcweir         return saInstance;
22*cdf0e10cSrcweir     }
23*cdf0e10cSrcweir 
24*cdf0e10cSrcweir 
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir     /** Create a new message area.  This method is private because the class is
28*cdf0e10cSrcweir         a singleton and may therefore not be instanciated from the outside.
29*cdf0e10cSrcweir     */
30*cdf0e10cSrcweir     private MessageArea ()
31*cdf0e10cSrcweir     {
32*cdf0e10cSrcweir         maText = new JTextArea();
33*cdf0e10cSrcweir         maText.setBackground (new Color (255,250,240));
34*cdf0e10cSrcweir         maText.setFont (new Font ("Helvetica", Font.PLAIN, 9));
35*cdf0e10cSrcweir         setViewportView (maText);
36*cdf0e10cSrcweir         setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
37*cdf0e10cSrcweir         setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir         printMessage (
40*cdf0e10cSrcweir             "class path is " + System.getProperty ("java.class.path") + "\n");
41*cdf0e10cSrcweir     }
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     /** Show the given string at the end of the message area and scroll to make
47*cdf0e10cSrcweir         it visible.
48*cdf0e10cSrcweir     */
49*cdf0e10cSrcweir     public static synchronized void print (String aMessage)
50*cdf0e10cSrcweir     {
51*cdf0e10cSrcweir         print (0, aMessage);
52*cdf0e10cSrcweir     }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     /** Show the given string at the end of the message area and scroll to make
58*cdf0e10cSrcweir         it visible.  Indent the string as requested.
59*cdf0e10cSrcweir     */
60*cdf0e10cSrcweir     public static synchronized void print (int nIndentation, String aMessage)
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir         while (nIndentation-- > 0)
63*cdf0e10cSrcweir             aMessage = " " + aMessage;
64*cdf0e10cSrcweir         Instance().printMessage(aMessage);
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     /** Show the given string at the end of the message area and scroll to make
71*cdf0e10cSrcweir         it visible.
72*cdf0e10cSrcweir     */
73*cdf0e10cSrcweir     public static void println (String aMessage)
74*cdf0e10cSrcweir     {
75*cdf0e10cSrcweir         println (0, aMessage);
76*cdf0e10cSrcweir     }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     /** Show the given string at the end of the message area and scroll to make
82*cdf0e10cSrcweir         it visible.
83*cdf0e10cSrcweir     */
84*cdf0e10cSrcweir     public static void println (int nIndentation, String aMessage)
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         print (nIndentation, aMessage+"\n");
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     public void paintComponent (Graphics g)
93*cdf0e10cSrcweir     {
94*cdf0e10cSrcweir         synchronized (g)
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             JScrollBar sb = getVerticalScrollBar();
97*cdf0e10cSrcweir             if (sb != null)
98*cdf0e10cSrcweir             {
99*cdf0e10cSrcweir                 int nScrollBarValue = sb.getMaximum() - sb.getVisibleAmount() - 1;
100*cdf0e10cSrcweir                 sb.setValue (nScrollBarValue);
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir             super.paintComponent (g);
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     /** Append the given string to the end of the text and scroll so that it
110*cdf0e10cSrcweir         becomes visible.  This is an internal method.  Use one of the static
111*cdf0e10cSrcweir         and public ones.
112*cdf0e10cSrcweir     */
113*cdf0e10cSrcweir     private synchronized void printMessage (String aMessage)
114*cdf0e10cSrcweir     {
115*cdf0e10cSrcweir         maText.append (aMessage);
116*cdf0e10cSrcweir     }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     private static MessageArea saInstance = null;
122*cdf0e10cSrcweir     private JTextArea maText;
123*cdf0e10cSrcweir }
124