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