capitalise.bsh 4.8 KB

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