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 package org.openoffice.accessibility.awb.tree;
25 
26 import com.sun.star.uno.UnoRuntime;
27 import com.sun.star.accessibility.XAccessible;
28 import com.sun.star.accessibility.XAccessibleContext;
29 
30 /*
31  * This class is dynamic in the way that it does not contain any children
32  * until the node is going to be expanded. It also releases all children
33  * as soon as the node is collapsed again.
34  */
35 class DynamicAccessibilityNode extends AccessibilityNode {
36 
DynamicAccessibilityNode(AccessibilityModel treeModel)37     public DynamicAccessibilityNode(AccessibilityModel treeModel) {
38         super(treeModel);
39     }
40 
41     // Populates the child list. Called by AccessibilityMode.treeWillExpand().
populate()42     protected void populate() {
43         try {
44             XAccessibleContext xAC = getAccessibleContext();
45             if (xAC != null) {
46                 int n = xAC.getAccessibleChildCount();
47                 for (int i=0; i<n; i++) {
48                     XAccessible xAccessible = xAC.getAccessibleChild(i);
49                     AccessibilityNode node = treeModel.findNode(xAccessible);
50                     if (node == null) {
51                         node = treeModel.createNode(xAccessible);
52                     }
53                     if (node != null) {
54                         // NOTE: do not send any tree notifications here !
55                         add(node);
56                     }
57                 }
58             }
59         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
60             // This should never happen since we previously checked the child
61             // count.
62             // FIXME: error message
63         } catch (com.sun.star.uno.RuntimeException e) {
64             // FIXME: error message
65         }
66     }
67 
68     // Clears the child list. Called by AccessibilityModel.treeCollapsed().
clear()69     protected void clear() {
70         removeAllChildren();
71     }
72 
73     /* This is called whenever the node is painted, no matter if collapsed
74      * or expanded. Making this a "life" value seems to be appropriate.
75      */
isLeaf()76     public boolean isLeaf() {
77         try {
78             XAccessibleContext xAC = getAccessibleContext();
79             if (xAC != null) {
80                 return xAC.getAccessibleChildCount() == 0;
81             }
82             return true;
83         } catch (com.sun.star.uno.RuntimeException e) {
84             return true;
85         }
86     }
87 
88 }
89