1 // Change the case of a selection, or current word from upper case,
2 // to first char upper case, to all lower case to upper case...
3 import com.sun.star.uno.UnoRuntime;
4 import com.sun.star.frame.XModel;
5 import com.sun.star.view.XSelectionSupplier;
6 import com.sun.star.container.XIndexAccess;
7 import com.sun.star.text.XText;
8 import com.sun.star.text.XTextRange;
9 import com.sun.star.text.XWordCursor;
10 import com.sun.star.script.provider.XScriptContext;
12 // return the new string based on the string passed in
13 String getNewString( theString ) {
15 if(theString==null || theString.length()==0) {
18 // should we tokenize on "."?
19 if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC
20 newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase();
21 } else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC
22 newString=theString.toLowerCase();
23 } else { // all to UC.
24 newString=theString.toUpperCase();
29 //the method that does the work
32 // get the number of regions selected
33 count = xIndexAccess.getCount();
34 if(count>=1) { //ie we have a selection
35 for(i=0;i<count;i++) {
36 // get the i-th region selected
37 xTextRange = (XTextRange)
38 UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
39 System.out.println("string: "+xTextRange.getString());
40 // get the selected string
41 theString = xTextRange.getString();
42 if(theString.length()==0) {
43 // sadly we can have a selection where nothing is selected
44 // in this case we get the XWordCursor and make a selection!
46 UnoRuntime.queryInterface(XText.class, xTextRange.getText());
47 xWordCursor = (XWordCursor)
48 UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange));
49 // move the Word cursor to the start of the word if its not
51 if(!xWordCursor.isStartOfWord()) {
52 xWordCursor.gotoStartOfWord(false);
54 // move the cursor to the next word, selecting all chars
56 xWordCursor.gotoNextWord(true);
57 // get the selected string
58 theString = xWordCursor.getString();
60 newString = getNewString(theString);
63 xWordCursor.setString(newString);
64 // keep the current selection
65 xSelectionSupplier.select(xWordCursor);
68 newString = getNewString( theString );
71 xTextRange.setString(newString);
72 // keep the current selection
73 xSelectionSupplier.select(xTextRange);
81 // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
82 // all BeanShell scripts executed by the Script Framework
84 UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
85 //the writer controller impl supports the css.view.XSelectionSupplier interface
86 xSelectionSupplier = (XSelectionSupplier)
87 UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
88 //see section 7.5.1 of developers' guide
89 xIndexAccess = (XIndexAccess)
90 UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
92 //call the method that does the work