Difference between revisions of "Documentation/DevGuide/Text/Example: Visible Cursor Position"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Documentation/Developers Guide/Text Documents)
 
(3 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
|NextPage=Documentation/DevGuide/Text/Handling Text Document Files
 
|NextPage=Documentation/DevGuide/Text/Handling Text Document Files
 
}}
 
}}
{{DISPLAYTITLE:Example: Visible Cursor Position}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Text/{{SUBPAGENAME}}}}
As discussed earlier, the {{PRODUCTNAME}} 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.  
+
{{DISPLAYTITLE:Example: Visible Cursor Position}}
 +
As discussed earlier, the {{AOo}} 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.
 
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.
Line 16: Line 17:
 
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.  
 
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.  
 
<!--[SOURCE:Text/TextDocuments.java]-->
 
<!--[SOURCE:Text/TextDocuments.java]-->
 
+
<syntaxhighlight lang="java">
 
   /** Sample for document changes, starting at the current view cursor position
 
   /** Sample for document changes, starting at the current view cursor position
 
       The sample changes the paragraph style and the character style at the current
 
       The sample changes the paragraph style and the character style at the current
Line 87: Line 88:
 
       xParagraphCursor.setString(" ***** Fin de semana! ******");
 
       xParagraphCursor.setString(" ***** Fin de semana! ******");
 
   }
 
   }
 
+
</syntaxhighlight>
  
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Text Documents]]
 
[[Category:Documentation/Developer's Guide/Text Documents]]

Latest revision as of 13:41, 3 January 2021



As discussed earlier, the Apache OpenOffice 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)UnoRuntime.queryInterface(
              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.gotoEndOfParagraph(false);
      xParagraphCursor.setString(" ***** Fin de semana! ******");
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages