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// Change the case of a selection, or current word from upper case,
22// to first char upper case, to all lower case to upper case...
23import com.sun.star.uno.UnoRuntime;
24import com.sun.star.frame.XModel;
25import com.sun.star.view.XSelectionSupplier;
26import com.sun.star.container.XIndexAccess;
27import com.sun.star.text.XText;
28import com.sun.star.text.XTextRange;
29import com.sun.star.text.XWordCursor;
30import com.sun.star.script.provider.XScriptContext;
31
32// return the new string based on the string passed in
33String getNewString( theString ) {
34    String newString;
35    if(theString==null || theString.length()==0) {
36        return newString;
37    }
38    // should we tokenize on "."?
39    if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC
40        newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase();
41    } else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC
42        newString=theString.toLowerCase();
43    } else { // all to UC.
44        newString=theString.toUpperCase();
45    }
46    return newString;
47}
48
49//the method that does the work
50void capitalise() {
51
52    // get the number of regions selected
53    count = xIndexAccess.getCount();
54    if(count>=1) { //ie we have a selection
55        for(i=0;i<count;i++) {
56            // get the i-th region selected
57            xTextRange = (XTextRange)
58                UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
59            System.out.println("string: "+xTextRange.getString());
60            // get the selected string
61            theString = xTextRange.getString();
62            if(theString.length()==0) {
63                // sadly we can have a selection where nothing is selected
64                // in this case we get the XWordCursor and make a selection!
65                xText = (XText)
66                    UnoRuntime.queryInterface(XText.class, xTextRange.getText());
67                xWordCursor = (XWordCursor)
68                    UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange));
69                // move the Word cursor to the start of the word if its not
70                // already there
71                if(!xWordCursor.isStartOfWord()) {
72                    xWordCursor.gotoStartOfWord(false);
73                }
74                // move the cursor to the next word, selecting all chars
75                // in between
76                xWordCursor.gotoNextWord(true);
77                // get the selected string
78                theString = xWordCursor.getString();
79                // get the new string
80                newString = getNewString(theString);
81                if(newString!=null) {
82                    // set the new string
83                    xWordCursor.setString(newString);
84                    // keep the current selection
85                    xSelectionSupplier.select(xWordCursor);
86                }
87            } else {
88                newString = getNewString( theString );
89                if(newString!=null) {
90                    // set the new string
91                    xTextRange.setString(newString);
92                    // keep the current selection
93                    xSelectionSupplier.select(xTextRange);
94                }
95            }
96
97        }
98    }
99}
100
101// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
102// all BeanShell scripts executed by the Script Framework
103xModel = (XModel)
104    UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
105//the writer controller impl supports the css.view.XSelectionSupplier interface
106xSelectionSupplier = (XSelectionSupplier)
107    UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
108//see section 7.5.1 of developers' guide
109xIndexAccess = (XIndexAccess)
110    UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
111
112//call the method that does the work
113capitalise();
114return 0;
115