1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 import java.awt.Font;
23 import java.awt.Rectangle;
24 import java.awt.Color;
25 import java.awt.Graphics;
26 import javax.swing.JScrollPane;
27 import javax.swing.JTextArea;
28 import javax.swing.JScrollBar;
29 
30 
31 
32 /** A message area displays text in a scrollable text widget.  It is a
33     singleton.  Other objects can access it directly to display messages.
34 */
35 public class MessageArea
36     extends JScrollPane
37 {
Instance()38     public static synchronized MessageArea Instance ()
39     {
40         if (saInstance == null)
41             saInstance = new MessageArea ();
42         return saInstance;
43     }
44 
45 
46 
47 
48     /** Create a new message area.  This method is private because the class is
49         a singleton and may therefore not be instanciated from the outside.
50     */
MessageArea()51     private MessageArea ()
52     {
53         maText = new JTextArea();
54         maText.setBackground (new Color (255,250,240));
55         maText.setFont (new Font ("Helvetica", Font.PLAIN, 9));
56         setViewportView (maText);
57         setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
58         setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
59 
60         printMessage (
61             "class path is " + System.getProperty ("java.class.path") + "\n");
62     }
63 
64 
65 
66 
67     /** Show the given string at the end of the message area and scroll to make
68         it visible.
69     */
print(String aMessage)70     public static synchronized void print (String aMessage)
71     {
72         print (0, aMessage);
73     }
74 
75 
76 
77 
78     /** Show the given string at the end of the message area and scroll to make
79         it visible.  Indent the string as requested.
80     */
print(int nIndentation, String aMessage)81     public static synchronized void print (int nIndentation, String aMessage)
82     {
83         while (nIndentation-- > 0)
84             aMessage = " " + aMessage;
85         Instance().printMessage(aMessage);
86     }
87 
88 
89 
90 
91     /** Show the given string at the end of the message area and scroll to make
92         it visible.
93     */
println(String aMessage)94     public static void println (String aMessage)
95     {
96         println (0, aMessage);
97     }
98 
99 
100 
101 
102     /** Show the given string at the end of the message area and scroll to make
103         it visible.
104     */
println(int nIndentation, String aMessage)105     public static void println (int nIndentation, String aMessage)
106     {
107         print (nIndentation, aMessage+"\n");
108     }
109 
110 
111 
112 
paintComponent(Graphics g)113     public void paintComponent (Graphics g)
114     {
115         synchronized (g)
116         {
117             JScrollBar sb = getVerticalScrollBar();
118             if (sb != null)
119             {
120                 int nScrollBarValue = sb.getMaximum() - sb.getVisibleAmount() - 1;
121                 sb.setValue (nScrollBarValue);
122             }
123             super.paintComponent (g);
124         }
125     }
126 
127 
128 
129 
130     /** Append the given string to the end of the text and scroll so that it
131         becomes visible.  This is an internal method.  Use one of the static
132         and public ones.
133     */
printMessage(String aMessage)134     private synchronized void printMessage (String aMessage)
135     {
136         maText.append (aMessage);
137     }
138 
139 
140 
141 
142     private static MessageArea saInstance = null;
143     private JTextArea maText;
144 }
145