1*cdf0e10cSrcweir//Provides a word count of the selected text in A Writer document. 2*cdf0e10cSrcweirimport com.sun.star.uno.UnoRuntime; 3*cdf0e10cSrcweirimport com.sun.star.frame.XModel; 4*cdf0e10cSrcweirimport com.sun.star.view.XSelectionSupplier; 5*cdf0e10cSrcweirimport com.sun.star.container.XIndexAccess; 6*cdf0e10cSrcweirimport com.sun.star.text.XText; 7*cdf0e10cSrcweirimport com.sun.star.text.XTextRange; 8*cdf0e10cSrcweirimport com.sun.star.script.provider.XScriptContext; 9*cdf0e10cSrcweir 10*cdf0e10cSrcweir// display the count in a Swing dialog 11*cdf0e10cSrcweirvoid doDisplay(numWords) { 12*cdf0e10cSrcweir wordsLabel = new JLabel("Word count = " + numWords); 13*cdf0e10cSrcweir closeButton = new JButton("Close"); 14*cdf0e10cSrcweir frame = new JFrame("Word Count"); 15*cdf0e10cSrcweir closeButton.addActionListener(new ActionListener() { 16*cdf0e10cSrcweir actionPerformed(ActionEvent e) { 17*cdf0e10cSrcweir frame.setVisible(false); 18*cdf0e10cSrcweir } 19*cdf0e10cSrcweir }); 20*cdf0e10cSrcweir frame.getContentPane().setLayout(new BorderLayout()); 21*cdf0e10cSrcweir frame.getContentPane().add(wordsLabel, BorderLayout.CENTER); 22*cdf0e10cSrcweir frame.getContentPane().add(closeButton, BorderLayout.SOUTH); 23*cdf0e10cSrcweir frame.pack(); 24*cdf0e10cSrcweir frame.setSize(190,90); 25*cdf0e10cSrcweir frame.setLocation(430,430); 26*cdf0e10cSrcweir frame.setVisible(true); 27*cdf0e10cSrcweir} 28*cdf0e10cSrcweir 29*cdf0e10cSrcweirint wordcount() { 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir result = 0; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir // iterate through each of the selections 34*cdf0e10cSrcweir count = xIndexAccess.getCount(); 35*cdf0e10cSrcweir for(i=0;i<count;i++) { 36*cdf0e10cSrcweir // get the XTextRange of the selection 37*cdf0e10cSrcweir xTextRange = (XTextRange) 38*cdf0e10cSrcweir UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i)); 39*cdf0e10cSrcweir //System.out.println("string: "+xTextRange.getString()); 40*cdf0e10cSrcweir // use the standard J2SE delimiters to tokenize the string 41*cdf0e10cSrcweir // obtained from the XTextRange 42*cdf0e10cSrcweir strTok = new StringTokenizer(xTextRange.getString()); 43*cdf0e10cSrcweir result += strTok.countTokens(); 44*cdf0e10cSrcweir } 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir doDisplay(result); 47*cdf0e10cSrcweir return result; 48*cdf0e10cSrcweir} 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to 51*cdf0e10cSrcweir// all BeanShell scripts executed by the Script Framework 52*cdf0e10cSrcweirxModel = (XModel) 53*cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument()); 54*cdf0e10cSrcweir//the writer controller impl supports the css.view.XSelectionSupplier interface 55*cdf0e10cSrcweirxSelectionSupplier = (XSelectionSupplier) 56*cdf0e10cSrcweir UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController()); 57*cdf0e10cSrcweir//see section 7.5.1 of developers' guide 58*cdf0e10cSrcweir// the getSelection provides an XIndexAccess to the one or more selections 59*cdf0e10cSrcweirxIndexAccess = (XIndexAccess) 60*cdf0e10cSrcweir UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection()); 61*cdf0e10cSrcweir 62*cdf0e10cSrcweircount = wordcount(); 63*cdf0e10cSrcweirSystem.out.println("count = "+count); 64*cdf0e10cSrcweirreturn 0; 65