1 /*************************************************************************
3 * $RCSfile: MessageArea.java,v $
7 * last change: $Author: hr $ $Date: 2003-06-30 15:06:53 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
42 import java
.awt
.Rectangle
;
43 import java
.awt
.Color
;
44 import java
.awt
.Graphics
;
45 import javax
.swing
.JScrollPane
;
46 import javax
.swing
.JTextArea
;
47 import javax
.swing
.JScrollBar
;
51 /** A message area displays text in a scrollable text widget. It is a
52 singleton. Other objects can access it directly to display messages.
54 public class MessageArea
57 public static synchronized MessageArea
Instance ()
59 if (saInstance
== null)
60 saInstance
= new MessageArea ();
67 /** Create a new message area. This method is private because the class is
68 a singleton and may therefore not be instanciated from the outside.
70 private MessageArea ()
72 maText
= new JTextArea();
73 maText
.setBackground (new Color (255,250,240));
74 maText
.setFont (new Font ("Helvetica", Font
.PLAIN
, 9));
75 setViewportView (maText
);
76 setVerticalScrollBarPolicy (JScrollPane
.VERTICAL_SCROLLBAR_ALWAYS
);
77 setHorizontalScrollBarPolicy (JScrollPane
.HORIZONTAL_SCROLLBAR_ALWAYS
);
80 "class path is " + System
.getProperty ("java.class.path") + "\n");
86 /** Show the given string at the end of the message area and scroll to make
89 public static synchronized void print (String aMessage
)
91 Instance().printMessage(aMessage
);
97 /** Show the given string at the end of the message area and scroll to make
100 public static void println (String aMessage
)
102 Instance().printMessage (aMessage
+"\n");
106 public void paintComponent (Graphics g
)
108 // Synchronize with the graphics object to prevent paint problems.
109 // Remember that this is not done by Swing itself.
112 JScrollBar sb
= getVerticalScrollBar();
115 int nScrollBarValue
= sb
.getMaximum() - sb
.getVisibleAmount() - 1;
116 sb
.setValue (nScrollBarValue
);
118 super.paintComponent (g
);
125 /** Append the given string to the end of the text and scroll so that it
126 becomes visible. This is an internal method. Use one of the static
129 private synchronized void printMessage (String aMessage
)
131 maText
.append (aMessage
);
137 private static MessageArea saInstance
= null;
138 private JTextArea maText
;