diff options
Diffstat (limited to 'scripting/examples/beanshell')
26 files changed, 1331 insertions, 0 deletions
diff --git a/scripting/examples/beanshell/Calc/CopyRange.bsh b/scripting/examples/beanshell/Calc/CopyRange.bsh new file mode 100644 index 000000000..d1e7a49f1 --- /dev/null +++ b/scripting/examples/beanshell/Calc/CopyRange.bsh @@ -0,0 +1,39 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.sheet.XCellAddressable; +import com.sun.star.sheet.XCellRangeAddressable; +import com.sun.star.table.CellAddress; +import com.sun.star.table.CellRangeAddress; +import com.sun.star.sheet.XCellRangeMovement; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); + +XCellRangeAddressable xAddr1 = UnoRuntime.queryInterface(XCellRangeAddressable.class, xSheet.getCellRangeByName("A1:A10") ); +CellRangeAddress source = xAddr1.getRangeAddress(); + +XCellAddressable xAddr2 = UnoRuntime.queryInterface(XCellAddressable.class, xSheet.getCellRangeByName("B1").getCellByPosition( 0, 0 ) ); +CellAddress target = xAddr2.getCellAddress(); + +XCellRangeMovement xCRM = UnoRuntime.queryInterface(XCellRangeMovement.class, xSheet); +xCRM.copyRange(target, source); + +return 0; diff --git a/scripting/examples/beanshell/Calc/FixView.bsh b/scripting/examples/beanshell/Calc/FixView.bsh new file mode 100644 index 000000000..4ea2c62f9 --- /dev/null +++ b/scripting/examples/beanshell/Calc/FixView.bsh @@ -0,0 +1,34 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.sheet.XViewFreezable; +import com.sun.star.sheet.XViewPane; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); + +XViewFreezable xFreeze = UnoRuntime.queryInterface(XViewFreezable.class, oDoc.getCurrentController() ); +xFreeze.freezeAtPosition(2, 3); + +XViewPane xViewPane = UnoRuntime.queryInterface(XViewPane.class, oDoc.getCurrentController() ); +xViewPane.setFirstVisibleColumn(12); +xViewPane.setFirstVisibleRow(149); + +return 0; diff --git a/scripting/examples/beanshell/Calc/InsertSheet.bsh b/scripting/examples/beanshell/Calc/InsertSheet.bsh new file mode 100644 index 000000000..ef68f9b3e --- /dev/null +++ b/scripting/examples/beanshell/Calc/InsertSheet.bsh @@ -0,0 +1,25 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.lang.XMultiServiceFactory; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); + +xSheets.insertNewByName("First new sheet", (short)0); +xSheets.insertNewByName("Second new sheet", (short)1); + +return 0; diff --git a/scripting/examples/beanshell/Calc/ProtectSheet.bsh b/scripting/examples/beanshell/Calc/ProtectSheet.bsh new file mode 100644 index 000000000..eea3bd511 --- /dev/null +++ b/scripting/examples/beanshell/Calc/ProtectSheet.bsh @@ -0,0 +1,30 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.container.XIndexAccess; +import com.sun.star.util.XProtectable; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(0)); +XProtectable xProtectable = UnoRuntime.queryInterface(XProtectable.class, xSheet); +xProtectable.protect("myPassword"); + +//xProtectable.unprotect("myPassword"); + +return 0; diff --git a/scripting/examples/beanshell/Calc/SelectCell.bsh b/scripting/examples/beanshell/Calc/SelectCell.bsh new file mode 100644 index 000000000..7d8f5a381 --- /dev/null +++ b/scripting/examples/beanshell/Calc/SelectCell.bsh @@ -0,0 +1,30 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.sheet.XSpreadsheetDocument; +import com.sun.star.sheet.XSpreadsheet; +import com.sun.star.sheet.XSpreadsheets; +import com.sun.star.view.XSelectionSupplier; +import com.sun.star.container.XIndexAccess; +import com.sun.star.table.XCellRange; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class,oDoc); +XSpreadsheets xSheets = xDoc.getSheets(); +XIndexAccess xSheetsIA = UnoRuntime.queryInterface(XIndexAccess.class, xSheets); +XSpreadsheet xSheet = UnoRuntime.queryInterface(com.sun.star.sheet.XSpreadsheet.class, xSheetsIA.getByIndex(0)); +XCellRange xResultRange = xSheet.getCellRangeByName("B20"); +XSelectionSupplier xSel = UnoRuntime.queryInterface(XSelectionSupplier.class, oDoc.getCurrentController()); +xSel.select(xResultRange); + +return 0; diff --git a/scripting/examples/beanshell/Calc/parcel-descriptor.xml b/scripting/examples/beanshell/Calc/parcel-descriptor.xml new file mode 100644 index 000000000..155d53c9b --- /dev/null +++ b/scripting/examples/beanshell/Calc/parcel-descriptor.xml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><parcel xmlns:parcel="scripting.dtd" language="BeanShell"> +<script language="BeanShell"><locale lang="en"><displayname value="SelectCell.bsh"/><description>SelectCell.bsh</description></locale><logicalname value="SelectCell.bsh"/><functionname value="SelectCell.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="InsertSheet.bsh"/><description>InsertSheet.bsh</description></locale><logicalname value="InsertSheet.bsh"/><functionname value="InsertSheet.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ProtectSheet.bsh"/><description>ProtectSheet.bsh</description></locale><logicalname value="ProtectSheet.bsh"/><functionname value="ProtectSheet.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="CopyRange.bsh"/><description>CopyRange.bsh</description></locale><logicalname value="CopyRange.bsh"/><functionname value="CopyRange.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="FixView.bsh"/><description>FixView.bsh</description></locale><logicalname value="FixView.bsh"/><functionname value="FixView.bsh"/></script></parcel>
\ No newline at end of file diff --git a/scripting/examples/beanshell/Capitalise/capitalise.bsh b/scripting/examples/beanshell/Capitalise/capitalise.bsh new file mode 100644 index 000000000..6b68e010f --- /dev/null +++ b/scripting/examples/beanshell/Capitalise/capitalise.bsh @@ -0,0 +1,111 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +// Change the case of a selection, or current word from upper case, +// to first char upper case, to all lower case to upper case... +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.view.XSelectionSupplier; +import com.sun.star.container.XIndexAccess; +import com.sun.star.text.XText; +import com.sun.star.text.XTextRange; +import com.sun.star.text.XWordCursor; +import com.sun.star.script.provider.XScriptContext; + +// return the new string based on the string passed in +String getNewString( theString ) { + String newString; + if(theString==null || theString.length()==0) { + return newString; + } + // should we tokenize on "."? + if(Character.isUpperCase(theString.charAt(0)) && theString.length()>=2 && Character.isUpperCase(theString.charAt(1))) { // first two chars are UC => first UC, rest LC + newString=theString.substring(0,1).toUpperCase()+theString.substring(1).toLowerCase(); + } else if (Character.isUpperCase(theString.charAt(0))) { // first char UC => all to LC + newString=theString.toLowerCase(); + } else { // all to UC. + newString=theString.toUpperCase(); + } + return newString; +} + +//the method that does the work +void capitalise() { + + // get the number of regions selected + count = xIndexAccess.getCount(); + if(count>=1) { //ie we have a selection + for(i=0;i<count;i++) { + // get the i-th region selected + xTextRange = (XTextRange) + UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i)); + System.out.println("string: "+xTextRange.getString()); + // get the selected string + theString = xTextRange.getString(); + if(theString.length()==0) { + // sadly we can have a selection where nothing is selected + // in this case we get the XWordCursor and make a selection! + xText = (XText) + UnoRuntime.queryInterface(XText.class, xTextRange.getText()); + xWordCursor = (XWordCursor) + UnoRuntime.queryInterface(XWordCursor.class, xText.createTextCursorByRange(xTextRange)); + // move the Word cursor to the start of the word if it's not + // already there + if(!xWordCursor.isStartOfWord()) { + xWordCursor.gotoStartOfWord(false); + } + // move the cursor to the next word, selecting all chars + // in between + xWordCursor.gotoNextWord(true); + // get the selected string + theString = xWordCursor.getString(); + // get the new string + newString = getNewString(theString); + if(newString!=null) { + // set the new string + xWordCursor.setString(newString); + // keep the current selection + xSelectionSupplier.select(xWordCursor); + } + } else { + newString = getNewString( theString ); + if(newString!=null) { + // set the new string + xTextRange.setString(newString); + // keep the current selection + xSelectionSupplier.select(xTextRange); + } + } + + } + } +} + +// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to +// all BeanShell scripts executed by the Script Framework +xModel = (XModel) + UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument()); +//the writer controller impl supports the css.view.XSelectionSupplier interface +xSelectionSupplier = (XSelectionSupplier) + UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController()); +//see section 7.5.1 of developers' guide +xIndexAccess = (XIndexAccess) + UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection()); + +//call the method that does the work +capitalise(); +return 0; diff --git a/scripting/examples/beanshell/Capitalise/parcel-descriptor.xml b/scripting/examples/beanshell/Capitalise/parcel-descriptor.xml new file mode 100644 index 000000000..d6fb419aa --- /dev/null +++ b/scripting/examples/beanshell/Capitalise/parcel-descriptor.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + + <script language="BeanShell"> + <locale lang="en"> + <displayname value="Capitalise"/> + <description> + Change the case of a selection, or current word from upper case, to first char upper case, to all lower case to upper case... + </description> + </locale> + <functionname value="capitalise.bsh"/> + <logicalname value="Capitalise.BeanShell"/> + </script> + +</parcel> diff --git a/scripting/examples/beanshell/HelloWorld/helloworld.bsh b/scripting/examples/beanshell/HelloWorld/helloworld.bsh new file mode 100644 index 000000000..4ff9cd42e --- /dev/null +++ b/scripting/examples/beanshell/HelloWorld/helloworld.bsh @@ -0,0 +1,34 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +// Hello World in BeanShell +import com.sun.star.uno.UnoRuntime; +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; +import com.sun.star.text.XTextRange; + +// get the document from the scripting context which is made available to all +// scripts +oDoc = XSCRIPTCONTEXT.getDocument(); +//get the XTextDocument interface +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +//get the XText interface +xText = xTextDoc.getText(); +// get an (empty) XTextRange at the end of the document +xTextRange = xText.getEnd(); +// set the string +xTextRange.setString( "Hello World (in BeanShell)" ); diff --git a/scripting/examples/beanshell/HelloWorld/parcel-descriptor.xml b/scripting/examples/beanshell/HelloWorld/parcel-descriptor.xml new file mode 100644 index 000000000..e4c073b5f --- /dev/null +++ b/scripting/examples/beanshell/HelloWorld/parcel-descriptor.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + + <script language="BeanShell"> + <locale lang="en"> + <displayname value="Hello World"/> + <description> + Adds the string "Hello World" into the current text doc. + </description> + </locale> + <functionname value="helloworld.bsh"/> + <logicalname value="HelloWorld.BeanShell"/> + </script> + +</parcel> diff --git a/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh b/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh new file mode 100644 index 000000000..ac6efacce --- /dev/null +++ b/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh @@ -0,0 +1,123 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +// this code is bound to the events generated by the buttons in the dialog +// it will close the dialog or find and highlight the text entered in the +// dialog (depending on the button pressed) +import com.sun.star.uno.*; +import com.sun.star.awt.*; +import com.sun.star.lang.*; +import com.sun.star.beans.*; +import com.sun.star.util.*; +import com.sun.star.script.framework.browse.DialogFactory; + +// Get the ActionEvent object from the ARGUMENTS list +ActionEvent event = (ActionEvent) ARGUMENTS[0]; + +// Each argument is of type Any so we must use the AnyConverter class to +// convert it into the interface or primitive type we expect +XButton button = (XButton)AnyConverter.toObject( + new Type(XButton.class), event.Source); + +// We can now query for the model of the button and get its properties +XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button); +XControlModel cmodel = control.getModel(); +XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface( + XPropertySet.class, cmodel); + +if (pset.getPropertyValue("Label").equals("Exit")) +{ + // We can get the XDialog in which this control appears by calling + // getContext() on the XControl interface + XDialog xDialog = (XDialog)UnoRuntime.queryInterface( + XDialog.class, control.getContext()); + + // Close the dialog + xDialog.endExecute(); +} +else +{ + // We can get the list of controls for this dialog by calling + // getContext() on the XControl interface of the button + XControlContainer controls = (XControlContainer)UnoRuntime.queryInterface( + XControlContainer.class, control.getContext()); + + // Now get the text field control from the list + XTextComponent textField = (XTextComponent) + UnoRuntime.queryInterface( + XTextComponent.class, controls.getControl("HighlightTextField")); + + String searchKey = textField.getText(); + + // highlight the text in red + java.awt.Color cRed = new java.awt.Color(255, 0, 0); + int red = cRed.getRGB(); + + XReplaceable replaceable = (XReplaceable) + UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument()); + + XReplaceDescriptor descriptor = + (XReplaceDescriptor) replaceable.createReplaceDescriptor(); + + // Gets a XPropertyReplace object for altering the properties + // of the replaced text + XPropertyReplace xPropertyReplace = (XPropertyReplace) + UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); + + // Sets the replaced text property fontweight value to Bold + PropertyValue wv = new PropertyValue("CharWeight", -1, + new Float(com.sun.star.awt.FontWeight.BOLD), + com.sun.star.beans.PropertyState.DIRECT_VALUE); + + // Sets the replaced text property color value to RGB parameter + PropertyValue cv = new PropertyValue("CharColor", -1, + new Integer(red), + com.sun.star.beans.PropertyState.DIRECT_VALUE); + + // Apply the properties + PropertyValue[] props = new PropertyValue[] { cv, wv }; + + try { + xPropertyReplace.setReplaceAttributes(props); + + // Only matches whole words and case sensitive + descriptor.setPropertyValue( + "SearchCaseSensitive", new Boolean(true)); + descriptor.setPropertyValue("SearchWords", new Boolean(true)); + } + catch (com.sun.star.beans.UnknownPropertyException upe) { + System.err.println("Error setting up search properties"); + return; + } + catch (com.sun.star.beans.PropertyVetoException pve) { + System.err.println("Error setting up search properties"); + return; + } + catch (com.sun.star.lang.WrappedTargetException wte) { + System.err.println("Error setting up search properties"); + return; + } + + // Replaces all instances of searchKey with new Text properties + // and gets the number of instances of the searchKey + descriptor.setSearchString(searchKey); + descriptor.setReplaceString(searchKey); + replaceable.replaceAll(descriptor); +} + +// BeanShell scripts in LibreOffice should always return 0 +return 0; diff --git a/scripting/examples/beanshell/Highlight/ShowDialog.bsh b/scripting/examples/beanshell/Highlight/ShowDialog.bsh new file mode 100644 index 000000000..95ccc32bb --- /dev/null +++ b/scripting/examples/beanshell/Highlight/ShowDialog.bsh @@ -0,0 +1,140 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +// this script serves as an example of how to launch a Basic Dialog +// from a script +import com.sun.star.uno.UnoRuntime; +import com.sun.star.script.provider.XScriptContext; +import com.sun.star.lang.XMultiComponentFactory; +import com.sun.star.lang.EventObject; +import com.sun.star.uno.Type; +import com.sun.star.uno.AnyConverter; +import com.sun.star.text.XTextDocument; +import com.sun.star.beans.PropertyValue; +import com.sun.star.script.XLibraryContainer; +import com.sun.star.awt.*; +import com.sun.star.util.*; + +boolean tryLoadingLibrary( xmcf, context, name ) +{ + try + { + obj = xmcf.createInstanceWithContext( + "com.sun.star.script.Application" + name + "LibraryContainer", + context.getComponentContext()); + + xLibraryContainer = (XLibraryContainer) + UnoRuntime.queryInterface(XLibraryContainer.class, obj); + + System.err.println("Got XLibraryContainer"); + + serviceObj = context.getComponentContext().getValueByName( + "/singletons/com.sun.star.util.theMacroExpander"); + + xme = (XMacroExpander) AnyConverter.toObject( + new Type(XMacroExpander.class), serviceObj); + + bootstrapName = "bootstraprc"; + if (System.getProperty("os.name").startsWith("Windows")) + { + bootstrapName = "bootstrap.ini"; + } + + libURL = xme.expandMacros( + "$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/basic/ScriptBindingLibrary/" + + name.toLowerCase() + ".xlb/"); + + System.err.println("libURL is: " + libURL); + + xLibraryContainer.createLibraryLink( + "ScriptBindingLibrary", libURL, false); + + System.err.println("liblink created"); + + } + catch (com.sun.star.uno.Exception e) + { + System.err.println("Got an exception loading lib: " + e.getMessage()); + return false; + } + return true; +} + +// get the XMultiComponentFactory from the XSCRIPTCONTEXT +XMultiComponentFactory xmcf = + XSCRIPTCONTEXT.getComponentContext().getServiceManager(); + +Object[] args = new Object[1]; +args[0] = XSCRIPTCONTEXT.getDocument(); + +Object obj; +try { + // try to create an instance of the DialogProvider + obj = xmcf.createInstanceWithArgumentsAndContext( + "com.sun.star.awt.DialogProvider", args, + XSCRIPTCONTEXT.getComponentContext()); + /* + obj = xmcf.createInstanceWithContext( + "com.sun.star.awt.DialogProvider", + XSCRIPTCONTEXT.getComponentContext()); + */ +} +catch (com.sun.star.uno.Exception e) { + System.err.println("Error getting DialogProvider object"); + return 0; +} + +// get the XDialogProvider interface from the object created above +XDialogProvider xDialogProvider = (XDialogProvider) + UnoRuntime.queryInterface(XDialogProvider.class, obj); + +System.err.println("Got DialogProvider, now get dialog"); + +try { + // try to create the Highlight dialog (found in the ScriptBindingLibrary) + findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + + "ScriptBindingLibrary.Highlight?location=application"); + if( findDialog == null ) + { + if (tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Dialog") == false || + tryLoadingLibrary(xmcf, XSCRIPTCONTEXT, "Script") == false) + { + System.err.println("Error loading ScriptBindingLibrary"); + return 0; + } + else + { + // try to create the Highlight dialog (found in the ScriptBindingLibrary) + findDialog = xDialogProvider.createDialog("vnd.sun.star.script:" + + "ScriptBindingLibrary.Highlight?location=application"); + } + } +} +catch (java.lang.Exception e) { + System.err.println("Got exception on first creating dialog: " + + e.getMessage()); +} + +// execute the dialog in a new thread (so that this script can finish) +Thread t = new Thread() { + public void run() { + findDialog.execute(); + } +}; +t.start(); + +return 0; diff --git a/scripting/examples/beanshell/Highlight/highlighter.bsh b/scripting/examples/beanshell/Highlight/highlighter.bsh new file mode 100644 index 000000000..a69f76e1a --- /dev/null +++ b/scripting/examples/beanshell/Highlight/highlighter.bsh @@ -0,0 +1,166 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +import com.sun.star.uno.UnoRuntime; +import com.sun.star.util.XReplaceable; +import com.sun.star.util.XReplaceDescriptor; +import com.sun.star.util.XPropertyReplace; +import com.sun.star.beans.PropertyValue; +import com.sun.star.text.XTextDocument; +import com.sun.star.script.provider.XScriptContext; + +int replaceText(searchKey, color, bold) { + + result = 0; + + try { + // Create an XReplaceable object and an XReplaceDescriptor + replaceable = (XReplaceable) + UnoRuntime.queryInterface(XReplaceable.class, xTextDocument); + + descriptor = + (XReplaceDescriptor) replaceable.createReplaceDescriptor(); + + // Gets a XPropertyReplace object for altering the properties + // of the replaced text + xPropertyReplace = (XPropertyReplace) + UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); + + // Sets the replaced text property fontweight value to Bold or Normal + wv = null; + if (bold) { + wv = new PropertyValue("CharWeight", -1, + new Float(com.sun.star.awt.FontWeight.BOLD), + com.sun.star.beans.PropertyState.DIRECT_VALUE); + } + else { + wv = new PropertyValue("CharWeight", -1, + new Float(com.sun.star.awt.FontWeight.NORMAL), + com.sun.star.beans.PropertyState.DIRECT_VALUE); + } + + // Sets the replaced text property color value to RGB color parameter + cv = new PropertyValue("CharColor", -1, new Integer(color), + com.sun.star.beans.PropertyState.DIRECT_VALUE); + + // Apply the properties + PropertyValue[] props = { cv, wv }; + xPropertyReplace.setReplaceAttributes(props); + + // Only matches whole words and case sensitive + descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true)); + descriptor.setPropertyValue("SearchWords", new Boolean(true)); + + // Replaces all instances of searchKey with new Text properties + // and gets the number of instances of the searchKey + descriptor.setSearchString(searchKey); + descriptor.setReplaceString(searchKey); + result = replaceable.replaceAll(descriptor); + + } + catch (Exception e) { + } + + return result; +} + +searchKey = ""; + +// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to +// all BeanShell scripts executed by the Script Framework +xTextDocument = (XTextDocument) + UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument()); + +// Create a JButton and add an ActionListener +// When clicked the value for the searchKey is read and passed to replaceText +myListener = new ActionListener() { + actionPerformed(ActionEvent e) { + searchKey = findTextBox.getText(); + + if(searchKey.equalsIgnoreCase("")) { + JOptionPane.showMessageDialog(null, + "No text entered for search", + "No text", JOptionPane.INFORMATION_MESSAGE); + } + else { + // highlight the text in red + cRed = new Color(255, 0, 0); + red = cRed.getRGB(); + num = replaceText(searchKey, red, true); + + if(num > 0) { + int response = JOptionPane.showConfirmDialog(null, + searchKey + " was found " + num + + " times\nDo you wish to keep the text highlighted?", + "Confirm highlight", JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); + + if (response == 1) { + cBlack = new Color(255, 255, 255); + black = cBlack.getRGB(); + replaceText(searchKey, black, false); + } + } + else { + JOptionPane.showMessageDialog(null, + "No matches were found", "Not found", + JOptionPane.INFORMATION_MESSAGE); + } + } + } +}; + + +exitListener = new ActionListener() { + actionPerformed(ActionEvent e) { + frame.dispose(); + } +}; + + +searchButton = new JButton("Highlight"); +searchButton.addActionListener(myListener); + +exitButton = new JButton("Exit"); +exitButton.addActionListener(exitListener); + +buttonPanel = new JPanel(); +buttonPanel.setLayout(new FlowLayout()); +buttonPanel.add(searchButton); +buttonPanel.add(exitButton); + + +// Create a JPanel containing one JTextField for the search text. +searchPanel = new JPanel(); +searchPanel.setLayout(new FlowLayout()); +findTextBox = new JTextField(20); +findWhat = new JLabel("Find What: "); +searchPanel.add(findWhat); +searchPanel.add(findTextBox); + +// Create frame and add a window listener +frame = new JFrame("Highlight Text"); +frame.setSize(350,130); +frame.setLocation(430,430); +frame.setResizable(false); +// Add the panel and button to the frame +frame.getContentPane().setLayout(new GridLayout(2,1,10,10)); +frame.getContentPane().add(searchPanel); +frame.getContentPane().add(buttonPanel); + +frame.setVisible(true); +frame.pack(); diff --git a/scripting/examples/beanshell/Highlight/parcel-descriptor.xml b/scripting/examples/beanshell/Highlight/parcel-descriptor.xml new file mode 100644 index 000000000..a2a3773a9 --- /dev/null +++ b/scripting/examples/beanshell/Highlight/parcel-descriptor.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + <script language="BeanShell"> + <locale lang="en"> + <displayname value="ShowDialog" /> + <description> + Example of how to show a dialog from BeanShell + </description> + </locale> + <functionname value="ShowDialog.bsh" /> + <logicalname value="ShowDialog.BeanShell" /> + </script> + <script language="BeanShell"> + <locale lang="en"> + <displayname value="ButtonPressHandler" /> + <description> + Example of handle button press events for the Dialog + </description> + </locale> + <functionname value="ButtonPressHandler.bsh" /> + <logicalname value="ButtonPressHandler.BeanShell" /> + </script> +</parcel> + diff --git a/scripting/examples/beanshell/InteractiveBeanShell/interactive.bsh b/scripting/examples/beanshell/InteractiveBeanShell/interactive.bsh new file mode 100644 index 000000000..0e82f1df0 --- /dev/null +++ b/scripting/examples/beanshell/InteractiveBeanShell/interactive.bsh @@ -0,0 +1,21 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +// Pops up a window into which you can type BeanShell code and run it +// against the current document +editor(); +return 0; diff --git a/scripting/examples/beanshell/InteractiveBeanShell/parcel-descriptor.xml b/scripting/examples/beanshell/InteractiveBeanShell/parcel-descriptor.xml new file mode 100644 index 000000000..0911ac56a --- /dev/null +++ b/scripting/examples/beanshell/InteractiveBeanShell/parcel-descriptor.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + + <script language="BeanShell"> + <locale lang="en"> + <displayname value="Interactive BeanShell"/> + <description> + Pops up a window into which you can type BeanShell code and run it against the current document + </description> + </locale> + <functionname value="interactive.bsh"/> + <logicalname value="Interactive.BeanShell"/> + </script> + +</parcel> diff --git a/scripting/examples/beanshell/MemoryUsage/memusage.bsh b/scripting/examples/beanshell/MemoryUsage/memusage.bsh new file mode 100644 index 000000000..3929f6435 --- /dev/null +++ b/scripting/examples/beanshell/MemoryUsage/memusage.bsh @@ -0,0 +1,137 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.AnyConverter; +import com.sun.star.uno.Type; +import com.sun.star.lang.XComponent; +import com.sun.star.lang.XMultiServiceFactory; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.document.XEmbeddedObjectSupplier; +import com.sun.star.awt.ActionEvent; +import com.sun.star.awt.Rectangle; +import com.sun.star.beans.XPropertySet; +import com.sun.star.beans.PropertyValue; + +import com.sun.star.container.*; +import com.sun.star.chart.*; +import com.sun.star.table.*; +import com.sun.star.sheet.*; + +import com.sun.star.script.provider.XScriptContext; + +createSpreadsheet() +{ + loader = (XComponentLoader) + UnoRuntime.queryInterface( + XComponentLoader.class, XSCRIPTCONTEXT.getDesktop()); + + comp = loader.loadComponentFromURL( + "private:factory/scalc", "_blank", 4, new PropertyValue[0]); + + doc = (XSpreadsheetDocument) + UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp); + + index = (XIndexAccess) + UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets()); + + sheet = (XSpreadsheet) AnyConverter.toObject( + new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0)); + + return sheet; +} + +addData(sheet, date, total, free) +{ + // set the labels + sheet.getCellByPosition(0, 0).setFormula("Used"); + sheet.getCellByPosition(0, 1).setFormula("Free"); + sheet.getCellByPosition(0, 2).setFormula("Total"); + + // set the values in the cells + sheet.getCellByPosition(1, 0).setValue(total - free); + sheet.getCellByPosition(1, 1).setValue(free); + sheet.getCellByPosition(1, 2).setValue(total); +} + +addChart(sheet) +{ + rect = new Rectangle(); + rect.X = 500; + rect.Y = 3000; + rect.Width = 10000; + rect.Height = 8000; + + range = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, sheet); + myRange = range.getCellRangeByName("A1:B2"); + + rangeAddr = (XCellRangeAddressable) + UnoRuntime.queryInterface(XCellRangeAddressable.class, myRange); + + myAddr = rangeAddr.getRangeAddress(); + + CellRangeAddress[] addr = new CellRangeAddress[1]; + addr[0] = myAddr; + + supp = (XTableChartsSupplier) + UnoRuntime.queryInterface( XTableChartsSupplier.class, sheet); + charts = supp.getCharts(); + charts.addNewByName("Example", rect, addr, false, true); + + try { Thread.sleep(3000); } catch (java.lang.InterruptedException e) { } + + // get the diagram and Change some of the properties + chartsAccess = (XNameAccess) + UnoRuntime.queryInterface( XNameAccess.class, charts); + + tchart = (XTableChart) + UnoRuntime.queryInterface( + XTableChart.class, chartsAccess.getByName("Example")); + + eos = (XEmbeddedObjectSupplier) + UnoRuntime.queryInterface( XEmbeddedObjectSupplier.class, tchart ); + xifc = eos.getEmbeddedObject(); + + xChart = (XChartDocument) + UnoRuntime.queryInterface(XChartDocument.class, xifc); + + xDocMSF = (XMultiServiceFactory) + UnoRuntime.queryInterface(XMultiServiceFactory.class, xChart); + + diagObject = xDocMSF.createInstance("com.sun.star.chart.PieDiagram"); + xDiagram = (XDiagram) + UnoRuntime.queryInterface(XDiagram.class, diagObject); + xChart.setDiagram(xDiagram); + + propset = (XPropertySet) + UnoRuntime.queryInterface( XPropertySet.class, xChart.getTitle() ); + propset.setPropertyValue("String", "JVM Memory Usage"); +} + +runtime = Runtime.getRuntime(); +generator = new Random(); +date = new Date(); + +// allocate a random number of bytes so that the data changes +len = (int)(generator.nextFloat() * runtime.freeMemory() / 5); +bytes = new byte[len]; + +sheet = createSpreadsheet(); +addData(sheet, date.toString(), runtime.totalMemory(), runtime.freeMemory()); +addChart(sheet); + +return 0; diff --git a/scripting/examples/beanshell/MemoryUsage/parcel-descriptor.xml b/scripting/examples/beanshell/MemoryUsage/parcel-descriptor.xml new file mode 100644 index 000000000..46c7ce422 --- /dev/null +++ b/scripting/examples/beanshell/MemoryUsage/parcel-descriptor.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + + <script language="BeanShell"> + <locale lang="en"> + <displayname value="BeanShell JVM Usage"/> + <description> + Updates a spreadsheet with the current memory usage statistics for the Java Virtual Machine + </description> + </locale> + <functionname value="memusage.bsh"/> + <logicalname value="MemoryUsage.BeanShell"/> + </script> + +</parcel> diff --git a/scripting/examples/beanshell/WordCount/parcel-descriptor.xml b/scripting/examples/beanshell/WordCount/parcel-descriptor.xml new file mode 100644 index 000000000..5887e4991 --- /dev/null +++ b/scripting/examples/beanshell/WordCount/parcel-descriptor.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . +--> +<parcel language="BeanShell" xmlns:parcel="scripting.dtd"> + + <script language="BeanShell"> + <locale lang="en"> + <displayname value="Word Count"/> + <description> + Provides a word count of the selected text in A Writer document. + </description> + </locale> + <functionname value="wordcount.bsh"/> + <logicalname value="WordCount.BeanShell"/> + </script> + +</parcel> diff --git a/scripting/examples/beanshell/WordCount/wordcount.bsh b/scripting/examples/beanshell/WordCount/wordcount.bsh new file mode 100644 index 000000000..b068d8a7d --- /dev/null +++ b/scripting/examples/beanshell/WordCount/wordcount.bsh @@ -0,0 +1,82 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +//Provides a word count of the selected text in a Writer document. +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.view.XSelectionSupplier; +import com.sun.star.container.XIndexAccess; +import com.sun.star.text.XText; +import com.sun.star.text.XTextRange; +import com.sun.star.script.provider.XScriptContext; + +// display the count in a Swing dialog +void doDisplay(numWords) { + wordsLabel = new JLabel("Word count = " + numWords); + closeButton = new JButton("Close"); + frame = new JFrame("Word Count"); + closeButton.addActionListener(new ActionListener() { + actionPerformed(ActionEvent e) { + frame.setVisible(false); + } + }); + frame.getContentPane().setLayout(new BorderLayout()); + frame.getContentPane().add(wordsLabel, BorderLayout.CENTER); + frame.getContentPane().add(closeButton, BorderLayout.SOUTH); + frame.pack(); + frame.setSize(190,90); + frame.setLocation(430,430); + frame.setVisible(true); +} + +int wordcount() { + + result = 0; + + // iterate through each of the selections + count = xIndexAccess.getCount(); + for(i=0;i<count;i++) { + // get the XTextRange of the selection + xTextRange = (XTextRange) + UnoRuntime.queryInterface(XTextRange.class, xIndexAccess.getByIndex(i)); + //System.out.println("string: "+xTextRange.getString()); + // use the standard J2SE delimiters to tokenize the string + // obtained from the XTextRange + strTok = new StringTokenizer(xTextRange.getString()); + result += strTok.countTokens(); + } + + doDisplay(result); + return result; +} + +// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to +// all BeanShell scripts executed by the Script Framework +xModel = (XModel) + UnoRuntime.queryInterface(XModel.class, XSCRIPTCONTEXT.getDocument()); +//the writer controller impl supports the css.view.XSelectionSupplier interface +xSelectionSupplier = (XSelectionSupplier) + UnoRuntime.queryInterface(XSelectionSupplier.class, xModel.getCurrentController()); +//see section 7.5.1 of developers' guide +// the getSelection provides an XIndexAccess to the one or more selections +xIndexAccess = (XIndexAccess) + UnoRuntime.queryInterface(XIndexAccess.class, xSelectionSupplier.getSelection()); + +count = wordcount(); +System.out.println("count = "+count); +return 0; diff --git a/scripting/examples/beanshell/Writer/ChangeFont.bsh b/scripting/examples/beanshell/Writer/ChangeFont.bsh new file mode 100644 index 000000000..7cbea8fc3 --- /dev/null +++ b/scripting/examples/beanshell/Writer/ChangeFont.bsh @@ -0,0 +1,36 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XEnumerationAccess; + +import com.sun.star.text.XTextDocument; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +XEnumerationAccess xEnumAcc = (XEnumerationAccess)(UnoRuntime.queryInterface(XEnumerationAccess.class, xText)); +XEnumeration xEnum = xEnumAcc.createEnumeration(); +while (xEnum.hasMoreElements()) { + Object xObj = xEnum.nextElement(); + XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xObj); + if (xServiceInfo.supportsService("com.sun.star.text.Paragraph")) { + XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xServiceInfo ); + xSet.setPropertyValue( "CharHeight", 28 ); + xSet.setPropertyValue( "CharFontName", "Liberation Sans" ); + } +} + +return 0; diff --git a/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh b/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh new file mode 100644 index 000000000..db5e5b66a --- /dev/null +++ b/scripting/examples/beanshell/Writer/ChangeParaAdjust.bsh @@ -0,0 +1,37 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.beans.XPropertySet; +import com.sun.star.lang.XServiceInfo; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.container.XEnumeration; +import com.sun.star.container.XEnumerationAccess; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +XEnumerationAccess xEnumAcc = (XEnumerationAccess)(UnoRuntime.queryInterface(XEnumerationAccess.class, xText)); +XEnumeration xEnum = xEnumAcc.createEnumeration(); +while (xEnum.hasMoreElements()) { + Object xObj = xEnum.nextElement(); + XServiceInfo xServiceInfo = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xObj); + if (xServiceInfo.supportsService("com.sun.star.text.Paragraph")) { + XPropertySet xSet = UnoRuntime.queryInterface(XPropertySet.class, xServiceInfo ); + // Set the justification to be center justified + xSet.setPropertyValue( "ParaAdjust", com.sun.star.style.ParagraphAdjust.CENTER ); + } +} + +return 0; diff --git a/scripting/examples/beanshell/Writer/InsertTable.bsh b/scripting/examples/beanshell/Writer/InsertTable.bsh new file mode 100644 index 000000000..099c45e68 --- /dev/null +++ b/scripting/examples/beanshell/Writer/InsertTable.bsh @@ -0,0 +1,32 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; +import com.sun.star.lang.XMultiServiceFactory; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; +import com.sun.star.text.XTextContent; +import com.sun.star.text.XTextTable; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +XMultiServiceFactory xDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); +Object oTab = xDocMSF.createInstance("com.sun.star.text.TextTable"); +XTextTable xTextTable = (XTextTable)UnoRuntime.queryInterface(XTextTable.class, oTab); +xTextTable.initialize(4,3); // four rows, three columns +xTextContent = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, xTextTable); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = (XText) UnoRuntime.queryInterface(XText.class, xTextDoc.getText()); +xText.insertTextContent(xText.getEnd(), xTextContent, false); + +return 0; diff --git a/scripting/examples/beanshell/Writer/InsertText.bsh b/scripting/examples/beanshell/Writer/InsertText.bsh new file mode 100644 index 000000000..a1cfd3566 --- /dev/null +++ b/scripting/examples/beanshell/Writer/InsertText.bsh @@ -0,0 +1,28 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; + +import com.sun.star.text.XTextDocument; +import com.sun.star.text.XText; +import com.sun.star.text.XTextRange; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +String sText = "This text is inserted before the existing text\n" + + "Here comes a second line\n"; + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xText = xTextDoc.getText(); +xTextRange = xText.getEnd(); +xTextRange.setString(sText); + +return 0; diff --git a/scripting/examples/beanshell/Writer/SetText.bsh b/scripting/examples/beanshell/Writer/SetText.bsh new file mode 100644 index 000000000..99f267c63 --- /dev/null +++ b/scripting/examples/beanshell/Writer/SetText.bsh @@ -0,0 +1,21 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.frame.XModel; + +import com.sun.star.text.XTextDocument; + +oDoc = UnoRuntime.queryInterface(XModel.class,XSCRIPTCONTEXT.getInvocationContext()); +if ( oDoc == null ) + oDoc = XSCRIPTCONTEXT.getDocument(); + +xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,oDoc); +xTextDoc.getText().setString("Hello from Beanshell!"); + +return 0; diff --git a/scripting/examples/beanshell/Writer/parcel-descriptor.xml b/scripting/examples/beanshell/Writer/parcel-descriptor.xml new file mode 100644 index 000000000..e0f034b3d --- /dev/null +++ b/scripting/examples/beanshell/Writer/parcel-descriptor.xml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?><parcel xmlns:parcel="scripting.dtd" language="BeanShell"> +<script language="BeanShell"><locale lang="en"><displayname value="InsertText.bsh"/><description>InsertText.bsh</description></locale><logicalname value="InsertText.bsh"/><functionname value="InsertText.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="SetText.bsh"/><description>SetText.bsh</description></locale><logicalname value="SetText.bsh"/><functionname value="SetText.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="InsertTable.bsh"/><description>InsertTable.bsh</description></locale><logicalname value="InsertTable.bsh"/><functionname value="InsertTable.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ChangeParaAdjust.bsh"/><description>ChangeParaAdjust.bsh</description></locale><logicalname value="ChangeParaAdjust.bsh"/><functionname value="ChangeParaAdjust.bsh"/></script><script language="BeanShell"><locale lang="en"><displayname value="ChangeFont.bsh"/><description>ChangeFont.bsh</description></locale><logicalname value="ChangeFont.bsh"/><functionname value="ChangeFont.bsh"/></script></parcel>
\ No newline at end of file |