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 import javax.swing.tree.TreeModel;
24 import javax.swing.tree.TreePath;
25 import javax.swing.event.TreeModelListener;
26 import javax.swing.event.TreeModelEvent;
27 
28 import java.util.Vector;
29 import java.util.HashMap;
30 import java.util.Enumeration;
31 
32 import com.sun.star.accessibility.*;
33 import com.sun.star.uno.*;
34 
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.uno.Any;
38 import com.sun.star.lang.EventObject;
39 import com.sun.star.lang.XServiceInfo;
40 import com.sun.star.lang.XServiceName;
41 
42 /** Objects of this class (usually one, singleton?) listen to accessible
43     events of all objects in all trees.
44 */
45 public class EventListener
46 {
47     public boolean mbVerbose = false;
48 
EventListener(AccessibilityTreeModel aTreeModel)49     public EventListener (AccessibilityTreeModel aTreeModel)
50     {
51         maTreeModel = aTreeModel;
52     }
53 
54 
objectToString(Object aObject)55     private static String objectToString(Object aObject)
56     {
57         if (aObject == null)
58             return null;
59         else
60             return aObject.toString();
61     }
62 
63 
64 
65     /** This method handles accessibility objects that are being disposed.
66      */
disposing(XAccessibleContext xContext)67     public void disposing (XAccessibleContext xContext)
68     {
69         if (mbVerbose)
70             System.out.println("disposing " + xContext);
71         maTreeModel.removeNode (xContext);
72     }
73 
74     /** This method is called from accessible objects that broadcast
75         modifications of themselves or from their children.  The event is
76         processed only, except printing some messages, if the tree is not
77         locked.  It should be locked during changes to its internal
78         structure like expanding nodes.
79     */
notifyEvent(AccessibleEventObject aEvent)80     public void notifyEvent (AccessibleEventObject aEvent)
81     {
82         EventHandler aHandler;
83 
84         switch (aEvent.EventId)
85         {
86             case AccessibleEventId.CHILD:
87                 aHandler = new ChildEventHandler (aEvent, maTreeModel);
88                 break;
89 
90             case AccessibleEventId.BOUNDRECT_CHANGED:
91             case AccessibleEventId.VISIBLE_DATA_CHANGED:
92                 aHandler = new GeometryEventHandler (aEvent, maTreeModel);
93                 break;
94 
95 
96             case AccessibleEventId.NAME_CHANGED:
97             case AccessibleEventId.DESCRIPTION_CHANGED:
98             case AccessibleEventId.STATE_CHANGED:
99             case AccessibleEventId.SELECTION_CHANGED:
100                 aHandler = new ContextEventHandler (aEvent, maTreeModel);
101                 break;
102 
103             case AccessibleEventId.TABLE_MODEL_CHANGED:
104             case AccessibleEventId.TABLE_CAPTION_CHANGED:
105             case AccessibleEventId.TABLE_COLUMN_DESCRIPTION_CHANGED:
106             case AccessibleEventId.TABLE_COLUMN_HEADER_CHANGED:
107             case AccessibleEventId.TABLE_ROW_DESCRIPTION_CHANGED:
108             case AccessibleEventId.TABLE_ROW_HEADER_CHANGED:
109             case AccessibleEventId.TABLE_SUMMARY_CHANGED:
110                 aHandler = new TableEventHandler (aEvent, maTreeModel);
111                 break;
112 
113             case AccessibleEventId.ACTION_CHANGED:
114                 aHandler = new EventHandler (aEvent, maTreeModel);
115                 break;
116 
117             case AccessibleEventId.HYPERTEXT_CHANGED:
118                 aHandler = new EventHandler (aEvent, maTreeModel);
119                 break;
120 
121             case AccessibleEventId.ACTIVE_DESCENDANT_CHANGED:
122             case AccessibleEventId.CARET_CHANGED:
123             case AccessibleEventId.TEXT_CHANGED:
124             case AccessibleEventId.VALUE_CHANGED:
125                 aHandler = new EventHandler (aEvent, maTreeModel);
126                 break;
127 
128             default:
129                 aHandler = null;
130                 break;
131         }
132 
133         if (aHandler == null)
134             System.out.println ("    unhandled event");
135         else
136         {
137             if (mbVerbose)
138                 aHandler.Print (System.out);
139             aHandler.Process ();
140         }
141     }
142 
143 
144     private AccessibilityTreeModel maTreeModel;
145 }
146