1*5b501c92SAndrew Rist/************************************************************** 2*5b501c92SAndrew Rist * 3*5b501c92SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*5b501c92SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*5b501c92SAndrew Rist * distributed with this work for additional information 6*5b501c92SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*5b501c92SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*5b501c92SAndrew Rist * "License"); you may not use this file except in compliance 9*5b501c92SAndrew Rist * with the License. You may obtain a copy of the License at 10*5b501c92SAndrew Rist * 11*5b501c92SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*5b501c92SAndrew Rist * 13*5b501c92SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*5b501c92SAndrew Rist * software distributed under the License is distributed on an 15*5b501c92SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*5b501c92SAndrew Rist * KIND, either express or implied. See the License for the 17*5b501c92SAndrew Rist * specific language governing permissions and limitations 18*5b501c92SAndrew Rist * under the License. 19*5b501c92SAndrew Rist * 20*5b501c92SAndrew Rist *************************************************************/ 21cdf0e10cSrcweir// Change the case of a selection, or current word from upper case, 22cdf0e10cSrcweir// to first char upper case, to all lower case to upper case... 23cdf0e10cSrcweirimport com.sun.star.uno.UnoRuntime; 24cdf0e10cSrcweirimport com.sun.star.frame.XModel; 25cdf0e10cSrcweirimport com.sun.star.view.XSelectionSupplier; 26cdf0e10cSrcweirimport com.sun.star.container.XIndexAccess; 27cdf0e10cSrcweirimport com.sun.star.text.XText; 28cdf0e10cSrcweirimport com.sun.star.text.XTextRange; 29cdf0e10cSrcweirimport com.sun.star.text.XWordCursor; 30cdf0e10cSrcweirimport com.sun.star.script.provider.XScriptContext; 31cdf0e10cSrcweir 32cdf0e10cSrcweir// return the new string based on the string passed in 33cdf0e10cSrcweirString getNewString( theString ) { 34cdf0e10cSrcweir String newString; 35cdf0e10cSrcweir if(theString==null || theString.length()==0) { 36cdf0e10cSrcweir return newString; 37cdf0e10cSrcweir } 38cdf0e10cSrcweir // should we tokenize on "."? 39cdf0e10cSrcweir if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC 40cdf0e10cSrcweir newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase(); 41cdf0e10cSrcweir } else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC 42cdf0e10cSrcweir newString=theString.toLowerCase(); 43cdf0e10cSrcweir } else { // all to UC. 44cdf0e10cSrcweir newString=theString.toUpperCase(); 45cdf0e10cSrcweir } 46cdf0e10cSrcweir return newString; 47cdf0e10cSrcweir} 48cdf0e10cSrcweir 49cdf0e10cSrcweir//the method that does the work 50cdf0e10cSrcweirvoid capitalise() { 51cdf0e10cSrcweir 52cdf0e10cSrcweir // get the number of regions selected 53cdf0e10cSrcweir count = xIndexAccess.getCount(); 54cdf0e10cSrcweir if(count>=1) { //ie we have a selection 55cdf0e10cSrcweir for(i=0;i<count;i++) { 56cdf0e10cSrcweir // get the i-th region selected 57cdf0e10cSrcweir xTextRange = (XTextRange) 58cdf0e10cSrcweir UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i)); 59cdf0e10cSrcweir System.out.println("string: "+xTextRange.getString()); 60cdf0e10cSrcweir // get the selected string 61cdf0e10cSrcweir theString = xTextRange.getString(); 62cdf0e10cSrcweir if(theString.length()==0) { 63cdf0e10cSrcweir // sadly we can have a selection where nothing is selected 64cdf0e10cSrcweir // in this case we get the XWordCursor and make a selection! 65cdf0e10cSrcweir xText = (XText) 66cdf0e10cSrcweir UnoRuntime.queryInterface(XText.class, xTextRange.getText()); 67cdf0e10cSrcweir xWordCursor = (XWordCursor) 68cdf0e10cSrcweir UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange)); 69cdf0e10cSrcweir // move the Word cursor to the start of the word if its not 70cdf0e10cSrcweir // already there 71cdf0e10cSrcweir if(!xWordCursor.isStartOfWord()) { 72cdf0e10cSrcweir xWordCursor.gotoStartOfWord(false); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir // move the cursor to the next word, selecting all chars 75cdf0e10cSrcweir // in between 76cdf0e10cSrcweir xWordCursor.gotoNextWord(true); 77cdf0e10cSrcweir // get the selected string 78cdf0e10cSrcweir theString = xWordCursor.getString(); 79cdf0e10cSrcweir // get the new string 80cdf0e10cSrcweir newString = getNewString(theString); 81cdf0e10cSrcweir if(newString!=null) { 82cdf0e10cSrcweir // set the new string 83cdf0e10cSrcweir xWordCursor.setString(newString); 84cdf0e10cSrcweir // keep the current selection 85cdf0e10cSrcweir xSelectionSupplier.select(xWordCursor); 86cdf0e10cSrcweir } 87cdf0e10cSrcweir } else { 88cdf0e10cSrcweir newString = getNewString( theString ); 89cdf0e10cSrcweir if(newString!=null) { 90cdf0e10cSrcweir // set the new string 91cdf0e10cSrcweir xTextRange.setString(newString); 92cdf0e10cSrcweir // keep the current selection 93cdf0e10cSrcweir xSelectionSupplier.select(xTextRange); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir } 98cdf0e10cSrcweir } 99cdf0e10cSrcweir} 100cdf0e10cSrcweir 101cdf0e10cSrcweir// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to 102cdf0e10cSrcweir// all BeanShell scripts executed by the Script Framework 103cdf0e10cSrcweirxModel = (XModel) 104cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument()); 105cdf0e10cSrcweir//the writer controller impl supports the css.view.XSelectionSupplier interface 106cdf0e10cSrcweirxSelectionSupplier = (XSelectionSupplier) 107cdf0e10cSrcweir UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController()); 108cdf0e10cSrcweir//see section 7.5.1 of developers' guide 109cdf0e10cSrcweirxIndexAccess = (XIndexAccess) 110cdf0e10cSrcweir UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection()); 111cdf0e10cSrcweir 112cdf0e10cSrcweir//call the method that does the work 113cdf0e10cSrcweircapitalise(); 114cdf0e10cSrcweirreturn 0; 115