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