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.event.ActionListener;
36 import java.awt.GridBagLayout;
37 import java.awt.GridBagConstraints;
38 import javax.swing.*;
39 
40 
41 /** The simple screen reader (SSR) registers at the toolkit as focus listener
42     and displays information about the currently focused object.
43 */
44 public class SSR
45     implements ActionListener
46 {
47     /** Just pass the control to the SSR class.
48     */
49     public static void main (String args[])
50     {
51         new SSR ();
52     }
53 
54 
55 
56 
57     /** Create a new instance of the simple screen reader.
58     */
59     public SSR ()
60     {
61         Layout ();
62 
63         // Create the event handler and tell it where to display information
64         // about the currently focused accessible object.
65         maEventHandler = new EventHandler ();
66         maEventHandler.addObjectDisplay (maTextualDisplay);
67         maEventHandler.addObjectDisplay (maGraphicalDisplay);
68     }
69 
70 
71 
72 
73     /** Setup the GUI.  It is divided into three areas.  The lower half is
74         ocupied by a message area that logs all the events received from
75         accessibility objects.  The upper half is shared by two different
76         displays of the currently focused object.  On left there is a textual
77         representation.  On the right there is a graphical view of the
78         objects's outline.
79     */
80     private void Layout ()
81     {
82         GridBagConstraints constraints;
83 
84         JPanel aPanel = new JPanel (true);
85         aPanel.setLayout (new GridBagLayout());
86         aPanel.setOpaque (true);
87 
88         mFrame = new JFrame ("Simple Screen Reader 0.3");
89         mFrame.setContentPane(aPanel);
90         mFrame.setSize (600,400);
91 
92 
93         addComponent (new JLabel ("Focused Object:"),
94             0,0, 1,1, 0,0, GridBagConstraints.WEST, GridBagConstraints.NONE);
95 
96 
97         maTextualDisplay = new TextualDisplay ();
98         addComponent (maTextualDisplay,
99             0,1, 1,1, 1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
100 
101         maGraphicalDisplay = new GraphicalDisplay ();
102         addComponent (maGraphicalDisplay,
103             1,0, 1,2, 1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
104 
105         addComponent (new JLabel ("Messages:"),
106             0,2, 1,1, 0,0, GridBagConstraints.WEST, GridBagConstraints.NONE);
107 
108         addComponent (MessageArea.Instance(),
109             0,3, 2,1, 1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
110 
111 
112         JButton aButton = new JButton ("Quit SSR");
113         addComponent (aButton,
114             0,4, 1,1, 0,0, GridBagConstraints.WEST,GridBagConstraints.NONE);
115         aButton.addActionListener (this);
116 
117         mFrame.show();
118     }
119 
120 
121 
122 
123     /** Add a GUI element with the given constraints to the main window.
124     */
125     private JComponent addComponent (JComponent aComponent,
126         int x, int y, int width, int height, double weightx, double weighty,
127         int anchor, int fill)
128     {
129         aComponent.setDoubleBuffered (false);
130         GridBagConstraints aConstraints = new GridBagConstraints();
131         aConstraints.gridx = x;
132         aConstraints.gridy = y;
133         aConstraints.gridwidth = width;
134         aConstraints.gridheight = height;
135         aConstraints.weightx = weightx;
136         aConstraints.weighty = weighty;
137         aConstraints.anchor = anchor;
138         aConstraints.fill = fill;
139 
140         mFrame.getContentPane().add (aComponent, aConstraints);
141 
142         return aComponent;
143     }
144 
145 
146 
147 
148     /** This call-back handles button presses.
149     */
150     public void actionPerformed (java.awt.event.ActionEvent e)
151     {
152         if (e.getActionCommand().equals ("Quit SSR"))
153         {
154             maEventHandler.finalize ();
155             System.exit(0);
156         }
157     }
158 
159 
160     /// The main frame that contains all other GUI elements.
161     private JFrame mFrame;
162 
163     /// A textutal representation of the currently focused object.
164     private TextualDisplay maTextualDisplay;
165 
166     /// A graphical representation of the currently focused object.
167     private GraphicalDisplay maGraphicalDisplay;
168 
169     /// The event handler that reacts to all the accessibility events.
170     private EventHandler maEventHandler;
171 }
172