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