1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22//Provides a word count of the selected text in a Writer document.
23import com.sun.star.uno.UnoRuntime;
24import com.sun.star.frame.XModel;
25import com.sun.star.view.XSelectionSupplier;
26import com.sun.star.container.XIndexAccess;
27import com.sun.star.text.XText;
28import com.sun.star.text.XTextRange;
29import com.sun.star.script.provider.XScriptContext;
30
31// display the count in a Swing dialog
32void doDisplay(numWords) {
33	wordsLabel = new JLabel("Word count = " + numWords);
34	closeButton = new JButton("Close");
35	frame = new JFrame("Word Count");
36	closeButton.addActionListener(new ActionListener() {
37		actionPerformed(ActionEvent e) {
38			frame.setVisible(false);
39		}
40	});
41	frame.getContentPane().setLayout(new BorderLayout());
42	frame.getContentPane().add(wordsLabel, BorderLayout.CENTER);
43	frame.getContentPane().add(closeButton, BorderLayout.SOUTH);
44	frame.pack();
45	frame.setSize(190,90);
46	frame.setLocation(430,430);
47	frame.setVisible(true);
48}
49
50int wordcount() {
51
52	result = 0;
53
54	// iterate through each of the selections
55	count = xIndexAccess.getCount();
56	for(i=0;i<count;i++) {
57		// get the XTextRange of the selection
58		xTextRange = (XTextRange)
59			UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i));
60		//System.out.println("string: "+xTextRange.getString());
61		// use the standard J2SE delimiters to tokenize the string
62		// obtained from the XTextRange
63		strTok = new StringTokenizer(xTextRange.getString());
64		result += strTok.countTokens();
65	}
66
67	doDisplay(result);
68	return result;
69}
70
71// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
72// all BeanShell scripts executed by the Script Framework
73xModel = (XModel)
74	UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument());
75//the writer controller impl supports the css.view.XSelectionSupplier interface
76xSelectionSupplier = (XSelectionSupplier)
77	UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController());
78//see section 7.5.1 of developers' guide
79// the getSelection provides an XIndexAccess to the one or more selections
80xIndexAccess = (XIndexAccess)
81	UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection());
82
83count = wordcount();
84System.out.println("count = "+count);
85return 0;
86