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