Difference between revisions of "Documentation/DevGuide/Text/Auto Text"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (FINAL VERSION FOR L10N)
Line 16: Line 16:
 
<!--[SOURCE:Text/TextDocuments.java]-->
 
<!--[SOURCE:Text/TextDocuments.java]-->
  
 +
<source lang="java">
 
   /** Insert an autotext at the current cursor position of given cursor mxDocCursor*/
 
   /** Insert an autotext at the current cursor position of given cursor mxDocCursor*/
 
    
 
    
Line 61: Line 62:
 
   xSimpleText.insertString(xText.getStart(),  
 
   xSimpleText.insertString(xText.getStart(),  
 
       "This string was inserted using the API!\n\n", false);
 
       "This string was inserted using the API!\n\n", false);
 +
</source>
  
 
<!--[BUG641+]-->
 
<!--[BUG641+]-->

Revision as of 11:43, 7 December 2009



The auto text function can be used to organize reusable text passages. They allow storing text, including the formatting and all other contents in a text block collection to apply them later. Three services deal with auto text in OpenOffice.org:

  /** Insert an autotext at the current cursor position of given cursor mxDocCursor*/
  
  // Get an XNameAccess interface to all auto text groups from the document factory
  XNameAccess xContainer = (XNameAccess) UnoRuntime.queryInterface( 
      XNameAccess.class, mxFactory.createInstance("com.sun.star.text.AutoTextContainer"));
  
  // Get the autotext group Standard
  xGroup = (XAutoTextGroup) UnoRuntime.queryInterface(
      XAutoTextGroup.class, xContainer.getByName("Standard"));
  
  // get the entry Best Wishes (BW)
  XAutoTextEntry xEntry = (XAutoTextEntry)UnoRuntime.queryInterface ( 
      XAutoTextEntry.class, xGroup.getByName ("BW"));
  
  // insert the modified autotext block at the cursor position
  xEntry.applyTo(mxDocCursor);
  
  /** Add a new autotext entry to the AutoTextContainer
   */
  // Select the last paragraph in the document
  xParaCursor.gotoPreviousParagraph(true);
  
  // Get the XAutoTextContainer interface of the AutoTextContainer service
  XAutoTextContainer xAutoTextCont = (XAutoTextContainer) UnoRuntime.queryInterface(
      XAutoTextContainer.class, xContainer );
  
  // If the APIExampleGroup already exists, remove it so we can add a new one
  if (xContainer.hasByName("APIExampleGroup"))
      xAutoTextCont.removeByName("APIExampleGroup" );
  
  // Create a new auto-text group called APIExampleGroup
  XAutoTextGroup xNewGroup = xAutoTextCont.insertNewByName ( "APIExampleGroup" );
  
  // Create and insert a new auto text entry containing the current cursor selection
  XAutoTextEntry xNewEntry = xNewGroup.insertNewByName(
      "NAE", "New AutoTextEntry", xParaCursor);
  
  // Get the XSimpleText and XText interfaces of the new autotext block
  XSimpleText xSimpleText = (XSimpleText) UnoRuntime.queryInterface(
      XSimpleText.class, xNewEntry);
          XText xText = (XText) UnoRuntime.queryInterface(XText.class, xNewEntry);
  
  // Insert a string at the beginning of the autotext block
  xSimpleText.insertString(xText.getStart(), 
      "This string was inserted using the API!\n\n", false);

The current implementation forces the user to close the AutoTextEntry instance when they are changed, so that the changes can take effect. However, the new AutoText is not written to disk until the destructor of the AutoTextEntry instance inside the writer is called. When this example has finished executing, the file on disk correctly contains the complete text "This string was inserted using the API!\n\nSome text for a new autotext block", but there is no way in Java to call the destructor. It is not clear when the garbage collector deletes the object and writes the modifications to disk.

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