2004-10-22 12:48:38 +00:00
// Change the case of a selection, or current word from upper case,
// to first char upper case, to all lower case to upper case...
2003-03-25 14:05:18 +00:00
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XModel;
import com.sun.star.view.XSelectionSupplier;
import com.sun.star.container.XIndexAccess;
import com.sun.star.text.XText;
import com.sun.star.text.XTextRange;
import com.sun.star.text.XWordCursor;
2004-10-22 12:48:38 +00:00
import com.sun.star.script.provider.XScriptContext;
2003-03-25 14:05:18 +00:00
2004-10-22 12:48:38 +00:00
// return the new string based on the string passed in
2003-03-25 14:05:18 +00:00
String getNewString( theString ) {
String newString;
if(theString==null || theString.length()==0) {
return newString;
}
// should we tokenize on "."?
if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC
newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase();
} else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC
newString=theString.toLowerCase();
} else { // all to UC.
newString=theString.toUpperCase();
}
return newString;
}
2004-10-22 12:48:38 +00:00
//the method that does the work
2003-03-25 14:05:18 +00:00
void capitalise() {
2004-10-22 12:48:38 +00:00
// get the number of regions selected
2003-03-25 14:05:18 +00:00
count = xIndexAccess.getCount();
if(count>=1) { //ie we have a selection
for(i=0;i<count;i++) {
2004-10-22 12:48:38 +00:00
// get the i-th region selected
2003-03-25 14:05:18 +00:00
xTextRange = (XTextRange)
UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
System.out.println("string: "+xTextRange.getString());
2004-10-22 12:48:38 +00:00
// get the selected string
2003-03-25 14:05:18 +00:00
theString = xTextRange.getString();
if(theString.length()==0) {
// sadly we can have a selection where nothing is selected
// in this case we get the XWordCursor and make a selection!
xText = (XText)
UnoRuntime.queryInterface(XText.class, xTextRange.getText());
xWordCursor = (XWordCursor)
UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange));
2004-10-22 12:48:38 +00:00
// move the Word cursor to the start of the word if its not
// already there
2003-03-25 14:05:18 +00:00
if(!xWordCursor.isStartOfWord()) {
xWordCursor.gotoStartOfWord(false);
}
2004-10-22 12:48:38 +00:00
// move the cursor to the next word, selecting all chars
// in between
2003-03-25 14:05:18 +00:00
xWordCursor.gotoNextWord(true);
2004-10-22 12:48:38 +00:00
// get the selected string
2003-03-25 14:05:18 +00:00
theString = xWordCursor.getString();
2004-10-22 12:48:38 +00:00
// get the new string
2003-03-25 14:05:18 +00:00
newString = getNewString(theString);
if(newString!=null) {
2004-10-22 12:48:38 +00:00
// set the new string
2003-03-25 14:05:18 +00:00
xWordCursor.setString(newString);
2004-10-22 12:48:38 +00:00
// keep the current selection
2003-03-25 14:05:18 +00:00
xSelectionSupplier.select(xWordCursor);
}
} else {
newString = getNewString( theString );
if(newString!=null) {
2004-10-22 12:48:38 +00:00
// set the new string
2003-03-25 14:05:18 +00:00
xTextRange.setString(newString);
2004-10-22 12:48:38 +00:00
// keep the current selection
2003-03-25 14:05:18 +00:00
xSelectionSupplier.select(xTextRange);
}
}
}
}
}
2004-10-22 12:48:38 +00:00
// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
2003-03-25 14:05:18 +00:00
// all BeanShell scripts executed by the Script Framework
xModel = (XModel)
2004-10-22 12:48:38 +00:00
UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
2003-03-25 14:05:18 +00:00
//the writer controller impl supports the css.view.XSelectionSupplier interface
xSelectionSupplier = (XSelectionSupplier)
UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
//see section 7.5.1 of developers' guide
xIndexAccess = (XIndexAccess)
UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
2004-10-22 12:48:38 +00:00
//call the method that does the work
2003-03-25 14:05:18 +00:00
capitalise();
return 0;