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