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