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...
3import com.sun.star.uno.UnoRuntime;
4import com.sun.star.frame.XModel;
5import com.sun.star.view.XSelectionSupplier;
6import com.sun.star.container.XIndexAccess;
7import com.sun.star.text.XText;
8import com.sun.star.text.XTextRange;
9import com.sun.star.text.XWordCursor;
10import com.sun.star.script.provider.XScriptContext;
11
12// return the new string based on the string passed in
13String getNewString( theString ) {
14    String newString;
15    if(theString==null || theString.length()==0) {
16        return newString;
17    }
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();
25    }
26    return newString;
27}
28
29//the method that does the work
30void capitalise() {
31
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!
45                xText = (XText)
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
50                // already there
51                if(!xWordCursor.isStartOfWord()) {
52                    xWordCursor.gotoStartOfWord(false);
53                }
54                // move the cursor to the next word, selecting all chars
55                // in between
56                xWordCursor.gotoNextWord(true);
57                // get the selected string
58                theString = xWordCursor.getString();
59                // get the new string
60                newString = getNewString(theString);
61                if(newString!=null) {
62                    // set the new string
63                    xWordCursor.setString(newString);
64                    // keep the current selection
65                    xSelectionSupplier.select(xWordCursor);
66                }
67            } else {
68                newString = getNewString( theString );
69                if(newString!=null) {
70                    // set the new string
71                    xTextRange.setString(newString);
72                    // keep the current selection
73                    xSelectionSupplier.select(xTextRange);
74                }
75            }
76
77        }
78    }
79}
80
81// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
82// all BeanShell scripts executed by the Script Framework
83xModel = (XModel)
84    UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
85//the writer controller impl supports the css.view.XSelectionSupplier interface
86xSelectionSupplier = (XSelectionSupplier)
87    UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
88//see section 7.5.1 of developers' guide
89xIndexAccess = (XIndexAccess)
90    UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
91
92//call the method that does the work
93capitalise();
94return 0;
95