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