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 com.sun.star.awt.*;
27 import com.sun.star.style.*;
28 import com.sun.star.uno.*;
29 import com.sun.star.accessibility.AccessibleTextType;
30 import com.sun.star.accessibility.XAccessibleEditableText;
31 
32 import javax.accessibility.AccessibleText;
33 import javax.swing.text.StyleConstants;
34 
35 /** The GenericAccessibleEditableText mapps the calls to the java AccessibleEditableText
36  *  interface to the corresponding methods of the UNO XAccessibleEditableText interface.
37  */
38 public class AccessibleEditableTextImpl extends AccessibleTextImpl implements javax.accessibility.AccessibleEditableText {
39     final static double toPointFactor = 1 / (7/10 + 34.5);
40 
41     /** Creates new GenericAccessibleEditableText object */
AccessibleEditableTextImpl(XAccessibleEditableText xAccessibleEditableText)42     public AccessibleEditableTextImpl(XAccessibleEditableText xAccessibleEditableText) {
43         super(xAccessibleEditableText);
44     }
45 
46     /** Cuts the text between two indices into the system clipboard */
cut(int startIndex, int endIndex)47     public void cut(int startIndex, int endIndex) {
48         try {
49             ((XAccessibleEditableText) unoObject).cutText(startIndex, endIndex);
50         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
51         } catch (com.sun.star.uno.RuntimeException e) {
52         }
53     }
54 
55     /** Deletes the text between two indices */
delete(int startIndex, int endIndex)56     public void delete(int startIndex, int endIndex) {
57         try {
58             ((XAccessibleEditableText) unoObject).deleteText(startIndex, endIndex);
59         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
60         } catch (com.sun.star.uno.RuntimeException e) {
61         }
62     }
63 
64     /** Returns the text range between two indices */
getTextRange(int startIndex, int endIndex)65     public String getTextRange(int startIndex, int endIndex) {
66         try {
67             return unoObject.getTextRange(startIndex, endIndex);
68         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
69         } catch (com.sun.star.uno.RuntimeException e) {
70         }
71         return null;
72     }
73 
74     /** Inserts the specified string at the given index */
insertTextAtIndex(int index, String s)75     public void insertTextAtIndex(int index, String s){
76         try {
77             ((XAccessibleEditableText) unoObject).insertText(s, index);
78         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
79         } catch (com.sun.star.uno.RuntimeException e) {
80         }
81     }
82 
83     /** Pastes the text form the system clipboard into the text starting at the specified index */
paste(int startIndex)84     public void paste(int startIndex) {
85         try {
86             ((XAccessibleEditableText) unoObject).pasteText(startIndex);
87         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
88         } catch (com.sun.star.uno.RuntimeException e) {
89         }
90     }
91 
92     /** Replaces the text between two indices with the specified string */
replaceText(int startIndex, int endIndex, String s)93     public void replaceText(int startIndex, int endIndex, String s) {
94         try {
95             ((XAccessibleEditableText) unoObject).replaceText(startIndex, endIndex, s);
96         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
97         } catch (com.sun.star.uno.RuntimeException e) {
98         }
99     }
100 
101     /** Selects the text between two indices */
selectText(int startIndex, int endIndex)102     public void selectText(int startIndex, int endIndex) {
103         try {
104             unoObject.setSelection(startIndex, endIndex);
105         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
106         } catch (com.sun.star.uno.RuntimeException e) {
107         }
108     }
109 
110     /** Sets the attributes for the text between two indices */
setAttributes(int startIndex, int endIndex, javax.swing.text.AttributeSet as)111     public void setAttributes(int startIndex, int endIndex, javax.swing.text.AttributeSet as) {
112         java.util.Vector propertyValues = new java.util.Vector();
113 
114         // Convert Alignment attribute
115         Object attribute = as.getAttribute(StyleConstants.Alignment);
116         if (null != attribute) {
117             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
118             propertyValue.Name = "ParaAdjust";
119 
120             switch (StyleConstants.getAlignment(as)) {
121                 case StyleConstants.ALIGN_RIGHT:
122                     propertyValue.Value = ParagraphAdjust.RIGHT;
123                     break;
124                 case StyleConstants.ALIGN_CENTER:
125                     propertyValue.Value = ParagraphAdjust.CENTER;
126                     break;
127                 case StyleConstants.ALIGN_JUSTIFIED:
128                     propertyValue.Value = ParagraphAdjust.BLOCK;
129                     break;
130                 default:
131                     propertyValue.Value = ParagraphAdjust.LEFT;
132                     break;
133             }
134             propertyValues.add(propertyValue);
135         }
136 
137         // Convert Background attribute
138         attribute = as.getAttribute(StyleConstants.Background);
139         if (null != attribute) {
140             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
141             propertyValue.Name = "CharBackColor";
142             propertyValue.Value = new Integer(StyleConstants.getBackground(as).getRGB());
143             propertyValues.add(propertyValue);
144         }
145 
146         // FIXME: BidiLevel
147 
148         // Set Bold attribute
149         attribute = as.getAttribute(StyleConstants.Bold);
150         if (null != attribute) {
151             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
152             propertyValue.Name = "CharWeight";
153             if (StyleConstants.isBold(as)) {
154                 propertyValue.Value =  new Float(150);
155             } else {
156                 propertyValue.Value =  new Float(100);
157             }
158             propertyValues.add(propertyValue);
159         }
160 
161         // FIXME: Java 1.4 ComponentAttribute, ComponentElementName, ComposedTextAttribute
162 
163         // Set FirstLineIndent attribute
164         attribute = as.getAttribute(StyleConstants.FirstLineIndent);
165         if (null != attribute) {
166             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
167             propertyValue.Name = "ParaFirstLineIndent";
168             propertyValue.Value = new Double(StyleConstants.getFirstLineIndent(as) / toPointFactor);
169             propertyValues.add(propertyValue);
170         }
171 
172         // Set font family attribute
173         attribute = as.getAttribute(StyleConstants.FontFamily);
174         if (null != attribute) {
175             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
176             propertyValue.Name = "CharFontPitch";
177 
178             if (StyleConstants.getFontFamily(as).equals( "Proportional" )) {
179                 propertyValue.Value = new Short("2");
180             } else {
181                 propertyValue.Value = new Short("1");
182             }
183             propertyValues.add(propertyValue);
184         }
185 
186         // Set font size attribute
187         attribute = as.getAttribute(StyleConstants.FontSize);
188         if (null != attribute) {
189             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
190             propertyValue.Name = "CharHeight";
191             propertyValue.Value = new Integer(StyleConstants.getFontSize(as));
192             propertyValues.add(propertyValue);
193         }
194 
195         // Map foreground color
196         attribute = as.getAttribute(StyleConstants.Foreground);
197         if (null != attribute) {
198             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
199             propertyValue.Name = "CharColor";
200             propertyValue.Value = new Integer (StyleConstants.getForeground(as).getRGB());
201             propertyValues.add(propertyValue);
202         }
203 
204         // FIXME: IconAttribute, IconElementName
205 
206         // Set italic attribute
207         attribute = as.getAttribute(StyleConstants.Italic);
208         if (null != attribute) {
209             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
210             propertyValue.Name = "CharPosture";
211 
212             if (StyleConstants.isItalic(as)) {
213                 propertyValue.Value = FontSlant.ITALIC;
214             } else {
215                 propertyValue.Value = FontSlant.DONTKNOW;
216             }
217             propertyValues.add(propertyValue);
218         }
219 
220         // Set left indent attribute
221         attribute = as.getAttribute(StyleConstants.LeftIndent);
222         if (null != attribute) {
223             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
224             propertyValue.Name = "ParaFirstLeftMargin";
225             propertyValue.Value = new Integer(new Double(StyleConstants.getLeftIndent(as) / toPointFactor).intValue());
226             propertyValues.add(propertyValue);
227         }
228 
229         // Set right indent attribute
230         attribute = as.getAttribute(StyleConstants.RightIndent);
231         if (null != attribute) {
232             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
233             propertyValue.Name = "ParaFirstRightMargin";
234             propertyValue.Value = new Integer(new Double(StyleConstants.getRightIndent(as) / toPointFactor).intValue());
235             propertyValues.add(propertyValue);
236         }
237 
238         // Set line spacing attribute
239         attribute = as.getAttribute(StyleConstants.LineSpacing);
240         if (null != attribute) {
241             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
242             propertyValue.Name = "ParaLineSpacing";
243             propertyValue.Value = new Integer(new Double(StyleConstants.getLineSpacing(as) / toPointFactor).intValue());
244             propertyValues.add(propertyValue);
245         }
246 
247         // FIXME: Java 1.4 NameAttribute, Orientation, ResolveAttribute
248 
249         // Set space above attribute
250         attribute = as.getAttribute(StyleConstants.SpaceAbove);
251         if (null != attribute) {
252             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
253             propertyValue.Name = "ParaTopMargin";
254             propertyValue.Value = new Integer(new Double( StyleConstants.getSpaceAbove(as) / toPointFactor).intValue());
255             propertyValues.add(propertyValue);
256         }
257 
258         // Set space below attribute
259         attribute = as.getAttribute(StyleConstants.SpaceBelow);
260         if (null != attribute) {
261             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
262                         propertyValue.Name = "ParaBottomMargin";
263             propertyValue.Value = new Integer(new Double(StyleConstants.getSpaceBelow(as) / toPointFactor).intValue());
264             propertyValues.add(propertyValue);
265         }
266 
267         // Set strike through attribute
268         attribute = as.getAttribute(StyleConstants.StrikeThrough);
269         if (null != attribute) {
270             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
271                         propertyValue.Name = "CharPosture";
272             if (StyleConstants.isStrikeThrough(as)) {
273                 propertyValue.Value = new Short(FontStrikeout.SINGLE);
274             } else {
275                 propertyValue.Value = new Short(FontStrikeout.NONE);
276             }
277             propertyValues.add(propertyValue);
278         }
279 
280         // Set sub-/superscript attribute
281         attribute = as.getAttribute(StyleConstants.Superscript);
282         if (null == attribute) {
283             attribute = as.getAttribute(StyleConstants.Subscript);
284         }
285         if (null != attribute) {
286             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
287             propertyValue.Name = "CharEscapement";
288 
289             if (StyleConstants.isSuperscript(as)) {
290                 propertyValue.Value = new Short( "1" );
291             } else if (StyleConstants.isSubscript(as)) {
292                 propertyValue.Value = new Short( "-1" );
293             } else {
294                 propertyValue.Value = new Short( "0" );
295             }
296             propertyValues.add(propertyValue);
297         }
298 
299         // Set tabset attribute
300         attribute = as.getAttribute(StyleConstants.TabSet);
301         if (null != attribute) {
302             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
303             propertyValue.Name = "ParaTabStops";
304 
305             javax.swing.text.TabSet tabSet = StyleConstants.getTabSet(as);
306             java.util.ArrayList tabStops = new java.util.ArrayList(tabSet.getTabCount());
307 
308             for (int i = 0, max = tabSet.getTabCount(); i < max; i++) {
309                 javax.swing.text.TabStop tab = tabSet.getTab(i);
310                 com.sun.star.style.TabStop unoTab = new com.sun.star.style.TabStop();
311 
312                 unoTab.Position = new Double(tab.getPosition() / toPointFactor).intValue();
313 
314                 switch (tab.getAlignment()) {
315                     case javax.swing.text.TabStop.ALIGN_CENTER:
316                         unoTab.Alignment = TabAlign.CENTER;
317                         break;
318                     case javax.swing.text.TabStop.ALIGN_RIGHT:
319                         unoTab.Alignment = TabAlign.RIGHT;
320                         break;
321                     case javax.swing.text.TabStop.ALIGN_DECIMAL:
322                         unoTab.Alignment = TabAlign.DECIMAL;
323                         break;
324                     default:
325                         unoTab.Alignment = TabAlign.LEFT;
326                         break;
327                 }
328 
329                 tabStops.add(unoTab);
330             }
331             propertyValue.Value = (com.sun.star.style.TabStop[]) tabStops.toArray();
332             propertyValues.add(propertyValue);
333         }
334 
335         // Set underline attribute
336         attribute = as.getAttribute(StyleConstants.Underline);
337         if (null != attribute) {
338             com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
339             propertyValue.Name = "CharUnderline";
340 
341             if (StyleConstants.isUnderline(as)) {
342                 propertyValue.Value = new Short(FontUnderline.SINGLE);
343             } else {
344                 propertyValue.Value = new Short(FontUnderline.NONE);
345             }
346             propertyValues.add(propertyValue);
347         }
348 
349         try {
350             ((XAccessibleEditableText) unoObject).setAttributes(startIndex, endIndex, (com.sun.star.beans.PropertyValue[]) propertyValues.toArray());
351         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
352         } catch (com.sun.star.uno.RuntimeException e) {
353         }
354     }
355 
356     /** Sets the text contents to the specified string */
setTextContents(String s)357     public void setTextContents(String s) {
358         try {
359             ((XAccessibleEditableText) unoObject).setText(s);
360         } catch (com.sun.star.uno.RuntimeException e) {
361         }
362     }
363 }
364