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