1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import java.awt.Font;
36 import java.awt.Rectangle;
37 import java.awt.Color;
38 import java.awt.Graphics;
39 import javax.swing.JScrollPane;
40 import javax.swing.JTextArea;
41 import javax.swing.JScrollBar;
42 
43 
44 
45 /** A message area displays text in a scrollable text widget.  It is a
46     singleton.  Other objects can access it directly to display messages.
47 */
48 public class MessageArea
49     extends JScrollPane
50 {
51     public static synchronized MessageArea Instance ()
52     {
53         if (saInstance == null)
54             saInstance = new MessageArea ();
55         return saInstance;
56     }
57 
58 
59 
60 
61     /** Create a new message area.  This method is private because the class is
62         a singleton and may therefore not be instanciated from the outside.
63     */
64     private MessageArea ()
65     {
66         maText = new JTextArea();
67         maText.setBackground (new Color (255,250,240));
68         maText.setFont (new Font ("Helvetica", Font.PLAIN, 9));
69         setViewportView (maText);
70         setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
71         setHorizontalScrollBarPolicy (JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
72 
73         printMessage (
74             "class path is " + System.getProperty ("java.class.path") + "\n");
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     */
83     public static synchronized void print (String aMessage)
84     {
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     */
94     public static void println (String aMessage)
95     {
96         Instance().printMessage (aMessage+"\n");
97     }
98 
99 
100     public void paintComponent (Graphics g)
101     {
102         // Synchronize with the graphics object to prevent paint problems.
103         // Remember that this is not done by Swing itself.
104         synchronized (g)
105         {
106             JScrollBar sb = getVerticalScrollBar();
107             if (sb != null)
108             {
109                 int nScrollBarValue = sb.getMaximum() - sb.getVisibleAmount() - 1;
110                 sb.setValue (nScrollBarValue);
111             }
112             super.paintComponent (g);
113         }
114     }
115 
116 
117 
118 
119     /** Append the given string to the end of the text and scroll so that it
120         becomes visible.  This is an internal method.  Use one of the static
121         and public ones.
122     */
123     private synchronized void printMessage (String aMessage)
124     {
125         maText.append (aMessage);
126     }
127 
128 
129 
130 
131     private static MessageArea saInstance = null;
132     private JTextArea maText;
133 }
134