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.java.accessibility;
25 
26 import javax.accessibility.AccessibleState;
27 
28 import com.sun.star.uno.*;
29 import com.sun.star.accessibility.*;
30 
31 /**
32  */
33 public abstract class AbstractButton extends Component {
34 
AbstractButton(XAccessible xAccessible, XAccessibleContext xAccessibleContext)35     protected AbstractButton(XAccessible xAccessible, XAccessibleContext xAccessibleContext) {
36         super(xAccessible, xAccessibleContext);
37     }
38 
39     protected abstract class AccessibleAbstractButton extends AccessibleUNOComponent
40         implements javax.accessibility.AccessibleAction {
41 
42         /**
43         * Though the class is abstract, this should be called by all sub-classes
44         */
AccessibleAbstractButton()45         protected AccessibleAbstractButton() {
46             super();
47         }
48 
49         /*
50         * AccessibleContext
51         */
52 
53         /** Gets the AccessibleAction associated with this object that supports one or more actions */
getAccessibleAction()54         public javax.accessibility.AccessibleAction getAccessibleAction() {
55             return this;
56         }
57 
58         /** Gets the AccessibleText associated with this object presenting text on the display */
getAccessibleText()59         public javax.accessibility.AccessibleText getAccessibleText() {
60 
61             if (disposed)
62                 return null;
63 
64             try {
65                 XAccessibleText unoAccessibleText = (XAccessibleText)
66                     UnoRuntime.queryInterface(XAccessibleText.class,unoAccessibleComponent);
67                 if (unoAccessibleText != null) {
68                     return new AccessibleTextImpl(unoAccessibleText);
69                 } else {
70                     return null;
71                 }
72             } catch (com.sun.star.uno.RuntimeException e) {
73                 return null;
74             }
75         }
76 
77         /** Returns the relation set of this object */
getAccessibleRelationSet()78         public javax.accessibility.AccessibleRelationSet getAccessibleRelationSet() {
79             try {
80                 XAccessibleRelationSet unoAccessibleRelationSet = unoAccessibleContext.getAccessibleRelationSet();
81 
82                 if (unoAccessibleRelationSet == null) {
83                     return null;
84                 }
85 
86                 javax.accessibility.AccessibleRelationSet relationSet = new javax.accessibility.AccessibleRelationSet();
87                 int count = unoAccessibleRelationSet.getRelationCount();
88 
89                 for (int i = 0; i < count; i++) {
90                     AccessibleRelation unoAccessibleRelation = unoAccessibleRelationSet.getRelation(i);
91 
92                     switch (unoAccessibleRelation.RelationType) {
93                         case AccessibleRelationType.MEMBER_OF:
94                             relationSet.add(new javax.accessibility.AccessibleRelation(
95                                     javax.accessibility.AccessibleRelation.MEMBER_OF,
96                                     getAccessibleComponents(
97                                         unoAccessibleRelation.TargetSet)));
98                             break;
99 
100                         case AccessibleRelationType.LABELED_BY:
101                             relationSet.add(new javax.accessibility.AccessibleRelation(
102                                     javax.accessibility.AccessibleRelation.LABELED_BY,
103                                     getAccessibleComponents(
104                                         unoAccessibleRelation.TargetSet)));
105                             break;
106                         default:
107                             break;
108                     }
109                 }
110 
111                 return relationSet;
112             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
113                 return null;
114             } catch (com.sun.star.uno.RuntimeException e) {
115                 return null;
116             }
117         }
118 
119         /*
120         * AccessibleAction
121         */
122 
123         /** Performs the specified Action on the object */
doAccessibleAction(int param)124         public boolean doAccessibleAction(int param) {
125             if (param == 0) {
126                 // HACK: this action might open a modal dialog and therefor block
127                 // until the dialog is closed. In case of this thread being the
128                 // AWT EventDispatcherThread this means, the opened dialog will
129                 // not be accessible, so deligate this request to another thread.
130                 if (java.awt.EventQueue.isDispatchThread()) {
131                     Thread t = new Thread () {
132                         public void run() {
133                             AbstractButton.AccessibleAbstractButton.this.doAccessibleAction(0);
134                         }
135                     };
136                     t.start();
137                     return true;
138                 } else {
139                     // Actions of MenuItems may also be performed if the item is not
140                     // visible, so just try ..
141                     try {
142                         XAccessibleContext xAccessibleContext = unoAccessibleContext;
143                         if (xAccessibleContext != null) {
144                             // Query for XAccessibleAction interface
145                             XAccessibleAction xAccessibleAction = (XAccessibleAction)
146                                 UnoRuntime.queryInterface(XAccessibleAction.class, xAccessibleContext);
147 
148                             if (xAccessibleAction != null) {
149                                 return xAccessibleAction.doAccessibleAction(0);
150                             }
151                         }
152                     } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
153                     } catch (com.sun.star.uno.RuntimeException e) {
154                     }
155                 }
156             }
157 
158             return false;
159         }
160 
161         /** Returns a description of the specified action of the object */
getAccessibleActionDescription(int param)162         public java.lang.String getAccessibleActionDescription(int param) {
163             return javax.swing.UIManager.getString("AbstractButton.clickText");
164         }
165 
166         /** Returns the number of accessible actions available in this object */
getAccessibleActionCount()167         public int getAccessibleActionCount() {
168             return 1;
169         }
170     }
171 }
172 
173