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