Difference between revisions of "Documentation/DevGuide/Text/Embedded Objects"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Documentation/Developers Guide/Text Documents)
m (FINAL VERSION FOR L10N)
 
Line 6: Line 6:
 
|NextPage=Documentation/DevGuide/Text/Graphic Objects
 
|NextPage=Documentation/DevGuide/Text/Graphic Objects
 
}}
 
}}
{{DISPLAYTITLE:Embedded Objects}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Text/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:Embedded Objects}}
 
A <code>TextEmbeddedObject</code> is a <idl>com.sun.star.text.BaseFrame</idl> providing the interface <idl>com.sun.star.document.XEmbeddedObjectSupplier</idl>. The only method of this interface,
 
A <code>TextEmbeddedObject</code> is a <idl>com.sun.star.text.BaseFrame</idl> providing the interface <idl>com.sun.star.document.XEmbeddedObjectSupplier</idl>. The only method of this interface,
  

Latest revision as of 10:11, 14 May 2009



A TextEmbeddedObject is a com.sun.star.text.BaseFrame providing the interface com.sun.star.document.XEmbeddedObjectSupplier. The only method of this interface,

 com::sun::star::lang::XComponent getEmbeddedObject ()

provides access to the model of the embedded document. That way, an embedded OpenOffice.org spreadsheet, drawing, chart or a formula document can be used in a text over its document model.

An embedded object is inserted by using the document's factory to create an instance of the the service com.sun.star.text.TextEmbeddedObject. The type of object is determined by setting the string property CLSID to an appropriate value before inserting the object as text content in the document.

 ///***************************************************************************
 // comment: Step 1: get the Desktop object from the office
 // Step 2: open an empty text document
 // Step 3: insert a sample text table
 // Step 4: insert a Chart object
 // Step 5: insert data from text table into Chart object
 //***************************************************************************
 
 import com.sun.star.uno.UnoRuntime;
 
 public class OleObject {
 
     public static void main(String args[]) {
         // 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();
         
         com.sun.star.text.XTextDocument xTextDocument =
             createTextdocument( xDesktop );
         
         com.sun.star.text.XTextTable xTextTable = 
             createExampleTable( xTextDocument );
         
         try {
             // create TextEmbeddedObject
             com.sun.star.lang.XMultiServiceFactory xDocMSF = (com.sun.star.lang.XMultiServiceFactory)
             UnoRuntime.queryInterface(com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
             com.sun.star.text.XTextContent xObj = (com.sun.star.text.XTextContent)
             UnoRuntime.queryInterface(com.sun.star.text.XTextContent.class, 
                 xDocMSF.createInstance( "com.sun.star.text.TextEmbeddedObject" ));
             
             // set class id for chart object to determine the type 
             // of object to be inserted
             com.sun.star.beans.XPropertySet xPS = (com.sun.star.beans.XPropertySet)
             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xObj);
             xPS.setPropertyValue( "CLSID", "12dcae26-281f-416f-a234-c3086127382e" );
             
             // insert object in document
             com.sun.star.text.XTextCursor xCursor = xTextDocument.getText().createTextCursor();
             com.sun.star.text.XTextRange xRange = (com.sun.star.text.XTextRange)
             UnoRuntime.queryInterface(com.sun.star.text.XTextRange.class, xCursor);
             xTextDocument.getText().insertTextContent( xRange, xObj, false );
             
             // access objects model
             com.sun.star.document.XEmbeddedObjectSupplier xEOS = (com.sun.star.document.XEmbeddedObjectSupplier)
             UnoRuntime.queryInterface(com.sun.star.document.XEmbeddedObjectSupplier.class, xObj);
             com.sun.star.lang.XComponent xModel = xEOS.getEmbeddedObject();
             
             // get table data
             com.sun.star.chart.XChartDataArray xDocCDA = (com.sun.star.chart.XChartDataArray)
             UnoRuntime.queryInterface(com.sun.star.chart.XChartDataArray.class, xTextTable);
             double[][] aData = xDocCDA.getData();
             
             // insert table data in Chart object
             com.sun.star.chart.XChartDocument xChartDoc = (com.sun.star.chart.XChartDocument)
             UnoRuntime.queryInterface(com.sun.star.chart.XChartDocument.class, xModel);
             com.sun.star.chart.XChartDataArray xChartDataArray = (com.sun.star.chart.XChartDataArray)
             UnoRuntime.queryInterface(com.sun.star.chart.XChartDataArray.class, xChartDoc.getData());
             xChartDataArray.setData( aData );
             
             // to remove the embedded object just uncomment the next line
             //xTextDocument.getText().removeTextContent( xObj );
         }
         catch( Exception e) {
             e.printStackTrace(System.err);
         } 
         
         System.out.println("Done");
         
         System.exit(0);
     }
               
     protected static com.sun.star.text.XTextTable createExampleTable(
         com.sun.star.text.XTextDocument xTextDocument )
     {
         com.sun.star.lang.XMultiServiceFactory xDocMSF =
             (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
                 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
         
         com.sun.star.text.XTextTable xTT = null;
         
         try {
             Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable");
             xTT = (com.sun.star.text.XTextTable)
             UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt);
             
             //initialize the text table with 4 columns an 5 rows
             xTT.initialize(4,5);
             
         } catch (Exception e) {
             System.err.println("Couldn't create instance "+ e);
             e.printStackTrace(System.err);
         }
         
         com.sun.star.text.XText xText = xTextDocument.getText();
         
         //create a cursor object
         com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
         
         //insert the table
         try {
             xText.insertTextContent(xTCursor, xTT, false);
         
         } catch (Exception e) {
             System.err.println("Couldn't insert the table " + e);
             e.printStackTrace(System.err);
         }
         
         // inserting sample data
         (xTT.getCellByName("A2")).setValue(5.0);
         (xTT.getCellByName("A3")).setValue(5.5);
         (xTT.getCellByName("A4")).setValue(5.7);
         (xTT.getCellByName("B2")).setValue(2.3);
         (xTT.getCellByName("B3")).setValue(2.2);
         (xTT.getCellByName("B4")).setValue(2.4);
         (xTT.getCellByName("C2")).setValue(6);
         (xTT.getCellByName("C3")).setValue(6);
         (xTT.getCellByName("C4")).setValue(6);
         (xTT.getCellByName("D2")).setValue(3);
         (xTT.getCellByName("D3")).setValue(3.5);
         (xTT.getCellByName("D4")).setValue(4);
         (xTT.getCellByName("E2")).setValue(8);
         (xTT.getCellByName("E3")).setValue(5);
         (xTT.getCellByName("E4")).setValue(3);
         
         return xTT;
     }
         
     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 = (com.sun.star.frame.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 = (com.sun.star.text.XTextDocument)
                 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 xValues[] =
             new com.sun.star.beans.PropertyValue[1];
         com.sun.star.beans.PropertyValue xEmptyArgs[] =
             new com.sun.star.beans.PropertyValue[0];
         
         try {
             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.err);
         }
             
         return xComponent ;
     }
 }


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages