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