1*b1cdbd2cSJim Jagielski# *************************************************************
2*b1cdbd2cSJim Jagielski#
3*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
6*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski#
11*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski#
13*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski#  under the License.
19*b1cdbd2cSJim Jagielski#
20*b1cdbd2cSJim Jagielski# *************************************************************
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski# helper function
24*b1cdbd2cSJim Jagielskidef getNewString( theString ) :
25*b1cdbd2cSJim Jagielski    if( not theString or len(theString) ==0) :
26*b1cdbd2cSJim Jagielski        return ""
27*b1cdbd2cSJim Jagielski    # should we tokenize on "."?
28*b1cdbd2cSJim Jagielski    if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() :
29*b1cdbd2cSJim Jagielski        # first two chars are UC => first UC, rest LC
30*b1cdbd2cSJim Jagielski        newString=theString[0:1].upper() + theString[1:].lower();
31*b1cdbd2cSJim Jagielski    elif theString[0].isupper():
32*b1cdbd2cSJim Jagielski        # first char UC => all to LC
33*b1cdbd2cSJim Jagielski        newString=theString.lower()
34*b1cdbd2cSJim Jagielski    else: # all to UC.
35*b1cdbd2cSJim Jagielski        newString=theString.upper()
36*b1cdbd2cSJim Jagielski    return newString;
37*b1cdbd2cSJim Jagielski
38*b1cdbd2cSJim Jagielskidef capitalisePython( ):
39*b1cdbd2cSJim Jagielski    """Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case..."""
40*b1cdbd2cSJim Jagielski    import string
41*b1cdbd2cSJim Jagielski
42*b1cdbd2cSJim Jagielski    # The context variable is of type XScriptContext and is available to
43*b1cdbd2cSJim Jagielski    # all BeanShell scripts executed by the Script Framework
44*b1cdbd2cSJim Jagielski    xModel = XSCRIPTCONTEXT.getDocument()
45*b1cdbd2cSJim Jagielski
46*b1cdbd2cSJim Jagielski    #the writer controller impl supports the css.view.XSelectionSupplier interface
47*b1cdbd2cSJim Jagielski    xSelectionSupplier = xModel.getCurrentController()
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim Jagielski    #see section 7.5.1 of developers' guide
50*b1cdbd2cSJim Jagielski    xIndexAccess = xSelectionSupplier.getSelection()
51*b1cdbd2cSJim Jagielski    count = xIndexAccess.getCount();
52*b1cdbd2cSJim Jagielski    if(count>=1):  #ie we have a selection
53*b1cdbd2cSJim Jagielski        i=0
54*b1cdbd2cSJim Jagielski        while i < count :
55*b1cdbd2cSJim Jagielski            xTextRange = xIndexAccess.getByIndex(i);
56*b1cdbd2cSJim Jagielski            #print "string: " + xTextRange.getString();
57*b1cdbd2cSJim Jagielski            theString = xTextRange.getString();
58*b1cdbd2cSJim Jagielski            if len(theString)==0 :
59*b1cdbd2cSJim Jagielski                # sadly we can have a selection where nothing is selected
60*b1cdbd2cSJim Jagielski                # in this case we get the XWordCursor and make a selection!
61*b1cdbd2cSJim Jagielski                xText = xTextRange.getText();
62*b1cdbd2cSJim Jagielski                xWordCursor = xText.createTextCursorByRange(xTextRange);
63*b1cdbd2cSJim Jagielski                if not xWordCursor.isStartOfWord():
64*b1cdbd2cSJim Jagielski                    xWordCursor.gotoStartOfWord(False);
65*b1cdbd2cSJim Jagielski                xWordCursor.gotoNextWord(True);
66*b1cdbd2cSJim Jagielski                theString = xWordCursor.getString();
67*b1cdbd2cSJim Jagielski                newString = getNewString(theString);
68*b1cdbd2cSJim Jagielski                if newString :
69*b1cdbd2cSJim Jagielski                    xWordCursor.setString(newString);
70*b1cdbd2cSJim Jagielski                    xSelectionSupplier.select(xWordCursor);
71*b1cdbd2cSJim Jagielski            else :
72*b1cdbd2cSJim Jagielski
73*b1cdbd2cSJim Jagielski                newString = getNewString( theString );
74*b1cdbd2cSJim Jagielski                if newString:
75*b1cdbd2cSJim Jagielski                    xTextRange.setString(newString);
76*b1cdbd2cSJim Jagielski                    xSelectionSupplier.select(xTextRange);
77*b1cdbd2cSJim Jagielski            i+= 1
78*b1cdbd2cSJim Jagielski
79*b1cdbd2cSJim Jagielski
80*b1cdbd2cSJim Jagielski# lists the scripts, that shall be visible inside OOo. Can be omited, if
81*b1cdbd2cSJim Jagielski# all functions shall be visible, however here getNewString shall be surpressed
82*b1cdbd2cSJim Jagielskig_exportedScripts = capitalisePython,
83