diff options
author | RĂ¼diger Timm <rt@openoffice.org> | 2005-01-31 16:18:02 +0000 |
---|---|---|
committer | RĂ¼diger Timm <rt@openoffice.org> | 2005-01-31 16:18:02 +0000 |
commit | 5787842ba4c54239e6f49690c073c383ce65e8b3 (patch) | |
tree | 38cb28e320eace41f5a146d08dc2d0f6b7f46963 | |
parent | f2ac5214cd9a41453edc39214eca89ee31a10179 (diff) |
INTEGRATION: CWS sdksample (1.4.40); FILE MERGED
2004/08/05 15:15:49 jsc 1.4.40.2: #i29308# use System.err for error output
2004/06/07 15:54:57 jsc 1.4.40.1: #i29308# use new bootstrap feature
-rw-r--r-- | odk/examples/java/Text/SWriter.java | 437 | ||||
-rw-r--r-- | odk/examples/java/Text/StyleCreation.java | 232 | ||||
-rw-r--r-- | odk/examples/java/Text/StyleInitialization.java | 216 |
3 files changed, 350 insertions, 535 deletions
diff --git a/odk/examples/java/Text/SWriter.java b/odk/examples/java/Text/SWriter.java index 4b1f4bf2268d..ba3d06d99cc0 100644 --- a/odk/examples/java/Text/SWriter.java +++ b/odk/examples/java/Text/SWriter.java @@ -2,9 +2,9 @@ * * $RCSfile: SWriter.java,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: hr $ $Date: 2004-02-02 20:13:42 $ + * last change: $Author: rt $ $Date: 2005-01-31 17:17:30 $ * * The Contents of this file are made available subject to the terms of * the BSD license. @@ -39,7 +39,7 @@ *************************************************************************/ //*************************************************************************** -// comment: Step 1: connect to the office an get the MSF +// 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 @@ -47,446 +47,357 @@ // Step 6: insert a text frame //*************************************************************************** -// base interface -import com.sun.star.uno.XInterface; - -// access the implementations via names -import com.sun.star.comp.servicemanager.ServiceManager; - - -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.connection.XConnector; -import com.sun.star.connection.XConnection; - -import com.sun.star.bridge.XUnoUrlResolver; import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XInterface; -import com.sun.star.uno.XNamingService; -import com.sun.star.uno.XComponentContext; - - -// staroffice interfaces to provide desktop and componentloader -// and components i.e. spreadsheets, writerdocs etc. -import com.sun.star.frame.XDesktop; -import com.sun.star.frame.XComponentLoader; - -// additional classes required -import com.sun.star.sheet.*; -import com.sun.star.container.*; -import com.sun.star.table.*; -import com.sun.star.beans.*; -import com.sun.star.style.*; -import com.sun.star.lang.*; -import com.sun.star.text.*; -import com.sun.star.drawing.*; -import com.sun.star.awt.Size; public class SWriter { - - public static void main(String args[]) { - //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooooo - // connect to the office an get the MultiServiceFactory - // this is necessary to create instances of Services - //*************************************************************************** - String sConnectionString = "uno:socket,host=localhost,port=2083;urp;StarOffice.NamingService"; - - // It is possible to use a different connection string, passed as argument - if ( args.length == 1 ) { - sConnectionString = args[0]; - } - - - XMultiServiceFactory xMSF = null; - XTextDocument myDoc = null; - - - // create connection(s) and get multiservicefactory - System.out.println( "getting MultiServiceFactory" ); + //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 { - xMSF = connect( sConnectionString ); - } catch( Exception Ex ) { - System.out.println( "Couldn't get MSF"+ Ex ); - return; + // 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 2oooooooooooooooooooooooooooooooooooooooooo + //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"); - myDoc = openWriter(xMSF); + com.sun.star.text.XTextDocument myDoc = openWriter(xContext); - //*************************************************************************** + //************************************************************************* - //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo + //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo // insert some text. // For this purpose get the Text-Object of the document an create the // cursor. Now it is possible to insert a text at the cursor-position // via insertString - //*************************************************************************** + //************************************************************************* //getting the text object - XText oText = myDoc.getText(); + com.sun.star.text.XText xText = myDoc.getText(); //create a cursor object - XTextCursor oCursor = oText.createTextCursor(); + com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor(); //inserting some Text - oText.insertString( oCursor, "The first line in the newly created text document.\n", false ); - + xText.insertString( xTCursor, "The first line in the newly created text document.\n", false ); //inserting a second line - oText.insertString( oCursor, "Now we're in the second line\n", false ); + xText.insertString( xTCursor, "Now we're in the second line\n", false ); - //*************************************************************************** + //************************************************************************* - //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooooo + //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"); + System.out.println("Inserting a text table"); //getting MSF of the document - XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( - XMultiServiceFactory.class, myDoc ); + com.sun.star.lang.XMultiServiceFactory xDocMSF = + (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, myDoc); //create instance of a text table - XTextTable TT = null; + com.sun.star.text.XTextTable xTT = null; try { - Object oInt = oDocMSF.createInstance("com.sun.star.text.TextTable"); - TT = (XTextTable) UnoRuntime.queryInterface(XTextTable.class,oInt); + Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable"); + xTT = (com.sun.star.text.XTextTable) + UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt); } catch (Exception e) { - System.out.println("Couldn't create instance "+ e); + System.err.println("Couldn't create instance "+ e); + e.printStackTrace(System.err); } //initialize the text table with 4 columns an 4 rows - TT.initialize(4,4); + xTT.initialize(4,4); - XPropertySet aRow = null; + com.sun.star.beans.XPropertySet xTTRowPS = null; //insert the table try { - oText.insertTextContent(oCursor, TT, false); + xText.insertTextContent(xTCursor, xTT, false); // get first Row - XIndexAccess theRows = TT.getRows(); - aRow = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, theRows.getByIndex(0)); + com.sun.star.container.XIndexAccess xTTRows = xTT.getRows(); + xTTRowPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTTRows.getByIndex(0)); } catch (Exception e) { - System.out.println("Couldn't insert the table " + e); + System.err.println("Couldn't insert the table " + e); + e.printStackTrace(System.err); } // get the property set of the text table - XPropertySet oTPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, TT ); + com.sun.star.beans.XPropertySet xTTPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT); // Change the BackColor try { - - oTPS.setPropertyValue("BackTransparent", new Boolean(false)); - oTPS.setPropertyValue("BackColor",new Integer(13421823)); - aRow.setPropertyValue("BackTransparent", new Boolean(false)); - aRow.setPropertyValue("BackColor",new Integer(6710932)); + xTTPS.setPropertyValue("BackTransparent", new Boolean(false)); + xTTPS.setPropertyValue("BackColor",new Integer(13421823)); + xTTRowPS.setPropertyValue("BackTransparent", new Boolean(false)); + xTTRowPS.setPropertyValue("BackColor",new Integer(6710932)); } catch (Exception e) { - System.out.println("Couldn't change the color " + 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"); + System.out.println("Write text in the table headers"); - insertIntoCell("A1","FirstColumn", TT); - insertIntoCell("B1","SecondColumn", TT) ; - insertIntoCell("C1","ThirdColumn", TT) ; - insertIntoCell("D1","SUM", TT) ; + 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"); - (TT.getCellByName("A2")).setValue(22.5); - (TT.getCellByName("B2")).setValue(5615.3); - (TT.getCellByName("C2")).setValue(-2315.7); - (TT.getCellByName("D2")).setFormula("sum <A2:C2>"); - - (TT.getCellByName("A3")).setValue(21.5); - (TT.getCellByName("B3")).setValue(615.3); - (TT.getCellByName("C3")).setValue(-315.7); - (TT.getCellByName("D3")).setFormula("sum <A3:C3>"); - - (TT.getCellByName("A4")).setValue(121.5); - (TT.getCellByName("B4")).setValue(-615.3); - (TT.getCellByName("C4")).setValue(415.7); - (TT.getCellByName("D4")).setFormula("sum <A4:C4>"); + (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 5oooooooooooooooooooooooooooooooooooooooooo + //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 - - XPropertySet oCPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oCursor ); + com.sun.star.beans.XPropertySet xTCPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, + xTCursor); Object oldValue = null; // Change the CharColor and add a Shadow try { - oCPS.setPropertyValue("CharColor",new Integer(255)); - oCPS.setPropertyValue("CharShadowed", new Boolean(true)); + xTCPS.setPropertyValue("CharColor",new Integer(255)); + xTCPS.setPropertyValue("CharShadowed", new Boolean(true)); } catch (Exception e) { - System.out.println("Couldn't change the color " + e); + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); } //create a paragraph break try { - - oText.insertControlCharacter( oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); } catch (Exception e) { - System.out.println("Couldn't insert break "+ e); + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); } - - //inserting colored Text - System.out.println("inserting colored Text"); + System.out.println("Inserting colored Text"); - oText.insertString( oCursor, " This is a colored Text - blue with shadow\n", false ); + xText.insertString(xTCursor, " This is a colored Text - blue with shadow\n", + false ); //create a paragraph break try { - - oText.insertControlCharacter( oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); } catch (Exception e) { - System.out.println("Couldn't insert break "+ e); + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); } - //*************************************************************************** - - - //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooooo + //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 an the corresponding cursor. // Insert some text via insertString. - //*************************************************************************** - + //************************************************************************* // Create a TextFrame - - XTextFrame TF = null; - XShape TFS = null; + com.sun.star.text.XTextFrame xTF = null; + com.sun.star.drawing.XShape xTFS = null; try { - Object oInt = oDocMSF.createInstance("com.sun.star.text.TextFrame"); - TF = (XTextFrame) UnoRuntime.queryInterface(XTextFrame.class,oInt); - TFS = (XShape) UnoRuntime.queryInterface(XShape.class,oInt); + Object oInt = xDocMSF.createInstance("com.sun.star.text.TextFrame"); + xTF = (com.sun.star.text.XTextFrame) UnoRuntime.queryInterface( + com.sun.star.text.XTextFrame.class,oInt); + xTFS = (com.sun.star.drawing.XShape) UnoRuntime.queryInterface( + com.sun.star.drawing.XShape.class,oInt); - Size aSize = new Size(); + com.sun.star.awt.Size aSize = new com.sun.star.awt.Size(); aSize.Height = 400; aSize.Width = 15000; - TFS.setSize(aSize); - + xTFS.setSize(aSize); } catch (Exception e) { - System.out.println("Couldn't create instance "+ e); + System.err.println("Couldn't create instance "+ e); + e.printStackTrace(System.err); } - // get the property set of the text frame - - XPropertySet oTFPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, TF ); + com.sun.star.beans.XPropertySet xTFPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF); // Change the AnchorType try { - - oTFPS.setPropertyValue("AnchorType", TextContentAnchorType.AS_CHARACTER); - + xTFPS.setPropertyValue("AnchorType", + com.sun.star.text.TextContentAnchorType.AS_CHARACTER); } catch (Exception e) { - System.out.println("Couldn't change the color " + e); + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); } - //insert the frame - System.out.println("insert the text frame"); + System.out.println("Insert the text frame"); try { - oText.insertTextContent(oCursor, TF, false); + xText.insertTextContent(xTCursor, xTF, false); } catch (Exception e) { - System.out.println("Couldn't insert the frame " + e); + System.err.println("Couldn't insert the frame " + e); + e.printStackTrace(System.err); } - //getting the text object of Frame - XText oTex = TF.getText(); + com.sun.star.text.XText xTextF = xTF.getText(); //create a cursor object - XTextCursor oCurso = oTex.createTextCursor(); + com.sun.star.text.XTextCursor xTCF = xTextF.createTextCursor(); //inserting some Text - oTex.insertString( oCurso, "The first line in the newly created text frame.", false ); + xTextF.insertString(xTCF, + "The first line in the newly created text frame.", false); - oTex.insertString( oCurso, "\nWith this second line the height of the frame raises.", false ); + xTextF.insertString(xTCF, + "\nWith this second line the height of the frame raises.", false); //insert a paragraph break try { - - oText.insertControlCharacter( oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); + xText.insertControlCharacter(xTCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); } catch (Exception e) { - System.out.println("Couldn't insert break "+ e); + System.err.println("Couldn't insert break "+ e); + e.printStackTrace(System.err); } - //*************************************************************************** - - - // Change the CharColor and add a Shadow try { - oCPS.setPropertyValue("CharColor",new Integer(65536)); - oCPS.setPropertyValue("CharShadowed", new Boolean(false)); + xTCPS.setPropertyValue("CharColor",new Integer(65536)); + xTCPS.setPropertyValue("CharShadowed", new Boolean(false)); } catch (Exception e) { - System.out.println("Couldn't change the color " + e); + System.err.println("Couldn't change the color " + e); + e.printStackTrace(System.err); } - - oText.insertString( oCursor, " That's all for now !!", false ); - - - //if the document should be disposed remove the slashes in front of the next line - //myDoc.dispose(); - + xText.insertString(xTCursor, " That's all for now !!", false ); System.out.println("done"); System.exit(0); - - - } // finish method main - - - public static XMultiServiceFactory connect( String connectStr ) - throws com.sun.star.uno.Exception, - com.sun.star.uno.RuntimeException, Exception { - // Get component context - XComponentContext xcomponentcontext = - com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( - null ); - - // initial serviceManager - XMultiComponentFactory xLocalServiceManager = - xcomponentcontext.getServiceManager(); - - // create a connector, so that it can contact the office - Object xUrlResolver = xLocalServiceManager.createInstanceWithContext( - "com.sun.star.bridge.UnoUrlResolver", xcomponentcontext ); - XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( - XUnoUrlResolver.class, xUrlResolver ); - - Object rInitialObject = urlResolver.resolve( connectStr ); - - XNamingService rName = (XNamingService)UnoRuntime.queryInterface( - XNamingService.class, rInitialObject ); - - XMultiServiceFactory xMSF = null; - if( rName != null ) { - System.err.println( "got the remote naming service !" ); - Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" ); - - xMSF = (XMultiServiceFactory) - UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr ); - } - - return ( xMSF ); } - public static XTextDocument openWriter(XMultiServiceFactory oMSF) { - - + public static com.sun.star.text.XTextDocument openWriter( + com.sun.star.uno.XComponentContext xContext) + { //define variables - XInterface oInterface; - XDesktop oDesktop; - XComponentLoader oCLoader; - XTextDocument oDoc = null; - XComponent aDoc = null; + 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 = (com.sun.star.frame.XComponentLoader) + 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 = (com.sun.star.text.XTextDocument) + UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, + xComp); + + } catch(Exception e){ + System.err.println(" Exception " + e); + e.printStackTrace(System.err); + } + return xDoc; + } - oInterface = (XInterface) oMSF.createInstance( "com.sun.star.frame.Desktop" ); - oDesktop = ( XDesktop ) UnoRuntime.queryInterface( XDesktop.class, oInterface ); - oCLoader = ( XComponentLoader ) UnoRuntime.queryInterface( XComponentLoader.class, oDesktop ); - PropertyValue [] szEmptyArgs = new PropertyValue [0]; - String doc = "private:factory/swriter"; - aDoc = oCLoader.loadComponentFromURL(doc, "_blank", 0, szEmptyArgs ); - oDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, aDoc); - - } // end of try - - catch(Exception e){ - - System.out.println(" Exception " + e); - - } // end of catch - - - return oDoc; - }//end of openWriter - - public static void insertIntoCell(String CellName, String theText, XTextTable TT1) { + public static void insertIntoCell(String CellName, String theText, + com.sun.star.text.XTextTable xTTbl) { - XText oTableText = (XText) UnoRuntime.queryInterface(XText.class, TT1.getCellByName(CellName)); + com.sun.star.text.XText xTableText = (com.sun.star.text.XText) + UnoRuntime.queryInterface(com.sun.star.text.XText.class, + xTTbl.getCellByName(CellName)); //create a cursor object - XTextCursor oC = oTableText.createTextCursor(); + com.sun.star.text.XTextCursor xTC = xTableText.createTextCursor(); - XPropertySet oTPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oC ); + com.sun.star.beans.XPropertySet xTPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTC); try { - oTPS.setPropertyValue("CharColor",new Integer(16777215)); + xTPS.setPropertyValue("CharColor",new Integer(16777215)); } catch (Exception e) { + System.err.println(" Exception " + e); + e.printStackTrace(System.err); } - //inserting some Text - oTableText.setString( theText ); + xTableText.setString( theText ); - } // end of insertIntoCell - -} // finish class SWriter + } +} diff --git a/odk/examples/java/Text/StyleCreation.java b/odk/examples/java/Text/StyleCreation.java index a2032d966a97..d3c26ff2df69 100644 --- a/odk/examples/java/Text/StyleCreation.java +++ b/odk/examples/java/Text/StyleCreation.java @@ -2,9 +2,9 @@ * * $RCSfile: StyleCreation.java,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: hr $ $Date: 2004-02-02 20:13:58 $ + * last change: $Author: rt $ $Date: 2005-01-31 17:17:43 $ * * The Contents of this file are made available subject to the terms of * the BSD license. @@ -39,7 +39,7 @@ *************************************************************************/ //*************************************************************************** -// comment: Step 1: connect to the office an get the MSF +// 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 @@ -47,90 +47,51 @@ // Chapter 4.1.3 Defining Your Own Style //*************************************************************************** -import com.sun.star.uno.XInterface; import com.sun.star.uno.UnoRuntime; -import com.sun.star.beans.PropertyValue; -import com.sun.star.beans.XPropertySet; - -// access the implementations via names -import com.sun.star.comp.servicemanager.ServiceManager; - -import com.sun.star.connection.XConnector; -import com.sun.star.connection.XConnection; - -import com.sun.star.bridge.XUnoUrlResolver; -import com.sun.star.uno.UnoRuntime; -import com.sun.star.uno.XInterface; -import com.sun.star.uno.XNamingService; -import com.sun.star.uno.XComponentContext; -import com.sun.star.container.XNameContainer; -import com.sun.star.container.XNameAccess; - -// access the implementations via names -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.text.ControlCharacter.*; - -import com.sun.star.text.XTextRange; -import com.sun.star.text.XText; -import com.sun.star.style.XStyle; -import com.sun.star.style.XStyleFamiliesSupplier; public class StyleCreation { public static void main(String args[]) { - String sConnectionString = "uno:socket,host=localhost,port=2083;urp;StarOffice.NamingService"; - - // It is possible to use a different connection string, passed as argument - if ( args.length == 1 ) { - sConnectionString = args[0]; - } - - XMultiServiceFactory xMSF = null; - try { - xMSF = connect( sConnectionString ); - } - catch( Exception e) { - e.printStackTrace(System.out); - System.exit( 0 ); - } - - if( xMSF != null ) - System.out.println( "Connecting to " + sConnectionString ); - // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote servie manager and the desktop object. com.sun.star.frame.XDesktop xDesktop = null; - xDesktop = getDesktop( xMSF ); + xDesktop = getDesktop(); try { // create text document com.sun.star.text.XTextDocument xTextDocument = null; - xTextDocument = createTextdocument( xDesktop ); + xTextDocument = createTextdocument(xDesktop); - // BEGIN: 'Defining your own style' Section from the Tutorial - - // the service '..ParagraphStyle' is context dependend, you need the multi service factory - // from the document to use the service - XMultiServiceFactory xDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( - XMultiServiceFactory.class, xTextDocument ); + // the service '..ParagraphStyle' is context dependend, you need + // the multi service factory from the document to use the service + com.sun.star.lang.XMultiServiceFactory xDocMSF = + (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xTextDocument); // use the service 'com.sun.star.style.ParagraphStyle' - XInterface xInterface = (XInterface) xDocMSF.createInstance( "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 - XStyleFamiliesSupplier xSupplier = ( XStyleFamiliesSupplier ) UnoRuntime.queryInterface( - XStyleFamiliesSupplier.class, xTextDocument ); + com.sun.star.style.XStyleFamiliesSupplier xSupplier = + (com.sun.star.style.XStyleFamiliesSupplier)UnoRuntime.queryInterface( + com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument ); // get the NameAccess interface from the Style family collection - XNameAccess xNameAccess = xSupplier.getStyleFamilies(); + com.sun.star.container.XNameAccess xNameAccess = + xSupplier.getStyleFamilies(); // select the Paragraph styles, you get the Paragraph style collection - XNameContainer xParaStyleCollection = (XNameContainer) UnoRuntime.queryInterface( - XNameContainer.class, xNameAccess.getByName( "ParagraphStyles" )); + com.sun.star.container.XNameContainer xParaStyleCollection = + (com.sun.star.container.XNameContainer) UnoRuntime.queryInterface( + com.sun.star.container.XNameContainer.class, + xNameAccess.getByName("ParagraphStyles")); // create a PropertySet to set the properties for the new Paragraphstyle - XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface( - XPropertySet.class, xInterface ); + com.sun.star.beans.XPropertySet xPropertySet = + (com.sun.star.beans.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 @@ -140,19 +101,21 @@ public class StyleCreation { 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 ) ); + xPropertySet.setPropertyValue("CharWeight", + new Float( com.sun.star.awt.FontWeight.BOLD ) ); System.out.println( "set the font attribute 'Bold'" ); xPropertySet.setPropertyValue("CharAutoKerning", new Boolean( true ) ); System.out.println( "set the paragraph attribute 'AutoKerning'" ); - - xPropertySet.setPropertyValue("ParaAdjust", new Integer( com.sun.star.style.ParagraphAdjust.CENTER_value ) ); + xPropertySet.setPropertyValue("ParaAdjust", + new Integer( com.sun.star.style.ParagraphAdjust.CENTER_value ) ); System.out.println( "set the paragraph adjust to LEFT" ); xPropertySet.setPropertyValue("ParaFirstLineIndent", new Integer( 0 ) ); System.out.println( "set the first line indent to 0 cm" ); - xPropertySet.setPropertyValue("BreakType", com.sun.star.style.BreakType.PAGE_AFTER ); + 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 @@ -160,20 +123,24 @@ public class StyleCreation { System.out.println( "create new paragraph style, with the values from the Propertyset"); // get the Textrange from the document - XTextRange xTextRange = xTextDocument.getText().getStart(); + com.sun.star.text.XTextRange xTextRange = + xTextDocument.getText().getStart(); // get the PropertySet from the current paragraph - XPropertySet xParagraphPropertySet = (XPropertySet) UnoRuntime.queryInterface( - 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", new String( "myheading" ) ); + com.sun.star.beans.XPropertySet xParagraphPropertySet = + (com.sun.star.beans.XPropertySet)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", + new String( "myheading" ) ); System.out.println( "apply the new paragraph style"); - - // END: 'Defining your own style' Section from the Tutorial } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); + System.exit(1); } System.out.println("done"); @@ -182,100 +149,81 @@ public class StyleCreation { } - public static XMultiServiceFactory connect( String connectStr ) - throws com.sun.star.uno.Exception, - com.sun.star.uno.RuntimeException, Exception { - // Get component context - XComponentContext xcomponentcontext = - com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( - null ); - - // initial serviceManager - XMultiComponentFactory xLocalServiceManager = - xcomponentcontext.getServiceManager(); - - // create a connector, so that it can contact the office - Object xUrlResolver = xLocalServiceManager.createInstanceWithContext( - "com.sun.star.bridge.UnoUrlResolver", xcomponentcontext ); - XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( - XUnoUrlResolver.class, xUrlResolver ); - - Object rInitialObject = urlResolver.resolve( connectStr ); - - XNamingService rName = (XNamingService)UnoRuntime.queryInterface( - XNamingService.class, rInitialObject ); - - XMultiServiceFactory xMSF = null; - if( rName != null ) { - System.err.println( "got the remote naming service !" ); - Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" ); - - xMSF = (XMultiServiceFactory) - UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr ); - } - + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; - return ( xMSF ); - } + try { + com.sun.star.uno.XComponentContext xContext = null; + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); - public static com.sun.star.frame.XDesktop getDesktop( XMultiServiceFactory xMSF ) { - XInterface xInterface = null; - com.sun.star.frame.XDesktop xDesktop = null; + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); - if( xMSF != null ) { - try { - xInterface = (XInterface) xMSF.createInstance("com.sun.star.frame.Desktop"); + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface( - com.sun.star.frame.XDesktop.class, xInterface); - } - catch( Exception e) { - e.printStackTrace(System.out); + 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); } - else - System.out.println( "Can't create a desktop. null pointer !" ); + return xDesktop; } - public static com.sun.star.text.XTextDocument createTextdocument( com.sun.star.frame.XDesktop 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 = null; - xComponent = CreateNewDocument( xDesktop, "swriter" ); - - aTextDocument = (com.sun.star.text.XTextDocument) UnoRuntime.queryInterface( - com.sun.star.text.XTextDocument.class, xComponent ); + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = (com.sun.star.text.XTextDocument) + UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } return aTextDocument; } - protected static com.sun.star.lang.XComponent CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType ) { + 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; - PropertyValue xValues[] = new PropertyValue[1]; - com.sun.star.frame.XComponentLoader xComponentLoader = null; - XInterface xInterface = null; - - PropertyValue[] xEmptyArgs = new PropertyValue[0]; - - xComponentLoader = (com.sun.star.frame.XComponentLoader) UnoRuntime.queryInterface( - com.sun.star.frame.XComponentLoader.class, xDesktop ); + com.sun.star.beans.PropertyValue xValues[] = + new com.sun.star.beans.PropertyValue[1]; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; try { - xComponent = xComponentLoader.loadComponentFromURL( sURL, "_blank", 0, xEmptyArgs); + xComponentLoader = (com.sun.star.frame.XComponentLoader) + UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } return xComponent ; diff --git a/odk/examples/java/Text/StyleInitialization.java b/odk/examples/java/Text/StyleInitialization.java index 602b24db952b..32b6bf9f7b00 100644 --- a/odk/examples/java/Text/StyleInitialization.java +++ b/odk/examples/java/Text/StyleInitialization.java @@ -2,9 +2,9 @@ * * $RCSfile: StyleInitialization.java,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: hr $ $Date: 2004-02-02 20:14:18 $ + * last change: $Author: rt $ $Date: 2005-01-31 17:18:02 $ * * The Contents of this file are made available subject to the terms of * the BSD license. @@ -39,64 +39,24 @@ *************************************************************************/ //*************************************************************************** -// comment: Step 1: connect to the office an get the MSF +// comment: Step 1: get the Desktop object from the office // Step 2: open an empty text document // Step 3: enter a example text // Step 4: use the paragraph collection // Step 5: apply a different paragraph style on the paragraphs //*************************************************************************** -import com.sun.star.uno.XInterface; -import com.sun.star.uno.UnoRuntime; -import com.sun.star.beans.PropertyValue; -import com.sun.star.beans.XPropertySet; - -// access the implementations via names -import com.sun.star.comp.servicemanager.ServiceManager; - -import com.sun.star.bridge.XUnoUrlResolver; -import com.sun.star.connection.XConnector; -import com.sun.star.connection.XConnection; - -import com.sun.star.uno.IBridge; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.AnyConverter; -import com.sun.star.uno.XInterface; -import com.sun.star.uno.XNamingService; -import com.sun.star.uno.XComponentContext; - -// access the implementations via names -import com.sun.star.lang.XMultiServiceFactory; -import com.sun.star.lang.XMultiComponentFactory; -import com.sun.star.text.ControlCharacter.*; - -import com.sun.star.text.XText; - public class StyleInitialization { public static void main(String args[]) { - String sConnectionString = "uno:socket,host=localhost,port=2083;urp;StarOffice.NamingService"; - - // It is possible to use a different connection string, passed as argument - if ( args.length == 1 ) { - sConnectionString = args[0]; - } - - XMultiServiceFactory xMSF = null; - try { - xMSF = connect( sConnectionString ); - } - catch( Exception e) { - e.printStackTrace(System.out); - System.exit( 0 ); - } - - if( xMSF != null ) System.out.println("Connecting to " + sConnectionString ); - // You need the desktop to create a document + // The getDesktop method does the UNO bootstrapping, gets the + // remote servie manager and the desktop object. com.sun.star.frame.XDesktop xDesktop = null; - xDesktop = getDesktop( xMSF ); + xDesktop = getDesktop(); try { // BEGIN: 'Style basics' Section from the Tutorial @@ -116,9 +76,12 @@ public class StyleInitialization { // 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 = (com.sun.star.text.XTextCursor) xTextDocument.getText().createTextCursor(); + xTextCursor = (com.sun.star.text.XTextCursor) + xTextDocument.getText().createTextCursor(); - XPropertySet oCPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, xTextCursor ); + com.sun.star.beans.XPropertySet oCPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xTextCursor); try { oCPS.setPropertyValue("CharFontName","Helvetica"); } @@ -134,7 +97,8 @@ public class StyleInitialization { catch (Exception ex) { } - xText.insertControlCharacter( xTextCursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false ); + xText.insertControlCharacter(xTextCursor, + com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false); xText.insertString( xTextCursor, sMyText, false ); @@ -143,29 +107,35 @@ public class StyleInitialization { // the text range not the cursor contains the 'parastyle' property xTextRange = xText.getEnd(); - xPropertySet = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface( - com.sun.star.beans.XPropertySet.class, xTextRange ); + xPropertySet = (com.sun.star.beans.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") ); + // 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 throught the paragraphs, with a paragraph cursor, or a enumeration. + // There are two way to travel throught the paragraphs, with a + // paragraph cursor, or a enumeration. // You find both ways in this example // The first way, with the paragraph cursor com.sun.star.text.XParagraphCursor xParagraphCursor = null; - xParagraphCursor = (com.sun.star.text.XParagraphCursor) UnoRuntime.queryInterface( - com.sun.star.text.XParagraphCursor.class, xTextRange ); + xParagraphCursor = (com.sun.star.text.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 = (com.sun.star.container.XEnumerationAccess) UnoRuntime.queryInterface( - com.sun.star.container.XEnumerationAccess.class, xText ); + xEnumerationAccess = (com.sun.star.container.XEnumerationAccess) + UnoRuntime.queryInterface( + com.sun.star.container.XEnumerationAccess.class, xText ); // the enumeration contains all paragraph form the document com.sun.star.container.XEnumeration xParagraphEnumeration = null; @@ -180,10 +150,13 @@ public class StyleInitialization { // check if a paragraph is available while ( xParagraphEnumeration.hasMoreElements() ) { // get the next paragraph - xParagraph = (com.sun.star.text.XTextContent) UnoRuntime.queryInterface( - com.sun.star.text.XTextContent.class, xParagraphEnumeration.nextElement()); + xParagraph = (com.sun.star.text.XTextContent) + UnoRuntime.queryInterface( + com.sun.star.text.XTextContent.class, + xParagraphEnumeration.nextElement()); - // you need the method getAnchor to a TextRange -> to manipulate the paragraph + // you need the method getAnchor to a TextRange -> to manipulate + // the paragraph String sText = xParagraph.getAnchor().getString(); // create a cursor from this paragraph @@ -194,16 +167,18 @@ public class StyleInitialization { xParaCursor.gotoStart( false ); xParaCursor.gotoEnd( true ); - // The enumeration from the paragraphs contain parts from the paragraph with a - // different attributes. + // The enumeration from the paragraphs contain parts from the + // paragraph with a different attributes. xParaEnumerationAccess = (com.sun.star.container.XEnumerationAccess) - UnoRuntime.queryInterface(com.sun.star.container.XEnumerationAccess.class, xParagraph); + 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 = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface( - com.sun.star.text.XTextRange.class, xPortionEnumeration.nextElement()); + com.sun.star.text.XTextRange.class, + xPortionEnumeration.nextElement()); String sWordString = xWord.getString(); System.out.println( "Content of the paragraph : " + sWordString ); } @@ -259,7 +234,7 @@ public class StyleInitialization { // END: 'Finding a suitable style' Section from the Tutorial } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } @@ -269,102 +244,83 @@ public class StyleInitialization { } - public static XMultiServiceFactory connect( String connectStr ) - throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException, Exception { - // Get component context - XComponentContext xcomponentcontext = - com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( - null ); - - // initial serviceManager - XMultiComponentFactory xLocalServiceManager = - xcomponentcontext.getServiceManager(); - - // create a connector, so that it can contact the office - Object xUrlResolver = xLocalServiceManager.createInstanceWithContext( - "com.sun.star.bridge.UnoUrlResolver", xcomponentcontext ); - XUnoUrlResolver urlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface( - XUnoUrlResolver.class, xUrlResolver ); - - Object rInitialObject = urlResolver.resolve( connectStr ); - - XNamingService rName = (XNamingService)UnoRuntime.queryInterface( - XNamingService.class, rInitialObject ); - - XMultiServiceFactory xMSF = null; - if( rName != null ) { - System.err.println( "got the remote naming service !" ); - Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager" ); - - xMSF = (XMultiServiceFactory) - UnoRuntime.queryInterface( XMultiServiceFactory.class, rXsmgr ); - } + public static com.sun.star.frame.XDesktop getDesktop() { + com.sun.star.frame.XDesktop xDesktop = null; + com.sun.star.lang.XMultiComponentFactory xMCF = null; - return ( xMSF ); - } + try { + com.sun.star.uno.XComponentContext xContext = null; + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); - public static com.sun.star.frame.XDesktop getDesktop( XMultiServiceFactory xMSF ) { - XInterface xInterface = null; - com.sun.star.frame.XDesktop xDesktop = null; + // get the remote office service manager + xMCF = xContext.getServiceManager(); + if( xMCF != null ) { + System.out.println("Connected to a running office ..."); - if( xMSF != null ) { - try { - xInterface = (XInterface) xMSF.createInstance("com.sun.star.frame.Desktop"); + Object oDesktop = xMCF.createInstanceWithContext( + "com.sun.star.frame.Desktop", xContext); xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface( - com.sun.star.frame.XDesktop.class, xInterface); - } - catch( Exception e) { - e.printStackTrace(System.out); + com.sun.star.frame.XDesktop.class, oDesktop); } + else + System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" ); } - else - System.out.println( "Can't create a desktop. null pointer !" ); + 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 ) { + 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 = null; - xComponent = CreateNewDocument( xDesktop, "swriter" ); - - aTextDocument = (com.sun.star.text.XTextDocument) UnoRuntime.queryInterface( - com.sun.star.text.XTextDocument.class, xComponent ); + com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop, + "swriter"); + aTextDocument = (com.sun.star.text.XTextDocument) + UnoRuntime.queryInterface( + com.sun.star.text.XTextDocument.class, xComponent); } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } return aTextDocument; } - protected static com.sun.star.lang.XComponent CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType ) { + 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; - PropertyValue xValues[] = new PropertyValue[1]; - com.sun.star.frame.XComponentLoader xComponentLoader = null; - XInterface xInterface = null; - - PropertyValue[] xEmptyArgs = new PropertyValue[0]; - - xComponentLoader = (com.sun.star.frame.XComponentLoader) UnoRuntime.queryInterface( - com.sun.star.frame.XComponentLoader.class, xDesktop ); + com.sun.star.beans.PropertyValue xValues[] = + new com.sun.star.beans.PropertyValue[1]; + com.sun.star.beans.PropertyValue xEmptyArgs[] = + new com.sun.star.beans.PropertyValue[0]; try { - xComponent = xComponentLoader.loadComponentFromURL( sURL, "_blank", 0, xEmptyArgs); + xComponentLoader = (com.sun.star.frame.XComponentLoader) + UnoRuntime.queryInterface( + com.sun.star.frame.XComponentLoader.class, xDesktop); + + xComponent = xComponentLoader.loadComponentFromURL( + sURL, "_blank", 0, xEmptyArgs); } catch( Exception e) { - e.printStackTrace(System.out); + e.printStackTrace(System.err); } return xComponent ; } - - } |