diff options
author | Rüdiger Timm <rt@openoffice.org> | 2005-01-31 16:10:06 +0000 |
---|---|---|
committer | Rüdiger Timm <rt@openoffice.org> | 2005-01-31 16:10:06 +0000 |
commit | 9b26895fa348243271ff19f487d4c9fea68e6e48 (patch) | |
tree | cfc6bd502f2792b61c30b34928a0b15d8559897f /odk/examples/java/Drawing | |
parent | 0f1e35dd3abadc5645073154fcdfcadc08ee3a22 (diff) |
INTEGRATION: CWS sdksample (1.4.40); FILE MERGED
2004/08/05 15:15:47 jsc 1.4.40.3: #i29308# use System.err for error output
2004/07/26 11:18:40 jsc 1.4.40.2: #i29308# change output
2004/06/04 14:52:35 jsc 1.4.40.1: #i29308# use of new bootstrap feature
Diffstat (limited to 'odk/examples/java/Drawing')
-rw-r--r-- | odk/examples/java/Drawing/SDraw.java | 348 |
1 files changed, 130 insertions, 218 deletions
diff --git a/odk/examples/java/Drawing/SDraw.java b/odk/examples/java/Drawing/SDraw.java index 2a46f322b873..7fdc0edc9320 100644 --- a/odk/examples/java/Drawing/SDraw.java +++ b/odk/examples/java/Drawing/SDraw.java @@ -2,9 +2,9 @@ * * $RCSfile: SDraw.java,v $ * - * $Revision: 1.4 $ + * $Revision: 1.5 $ * - * last change: $Author: hr $ $Date: 2004-02-02 20:10:56 $ + * last change: $Author: rt $ $Date: 2005-01-31 17:10:06 $ * * 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: get the drawpage an insert some shapes //*************************************************************************** @@ -47,258 +47,170 @@ import java.lang.Math; -// 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.drawing.*; -import com.sun.star.text.XText; -import com.sun.star.awt.*; public class SDraw { 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; - XComponent xDrawDoc = null; - XDrawPage oObj = null; - XShape oShape = null; - XShape oShape1 = null; - XShapes oShapes = null; - - - // create connection(s) and get multiservicefactory - - // 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 ); + // get the remote office component context + xContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); + System.out.println("Connected to a running office ..."); + } + catch( Exception e) { + e.printStackTrace(System.err); + System.exit(1); } - //*************************************************************************** + com.sun.star.lang.XComponent xDrawDoc = null; + com.sun.star.drawing.XDrawPage xDrawPage = null; - //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooooo + //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo // open an empty document. In this case it's a draw 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 document - //Draw + System.out.println("Opening an empty Draw document ..."); + xDrawDoc = openDraw(xContext); - System.out.println("Opening an empty Draw document"); - xDrawDoc = openDraw(xMSF); - - - //*************************************************************************** - - //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooooo + //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo // get the drawpage an insert some shapes. // the documents DrawPageSupplier supplies the DrawPage vi IndexAccess // To add a shape get the MultiServiceFaktory of the document, create an // instance of the ShapeType and add it to the Shapes-container // provided by the drawpage - //*************************************************************************** + //************************************************************************* // get the drawpage of drawing here try { System.out.println( "getting Drawpage" ); - XDrawPagesSupplier oDPS = (XDrawPagesSupplier) UnoRuntime.queryInterface( - XDrawPagesSupplier.class,xDrawDoc); - XDrawPages oDPn = oDPS.getDrawPages(); - XIndexAccess oDPi = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class,oDPn); - oObj = (XDrawPage) UnoRuntime.queryInterface(XDrawPage.class, oDPi.getByIndex(0)); + com.sun.star.drawing.XDrawPagesSupplier xDPS = + (com.sun.star.drawing.XDrawPagesSupplier)UnoRuntime.queryInterface( + com.sun.star.drawing.XDrawPagesSupplier.class, xDrawDoc); + com.sun.star.drawing.XDrawPages xDPn = xDPS.getDrawPages(); + com.sun.star.container.XIndexAccess xDPi = + (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface( + com.sun.star.container.XIndexAccess.class, xDPn); + xDrawPage = (com.sun.star.drawing.XDrawPage)UnoRuntime.queryInterface( + com.sun.star.drawing.XDrawPage.class, xDPi.getByIndex(0)); } catch ( Exception e ) { - System.out.println( "Couldn't create document"+ e ); + System.err.println( "Couldn't create document"+ e ); + e.printStackTrace(System.err); } - createSequence(xDrawDoc, oObj); + createSequence(xDrawDoc, xDrawPage); //put something on the drawpage System.out.println( "inserting some Shapes" ); - oShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,oObj); - oShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0)); - oShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680)); - oShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932)); - - //*************************************************************************** - - - + com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes) + UnoRuntime.queryInterface( + com.sun.star.drawing.XShapes.class, xDrawPage); + xShapes.add(createShape(xDrawDoc,2000,1500,1000,1000,"Line",0)); + xShapes.add(createShape(xDrawDoc,3000,4500,15000,1000,"Ellipse",16711680)); + xShapes.add(createShape(xDrawDoc,5000,3500,7500,5000,"Rectangle",6710932)); + + //************************************************************************* 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 { - /* Bootstraps a component context with the jurt base components - registered. Component context to be granted to a component for running. - Arbitrary values can be retrieved from the context. */ - XComponentContext xcomponentcontext = - com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null ); - - /* Gets the service manager instance to be used (or null). This method has - been added for convenience, because the service manager is a often used - object. */ - XMultiComponentFactory xmulticomponentfactory = - xcomponentcontext.getServiceManager(); - - /* Creates an instance of the component UnoUrlResolver which - supports the services specified by the factory. */ - Object objectUrlResolver = - xmulticomponentfactory.createInstanceWithContext( - "com.sun.star.bridge.UnoUrlResolver", xcomponentcontext ); - - // Create a new url resolver - XUnoUrlResolver xurlresolver = ( XUnoUrlResolver ) - UnoRuntime.queryInterface( XUnoUrlResolver.class, - objectUrlResolver ); - - Object rInitialObject = xurlresolver.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 XComponent openDraw(XMultiServiceFactory oMSF) { - - - //define variables - XInterface oInterface; - XDesktop oDesktop; - XComponentLoader oCLoader; - XComponent aDoc = null; + public static com.sun.star.lang.XComponent openDraw( + com.sun.star.uno.XComponentContext xContext) + { + 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/sdraw"; + xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs); + + } catch(Exception e){ + System.err.println(" Exception " + e); + e.printStackTrace(System.err); + } - 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/sdraw"; - aDoc = oCLoader.loadComponentFromURL(doc, "_blank", 0, szEmptyArgs ); - - } // end of try - - catch(Exception e){ - - System.out.println(" Exception " + e); - - } // end of catch - - - return aDoc; - }//end of openDraw + return xComp; + } - public static XShape createShape(XComponent oDoc, int height, int width, int x, int y, String kind, int col) { + public static com.sun.star.drawing.XShape createShape( + com.sun.star.lang.XComponent xDocComp, int height, int width, int x, + int y, String kind, int col) + { //possible values for kind are 'Ellipse', 'Line' and 'Rectangle' - Size size = new Size(); - Point position = new Point(); - XShape oShape = null; + com.sun.star.awt.Size size = new com.sun.star.awt.Size(); + com.sun.star.awt.Point position = new com.sun.star.awt.Point(); + com.sun.star.drawing.XShape xShape = null; //get MSF - XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( XMultiServiceFactory.class, oDoc ); + com.sun.star.lang.XMultiServiceFactory xDocMSF = + (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xDocComp ); - try{ - Object oInt = oDocMSF.createInstance("com.sun.star.drawing."+kind+"Shape"); - oShape = (XShape)UnoRuntime.queryInterface( XShape.class, oInt ); + try { + Object oInt = xDocMSF.createInstance("com.sun.star.drawing." + +kind + "Shape"); + xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface( + com.sun.star.drawing.XShape.class, oInt); size.Height = height; size.Width = width; position.X = x; position.Y = y; - oShape.setSize(size); - oShape.setPosition(position); + xShape.setSize(size); + xShape.setPosition(position); } catch ( Exception e ) { - // Some exception occures.FAILED - System.out.println( "Couldn't create instance "+ e ); + System.err.println( "Couldn't create instance "+ e ); + e.printStackTrace(System.err); } - XPropertySet oSPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oShape); + com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface( + com.sun.star.beans.XPropertySet.class, xShape); try { - - oSPS.setPropertyValue("FillColor",new Integer(col)); - - + xSPS.setPropertyValue("FillColor", new Integer(col)); } catch (Exception e) { - - System.out.println("Can't change colors " + e); - + System.err.println("Can't change colors " + e); + e.printStackTrace(System.err); } - - return oShape; - + return xShape; } - public static XShape createSequence(XComponent oDoc, XDrawPage oDP) { - Size size = new Size(); - Point position = new Point(); - XShape oShape = null; - XShapes oShapes = (XShapes) UnoRuntime.queryInterface(XShapes.class,oDP); + public static com.sun.star.drawing.XShape createSequence( + com.sun.star.lang.XComponent xDocComp, com.sun.star.drawing.XDrawPage xDP) + { + com.sun.star.awt.Size size = new com.sun.star.awt.Size(); + com.sun.star.awt.Point position = new com.sun.star.awt.Point(); + com.sun.star.drawing.XShape xShape = null; + com.sun.star.drawing.XShapes xShapes = (com.sun.star.drawing.XShapes) + UnoRuntime.queryInterface(com.sun.star.drawing.XShapes.class, xDP); int height = 3000; int width = 3500; int x = 1900; @@ -309,58 +221,58 @@ public class SDraw { int b = 80; //get MSF - XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface( - XMultiServiceFactory.class, oDoc ); + com.sun.star.lang.XMultiServiceFactory xDocMSF = + (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface( + com.sun.star.lang.XMultiServiceFactory.class, xDocComp ); for (int i=0; i<370; i=i+25) { try{ - oInt = oDocMSF.createInstance("com.sun.star.drawing.EllipseShape"); - oShape = (XShape)UnoRuntime.queryInterface( XShape.class, oInt ); + oInt = xDocMSF.createInstance("com.sun.star.drawing.EllipseShape"); + xShape = (com.sun.star.drawing.XShape)UnoRuntime.queryInterface( + com.sun.star.drawing.XShape.class, oInt); size.Height = height; size.Width = width; position.X = (x+(i*40)); - position.Y = (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue(); - oShape.setSize(size); - oShape.setPosition(position); + position.Y = + (new Float(y+(Math.sin((i*Math.PI)/180))*5000)).intValue(); + xShape.setSize(size); + xShape.setPosition(position); } catch ( Exception e ) { // Some exception occures.FAILED - System.out.println( "Couldn't get Shape "+ e ); + System.err.println( "Couldn't get Shape "+ e ); + e.printStackTrace(System.err); } b=b+8; - XPropertySet oSPS = (XPropertySet)UnoRuntime.queryInterface( XPropertySet.class, oShape); + com.sun.star.beans.XPropertySet xSPS = (com.sun.star.beans.XPropertySet) + UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, + xShape); try { - - oSPS.setPropertyValue("FillColor",new Integer(getCol(r,g,b))); - oSPS.setPropertyValue("Shadow",new Boolean(true)); - + xSPS.setPropertyValue("FillColor", new Integer(getCol(r,g,b))); + xSPS.setPropertyValue("Shadow", new Boolean(true)); } catch (Exception e) { - - System.out.println("Can't change colors " + e); - + System.err.println("Can't change colors " + e); + e.printStackTrace(System.err); } - oShapes.add(oShape); + xShapes.add(xShape); } - XShapeGrouper gr = (XShapeGrouper)UnoRuntime.queryInterface( XShapeGrouper.class, oDP ); + com.sun.star.drawing.XShapeGrouper xSGrouper = + (com.sun.star.drawing.XShapeGrouper)UnoRuntime.queryInterface( + com.sun.star.drawing.XShapeGrouper.class, xDP); - oShape = gr.group(oShapes); + xShape = xSGrouper.group(xShapes); - - return oShape; + return xShape; } public static int getCol(int r, int g, int b) { - return r*65536+g*256+b; - } - - -} // finish class SDraw +} |