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