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
22
23# helper function
24def getNewString( theString ) :
25    if( not theString or len(theString) ==0) :
26        return ""
27    # should we tokenize on "."?
28    if theString[0].isupper() and len(theString)>=2 and theString[1].isupper() :
29        # first two chars are UC => first UC, rest LC
30        newString=theString[0:1].upper() + theString[1:].lower();
31    elif theString[0].isupper():
32        # first char UC => all to LC
33        newString=theString.lower()
34    else: # all to UC.
35        newString=theString.upper()
36    return newString;
37
38def capitalisePython( ):
39    """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    import string
41
42    # The context variable is of type XScriptContext and is available to
43    # all BeanShell scripts executed by the Script Framework
44    xModel = XSCRIPTCONTEXT.getDocument()
45
46    #the writer controller impl supports the css.view.XSelectionSupplier interface
47    xSelectionSupplier = xModel.getCurrentController()
48
49    #see section 7.5.1 of developers' guide
50    xIndexAccess = xSelectionSupplier.getSelection()
51    count = xIndexAccess.getCount();
52    if(count>=1):  #ie we have a selection
53        i=0
54        while i < count :
55            xTextRange = xIndexAccess.getByIndex(i);
56            #print "string: " + xTextRange.getString();
57            theString = xTextRange.getString();
58            if len(theString)==0 :
59                # sadly we can have a selection where nothing is selected
60                # in this case we get the XWordCursor and make a selection!
61                xText = xTextRange.getText();
62                xWordCursor = xText.createTextCursorByRange(xTextRange);
63                if not xWordCursor.isStartOfWord():
64                    xWordCursor.gotoStartOfWord(False);
65                xWordCursor.gotoNextWord(True);
66                theString = xWordCursor.getString();
67                newString = getNewString(theString);
68                if newString :
69                    xWordCursor.setString(newString);
70                    xSelectionSupplier.select(xWordCursor);
71            else :
72
73                newString = getNewString( theString );
74                if newString:
75                    xTextRange.setString(newString);
76                    xSelectionSupplier.select(xTextRange);
77            i+= 1
78
79
80# lists the scripts, that shall be visible inside OOo. Can be omited, if
81# all functions shall be visible, however here getNewString shall be surpressed
82g_exportedScripts = capitalisePython,
83