Example: Visible Cursor Position

From Apache OpenOffice Wiki
Jump to: navigation, search

As discussed earlier, the OpenOffice.org API distinguishes between the model and controller. This difference is mirrored in two different kinds of cursors in the API: model cursors and visible cursors. The visible cursor is also called view cursor.

The second example assumes that the user has selected a text range in a paragraph and expects something to happen at that cursor position. Setting character and paragraph styles, and retrieving the current page number at the view cursor position is demonstrated in the example. The view cursor will be transformed into a model cursor.

We want to work with the current document, therefore we cannot use loadComponentFromURL(). Rather, we ask the com.sun.star.frame.Desktop service for the current component. Once we have the current component - which is our document model - we go from the model to the controller and get the view cursor.

The view cursor has properties for the current character and paragraph style. The example uses built-in styles and sets the property CharStyleName to "Quotation" and ParaStyleName to "Quotations". Furthermore, the view cursor knows about the automatic page breaks. Because we are interested in the current page number, we get it from the view cursor and print it out.

The model cursor is much more powerful than the view cursor when it comes to possible movements and editing capabilities. We create a model cursor from the view cursor. Two steps are necessary: We ask the view cursor for its Text service, then we have the Text service create a model cursor based on the current cursor position. The model cursor knows where the paragraph ends, so we go there and insert a string.

  /** Sample for document changes, starting at the current view cursor position
      The sample changes the paragraph style and the character style at the current
      view cursor selection
      Open the sample file ViewCursorExampleFile, select some text and run the example
      The current paragraph will be set to Quotations paragraph style 
      The selected text will be set to Quotation character style
  private void viewCursorExample() throws java.lang.Exception {
      // get the remote service manager
      mxRemoteServiceManager = this.getRemoteServiceManager(unoUrl);
      // get the Desktop service
      Object desktop = mxRemoteServiceManager.createInstanceWithContext(
          "com.sun.star.frame.Desktop", mxRemoteContext);
      // query its XDesktop interface, we need the current component
      XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
          XDesktop.class, desktop);
      // retrieve the current component and access the controller
      XComponent xCurrentComponent = xDesktop.getCurrentComponent();
      // get the XModel interface from the component
      XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xCurrentComponent);
      // the model knows its controller
      XController xController = xModel.getCurrentController();
      // the controller gives us the TextViewCursor
      // query the viewcursor supplier interface 
      XTextViewCursorSupplier xViewCursorSupplier = 
              XTextViewCursorSupplier.class, xController);
      // get the cursor 
      XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
      // query its XPropertySet interface, we want to set character and paragraph properties
      XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
          XPropertySet.class, xViewCursor);
      // set the appropriate properties for character and paragraph style
      xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
          xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");
      // print the current page number - we need the XPageCursor interface for this
      XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
          XPageCursor.class, xViewCursor);
      System.out.println("The current page number is " + xPageCursor.getPage());
      // the model cursor is much more powerful, so 
      // we create a model cursor at the current view cursor position with the following steps:
      // we get the Text service from the TextViewCursor, the cursor is an XTextRange and has 
      // therefore a method getText()
      XText xDocumentText = xViewCursor.getText();
      // the text creates a model cursor from the viewcursor
      XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());
      // now we could query XWordCursor, XSentenceCursor and XParagraphCursor
      // or XDocumentInsertable, XSortable or XContentEnumerationAccess
      // and work with the properties of com.sun.star.text.TextCursor
      // in this case we just go to the end of the paragraph and add some text.
      XParagraphCursor xParagraphCursor = (XParagraphCursor)UnoRuntime.queryInterface(
          XParagraphCursor.class, xModelCursor);
      // goto the end of the paragraph
      xParagraphCursor.setString(" ***** Fin de semana! ******");
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages