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