2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 // Change the case of a selection, or current word from upper case,
19 // to first char upper case, to all lower case to upper case...
20 import com.sun.star.uno.UnoRuntime;
21 import com.sun.star.frame.XModel;
22 import com.sun.star.view.XSelectionSupplier;
23 import com.sun.star.container.XIndexAccess;
24 import com.sun.star.text.XText;
25 import com.sun.star.text.XTextRange;
26 import com.sun.star.text.XWordCursor;
27 import com.sun.star.script.provider.XScriptContext;
29 // return the new string based on the string passed in
30 String getNewString( theString ) {
32 if(theString==null || theString.length()==0) {
35 // should we tokenize on "."?
36 if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC
37 newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase();
38 } else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC
39 newString=theString.toLowerCase();
40 } else { // all to UC.
41 newString=theString.toUpperCase();
46 //the method that does the work
49 // get the number of regions selected
50 count = xIndexAccess.getCount();
51 if(count>=1) { //ie we have a selection
52 for(i=0;i<count;i++) {
53 // get the i-th region selected
54 xTextRange = (XTextRange)
55 UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
56 System.out.println("string: "+xTextRange.getString());
57 // get the selected string
58 theString = xTextRange.getString();
59 if(theString.length()==0) {
60 // sadly we can have a selection where nothing is selected
61 // in this case we get the XWordCursor and make a selection!
63 UnoRuntime.queryInterface(XText.class, xTextRange.getText());
64 xWordCursor = (XWordCursor)
65 UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange));
66 // move the Word cursor to the start of the word if its not
68 if(!xWordCursor.isStartOfWord()) {
69 xWordCursor.gotoStartOfWord(false);
71 // move the cursor to the next word, selecting all chars
73 xWordCursor.gotoNextWord(true);
74 // get the selected string
75 theString = xWordCursor.getString();
77 newString = getNewString(theString);
80 xWordCursor.setString(newString);
81 // keep the current selection
82 xSelectionSupplier.select(xWordCursor);
85 newString = getNewString( theString );
88 xTextRange.setString(newString);
89 // keep the current selection
90 xSelectionSupplier.select(xTextRange);
98 // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
99 // all BeanShell scripts executed by the Script Framework
101 UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
102 //the writer controller impl supports the css.view.XSelectionSupplier interface
103 xSelectionSupplier = (XSelectionSupplier)
104 UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
105 //see section 7.5.1 of developers' guide
106 xIndexAccess = (XIndexAccess)
107 UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
109 //call the method that does the work