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 package org.openoffice.accessibility.misc;
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         print (0, 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.  Indent the string as requested.
82     */
print(int nIndentation, String aMessage)83     public static synchronized void print (int nIndentation, String aMessage)
84     {
85         while (nIndentation-- > 0)
86             aMessage = " " + aMessage;
87         Instance().printMessage(aMessage);
88     }
89 
90 
91 
92 
93     /** Show the given string at the end of the message area and scroll to make
94         it visible.
95     */
println(String aMessage)96     public static void println (String aMessage)
97     {
98         println (0, aMessage);
99     }
100 
101 
102 
103 
104     /** Show the given string at the end of the message area and scroll to make
105         it visible.
106     */
println(int nIndentation, String aMessage)107     public static void println (int nIndentation, String aMessage)
108     {
109         print (nIndentation, aMessage+"\n");
110     }
111 
112 
113 
114 
paintComponent(Graphics g)115     public void paintComponent (Graphics g)
116     {
117         synchronized (g)
118         {
119             JScrollBar sb = getVerticalScrollBar();
120             if (sb != null)
121             {
122                 int nScrollBarValue = sb.getMaximum() - sb.getVisibleAmount() - 1;
123                 sb.setValue (nScrollBarValue);
124             }
125             super.paintComponent (g);
126         }
127     }
128 
129 
130 
131 
132     /** Append the given string to the end of the text and scroll so that it
133         becomes visible.  This is an internal method.  Use one of the static
134         and public ones.
135     */
printMessage(String aMessage)136     private synchronized void printMessage (String aMessage)
137     {
138         maText.append (aMessage);
139     }
140 
141 
142 
143 
144     private static MessageArea saInstance = null;
145     private JTextArea maText;
146 }
147