diff options
Diffstat (limited to '')
-rw-r--r-- | odk/examples/java/Text/BookmarkInsertion.java | 275 | ||||
-rw-r--r-- | odk/examples/java/Text/GraphicsInserter.java | 195 | ||||
-rw-r--r-- | odk/examples/java/Text/HardFormatting.java | 279 | ||||
-rw-r--r-- | odk/examples/java/Text/Makefile | 135 | ||||
-rw-r--r-- | odk/examples/java/Text/SWriter.java | 390 | ||||
-rw-r--r-- | odk/examples/java/Text/StyleCreation.java | 224 | ||||
-rw-r--r-- | odk/examples/java/Text/StyleInitialization.java | 311 | ||||
-rw-r--r-- | odk/examples/java/Text/TextDocumentStructure.java | 201 | ||||
-rw-r--r-- | odk/examples/java/Text/TextReplace.java | 224 | ||||
-rw-r--r-- | odk/examples/java/Text/WriterSelector.java | 161 | ||||
-rw-r--r-- | odk/examples/java/Text/oo_smiley.gif | bin | 0 -> 46109 bytes |
11 files changed, 2395 insertions, 0 deletions
diff --git a/odk/examples/java/Text/BookmarkInsertion.java b/odk/examples/java/Text/BookmarkInsertion.java new file mode 100644 index 000000000..0b9dd3fa9 --- /dev/null +++ b/odk/examples/java/Text/BookmarkInsertion.java @@ -0,0 +1,275 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: get the Desktop object from the office +// Step 2: open an empty text document +// Step 3: enter an example text +// Step 4: insert some bookmarks + +// Chapter 5.1.1.4 Inserting bookmarks + + +import com.sun.star.uno.UnoRuntime; + +public class BookmarkInsertion { + + public static void main(String args[]) { + // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote service manager and the desktop object. + com.sun.star.frame.XDesktop xDesktop = null; + xDesktop = getDesktop(); + + // create text document + com.sun.star.text.XTextDocument xTextDocument = null; + xTextDocument = createTextdocument(xDesktop); + + // put example text in document + createExampleData(xTextDocument); + + + String mOffending[] = { "negro(e|es)?","bor(ed|ing)?", + "bloody?", "bleed(ing)?" }; + String mBad[] = { "possib(le|ilit(y|ies))", "real(ly)+", "brilliant" }; + + String sOffendPrefix = "Offending"; + String sBadPrefix = "BadStyle"; + + markList(xTextDocument, mOffending, sOffendPrefix); + markList(xTextDocument, mBad, sBadPrefix); + + System.out.println("Done"); + + System.exit(0); + } + + public static void markList(com.sun.star.text.XTextDocument xTextDocument, + String mList[], String sPrefix) { + int iCounter=0; + com.sun.star.uno.XInterface xSearchInterface = null; + com.sun.star.text.XTextRange xSearchTextRange = null; + + try { + for( iCounter = 0; iCounter < mList.length; iCounter++ ) { + // the findfirst returns a XInterface + xSearchInterface = FindFirst( + xTextDocument, mList[ iCounter ] ); + + if( xSearchInterface != null ) { + // get the TextRange from the XInterface + xSearchTextRange = UnoRuntime.queryInterface( + com.sun.star.text.XTextRange.class, xSearchInterface); + + InsertBookmark(xTextDocument, xSearchTextRange, + sPrefix + iCounter); + } + } + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + } + + + public static void InsertBookmark(com.sun.star.text.XTextDocument xTextDocument, + com.sun.star.text.XTextRange xTextRange, + String sBookName) { + // create a bookmark on a TextRange + try { + // get the MultiServiceFactory from the text document + com.sun.star.lang.XMultiServiceFactory xDocMSF; + xDocMSF = UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xTextDocument); + + // the bookmark service is a context dependent service, you need + // the MultiServiceFactory from the document + Object xObject = xDocMSF.createInstance("com.sun.star.text.Bookmark"); + + // set the name from the bookmark + com.sun.star.container.XNamed xNameAccess = null; + xNameAccess = UnoRuntime.queryInterface( + com.sun.star.container.XNamed.class, xObject); + + xNameAccess.setName(sBookName); + + // create a XTextContent, for the method 'insertTextContent' + com.sun.star.text.XTextContent xTextContent = null; + xTextContent = UnoRuntime.queryInterface( + com.sun.star.text.XTextContent.class, xNameAccess); + + // insertTextContent need a TextRange not a cursor to specify the + // position from the bookmark + xTextDocument.getText().insertTextContent(xTextRange, xTextContent, true); + + System.out.println("Insert bookmark: " + sBookName); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + } + + protected static com.sun.star.uno.XInterface FindFirst( + com.sun.star.text.XTextDocument xTextDocument, String sSearchString) + { + com.sun.star.util.XSearchDescriptor xSearchDescriptor = null; + com.sun.star.util.XSearchable xSearchable = null; + com.sun.star.uno.XInterface xSearchInterface = null; + + try { + xSearchable = UnoRuntime.queryInterface( + com.sun.star.util.XSearchable.class, xTextDocument); + xSearchDescriptor = xSearchable.createSearchDescriptor(); + + xSearchDescriptor.setSearchString(sSearchString); + + com.sun.star.beans.XPropertySet xPropertySet = null; + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xSearchDescriptor); + + xPropertySet.setPropertyValue("SearchRegularExpression", + Boolean.TRUE ); + + xSearchInterface = (com.sun.star.uno.XInterface) + xSearchable.findFirst(xSearchDescriptor); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xSearchInterface; + } + + protected static void createExampleData( + com.sun.star.text.XTextDocument xTextDocument ) + { + com.sun.star.text.XTextCursor xTextCursor = null; + + try { + xTextCursor = xTextDocument.getText().createTextCursor(); + + xTextCursor.setString( "He heard quiet steps behind him. That didn't bode well. Who could be following him this late at night and in this deadbeat part of town? And at this particular moment, just after he pulled off the big time and was making off with the greenbacks. Was there another crook who'd had the same idea, and was now watching him and waiting for a chance to grab the fruit of his labor?" ); + xTextCursor.collapseToEnd(); + xTextCursor.setString( "Or did the steps behind him mean that one of many bloody officers in town was on to him and just waiting to pounce and snap those cuffs on his wrists? He nervously looked all around. Suddenly he saw the alley. Like lightning he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the middle of the sidewalk. He tried to nervously tap his way along in the inky darkness and suddenly stiffened: it was a dead-end, he would have to go back the way he had come" ); + xTextCursor.collapseToEnd(); + xTextCursor.setString( "The steps got louder and louder, he saw the black outline of a figure coming around the corner. Is this the end of the line? he thought pressing himself back against the wall trying to make himself invisible in the dark, was all that planning and energy wasted? He was dripping with sweat now, cold and wet, he could smell the brilliant fear coming off his clothes. Suddenly next to him, with a barely noticeable squeak, a door swung quietly to and fro in the night's breeze." ); + + xTextCursor.gotoStart(false); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + } + + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; + + try { + com.sun.star.uno.XComponentContext xContext = null; + + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + xDesktop = UnoRuntime.queryInterface( + com.sun.star.frame.XDesktop.class, oDesktop); + } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + return xDesktop; + } + + public static com.sun.star.text.XTextDocument createTextdocument( + com.sun.star.frame.XDesktop xDesktop ) + { + com.sun.star.text.XTextDocument aTextDocument = null; + + try { + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return aTextDocument; + } + + + protected static com.sun.star.lang.XComponent CreateNewDocument( + com.sun.star.frame.XDesktop xDesktop, + String sDocumentType ) + { + String sURL = "private:factory/" + sDocumentType; + + com.sun.star.lang.XComponent xComponent = null; + com.sun.star.frame.XComponentLoader xComponentLoader = null; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; + + try { + xComponentLoader = UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xComponent ; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/GraphicsInserter.java b/odk/examples/java/Text/GraphicsInserter.java new file mode 100644 index 000000000..b4b5b49ea --- /dev/null +++ b/odk/examples/java/Text/GraphicsInserter.java @@ -0,0 +1,195 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +import com.sun.star.uno.UnoRuntime; + +import java.io.PrintWriter; +import java.io.BufferedWriter; +import java.io.FileWriter; + + +public class GraphicsInserter { + public static void main(String args[]) { + if ( args.length < 1 ) + { + System.out.println( + "usage: java -jar GraphicsInserter.jar \"<Graphic URL|path>\"" ); + System.out.println( "\ne.g.:" ); + System.out.println( + "java -jar GraphicsInserter.jar \"file:///f:/TestGraphics.gif\"" ); + System.exit( 1 ); + } + + com.sun.star.uno.XComponentContext xContext = null; + + try { + + // bootstrap UNO and get the remote component context. The context can + // be used to get the service manager + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + System.out.println("Connected to a running office ..."); + + // get the remote office service manager + com.sun.star.lang.XMultiComponentFactory xMCF = + xContext.getServiceManager(); + + /* A desktop environment contains tasks with one or more + frames in which components can be loaded. Desktop is the + environment for components which can instantiate within + frames. */ + com.sun.star.frame.XDesktop xDesktop = UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, + xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", + xContext ) ); + + com.sun.star.frame.XComponentLoader xCompLoader = + UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + // Load a Writer document, which will be automatically displayed + com.sun.star.lang.XComponent xComp = xCompLoader.loadComponentFromURL( + "private:factory/swriter", "_blank", 0, + new com.sun.star.beans.PropertyValue[0]); + + // Querying for the interface XTextDocument on the xcomponent + com.sun.star.text.XTextDocument xTextDoc = + UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComp); + + // Querying for the interface XMultiServiceFactory on the xtextdocument + com.sun.star.lang.XMultiServiceFactory xMSFDoc = + UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xTextDoc); + + // Providing a log file for output + PrintWriter printwriterLog = new PrintWriter( + new BufferedWriter( new FileWriter("log.txt") ) ); + + Object oGraphic = null; + try { + // Creating the service GraphicObject + oGraphic =xMSFDoc + .createInstance("com.sun.star.text.TextGraphicObject"); + } + catch ( Exception exception ) { + System.out.println( "Could not create instance" ); + exception.printStackTrace( printwriterLog ); + } + + // Getting the text + com.sun.star.text.XText xText = xTextDoc.getText(); + + // Getting the cursor on the document + com.sun.star.text.XTextCursor xTextCursor = xText.createTextCursor(); + + // Querying for the interface XTextContent on the GraphicObject + com.sun.star.text.XTextContent xTextContent = + UnoRuntime.queryInterface( + com.sun.star.text.XTextContent.class, oGraphic ); + + // Printing information to the log file + printwriterLog.println( "inserting graphic" ); + try { + // Inserting the content + xText.insertTextContent(xTextCursor, xTextContent, true); + } catch ( Exception exception ) { + System.out.println( "Could not insert Content" ); + exception.printStackTrace(System.err); + } + + // Printing information to the log file + printwriterLog.println( "adding graphic" ); + + // Querying for the interface XPropertySet on GraphicObject + com.sun.star.beans.XPropertySet xPropSet = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, oGraphic); + try { + // Creating a string for the graphic url + java.io.File sourceFile = new java.io.File(args[0]); + StringBuffer sUrl = new StringBuffer("file:///"); + sUrl.append(sourceFile.getCanonicalPath().replace('\\', '/')); + System.out.println( "insert graphic \"" + sUrl + "\""); + + com.sun.star.graphic.XGraphicProvider xGraphicProvider = + UnoRuntime.queryInterface(com.sun.star.graphic.XGraphicProvider.class, + xMCF.createInstanceWithContext("com.sun.star.graphic.GraphicProvider", + xContext)); + + + com.sun.star.beans.PropertyValue[] aMediaProps = new com.sun.star.beans.PropertyValue[] { new com.sun.star.beans.PropertyValue() }; + aMediaProps[0].Name = "URL"; + aMediaProps[0].Value = sUrl; + + com.sun.star.graphic.XGraphic xGraphic = + UnoRuntime.queryInterface(com.sun.star.graphic.XGraphic.class, + xGraphicProvider.queryGraphic(aMediaProps)); + + // Setting the anchor type + xPropSet.setPropertyValue("AnchorType", + com.sun.star.text.TextContentAnchorType.AT_PARAGRAPH ); + + // Setting the graphic url + xPropSet.setPropertyValue( "Graphic", xGraphic ); + + // Setting the horizontal position + xPropSet.setPropertyValue( "HoriOrientPosition", + Integer.valueOf( 5500 ) ); + + // Setting the vertical position + xPropSet.setPropertyValue( "VertOrientPosition", + Integer.valueOf( 4200 ) ); + + // Setting the width + xPropSet.setPropertyValue( "Width", Integer.valueOf( 4400 ) ); + + // Setting the height + xPropSet.setPropertyValue( "Height", Integer.valueOf( 4000 ) ); + } catch ( Exception exception ) { + System.out.println( "Couldn't set property 'GraphicURL'" ); + exception.printStackTrace( printwriterLog ); + } + + xContext = null; + + System.exit(0); + } + catch( Exception e ) { + e.printStackTrace(System.err); + System.exit(1); + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/HardFormatting.java b/odk/examples/java/Text/HardFormatting.java new file mode 100644 index 000000000..81a294c86 --- /dev/null +++ b/odk/examples/java/Text/HardFormatting.java @@ -0,0 +1,279 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: get the Desktop object from the office +// Step 2: open an empty text document +// Step 3: enter an example text +// Step 4: get some text attributes +// Step 5: check the PropertyState from the selection + +// Chapter 4.1.4 Hard formatting + + +import com.sun.star.uno.UnoRuntime; + +public class HardFormatting { + + public static void main(String args[]) { + // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote service manager and the desktop object. + com.sun.star.frame.XDesktop xDesktop = null; + xDesktop = getDesktop(); + + try { + // create text document + com.sun.star.text.XTextDocument xTextDocument = null; + xTextDocument = createTextdocument(xDesktop); + + // the text interface contains all methods and properties to + // manipulate the content from a text document + com.sun.star.text.XText xText = null; + xText = xTextDocument.getText(); + + String sMyText = "A very short paragraph for illustration only"; + + // you can travel with the cursor through the text document. + // you travel only at the model, not at the view. The cursor that you can + // see on the document doesn't change the position + com.sun.star.text.XTextCursor xTextCursor = null; + xTextCursor = xTextDocument.getText().createTextCursor(); + + xText.insertString( xTextCursor, "Headline", false ); + xText.insertControlCharacter(xTextCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); + + xText.insertString(xTextCursor, sMyText, false); + + com.sun.star.text.XTextRange xTextRange = null; + com.sun.star.beans.XPropertySet xPropertySet = null; + + // BEGIN: 'Hard formatting' + // the text range not the cursor contains the 'parastyle' property + xTextRange = xText.getEnd(); + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextRange); + + // create a paragraph cursor to travel through the paragraphs + com.sun.star.text.XParagraphCursor xParagraphCursor = null; + xParagraphCursor = UnoRuntime.queryInterface( + com.sun.star.text.XParagraphCursor.class, xTextRange); + + xParagraphCursor.gotoStart( false ); + xParagraphCursor.gotoEndOfParagraph( true ); + xTextRange = xParagraphCursor.getText().getStart(); + + // create a WordCursor to travel into the paragraph + com.sun.star.text.XWordCursor xWordCursor = null; + xWordCursor = UnoRuntime.queryInterface( + com.sun.star.text.XWordCursor.class, xTextRange); + + // the PropertySet from the cursor contains the text attributes + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xWordCursor); + System.out.println( + "Parastyle : " + +xPropertySet.getPropertyValue("ParaStyleName").toString() + + "\nFontname : " + + xPropertySet.getPropertyValue("CharFontName").toString() + + "\nWeight : " + + xPropertySet.getPropertyValue("CharWeight").toString() ); + + xWordCursor.gotoNextWord(false); + xWordCursor.gotoNextWord(false); + xWordCursor.gotoEndOfWord(true); + + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xWordCursor); + xPropertySet.setPropertyValue("CharWeight", + new Float(com.sun.star.awt.FontWeight.BOLD)); + xPropertySet.setPropertyValue("CharColor", Integer.valueOf( 255 ) ); + + System.out.println( + "Parastyle : " + + xPropertySet.getPropertyValue("ParaStyleName").toString() + + "\nFontname : " + + xPropertySet.getPropertyValue("CharFontName").toString() + + "\nWeight : " + + xPropertySet.getPropertyValue("CharWeight").toString() ); + + // the PropertyState contains information where the attribute is set, + // is a text part hard formatted or not. + com.sun.star.beans.XPropertyState xPropertyState = null; + xPropertyState = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertyState.class, xWordCursor); + + com.sun.star.beans.PropertyState xPropertyStateValue = + xPropertyState.getPropertyState("CharWeight"); + + checkPropertyState( xWordCursor, xPropertyStateValue ); + + xWordCursor.goRight( (short) 3 , true ); + xPropertyStateValue = xPropertyState.getPropertyState("CharWeight"); + + System.out.println("Increase the selection with three characters"); + checkPropertyState(xWordCursor, xPropertyStateValue); + + xPropertyState.setPropertyToDefault("CharWeight"); + + System.out.println("Set the default value on the selection"); + xPropertyStateValue = xPropertyState.getPropertyState("CharWeight"); + checkPropertyState(xWordCursor, xPropertyStateValue); + + // END: 'Hard formatting' Section from the Cookbook + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + System.out.println("Done"); + + System.exit(0); + + } + + + public static void checkPropertyState( + com.sun.star.text.XWordCursor xWordCursor, + com.sun.star.beans.PropertyState xPropertyStateValue ) + { + switch( xPropertyStateValue.getValue() ) { + case com.sun.star.beans.PropertyState.DIRECT_VALUE_value: { + System.out.println( "-> The selection '" + + xWordCursor.getString() + + "' completely hard formatted" ); + break; + } + + case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: { + System.out.println( "-> The selection '" + + xWordCursor.getString() + + "' isn't hard formatted" ); + break; + } + + case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: { + System.out.println( "-> The selection '" + + xWordCursor.getString() + + "' isn't completely hard formatted" ); + break; + } + + default: + System.out.println( "No PropertyState found" ); + } + } + + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; + + try { + com.sun.star.uno.XComponentContext xContext = null; + + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + xDesktop = UnoRuntime.queryInterface( + com.sun.star.frame.XDesktop.class, oDesktop); + } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + return xDesktop; + } + + public static com.sun.star.text.XTextDocument createTextdocument( + com.sun.star.frame.XDesktop xDesktop ) + { + com.sun.star.text.XTextDocument aTextDocument = null; + + try { + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return aTextDocument; + } + + + protected static com.sun.star.lang.XComponent CreateNewDocument( + com.sun.star.frame.XDesktop xDesktop, + String sDocumentType ) + { + String sURL = "private:factory/" + sDocumentType; + + com.sun.star.lang.XComponent xComponent = null; + com.sun.star.frame.XComponentLoader xComponentLoader = null; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; + + try { + xComponentLoader = UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xComponent ; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/Makefile b/odk/examples/java/Text/Makefile new file mode 100644 index 000000000..cdfdcb29f --- /dev/null +++ b/odk/examples/java/Text/Makefile @@ -0,0 +1,135 @@ +#************************************************************************* +# +# The Contents of this file are made available subject to the terms of +# the BSD license. +# +# Copyright 2000, 2010 Oracle and/or its affiliates. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# 3. Neither the name of Sun Microsystems, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE +# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +#************************************************************************** + +# Builds the Java Text examples of the SDK. + +PRJ=../../.. +SETTINGS=$(PRJ)/settings + +include $(SETTINGS)/settings.mk +include $(SETTINGS)/std.mk + +# Define non-platform/compiler specific settings +EXAMPLE_NAME=JavaTextExamples +OUT_APP_CLASS = $(OUT_CLASS)/$(EXAMPLE_NAME) + +APP1_NAME=BookmarkInsertion +APP1_JAR=$(OUT_APP_CLASS)/$(APP1_NAME).jar +APP2_NAME=HardFormatting +APP2_JAR=$(OUT_APP_CLASS)/$(APP2_NAME).jar +APP3_NAME=StyleCreation +APP3_JAR=$(OUT_APP_CLASS)/$(APP3_NAME).jar +APP4_NAME=StyleInitialization +APP4_JAR=$(OUT_APP_CLASS)/$(APP4_NAME).jar +APP5_NAME=SWriter +APP5_JAR=$(OUT_APP_CLASS)/$(APP5_NAME).jar +APP6_NAME=TextDocumentStructure +APP6_JAR=$(OUT_APP_CLASS)/$(APP6_NAME).jar +APP7_NAME=TextReplace +APP7_JAR=$(OUT_APP_CLASS)/$(APP7_NAME).jar +APP8_NAME=WriterSelector +APP8_JAR=$(OUT_APP_CLASS)/$(APP8_NAME).jar +APP9_NAME=GraphicsInserter +APP9_JAR=$(OUT_APP_CLASS)/$(APP9_NAME).jar + +SDK_CLASSPATH = $(subst $(EMPTYSTRING) $(PATH_SEPARATOR),$(PATH_SEPARATOR),$(CLASSPATH)\ + $(PATH_SEPARATOR)$(OUT_APP_CLASS)) + +# Targets +.PHONY: ALL +ALL : \ + $(EXAMPLE_NAME) + +include $(SETTINGS)/stdtarget.mk + +$(OUT_APP_CLASS)/%.class : %.java + -$(MKDIR) $(subst /,$(PS),$(@D)) + $(SDK_JAVAC) $(JAVAC_FLAGS) -classpath "$(SDK_CLASSPATH)" -d $(OUT_APP_CLASS) $< + +$(OUT_APP_CLASS)/%.mf : + -$(MKDIR) $(subst /,$(PS),$(@D)) + @echo Main-Class: com.sun.star.lib.loader.Loader> $@ + $(ECHOLINE)>> $@ + @echo Name: com/sun/star/lib/loader/Loader.class>> $@ + @echo Application-Class: $*>> $@ + +$(OUT_APP_CLASS)/%.jar : $(OUT_APP_CLASS)/%.mf $(OUT_APP_CLASS)/%.class + -$(MKDIR) $(subst /,$(PS),$(@D)) + +cd $(subst /,$(PS),$(OUT_APP_CLASS)) && $(SDK_JAR) cvfm $(@F) $*.mf $*.class + +$(SDK_JAR) uvf $@ $(SDK_JAVA_UNO_BOOTSTRAP_FILES) + + +$(APP1_JAR) : $(OUT_APP_CLASS)/$(APP1_NAME).mf $(OUT_APP_CLASS)/$(APP1_NAME).class +$(APP2_JAR) : $(OUT_APP_CLASS)/$(APP2_NAME).mf $(OUT_APP_CLASS)/$(APP2_NAME).class +$(APP3_JAR) : $(OUT_APP_CLASS)/$(APP3_NAME).mf $(OUT_APP_CLASS)/$(APP3_NAME).class +$(APP4_JAR) : $(OUT_APP_CLASS)/$(APP4_NAME).mf $(OUT_APP_CLASS)/$(APP4_NAME).class +$(APP5_JAR) : $(OUT_APP_CLASS)/$(APP5_NAME).mf $(OUT_APP_CLASS)/$(APP5_NAME).class +$(APP6_JAR) : $(OUT_APP_CLASS)/$(APP6_NAME).mf $(OUT_APP_CLASS)/$(APP6_NAME).class +$(APP7_JAR) : $(OUT_APP_CLASS)/$(APP7_NAME).mf $(OUT_APP_CLASS)/$(APP7_NAME).class +$(APP8_JAR) : $(OUT_APP_CLASS)/$(APP8_NAME).mf $(OUT_APP_CLASS)/$(APP8_NAME).class +$(APP9_JAR) : $(OUT_APP_CLASS)/$(APP9_NAME).mf $(OUT_APP_CLASS)/$(APP9_NAME).class + +$(EXAMPLE_NAME) : $(APP1_JAR) $(APP2_JAR) $(APP3_JAR) $(APP4_JAR) $(APP5_JAR) $(APP6_JAR) $(APP7_JAR) $(APP8_JAR) $(APP9_JAR) + @echo -------------------------------------------------------------------------------- + @echo The GraphicsInserter example loads the graphic "$(QM)./oo_smiley.gif$(QM)" into a + @echo new document. + @echo - + @echo Please use one of the following commands to execute the examples! + @echo ------ + @echo $(MAKE) BookmarkInsertion.run + @echo $(MAKE) HardFormatting.run + @echo $(MAKE) StyleCreation.run + @echo $(MAKE) StyleInitialization.run + @echo $(MAKE) SWriter.run + @echo $(MAKE) TextDocumentStructure.run + @echo $(MAKE) TextReplace.run + @echo $(MAKE) WriterSelector.run + @echo $(MAKE) GraphicsInserter.run + @echo -------- + @echo The GraphicsInserter needs parameters. Please use the following command to + @echo start the demo if you do not want the default parameters specified in the + @echo this makefile. Starting without parameters print a command line help: + @echo --- GraphicsInserter --- + @echo java -jar GraphicsInserter.jar "$(QM)graphic Url|path$(QM)" + @echo -------------------------------------------------------------------------------- + +%.run: $(OUT_APP_CLASS)/%.jar + $(SDK_JAVA) -Dcom.sun.star.lib.loader.unopath="$(OFFICE_PROGRAM_PATH)" -jar $< + +GraphicsInserter.run: $(OUT_APP_CLASS)/GraphicsInserter.jar + $(SDK_JAVA) -Dcom.sun.star.lib.loader.unopath="$(OFFICE_PROGRAM_PATH)" -jar $< oo_smiley.gif + +.PHONY: clean +clean : + -$(DELRECURSIVE) $(subst /,$(PS),$(OUT_APP_CLASS)) diff --git a/odk/examples/java/Text/SWriter.java b/odk/examples/java/Text/SWriter.java new file mode 100644 index 000000000..fc960c1f2 --- /dev/null +++ b/odk/examples/java/Text/SWriter.java @@ -0,0 +1,390 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: bootstrap UNO and get the remote component context +// Step 2: open an empty text document +// Step 3: enter some text +// Step 4: insert a text table +// Step 5: insert colored text +// Step 6: insert a text frame + + +import com.sun.star.uno.UnoRuntime; + +public class SWriter { + + public static void main(String args[]) { + + + //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo + // bootstrap UNO and get the remote component context. The context can + // be used to get the service manager + + com.sun.star.uno.XComponentContext xContext = null; + + try { + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + if( xContext != null ) + System.out.println("Connected to a running office ..."); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo + // open an empty document. In this case it's a writer document. + // For this purpose an instance of com.sun.star.frame.Desktop + // is created. It's interface XDesktop provides the XComponentLoader, + // which is used to open the document via loadComponentFromURL + + + //Open Writer document + + System.out.println("Opening an empty Writer document"); + com.sun.star.text.XTextDocument myDoc = openWriter(xContext); + + + + + //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo + // insert some text. + // For this purpose get the Text-Object of the document and create the + // cursor. Now it is possible to insert a text at the cursor-position + // via insertString + + + + //getting the text object + com.sun.star.text.XText xText = myDoc.getText(); + + //create a cursor object + com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor(); + + //inserting some Text + xText.insertString( xTCursor, "The first line in the newly created text document.\n", false ); + + //inserting a second line + xText.insertString( xTCursor, "Now we're in the second line\n", false ); + + + + + //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooo + // insert a text table. + // For this purpose get MultiServiceFactory of the document, create an + // instance of com.sun.star.text.TextTable and initialize it. Now it can + // be inserted at the cursor position via insertTextContent. + // After that some properties are changed and some data is inserted. + + + //inserting a text table + System.out.println("Inserting a text table"); + + //getting MSF of the document + com.sun.star.lang.XMultiServiceFactory xDocMSF = + UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, myDoc); + + //create instance of a text table + com.sun.star.text.XTextTable xTT = null; + + try { + Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable"); + xTT = UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt); + } catch (Exception e) { + System.err.println("Couldn't create instance "+ e); + e.printStackTrace(System.err); + } + + //initialize the text table with 4 columns an 4 rows + xTT.initialize(4,4); + + com.sun.star.beans.XPropertySet xTTRowPS = null; + + //insert the table + try { + xText.insertTextContent(xTCursor, xTT, false); + // get first Row + com.sun.star.container.XIndexAccess xTTRows = xTT.getRows(); + xTTRowPS = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTTRows.getByIndex(0)); + + } catch (Exception e) { + System.err.println("Couldn't insert the table " + e); + e.printStackTrace(System.err); + } + + + // get the property set of the text table + + com.sun.star.beans.XPropertySet xTTPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT); + + // Change the BackColor + try { + xTTPS.setPropertyValue("BackTransparent", Boolean.FALSE); + xTTPS.setPropertyValue("BackColor",Integer.valueOf(13421823)); + xTTRowPS.setPropertyValue("BackTransparent", Boolean.FALSE); + xTTRowPS.setPropertyValue("BackColor",Integer.valueOf(6710932)); + + } catch (Exception e) { + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); + } + + // write Text in the Table headers + System.out.println("Write text in the table headers"); + + insertIntoCell("A1","FirstColumn", xTT); + insertIntoCell("B1","SecondColumn", xTT) ; + insertIntoCell("C1","ThirdColumn", xTT) ; + insertIntoCell("D1","SUM", xTT) ; + + + //Insert Something in the text table + System.out.println("Insert something in the text table"); + + (xTT.getCellByName("A2")).setValue(22.5); + (xTT.getCellByName("B2")).setValue(5615.3); + (xTT.getCellByName("C2")).setValue(-2315.7); + (xTT.getCellByName("D2")).setFormula("sum <A2:C2>"); + + (xTT.getCellByName("A3")).setValue(21.5); + (xTT.getCellByName("B3")).setValue(615.3); + (xTT.getCellByName("C3")).setValue(-315.7); + (xTT.getCellByName("D3")).setFormula("sum <A3:C3>"); + + (xTT.getCellByName("A4")).setValue(121.5); + (xTT.getCellByName("B4")).setValue(-615.3); + (xTT.getCellByName("C4")).setValue(415.7); + (xTT.getCellByName("D4")).setFormula("sum <A4:C4>"); + + + //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo + // insert a colored text. + // Get the propertySet of the cursor, change the CharColor and add a + // shadow. Then insert the Text via InsertString at the cursor position. + + + // get the property set of the cursor + com.sun.star.beans.XPropertySet xTCPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, + xTCursor); + + // Change the CharColor and add a Shadow + try { + xTCPS.setPropertyValue("CharColor",Integer.valueOf(255)); + xTCPS.setPropertyValue("CharShadowed", Boolean.TRUE); + } catch (Exception e) { + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); + } + + //create a paragraph break + try { + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); + + } catch (Exception e) { + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); + } + + //inserting colored Text + System.out.println("Inserting colored Text"); + + xText.insertString(xTCursor, " This is a colored Text - blue with shadow\n", + false ); + + //create a paragraph break + try { + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); + + } catch (Exception e) { + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); + } + + //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo + // insert a text frame. + // create an instance of com.sun.star.text.TextFrame using the MSF of the + // document. Change some properties an insert it. + // Now get the text-Object of the frame and the corresponding cursor. + // Insert some text via insertString. + + + // Create a TextFrame + com.sun.star.text.XTextFrame xTF = null; + com.sun.star.drawing.XShape xTFS = null; + + try { + Object oInt = xDocMSF.createInstance("com.sun.star.text.TextFrame"); + xTF = UnoRuntime.queryInterface( + com.sun.star.text.XTextFrame.class,oInt); + xTFS = UnoRuntime.queryInterface( + com.sun.star.drawing.XShape.class,oInt); + + com.sun.star.awt.Size aSize = new com.sun.star.awt.Size(); + aSize.Height = 400; + aSize.Width = 15000; + + xTFS.setSize(aSize); + } catch (Exception e) { + System.err.println("Couldn't create instance "+ e); + e.printStackTrace(System.err); + } + + // get the property set of the text frame + com.sun.star.beans.XPropertySet xTFPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF); + + // Change the AnchorType + try { + xTFPS.setPropertyValue("AnchorType", + com.sun.star.text.TextContentAnchorType.AS_CHARACTER); + } catch (Exception e) { + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); + } + + //insert the frame + System.out.println("Insert the text frame"); + + try { + xText.insertTextContent(xTCursor, xTF, false); + } catch (Exception e) { + System.err.println("Couldn't insert the frame " + e); + e.printStackTrace(System.err); + } + + //getting the text object of Frame + com.sun.star.text.XText xTextF = xTF.getText(); + + //create a cursor object + com.sun.star.text.XTextCursor xTCF = xTextF.createTextCursor(); + + //inserting some Text + xTextF.insertString(xTCF, + "The first line in the newly created text frame.", false); + + + xTextF.insertString(xTCF, + "\nWith this second line the height of the frame raises.", false); + + //insert a paragraph break + try { + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); + + } catch (Exception e) { + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); + } + + // Change the CharColor and add a Shadow + try { + xTCPS.setPropertyValue("CharColor",Integer.valueOf(65536)); + xTCPS.setPropertyValue("CharShadowed", Boolean.FALSE); + } catch (Exception e) { + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); + } + + xText.insertString(xTCursor, " That's all for now !!", false ); + + System.out.println("done"); + + System.exit(0); + } + + + public static com.sun.star.text.XTextDocument openWriter( + com.sun.star.uno.XComponentContext xContext) + { + //define variables + com.sun.star.frame.XComponentLoader xCLoader; + com.sun.star.text.XTextDocument xDoc = null; + com.sun.star.lang.XComponent xComp = null; + + try { + // get the remote office service manager + com.sun.star.lang.XMultiComponentFactory xMCF = + xContext.getServiceManager(); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + + xCLoader = UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, + oDesktop); + com.sun.star.beans.PropertyValue [] szEmptyArgs = + new com.sun.star.beans.PropertyValue [0]; + String strDoc = "private:factory/swriter"; + xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs); + xDoc = UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, + xComp); + + } catch(Exception e){ + System.err.println(" Exception " + e); + e.printStackTrace(System.err); + } + return xDoc; + } + + public static void insertIntoCell(String CellName, String theText, + com.sun.star.text.XTextTable xTTbl) { + + com.sun.star.text.XText xTableText = UnoRuntime.queryInterface(com.sun.star.text.XText.class, + xTTbl.getCellByName(CellName)); + + //create a cursor object + com.sun.star.text.XTextCursor xTC = xTableText.createTextCursor(); + + com.sun.star.beans.XPropertySet xTPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTC); + + try { + xTPS.setPropertyValue("CharColor",Integer.valueOf(16777215)); + } catch (Exception e) { + System.err.println(" Exception " + e); + e.printStackTrace(System.err); + } + + //inserting some Text + xTableText.setString( theText ); + + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/StyleCreation.java b/odk/examples/java/Text/StyleCreation.java new file mode 100644 index 000000000..d5151eca7 --- /dev/null +++ b/odk/examples/java/Text/StyleCreation.java @@ -0,0 +1,224 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: get the Desktop object from the office +// Step 2: open an empty text document +// Step 3: create a new Paragraph style +// Step 4: apply the Paragraph style + +// Chapter 4.1.3 Defining Your Own Style + + +import com.sun.star.uno.UnoRuntime; + + +public class StyleCreation { + public static void main(String args[]) { + // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote service manager and the desktop object. + com.sun.star.frame.XDesktop xDesktop = null; + xDesktop = getDesktop(); + + try { + // create text document + com.sun.star.text.XTextDocument xTextDocument = null; + xTextDocument = createTextdocument(xDesktop); + + // the service '..ParagraphStyle' is context dependent, you need + // the multi service factory from the document to use the service + com.sun.star.lang.XMultiServiceFactory xDocMSF = + UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xTextDocument); + + // use the service 'com.sun.star.style.ParagraphStyle' + com.sun.star.uno.XInterface xInterface = (com.sun.star.uno.XInterface) + xDocMSF.createInstance("com.sun.star.style.ParagraphStyle"); + + // create a supplier to get the Style family collection + com.sun.star.style.XStyleFamiliesSupplier xSupplier = + UnoRuntime.queryInterface( + com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument ); + + // get the NameAccess interface from the Style family collection + com.sun.star.container.XNameAccess xNameAccess = + xSupplier.getStyleFamilies(); + + // select the Paragraph styles, you get the Paragraph style collection + com.sun.star.container.XNameContainer xParaStyleCollection = + UnoRuntime.queryInterface( + com.sun.star.container.XNameContainer.class, + xNameAccess.getByName("ParagraphStyles")); + + // create a PropertySet to set the properties for the new Paragraphstyle + com.sun.star.beans.XPropertySet xPropertySet = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xInterface ); + System.out.println( "create a PropertySet to set the properties for the new Paragraphstyle" ); + + // set some properties from the Paragraph style + xPropertySet.setPropertyValue("CharFontName", "Helvetica" ); + System.out.println( "set name of the font to 'Helvetica'" ); + + xPropertySet.setPropertyValue("CharHeight", new Float( 36 ) ); + System.out.println( "Change the height of th font to 36" ); + + xPropertySet.setPropertyValue("CharWeight", + new Float( com.sun.star.awt.FontWeight.BOLD ) ); + System.out.println( "set the font attribute 'Bold'" ); + + xPropertySet.setPropertyValue("CharAutoKerning", Boolean.TRUE ); + System.out.println( "set the paragraph attribute 'AutoKerning'" ); + xPropertySet.setPropertyValue("ParaAdjust", + Integer.valueOf( com.sun.star.style.ParagraphAdjust.CENTER_value ) ); + System.out.println( "set the paragraph adjust to LEFT" ); + + xPropertySet.setPropertyValue("ParaFirstLineIndent", Integer.valueOf( 0 ) ); + System.out.println( "set the first line indent to 0 cm" ); + + xPropertySet.setPropertyValue("BreakType", + com.sun.star.style.BreakType.PAGE_AFTER ); + System.out.println( "set the paragraph attribute Breaktype to PageAfter" ); + + // insert the new Paragraph style in the Paragraph style collection + xParaStyleCollection.insertByName( "myheading", xPropertySet ); + System.out.println( "create new paragraph style, with the values from the Propertyset"); + + // get the Textrange from the document + com.sun.star.text.XTextRange xTextRange = + xTextDocument.getText().getStart(); + + // get the PropertySet from the current paragraph + com.sun.star.beans.XPropertySet xParagraphPropertySet = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextRange ); + // change the value from the property 'ParaStyle' to apply the + // Paragraph style + // To run the sample with StarOffice 5.2 you'll have to change + // 'ParaStyleName' to 'ParaStyle' in the next line + xParagraphPropertySet.setPropertyValue("ParaStyleName", + "myheading" ); + System.out.println( "apply the new paragraph style"); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + System.out.println("done"); + + System.exit(0); + } + + + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; + + try { + com.sun.star.uno.XComponentContext xContext = null; + + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + xDesktop = UnoRuntime.queryInterface( + com.sun.star.frame.XDesktop.class, oDesktop); + } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + return xDesktop; + } + + public static com.sun.star.text.XTextDocument createTextdocument( + com.sun.star.frame.XDesktop xDesktop ) + { + com.sun.star.text.XTextDocument aTextDocument = null; + + try { + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return aTextDocument; + } + + + protected static com.sun.star.lang.XComponent CreateNewDocument( + com.sun.star.frame.XDesktop xDesktop, + String sDocumentType ) + { + String sURL = "private:factory/" + sDocumentType; + + com.sun.star.lang.XComponent xComponent = null; + com.sun.star.frame.XComponentLoader xComponentLoader = null; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; + + try { + xComponentLoader = UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xComponent ; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/StyleInitialization.java b/odk/examples/java/Text/StyleInitialization.java new file mode 100644 index 000000000..63ea29b24 --- /dev/null +++ b/odk/examples/java/Text/StyleInitialization.java @@ -0,0 +1,311 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: get the Desktop object from the office +// Step 2: open an empty text document +// Step 3: enter an example text +// Step 4: use the paragraph collection +// Step 5: apply a different paragraph style on the paragraphs + + +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.AnyConverter; + +public class StyleInitialization { + + public static void main(String args[]) { + // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote service manager and the desktop object. + com.sun.star.frame.XDesktop xDesktop = null; + xDesktop = getDesktop(); + + try { + // BEGIN: 'Style basics' Section from the Tutorial + + // create text document + com.sun.star.text.XTextDocument xTextDocument = null; + xTextDocument = createTextdocument( xDesktop ); + + // the text interface contains all methods and properties to + // manipulate the content from a text document + com.sun.star.text.XText xText = null; + xText = xTextDocument.getText(); + + String sMyText = "A very short paragraph for illustration only"; + + // you can travel with the cursor through the text document. + // you travel only at the model, not at the view. The cursor that you can + // see on the document doesn't change the position + com.sun.star.text.XTextCursor xTextCursor = null; + xTextCursor = xTextDocument.getText().createTextCursor(); + + com.sun.star.beans.XPropertySet oCPS = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextCursor); + try { + oCPS.setPropertyValue("CharFontName","Helvetica"); + } + catch (Exception ex) { + + } + + xText.insertString( xTextCursor, "Headline", false ); + + try { + oCPS.setPropertyValue("CharFontName","Times"); + } + catch (Exception ex) { + + } + xText.insertControlCharacter(xTextCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); + + xText.insertString( xTextCursor, sMyText, false ); + + com.sun.star.text.XTextRange xTextRange = null; + com.sun.star.beans.XPropertySet xPropertySet = null; + + // the text range not the cursor contains the 'parastyle' property + xTextRange = xText.getEnd(); + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextRange ); + + // To run the sample with StarOffice 5.2 you'll have to change + // 'ParaStyleName' to 'ParaStyle' in the next line + System.out.println( "Current Parastyle : " + + xPropertySet.getPropertyValue("ParaStyleName") ); + + // END: 'Style basics' Section from the Tutorial + + // There are two way to travel through the paragraphs, with a + // paragraph cursor, or an enumeration. + // You find both ways in this example + + // The first way, with the paragraph cursor + com.sun.star.text.XParagraphCursor xParagraphCursor = null; + xParagraphCursor = UnoRuntime.queryInterface( + com.sun.star.text.XParagraphCursor.class, xTextRange ); + + xParagraphCursor.gotoStart( false ); + xParagraphCursor.gotoEndOfParagraph( true ); + + // The second way, with the paragraph enumeration + com.sun.star.container.XEnumerationAccess xEnumerationAccess = null; + xEnumerationAccess = UnoRuntime.queryInterface( + com.sun.star.container.XEnumerationAccess.class, xText ); + + // the enumeration contains all paragraph from the document + com.sun.star.container.XEnumeration xParagraphEnumeration = null; + xParagraphEnumeration = xEnumerationAccess.createEnumeration(); + + com.sun.star.text.XTextContent xParagraph = null; + com.sun.star.text.XTextRange xWord = null; + + com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null; + com.sun.star.container.XEnumeration xPortionEnumeration = null; + + // check if a paragraph is available + while ( xParagraphEnumeration.hasMoreElements() ) { + // get the next paragraph + xParagraph = UnoRuntime.queryInterface( + com.sun.star.text.XTextContent.class, + xParagraphEnumeration.nextElement()); + + // you need the method getAnchor to a TextRange -> to manipulate + // the paragraph + String sText = xParagraph.getAnchor().getString(); + + // create a cursor from this paragraph + com.sun.star.text.XTextCursor xParaCursor = null; + xParaCursor = xParagraph.getAnchor().getText().createTextCursor(); + + // goto the start and end of the paragraph + xParaCursor.gotoStart( false ); + xParaCursor.gotoEnd( true ); + + // The enumeration from the paragraphs contain parts from the + // paragraph with a different attributes. + xParaEnumerationAccess = UnoRuntime.queryInterface( + com.sun.star.container.XEnumerationAccess.class, xParagraph); + xPortionEnumeration = xParaEnumerationAccess.createEnumeration(); + + while ( xPortionEnumeration.hasMoreElements() ) { + // output of all parts from the paragraph with different attributes + xWord = UnoRuntime.queryInterface( + com.sun.star.text.XTextRange.class, + xPortionEnumeration.nextElement()); + String sWordString = xWord.getString(); + System.out.println( "Content of the paragraph : " + sWordString ); + } + } + + // BEGIN: 'Finding a suitable style' Section from the Tutorial + + // create a supplier to get the styles-collection + com.sun.star.style.XStyleFamiliesSupplier xSupplier = null; + xSupplier = UnoRuntime.queryInterface( + com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument ); + + // use the name access from the collection + com.sun.star.container.XNameAccess xNameAccess = null; + xNameAccess = xSupplier.getStyleFamilies(); + + com.sun.star.container.XNameContainer xParaStyleCollection = null; + xParaStyleCollection = UnoRuntime.queryInterface( + com.sun.star.container.XNameContainer.class, xNameAccess.getByName( "ParagraphStyles" )); + + // create an array from strings with the name of all paragraph styles from the text document + String[] sElementNames = xParaStyleCollection.getElementNames(); + int iElementCount = sElementNames.length; + + for( int iCounter = 0; iCounter < iElementCount; iCounter++ ) { + // specify one paragraph style + com.sun.star.style.XStyle xStyle = null; + xStyle = UnoRuntime.queryInterface( + com.sun.star.style.XStyle.class, + xParaStyleCollection.getByName( sElementNames[iCounter] )); + + // create a property set of all properties from the style + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xStyle ); + + String sFontname = AnyConverter.toString(xPropertySet.getPropertyValue("CharFontName")); + sFontname = sFontname.toLowerCase(); + + // if the style use the font 'Albany', apply it to the current paragraph + if( sFontname.equals("albany") ) { + // create a property set from the current paragraph, to change the paragraph style + xPropertySet = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextRange ); + + // To run the sample with StarOffice 5.2 you'll have to change 'ParaStyleName' + // to 'ParaStyle' in the next line + xPropertySet.setPropertyValue("ParaStyleName", sElementNames[iCounter] ); + System.out.println( "Apply the paragraph style : " + sElementNames[iCounter] ); + break; + } + } + // END: 'Finding a suitable style' Section from the Tutorial + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + + System.out.println("Done"); + + System.exit(0); + + } + + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; + + try { + com.sun.star.uno.XComponentContext xContext = null; + + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + xDesktop = UnoRuntime.queryInterface( + com.sun.star.frame.XDesktop.class, oDesktop); + } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + return xDesktop; + } + + public static com.sun.star.text.XTextDocument createTextdocument( + com.sun.star.frame.XDesktop xDesktop ) + { + com.sun.star.text.XTextDocument aTextDocument = null; + + try { + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return aTextDocument; + } + + + protected static com.sun.star.lang.XComponent CreateNewDocument( + com.sun.star.frame.XDesktop xDesktop, + String sDocumentType ) + { + String sURL = "private:factory/" + sDocumentType; + + com.sun.star.lang.XComponent xComponent = null; + com.sun.star.frame.XComponentLoader xComponentLoader = null; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; + + try { + xComponentLoader = UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xComponent ; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/TextDocumentStructure.java b/odk/examples/java/Text/TextDocumentStructure.java new file mode 100644 index 000000000..7f284556d --- /dev/null +++ b/odk/examples/java/Text/TextDocumentStructure.java @@ -0,0 +1,201 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: bootstrap UNO and get the remote component context +// Step 2: open an empty text document +// Step 3: create an enumeration of all paragraphs +// Step 4: create an enumeration of all text portions + + +import com.sun.star.uno.UnoRuntime; + +public class TextDocumentStructure { + + public static void main(String args[]) { + com.sun.star.uno.XComponentContext xContext = null; + + try { + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + System.out.println("Connected to a running office ..."); + + // get the remote service manager + com.sun.star.lang.XMultiComponentFactory xMCF = + xContext.getServiceManager(); + + // create a new instance of the desktop + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + + // get the component laoder from the desktop to create a new + // text document + com.sun.star.frame.XComponentLoader xCLoader = + UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class,oDesktop); + com.sun.star.beans.PropertyValue [] szEmptyArgs = + new com.sun.star.beans.PropertyValue [0]; + String strDoc = "private:factory/swriter"; + + System.out.println("create new text document"); + + com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL( + strDoc, "_blank", 0, szEmptyArgs); + + // query the new document for the XTextDocument interface + com.sun.star.text.XTextDocument xTextDocument = + UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComp); + + // create some example data + com.sun.star.text.XText xText = xTextDocument.getText(); + createExampleData( xText ); + + // Begin section 'The structure of text documents' of the Tutorial + + com.sun.star.container.XEnumeration xParagraphEnumeration = null; + com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null; + com.sun.star.container.XEnumeration xTextPortionEnum; + com.sun.star.text.XTextContent xTextElement = null; + + System.out.println("create an enumeration of all paragraphs"); + // create an enumeration access of all paragraphs of a document + com.sun.star.container.XEnumerationAccess xEnumerationAccess = + UnoRuntime.queryInterface( + com.sun.star.container.XEnumerationAccess.class, xText); + xParagraphEnumeration = xEnumerationAccess.createEnumeration(); + + // Loop through all paragraphs of the document + while ( xParagraphEnumeration.hasMoreElements() ) { + xTextElement = UnoRuntime.queryInterface( + com.sun.star.text.XTextContent.class, + xParagraphEnumeration.nextElement()); + com.sun.star.lang.XServiceInfo xServiceInfo = + UnoRuntime.queryInterface( + com.sun.star.lang.XServiceInfo.class, xTextElement); + + // check ifs the current paragraph really a paragraph or an + // anchor of a frame or picture + if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) { + com.sun.star.text.XTextRange xTextRange = + xTextElement.getAnchor(); + System.out.println( "This is a Paragraph" ); + + // create another enumeration to get all text portions of + // the paragraph + xParaEnumerationAccess = + UnoRuntime.queryInterface( + com.sun.star.container.XEnumerationAccess.class, + xTextElement); + xTextPortionEnum = xParaEnumerationAccess.createEnumeration(); + + while ( xTextPortionEnum.hasMoreElements() ) { + com.sun.star.text.XTextRange xTextPortion = + UnoRuntime.queryInterface( + com.sun.star.text.XTextRange.class, + xTextPortionEnum.nextElement()); + System.out.println( "Text from the portion : " + + xTextPortion.getString() ); + + com.sun.star.beans.XPropertySet xPropertySet = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, + xTextPortion); + System.out.println( "Name of the font : " + + xPropertySet.getPropertyValue( "CharFontName" ) ); + + // PropertyState status of each text portion. + com.sun.star.beans.XPropertyState xPropertyState = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertyState.class, + xTextPortion); + + if( xPropertyState.getPropertyState("CharWeight").equals( + com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) ) + System.out.println( "- The text range contains more than one different attributes" ); + + if( xPropertyState.getPropertyState( "CharWeight" ).equals( + com.sun.star.beans.PropertyState.DIRECT_VALUE ) ) + System.out.println( " - The text range contains hard formats" ); + + if( xPropertyState.getPropertyState( "CharWeight" ).equals( + com.sun.star.beans.PropertyState.DEFAULT_VALUE ) ) + System.out.println( " - The text range doesn't contains hard formats" ); + } + } + else + System.out.println( "The text portion isn't a text paragraph" ); + // End section 'The structure of text documents' of the Tutorial + } + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + System.out.println("done"); + System.exit(0); + } + + public static void createExampleData( com.sun.star.text.XText xText ) { + + try { + xText.setString( "This is an example sentence" ); + + com.sun.star.text.XWordCursor xWordCursor = + UnoRuntime.queryInterface( + com.sun.star.text.XWordCursor.class, xText.getStart()); + + xWordCursor.gotoNextWord(false); + xWordCursor.gotoNextWord(false); + xWordCursor.gotoEndOfWord(true); + + com.sun.star.beans.XPropertySet xPropertySet = + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xWordCursor ); + xPropertySet.setPropertyValue("CharWeight", + new Float( com.sun.star.awt.FontWeight.BOLD )); + + System.out.println("create example data"); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/TextReplace.java b/odk/examples/java/Text/TextReplace.java new file mode 100644 index 000000000..588d88ae6 --- /dev/null +++ b/odk/examples/java/Text/TextReplace.java @@ -0,0 +1,224 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + + +// comment: Step 1: get the Desktop object from the office +// Step 2: open an empty text document +// Step 3: enter an example text +// Step 4: replace some english spelled words with US spelled + + + +import com.sun.star.uno.UnoRuntime; + +public class TextReplace { + + public static void main(String args[]) { + // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote service manager and the desktop object. + com.sun.star.frame.XDesktop xDesktop = null; + xDesktop = getDesktop(); + + com.sun.star.text.XTextDocument xTextDocument = + createTextdocument( xDesktop ); + + createExampleData( xTextDocument ); + + String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour", + "metre", "through" }; + String mUSWords[] = { "color", "neighbor", "center", "behavior", + "meter", "thru" }; + + try { + com.sun.star.util.XReplaceDescriptor xReplaceDescr = null; + com.sun.star.util.XReplaceable xReplaceable = null; + + xReplaceable = UnoRuntime.queryInterface( + com.sun.star.util.XReplaceable.class, xTextDocument); + + // You need a descriptor to set properties for Replace + xReplaceDescr = xReplaceable.createReplaceDescriptor(); + + System.out.println("Change all occurrences of ..."); + for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length; + iArrayCounter++ ) + { + System.out.println(mBritishWords[iArrayCounter] + + " -> " + mUSWords[iArrayCounter]); + // Set the properties the replace method need + xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] ); + xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] ); + + // Replace all words + xReplaceable.replaceAll( xReplaceDescr ); + } + + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + System.out.println("Done"); + + System.exit(0); + + } + + protected static void createExampleData( + com.sun.star.text.XTextDocument xTextDocument ) + { + // Create textdocument and insert example text + com.sun.star.text.XTextCursor xTextCursor = null; + + try { + xTextCursor = xTextDocument.getText().createTextCursor(); + com.sun.star.text.XText xText = xTextDocument.getText(); + + xText.insertString( xTextCursor, + "He nervously looked all around. Suddenly he saw his ", false ); + + xText.insertString( xTextCursor, "neighbour ", true ); + com.sun.star.beans.XPropertySet xCPS = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextCursor); + // Set the word blue + xCPS.setPropertyValue( "CharColor", Integer.valueOf( 255 ) ); + // Go to last character + xTextCursor.gotoEnd(false); + xCPS.setPropertyValue( "CharColor", Integer.valueOf( 0 ) ); + + xText.insertString( xTextCursor, "in the alley. Like lightning he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the ", false ); + + xText.insertString( xTextCursor, "centre ", true ); + xCPS = UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextCursor); + // Set the word blue + xCPS.setPropertyValue( "CharColor", Integer.valueOf( 255 ) ); + // Go to last character + xTextCursor.gotoEnd(false); + xCPS.setPropertyValue( "CharColor", Integer.valueOf( 0 ) ); + + xText.insertString( xTextCursor, "of the sidewalk.", false ); + + xText.insertControlCharacter( xTextCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); + xText.insertString( xTextCursor, "He tried to nervously tap his way along in the inky darkness and suddenly stiffened: it was a dead-end, he would have to go back the way he had come.", false ); + + xTextCursor.gotoStart(false); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + } + + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; + + try { + com.sun.star.uno.XComponentContext xContext = null; + + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); + + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); + xDesktop = UnoRuntime.queryInterface( + com.sun.star.frame.XDesktop.class, oDesktop); + } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); + } + + + return xDesktop; + } + + public static com.sun.star.text.XTextDocument createTextdocument( + com.sun.star.frame.XDesktop xDesktop ) + { + com.sun.star.text.XTextDocument aTextDocument = null; + + try { + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return aTextDocument; + } + + + protected static com.sun.star.lang.XComponent CreateNewDocument( + com.sun.star.frame.XDesktop xDesktop, + String sDocumentType ) + { + String sURL = "private:factory/" + sDocumentType; + + com.sun.star.lang.XComponent xComponent = null; + com.sun.star.frame.XComponentLoader xComponentLoader = null; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; + + try { + xComponentLoader = UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); + } + catch( Exception e) { + e.printStackTrace(System.err); + } + + return xComponent ; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/WriterSelector.java b/odk/examples/java/Text/WriterSelector.java new file mode 100644 index 000000000..31d873733 --- /dev/null +++ b/odk/examples/java/Text/WriterSelector.java @@ -0,0 +1,161 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/************************************************************************* + * + * The Contents of this file are made available subject to the terms of + * the BSD license. + * + * Copyright 2000, 2010 Oracle and/or its affiliates. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Sun Microsystems, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + *************************************************************************/ + +import com.sun.star.uno.UnoRuntime; + +/** This class gives you information on the selected objects (text range, text + * frame, or graphics) at an OpenOffice.org Server. The Office must be started in + * advance and you must have selected something (text, graphics, ...) + */ +public class WriterSelector { + /** + * @param args No arguments. + */ + public static void main(String args[]) { + com.sun.star.uno.XComponentContext xContext = null; + + try { + + // bootstrap UNO and get the remote component context. The context can + // be used to get the service manager + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + System.out.println("Connected to a running office ..."); + + // get the remote office service manager + com.sun.star.lang.XMultiComponentFactory xMCF = + xContext.getServiceManager(); + + // get a new instance of the desktop + com.sun.star.frame.XDesktop xDesktop = UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, + xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", + xContext ) ); + + com.sun.star.frame.XComponentLoader xCompLoader = + UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + com.sun.star.lang.XComponent xComponent = + xCompLoader.loadComponentFromURL("private:factory/swriter", + "_blank", 0, new com.sun.star.beans.PropertyValue[0]); + { + com.sun.star.text.XTextDocument xDoc =UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, + xComponent); + xDoc.getText().setString("Please select something in this text and press then \"return\" in the shell where you have started the example.\n"); + + // ensure that the document content is optimal visible + com.sun.star.frame.XModel xModel = + UnoRuntime.queryInterface( + com.sun.star.frame.XModel.class, xDoc); + + com.sun.star.view.XViewSettingsSupplier xViewSettings = + UnoRuntime.queryInterface( + com.sun.star.view.XViewSettingsSupplier.class, xModel.getCurrentController()); + xViewSettings.getViewSettings().setPropertyValue( + "ZoomType", Short.valueOf((short)0)); + } + // test document will be closed later + + System.out.println("\nPlease select something in the test document and press then \"return\" to continues the example ..."); + char c = 'X'; + do{ + c = (char) System.in.read(); + }while ((c != 13) && (c != 10)); + + // Getting the current frame from the OpenOffice.org Server. + com.sun.star.frame.XFrame xframe = xDesktop.getCurrentFrame(); + + // Getting the controller. + com.sun.star.frame.XController xController = xframe.getController(); + + com.sun.star.view.XSelectionSupplier xSelSupplier = + UnoRuntime.queryInterface( + com.sun.star.view.XSelectionSupplier.class, xController ); + + Object oSelection = xSelSupplier.getSelection(); + + com.sun.star.lang.XServiceInfo xServInfo = + UnoRuntime.queryInterface( + com.sun.star.lang.XServiceInfo.class, oSelection ); + + if ( xServInfo.supportsService("com.sun.star.text.TextRanges") ) + { + com.sun.star.container.XIndexAccess xIndexAccess = + UnoRuntime.queryInterface( + com.sun.star.container.XIndexAccess.class, oSelection); + + int count = xIndexAccess.getCount(); + com.sun.star.text.XTextRange xTextRange = null; + for ( int i = 0; i < count; i++ ) { + xTextRange = UnoRuntime.queryInterface( + com.sun.star.text.XTextRange.class, + xIndexAccess.getByIndex(i)); + + System.out.println( "You have selected a text range: \"" + + xTextRange.getString() + "\"." ); + } + } + + if ( xServInfo.supportsService("com.sun.star.text.TextGraphicObject") ) + { + System.out.println( "You have selected a graphics." ); + } + + if ( xServInfo.supportsService("com.sun.star.text.TextTableCursor") ) + { + System.out.println( "You have selected a text table." ); + } + + + // close test document + com.sun.star.util.XCloseable xCloseable = UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class, + xComponent ); + + if (xCloseable != null ) { + xCloseable.close(false); + } else + { + xComponent.dispose(); + } + + System.exit(0); + } + catch( Exception e ) { + e.printStackTrace(System.err); + System.exit(1); + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/odk/examples/java/Text/oo_smiley.gif b/odk/examples/java/Text/oo_smiley.gif Binary files differnew file mode 100644 index 000000000..f9fc08d50 --- /dev/null +++ b/odk/examples/java/Text/oo_smiley.gif |