wordcount.bsh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. //Provides a word count of the selected text in a Writer document.
  19. import com.sun.star.uno.UnoRuntime;
  20. import com.sun.star.frame.XModel;
  21. import com.sun.star.view.XSelectionSupplier;
  22. import com.sun.star.container.XIndexAccess;
  23. import com.sun.star.text.XText;
  24. import com.sun.star.text.XTextRange;
  25. import com.sun.star.script.provider.XScriptContext;
  26. // display the count in a Swing dialog
  27. void doDisplay(numWords) {
  28. wordsLabel = new JLabel("Word count = " + numWords);
  29. closeButton = new JButton("Close");
  30. frame = new JFrame("Word Count");
  31. closeButton.addActionListener(new ActionListener() {
  32. actionPerformed(ActionEvent e) {
  33. frame.setVisible(false);
  34. }
  35. });
  36. frame.getContentPane().setLayout(new BorderLayout());
  37. frame.getContentPane().add(wordsLabel, BorderLayout.CENTER);
  38. frame.getContentPane().add(closeButton, BorderLayout.SOUTH);
  39. frame.pack();
  40. frame.setSize(190,90);
  41. frame.setLocation(430,430);
  42. frame.setVisible(true);
  43. }
  44. int wordcount() {
  45. result = 0;
  46. // iterate through each of the selections
  47. count = xIndexAccess.getCount();
  48. for(i=0;i<count;i++) {
  49. // get the XTextRange of the selection
  50. xTextRange = (XTextRange)
  51. UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
  52. //System.out.println("string: "+xTextRange.getString());
  53. // use the standard J2SE delimiters to tokenize the string
  54. // obtained from the XTextRange
  55. strTok = new StringTokenizer(xTextRange.getString());
  56. result += strTok.countTokens();
  57. }
  58. doDisplay(result);
  59. return result;
  60. }
  61. // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
  62. // all BeanShell scripts executed by the Script Framework
  63. xModel = (XModel)
  64. UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
  65. //the writer controller impl supports the css.view.XSelectionSupplier interface
  66. xSelectionSupplier = (XSelectionSupplier)
  67. UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
  68. //see section 7.5.1 of developers' guide
  69. // the getSelection provides an XIndexAccess to the one or more selections
  70. xIndexAccess = (XIndexAccess)
  71. UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
  72. count = wordcount();
  73. System.out.println("count = "+count);
  74. return 0;