15b501c92SAndrew Rist/************************************************************** 25b501c92SAndrew Rist * 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 105b501c92SAndrew Rist * 115b501c92SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 125b501c92SAndrew Rist * 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. 195b501c92SAndrew Rist * 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) { 33cdf0e10cSrcweir wordsLabel = new JLabel("Word count = " + numWords); 34cdf0e10cSrcweir closeButton = new JButton("Close"); 35cdf0e10cSrcweir frame = new JFrame("Word Count"); 36cdf0e10cSrcweir closeButton.addActionListener(new ActionListener() { 37cdf0e10cSrcweir actionPerformed(ActionEvent e) { 38cdf0e10cSrcweir frame.setVisible(false); 39cdf0e10cSrcweir } 40cdf0e10cSrcweir }); 41cdf0e10cSrcweir frame.getContentPane().setLayout(new BorderLayout()); 42cdf0e10cSrcweir frame.getContentPane().add(wordsLabel, BorderLayout.CENTER); 43cdf0e10cSrcweir frame.getContentPane().add(closeButton, BorderLayout.SOUTH); 44cdf0e10cSrcweir frame.pack(); 45cdf0e10cSrcweir frame.setSize(190,90); 46cdf0e10cSrcweir frame.setLocation(430,430); 47cdf0e10cSrcweir frame.setVisible(true); 48cdf0e10cSrcweir} 49cdf0e10cSrcweir 50cdf0e10cSrcweirint wordcount() { 51cdf0e10cSrcweir 52cdf0e10cSrcweir result = 0; 53cdf0e10cSrcweir 54cdf0e10cSrcweir // iterate through each of the selections 55cdf0e10cSrcweir count = xIndexAccess.getCount(); 56cdf0e10cSrcweir for(i=0;i<count;i++) { 57cdf0e10cSrcweir // get the XTextRange of the selection 58cdf0e10cSrcweir xTextRange = (XTextRange) 59cdf0e10cSrcweir UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i)); 60cdf0e10cSrcweir //System.out.println("string: "+xTextRange.getString()); 61cdf0e10cSrcweir // use the standard J2SE delimiters to tokenize the string 62cdf0e10cSrcweir // obtained from the XTextRange 63cdf0e10cSrcweir strTok = new StringTokenizer(xTextRange.getString()); 64cdf0e10cSrcweir result += strTok.countTokens(); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir doDisplay(result); 68cdf0e10cSrcweir 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) 74cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument()); 75cdf0e10cSrcweir//the writer controller impl supports the css.view.XSelectionSupplier interface 76cdf0e10cSrcweirxSelectionSupplier = (XSelectionSupplier) 77cdf0e10cSrcweir 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) 81cdf0e10cSrcweir UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection()); 82cdf0e10cSrcweir 83cdf0e10cSrcweircount = wordcount(); 84cdf0e10cSrcweirSystem.out.println("count = "+count); 85cdf0e10cSrcweirreturn 0; 86